]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/proc/base.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * | |
6 | * proc base directory handling functions | |
7 | * | |
8 | * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. | |
9 | * Instead of using magical inumbers to determine the kind of object | |
10 | * we allocate and fill in-core inodes upon lookup. They don't even | |
11 | * go into icache. We cache the reference to task_struct upon lookup too. | |
12 | * Eventually it should become a filesystem in its own. We don't use the | |
13 | * rest of procfs anymore. | |
e070ad49 ML |
14 | * |
15 | * | |
16 | * Changelog: | |
17 | * 17-Jan-2005 | |
18 | * Allan Bezerra | |
19 | * Bruna Moreira <bruna.moreira@indt.org.br> | |
20 | * Edjard Mota <edjard.mota@indt.org.br> | |
21 | * Ilias Biris <ilias.biris@indt.org.br> | |
22 | * Mauricio Lin <mauricio.lin@indt.org.br> | |
23 | * | |
24 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT | |
25 | * | |
26 | * A new process specific entry (smaps) included in /proc. It shows the | |
27 | * size of rss for each memory area. The maps entry lacks information | |
28 | * about physical memory size (rss) for each mapped file, i.e., | |
29 | * rss information for executables and library files. | |
30 | * This additional information is useful for any tools that need to know | |
31 | * about physical memory consumption for a process specific library. | |
32 | * | |
33 | * Changelog: | |
34 | * 21-Feb-2005 | |
35 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT | |
36 | * Pud inclusion in the page table walking. | |
37 | * | |
38 | * ChangeLog: | |
39 | * 10-Mar-2005 | |
40 | * 10LE Instituto Nokia de Tecnologia - INdT: | |
41 | * A better way to walks through the page table as suggested by Hugh Dickins. | |
42 | * | |
43 | * Simo Piiroinen <simo.piiroinen@nokia.com>: | |
44 | * Smaps information related to shared, private, clean and dirty pages. | |
45 | * | |
46 | * Paul Mundt <paul.mundt@nokia.com>: | |
47 | * Overall revision about smaps. | |
1da177e4 LT |
48 | */ |
49 | ||
50 | #include <asm/uaccess.h> | |
51 | ||
52 | #include <linux/config.h> | |
53 | #include <linux/errno.h> | |
54 | #include <linux/time.h> | |
55 | #include <linux/proc_fs.h> | |
56 | #include <linux/stat.h> | |
57 | #include <linux/init.h> | |
58 | #include <linux/file.h> | |
59 | #include <linux/string.h> | |
60 | #include <linux/seq_file.h> | |
61 | #include <linux/namei.h> | |
62 | #include <linux/namespace.h> | |
63 | #include <linux/mm.h> | |
64 | #include <linux/smp_lock.h> | |
65 | #include <linux/kallsyms.h> | |
66 | #include <linux/mount.h> | |
67 | #include <linux/security.h> | |
68 | #include <linux/ptrace.h> | |
69 | #include <linux/seccomp.h> | |
70 | #include <linux/cpuset.h> | |
71 | #include <linux/audit.h> | |
72 | #include "internal.h" | |
73 | ||
74 | /* | |
75 | * For hysterical raisins we keep the same inumbers as in the old procfs. | |
76 | * Feel free to change the macro below - just keep the range distinct from | |
77 | * inumbers of the rest of procfs (currently those are in 0x0000--0xffff). | |
78 | * As soon as we'll get a separate superblock we will be able to forget | |
79 | * about magical ranges too. | |
80 | */ | |
81 | ||
82 | #define fake_ino(pid,ino) (((pid)<<16)|(ino)) | |
83 | ||
84 | enum pid_directory_inos { | |
85 | PROC_TGID_INO = 2, | |
86 | PROC_TGID_TASK, | |
87 | PROC_TGID_STATUS, | |
88 | PROC_TGID_MEM, | |
89 | #ifdef CONFIG_SECCOMP | |
90 | PROC_TGID_SECCOMP, | |
91 | #endif | |
92 | PROC_TGID_CWD, | |
93 | PROC_TGID_ROOT, | |
94 | PROC_TGID_EXE, | |
95 | PROC_TGID_FD, | |
96 | PROC_TGID_ENVIRON, | |
97 | PROC_TGID_AUXV, | |
98 | PROC_TGID_CMDLINE, | |
99 | PROC_TGID_STAT, | |
100 | PROC_TGID_STATM, | |
101 | PROC_TGID_MAPS, | |
6e21c8f1 | 102 | PROC_TGID_NUMA_MAPS, |
1da177e4 LT |
103 | PROC_TGID_MOUNTS, |
104 | PROC_TGID_WCHAN, | |
e070ad49 | 105 | PROC_TGID_SMAPS, |
1da177e4 LT |
106 | #ifdef CONFIG_SCHEDSTATS |
107 | PROC_TGID_SCHEDSTAT, | |
108 | #endif | |
109 | #ifdef CONFIG_CPUSETS | |
110 | PROC_TGID_CPUSET, | |
111 | #endif | |
112 | #ifdef CONFIG_SECURITY | |
113 | PROC_TGID_ATTR, | |
114 | PROC_TGID_ATTR_CURRENT, | |
115 | PROC_TGID_ATTR_PREV, | |
116 | PROC_TGID_ATTR_EXEC, | |
117 | PROC_TGID_ATTR_FSCREATE, | |
118 | #endif | |
119 | #ifdef CONFIG_AUDITSYSCALL | |
120 | PROC_TGID_LOGINUID, | |
121 | #endif | |
1da177e4 LT |
122 | PROC_TGID_OOM_SCORE, |
123 | PROC_TGID_OOM_ADJUST, | |
124 | PROC_TID_INO, | |
125 | PROC_TID_STATUS, | |
126 | PROC_TID_MEM, | |
127 | #ifdef CONFIG_SECCOMP | |
128 | PROC_TID_SECCOMP, | |
129 | #endif | |
130 | PROC_TID_CWD, | |
131 | PROC_TID_ROOT, | |
132 | PROC_TID_EXE, | |
133 | PROC_TID_FD, | |
134 | PROC_TID_ENVIRON, | |
135 | PROC_TID_AUXV, | |
136 | PROC_TID_CMDLINE, | |
137 | PROC_TID_STAT, | |
138 | PROC_TID_STATM, | |
139 | PROC_TID_MAPS, | |
6e21c8f1 | 140 | PROC_TID_NUMA_MAPS, |
1da177e4 LT |
141 | PROC_TID_MOUNTS, |
142 | PROC_TID_WCHAN, | |
e070ad49 | 143 | PROC_TID_SMAPS, |
1da177e4 LT |
144 | #ifdef CONFIG_SCHEDSTATS |
145 | PROC_TID_SCHEDSTAT, | |
146 | #endif | |
147 | #ifdef CONFIG_CPUSETS | |
148 | PROC_TID_CPUSET, | |
149 | #endif | |
150 | #ifdef CONFIG_SECURITY | |
151 | PROC_TID_ATTR, | |
152 | PROC_TID_ATTR_CURRENT, | |
153 | PROC_TID_ATTR_PREV, | |
154 | PROC_TID_ATTR_EXEC, | |
155 | PROC_TID_ATTR_FSCREATE, | |
156 | #endif | |
157 | #ifdef CONFIG_AUDITSYSCALL | |
158 | PROC_TID_LOGINUID, | |
159 | #endif | |
1da177e4 LT |
160 | PROC_TID_OOM_SCORE, |
161 | PROC_TID_OOM_ADJUST, | |
5e21ccb1 MS |
162 | |
163 | /* Add new entries before this */ | |
164 | PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */ | |
1da177e4 LT |
165 | }; |
166 | ||
167 | struct pid_entry { | |
168 | int type; | |
169 | int len; | |
170 | char *name; | |
171 | mode_t mode; | |
172 | }; | |
173 | ||
174 | #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)} | |
175 | ||
176 | static struct pid_entry tgid_base_stuff[] = { | |
177 | E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO), | |
178 | E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), | |
179 | E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR), | |
180 | E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR), | |
181 | E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO), | |
182 | E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), | |
183 | E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO), | |
184 | E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO), | |
185 | E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO), | |
6e21c8f1 CL |
186 | #ifdef CONFIG_NUMA |
187 | E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), | |
188 | #endif | |
1da177e4 LT |
189 | E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), |
190 | #ifdef CONFIG_SECCOMP | |
191 | E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), | |
192 | #endif | |
193 | E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO), | |
194 | E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO), | |
195 | E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO), | |
196 | E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO), | |
e070ad49 | 197 | E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO), |
1da177e4 LT |
198 | #ifdef CONFIG_SECURITY |
199 | E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), | |
200 | #endif | |
201 | #ifdef CONFIG_KALLSYMS | |
202 | E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO), | |
203 | #endif | |
204 | #ifdef CONFIG_SCHEDSTATS | |
205 | E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO), | |
206 | #endif | |
207 | #ifdef CONFIG_CPUSETS | |
208 | E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO), | |
209 | #endif | |
210 | E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), | |
211 | E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR), | |
212 | #ifdef CONFIG_AUDITSYSCALL | |
213 | E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), | |
214 | #endif | |
215 | {0,0,NULL,0} | |
216 | }; | |
217 | static struct pid_entry tid_base_stuff[] = { | |
218 | E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), | |
219 | E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR), | |
220 | E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR), | |
221 | E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO), | |
222 | E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), | |
223 | E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO), | |
224 | E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO), | |
225 | E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO), | |
6e21c8f1 CL |
226 | #ifdef CONFIG_NUMA |
227 | E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), | |
228 | #endif | |
1da177e4 LT |
229 | E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), |
230 | #ifdef CONFIG_SECCOMP | |
231 | E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), | |
232 | #endif | |
233 | E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO), | |
234 | E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO), | |
235 | E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO), | |
236 | E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO), | |
e070ad49 | 237 | E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO), |
1da177e4 LT |
238 | #ifdef CONFIG_SECURITY |
239 | E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), | |
240 | #endif | |
241 | #ifdef CONFIG_KALLSYMS | |
242 | E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO), | |
243 | #endif | |
244 | #ifdef CONFIG_SCHEDSTATS | |
245 | E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO), | |
246 | #endif | |
247 | #ifdef CONFIG_CPUSETS | |
248 | E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO), | |
249 | #endif | |
250 | E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), | |
251 | E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR), | |
252 | #ifdef CONFIG_AUDITSYSCALL | |
253 | E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), | |
254 | #endif | |
255 | {0,0,NULL,0} | |
256 | }; | |
257 | ||
258 | #ifdef CONFIG_SECURITY | |
259 | static struct pid_entry tgid_attr_stuff[] = { | |
260 | E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), | |
261 | E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), | |
262 | E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), | |
263 | E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), | |
264 | {0,0,NULL,0} | |
265 | }; | |
266 | static struct pid_entry tid_attr_stuff[] = { | |
267 | E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), | |
268 | E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), | |
269 | E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), | |
270 | E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), | |
271 | {0,0,NULL,0} | |
272 | }; | |
273 | #endif | |
274 | ||
275 | #undef E | |
276 | ||
277 | static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) | |
278 | { | |
279 | struct task_struct *task = proc_task(inode); | |
280 | struct files_struct *files; | |
281 | struct file *file; | |
282 | int fd = proc_type(inode) - PROC_TID_FD_DIR; | |
283 | ||
284 | files = get_files_struct(task); | |
285 | if (files) { | |
286 | spin_lock(&files->file_lock); | |
287 | file = fcheck_files(files, fd); | |
288 | if (file) { | |
289 | *mnt = mntget(file->f_vfsmnt); | |
290 | *dentry = dget(file->f_dentry); | |
291 | spin_unlock(&files->file_lock); | |
292 | put_files_struct(files); | |
293 | return 0; | |
294 | } | |
295 | spin_unlock(&files->file_lock); | |
296 | put_files_struct(files); | |
297 | } | |
298 | return -ENOENT; | |
299 | } | |
300 | ||
0494f6ec | 301 | static struct fs_struct *get_fs_struct(struct task_struct *task) |
1da177e4 LT |
302 | { |
303 | struct fs_struct *fs; | |
0494f6ec MS |
304 | task_lock(task); |
305 | fs = task->fs; | |
1da177e4 LT |
306 | if(fs) |
307 | atomic_inc(&fs->count); | |
0494f6ec MS |
308 | task_unlock(task); |
309 | return fs; | |
310 | } | |
311 | ||
312 | static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) | |
313 | { | |
314 | struct fs_struct *fs = get_fs_struct(proc_task(inode)); | |
315 | int result = -ENOENT; | |
1da177e4 LT |
316 | if (fs) { |
317 | read_lock(&fs->lock); | |
318 | *mnt = mntget(fs->pwdmnt); | |
319 | *dentry = dget(fs->pwd); | |
320 | read_unlock(&fs->lock); | |
321 | result = 0; | |
322 | put_fs_struct(fs); | |
323 | } | |
324 | return result; | |
325 | } | |
326 | ||
327 | static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) | |
328 | { | |
0494f6ec | 329 | struct fs_struct *fs = get_fs_struct(proc_task(inode)); |
1da177e4 | 330 | int result = -ENOENT; |
1da177e4 LT |
331 | if (fs) { |
332 | read_lock(&fs->lock); | |
333 | *mnt = mntget(fs->rootmnt); | |
334 | *dentry = dget(fs->root); | |
335 | read_unlock(&fs->lock); | |
336 | result = 0; | |
337 | put_fs_struct(fs); | |
338 | } | |
339 | return result; | |
340 | } | |
341 | ||
342 | #define MAY_PTRACE(task) \ | |
343 | (task == current || \ | |
344 | (task->parent == current && \ | |
345 | (task->ptrace & PT_PTRACED) && \ | |
346 | (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ | |
347 | security_ptrace(current,task) == 0)) | |
348 | ||
349 | static int may_ptrace_attach(struct task_struct *task) | |
350 | { | |
351 | int retval = 0; | |
352 | ||
353 | task_lock(task); | |
354 | ||
355 | if (!task->mm) | |
356 | goto out; | |
357 | if (((current->uid != task->euid) || | |
358 | (current->uid != task->suid) || | |
359 | (current->uid != task->uid) || | |
360 | (current->gid != task->egid) || | |
361 | (current->gid != task->sgid) || | |
362 | (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE)) | |
363 | goto out; | |
364 | rmb(); | |
d6e71144 | 365 | if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE)) |
1da177e4 LT |
366 | goto out; |
367 | if (security_ptrace(current, task)) | |
368 | goto out; | |
369 | ||
370 | retval = 1; | |
371 | out: | |
372 | task_unlock(task); | |
373 | return retval; | |
374 | } | |
375 | ||
376 | static int proc_pid_environ(struct task_struct *task, char * buffer) | |
377 | { | |
378 | int res = 0; | |
379 | struct mm_struct *mm = get_task_mm(task); | |
380 | if (mm) { | |
381 | unsigned int len = mm->env_end - mm->env_start; | |
382 | if (len > PAGE_SIZE) | |
383 | len = PAGE_SIZE; | |
384 | res = access_process_vm(task, mm->env_start, buffer, len, 0); | |
385 | if (!may_ptrace_attach(task)) | |
386 | res = -ESRCH; | |
387 | mmput(mm); | |
388 | } | |
389 | return res; | |
390 | } | |
391 | ||
392 | static int proc_pid_cmdline(struct task_struct *task, char * buffer) | |
393 | { | |
394 | int res = 0; | |
395 | unsigned int len; | |
396 | struct mm_struct *mm = get_task_mm(task); | |
397 | if (!mm) | |
398 | goto out; | |
399 | if (!mm->arg_end) | |
400 | goto out_mm; /* Shh! No looking before we're done */ | |
401 | ||
402 | len = mm->arg_end - mm->arg_start; | |
403 | ||
404 | if (len > PAGE_SIZE) | |
405 | len = PAGE_SIZE; | |
406 | ||
407 | res = access_process_vm(task, mm->arg_start, buffer, len, 0); | |
408 | ||
409 | // If the nul at the end of args has been overwritten, then | |
410 | // assume application is using setproctitle(3). | |
411 | if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { | |
412 | len = strnlen(buffer, res); | |
413 | if (len < res) { | |
414 | res = len; | |
415 | } else { | |
416 | len = mm->env_end - mm->env_start; | |
417 | if (len > PAGE_SIZE - res) | |
418 | len = PAGE_SIZE - res; | |
419 | res += access_process_vm(task, mm->env_start, buffer+res, len, 0); | |
420 | res = strnlen(buffer, res); | |
421 | } | |
422 | } | |
423 | out_mm: | |
424 | mmput(mm); | |
425 | out: | |
426 | return res; | |
427 | } | |
428 | ||
429 | static int proc_pid_auxv(struct task_struct *task, char *buffer) | |
430 | { | |
431 | int res = 0; | |
432 | struct mm_struct *mm = get_task_mm(task); | |
433 | if (mm) { | |
434 | unsigned int nwords = 0; | |
435 | do | |
436 | nwords += 2; | |
437 | while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ | |
438 | res = nwords * sizeof(mm->saved_auxv[0]); | |
439 | if (res > PAGE_SIZE) | |
440 | res = PAGE_SIZE; | |
441 | memcpy(buffer, mm->saved_auxv, res); | |
442 | mmput(mm); | |
443 | } | |
444 | return res; | |
445 | } | |
446 | ||
447 | ||
448 | #ifdef CONFIG_KALLSYMS | |
449 | /* | |
450 | * Provides a wchan file via kallsyms in a proper one-value-per-file format. | |
451 | * Returns the resolved symbol. If that fails, simply return the address. | |
452 | */ | |
453 | static int proc_pid_wchan(struct task_struct *task, char *buffer) | |
454 | { | |
455 | char *modname; | |
456 | const char *sym_name; | |
457 | unsigned long wchan, size, offset; | |
458 | char namebuf[KSYM_NAME_LEN+1]; | |
459 | ||
460 | wchan = get_wchan(task); | |
461 | ||
462 | sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf); | |
463 | if (sym_name) | |
464 | return sprintf(buffer, "%s", sym_name); | |
465 | return sprintf(buffer, "%lu", wchan); | |
466 | } | |
467 | #endif /* CONFIG_KALLSYMS */ | |
468 | ||
469 | #ifdef CONFIG_SCHEDSTATS | |
470 | /* | |
471 | * Provides /proc/PID/schedstat | |
472 | */ | |
473 | static int proc_pid_schedstat(struct task_struct *task, char *buffer) | |
474 | { | |
475 | return sprintf(buffer, "%lu %lu %lu\n", | |
476 | task->sched_info.cpu_time, | |
477 | task->sched_info.run_delay, | |
478 | task->sched_info.pcnt); | |
479 | } | |
480 | #endif | |
481 | ||
482 | /* The badness from the OOM killer */ | |
483 | unsigned long badness(struct task_struct *p, unsigned long uptime); | |
484 | static int proc_oom_score(struct task_struct *task, char *buffer) | |
485 | { | |
486 | unsigned long points; | |
487 | struct timespec uptime; | |
488 | ||
489 | do_posix_clock_monotonic_gettime(&uptime); | |
490 | points = badness(task, uptime.tv_sec); | |
491 | return sprintf(buffer, "%lu\n", points); | |
492 | } | |
493 | ||
494 | /************************************************************************/ | |
495 | /* Here the fs part begins */ | |
496 | /************************************************************************/ | |
497 | ||
498 | /* permission checks */ | |
499 | ||
500 | static int proc_check_root(struct inode *inode) | |
501 | { | |
502 | struct dentry *de, *base, *root; | |
503 | struct vfsmount *our_vfsmnt, *vfsmnt, *mnt; | |
504 | int res = 0; | |
505 | ||
506 | if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */ | |
507 | return -ENOENT; | |
508 | read_lock(¤t->fs->lock); | |
509 | our_vfsmnt = mntget(current->fs->rootmnt); | |
510 | base = dget(current->fs->root); | |
511 | read_unlock(¤t->fs->lock); | |
512 | ||
513 | spin_lock(&vfsmount_lock); | |
514 | de = root; | |
515 | mnt = vfsmnt; | |
516 | ||
517 | while (vfsmnt != our_vfsmnt) { | |
518 | if (vfsmnt == vfsmnt->mnt_parent) | |
519 | goto out; | |
520 | de = vfsmnt->mnt_mountpoint; | |
521 | vfsmnt = vfsmnt->mnt_parent; | |
522 | } | |
523 | ||
524 | if (!is_subdir(de, base)) | |
525 | goto out; | |
526 | spin_unlock(&vfsmount_lock); | |
527 | ||
528 | exit: | |
529 | dput(base); | |
530 | mntput(our_vfsmnt); | |
531 | dput(root); | |
532 | mntput(mnt); | |
533 | return res; | |
534 | out: | |
535 | spin_unlock(&vfsmount_lock); | |
536 | res = -EACCES; | |
537 | goto exit; | |
538 | } | |
539 | ||
540 | static int proc_permission(struct inode *inode, int mask, struct nameidata *nd) | |
541 | { | |
542 | if (generic_permission(inode, mask, NULL) != 0) | |
543 | return -EACCES; | |
544 | return proc_check_root(inode); | |
545 | } | |
546 | ||
547 | extern struct seq_operations proc_pid_maps_op; | |
548 | static int maps_open(struct inode *inode, struct file *file) | |
549 | { | |
550 | struct task_struct *task = proc_task(inode); | |
551 | int ret = seq_open(file, &proc_pid_maps_op); | |
552 | if (!ret) { | |
553 | struct seq_file *m = file->private_data; | |
554 | m->private = task; | |
555 | } | |
556 | return ret; | |
557 | } | |
558 | ||
559 | static struct file_operations proc_maps_operations = { | |
560 | .open = maps_open, | |
561 | .read = seq_read, | |
562 | .llseek = seq_lseek, | |
563 | .release = seq_release, | |
564 | }; | |
565 | ||
6e21c8f1 CL |
566 | #ifdef CONFIG_NUMA |
567 | extern struct seq_operations proc_pid_numa_maps_op; | |
568 | static int numa_maps_open(struct inode *inode, struct file *file) | |
569 | { | |
570 | struct task_struct *task = proc_task(inode); | |
571 | int ret = seq_open(file, &proc_pid_numa_maps_op); | |
572 | if (!ret) { | |
573 | struct seq_file *m = file->private_data; | |
574 | m->private = task; | |
575 | } | |
576 | return ret; | |
577 | } | |
578 | ||
579 | static struct file_operations proc_numa_maps_operations = { | |
580 | .open = numa_maps_open, | |
581 | .read = seq_read, | |
582 | .llseek = seq_lseek, | |
583 | .release = seq_release, | |
584 | }; | |
585 | #endif | |
586 | ||
e070ad49 ML |
587 | extern struct seq_operations proc_pid_smaps_op; |
588 | static int smaps_open(struct inode *inode, struct file *file) | |
589 | { | |
590 | struct task_struct *task = proc_task(inode); | |
591 | int ret = seq_open(file, &proc_pid_smaps_op); | |
592 | if (!ret) { | |
593 | struct seq_file *m = file->private_data; | |
594 | m->private = task; | |
595 | } | |
596 | return ret; | |
597 | } | |
598 | ||
599 | static struct file_operations proc_smaps_operations = { | |
600 | .open = smaps_open, | |
601 | .read = seq_read, | |
602 | .llseek = seq_lseek, | |
603 | .release = seq_release, | |
604 | }; | |
605 | ||
1da177e4 LT |
606 | extern struct seq_operations mounts_op; |
607 | static int mounts_open(struct inode *inode, struct file *file) | |
608 | { | |
609 | struct task_struct *task = proc_task(inode); | |
610 | int ret = seq_open(file, &mounts_op); | |
611 | ||
612 | if (!ret) { | |
613 | struct seq_file *m = file->private_data; | |
614 | struct namespace *namespace; | |
615 | task_lock(task); | |
616 | namespace = task->namespace; | |
617 | if (namespace) | |
618 | get_namespace(namespace); | |
619 | task_unlock(task); | |
620 | ||
621 | if (namespace) | |
622 | m->private = namespace; | |
623 | else { | |
624 | seq_release(inode, file); | |
625 | ret = -EINVAL; | |
626 | } | |
627 | } | |
628 | return ret; | |
629 | } | |
630 | ||
631 | static int mounts_release(struct inode *inode, struct file *file) | |
632 | { | |
633 | struct seq_file *m = file->private_data; | |
634 | struct namespace *namespace = m->private; | |
635 | put_namespace(namespace); | |
636 | return seq_release(inode, file); | |
637 | } | |
638 | ||
639 | static struct file_operations proc_mounts_operations = { | |
640 | .open = mounts_open, | |
641 | .read = seq_read, | |
642 | .llseek = seq_lseek, | |
643 | .release = mounts_release, | |
644 | }; | |
645 | ||
646 | #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ | |
647 | ||
648 | static ssize_t proc_info_read(struct file * file, char __user * buf, | |
649 | size_t count, loff_t *ppos) | |
650 | { | |
651 | struct inode * inode = file->f_dentry->d_inode; | |
652 | unsigned long page; | |
653 | ssize_t length; | |
654 | struct task_struct *task = proc_task(inode); | |
655 | ||
656 | if (count > PROC_BLOCK_SIZE) | |
657 | count = PROC_BLOCK_SIZE; | |
658 | if (!(page = __get_free_page(GFP_KERNEL))) | |
659 | return -ENOMEM; | |
660 | ||
661 | length = PROC_I(inode)->op.proc_read(task, (char*)page); | |
662 | ||
663 | if (length >= 0) | |
664 | length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); | |
665 | free_page(page); | |
666 | return length; | |
667 | } | |
668 | ||
669 | static struct file_operations proc_info_file_operations = { | |
670 | .read = proc_info_read, | |
671 | }; | |
672 | ||
673 | static int mem_open(struct inode* inode, struct file* file) | |
674 | { | |
675 | file->private_data = (void*)((long)current->self_exec_id); | |
676 | return 0; | |
677 | } | |
678 | ||
679 | static ssize_t mem_read(struct file * file, char __user * buf, | |
680 | size_t count, loff_t *ppos) | |
681 | { | |
682 | struct task_struct *task = proc_task(file->f_dentry->d_inode); | |
683 | char *page; | |
684 | unsigned long src = *ppos; | |
685 | int ret = -ESRCH; | |
686 | struct mm_struct *mm; | |
687 | ||
688 | if (!MAY_PTRACE(task) || !may_ptrace_attach(task)) | |
689 | goto out; | |
690 | ||
691 | ret = -ENOMEM; | |
692 | page = (char *)__get_free_page(GFP_USER); | |
693 | if (!page) | |
694 | goto out; | |
695 | ||
696 | ret = 0; | |
697 | ||
698 | mm = get_task_mm(task); | |
699 | if (!mm) | |
700 | goto out_free; | |
701 | ||
702 | ret = -EIO; | |
703 | ||
704 | if (file->private_data != (void*)((long)current->self_exec_id)) | |
705 | goto out_put; | |
706 | ||
707 | ret = 0; | |
708 | ||
709 | while (count > 0) { | |
710 | int this_len, retval; | |
711 | ||
712 | this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; | |
713 | retval = access_process_vm(task, src, page, this_len, 0); | |
714 | if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) { | |
715 | if (!ret) | |
716 | ret = -EIO; | |
717 | break; | |
718 | } | |
719 | ||
720 | if (copy_to_user(buf, page, retval)) { | |
721 | ret = -EFAULT; | |
722 | break; | |
723 | } | |
724 | ||
725 | ret += retval; | |
726 | src += retval; | |
727 | buf += retval; | |
728 | count -= retval; | |
729 | } | |
730 | *ppos = src; | |
731 | ||
732 | out_put: | |
733 | mmput(mm); | |
734 | out_free: | |
735 | free_page((unsigned long) page); | |
736 | out: | |
737 | return ret; | |
738 | } | |
739 | ||
740 | #define mem_write NULL | |
741 | ||
742 | #ifndef mem_write | |
743 | /* This is a security hazard */ | |
744 | static ssize_t mem_write(struct file * file, const char * buf, | |
745 | size_t count, loff_t *ppos) | |
746 | { | |
747 | int copied = 0; | |
748 | char *page; | |
749 | struct task_struct *task = proc_task(file->f_dentry->d_inode); | |
750 | unsigned long dst = *ppos; | |
751 | ||
752 | if (!MAY_PTRACE(task) || !may_ptrace_attach(task)) | |
753 | return -ESRCH; | |
754 | ||
755 | page = (char *)__get_free_page(GFP_USER); | |
756 | if (!page) | |
757 | return -ENOMEM; | |
758 | ||
759 | while (count > 0) { | |
760 | int this_len, retval; | |
761 | ||
762 | this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; | |
763 | if (copy_from_user(page, buf, this_len)) { | |
764 | copied = -EFAULT; | |
765 | break; | |
766 | } | |
767 | retval = access_process_vm(task, dst, page, this_len, 1); | |
768 | if (!retval) { | |
769 | if (!copied) | |
770 | copied = -EIO; | |
771 | break; | |
772 | } | |
773 | copied += retval; | |
774 | buf += retval; | |
775 | dst += retval; | |
776 | count -= retval; | |
777 | } | |
778 | *ppos = dst; | |
779 | free_page((unsigned long) page); | |
780 | return copied; | |
781 | } | |
782 | #endif | |
783 | ||
784 | static loff_t mem_lseek(struct file * file, loff_t offset, int orig) | |
785 | { | |
786 | switch (orig) { | |
787 | case 0: | |
788 | file->f_pos = offset; | |
789 | break; | |
790 | case 1: | |
791 | file->f_pos += offset; | |
792 | break; | |
793 | default: | |
794 | return -EINVAL; | |
795 | } | |
796 | force_successful_syscall_return(); | |
797 | return file->f_pos; | |
798 | } | |
799 | ||
800 | static struct file_operations proc_mem_operations = { | |
801 | .llseek = mem_lseek, | |
802 | .read = mem_read, | |
803 | .write = mem_write, | |
804 | .open = mem_open, | |
805 | }; | |
806 | ||
807 | static ssize_t oom_adjust_read(struct file *file, char __user *buf, | |
808 | size_t count, loff_t *ppos) | |
809 | { | |
810 | struct task_struct *task = proc_task(file->f_dentry->d_inode); | |
811 | char buffer[8]; | |
812 | size_t len; | |
813 | int oom_adjust = task->oomkilladj; | |
814 | loff_t __ppos = *ppos; | |
815 | ||
816 | len = sprintf(buffer, "%i\n", oom_adjust); | |
817 | if (__ppos >= len) | |
818 | return 0; | |
819 | if (count > len-__ppos) | |
820 | count = len-__ppos; | |
821 | if (copy_to_user(buf, buffer + __ppos, count)) | |
822 | return -EFAULT; | |
823 | *ppos = __ppos + count; | |
824 | return count; | |
825 | } | |
826 | ||
827 | static ssize_t oom_adjust_write(struct file *file, const char __user *buf, | |
828 | size_t count, loff_t *ppos) | |
829 | { | |
830 | struct task_struct *task = proc_task(file->f_dentry->d_inode); | |
831 | char buffer[8], *end; | |
832 | int oom_adjust; | |
833 | ||
834 | if (!capable(CAP_SYS_RESOURCE)) | |
835 | return -EPERM; | |
836 | memset(buffer, 0, 8); | |
837 | if (count > 6) | |
838 | count = 6; | |
839 | if (copy_from_user(buffer, buf, count)) | |
840 | return -EFAULT; | |
841 | oom_adjust = simple_strtol(buffer, &end, 0); | |
79befd0c | 842 | if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE) |
1da177e4 LT |
843 | return -EINVAL; |
844 | if (*end == '\n') | |
845 | end++; | |
846 | task->oomkilladj = oom_adjust; | |
847 | if (end - buffer == 0) | |
848 | return -EIO; | |
849 | return end - buffer; | |
850 | } | |
851 | ||
852 | static struct file_operations proc_oom_adjust_operations = { | |
853 | .read = oom_adjust_read, | |
854 | .write = oom_adjust_write, | |
855 | }; | |
856 | ||
857 | static struct inode_operations proc_mem_inode_operations = { | |
858 | .permission = proc_permission, | |
859 | }; | |
860 | ||
861 | #ifdef CONFIG_AUDITSYSCALL | |
862 | #define TMPBUFLEN 21 | |
863 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, | |
864 | size_t count, loff_t *ppos) | |
865 | { | |
866 | struct inode * inode = file->f_dentry->d_inode; | |
867 | struct task_struct *task = proc_task(inode); | |
868 | ssize_t length; | |
869 | char tmpbuf[TMPBUFLEN]; | |
870 | ||
871 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", | |
872 | audit_get_loginuid(task->audit_context)); | |
873 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); | |
874 | } | |
875 | ||
876 | static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, | |
877 | size_t count, loff_t *ppos) | |
878 | { | |
879 | struct inode * inode = file->f_dentry->d_inode; | |
880 | char *page, *tmp; | |
881 | ssize_t length; | |
882 | struct task_struct *task = proc_task(inode); | |
883 | uid_t loginuid; | |
884 | ||
885 | if (!capable(CAP_AUDIT_CONTROL)) | |
886 | return -EPERM; | |
887 | ||
888 | if (current != task) | |
889 | return -EPERM; | |
890 | ||
891 | if (count > PAGE_SIZE) | |
892 | count = PAGE_SIZE; | |
893 | ||
894 | if (*ppos != 0) { | |
895 | /* No partial writes. */ | |
896 | return -EINVAL; | |
897 | } | |
898 | page = (char*)__get_free_page(GFP_USER); | |
899 | if (!page) | |
900 | return -ENOMEM; | |
901 | length = -EFAULT; | |
902 | if (copy_from_user(page, buf, count)) | |
903 | goto out_free_page; | |
904 | ||
905 | loginuid = simple_strtoul(page, &tmp, 10); | |
906 | if (tmp == page) { | |
907 | length = -EINVAL; | |
908 | goto out_free_page; | |
909 | ||
910 | } | |
456be6cd | 911 | length = audit_set_loginuid(task, loginuid); |
1da177e4 LT |
912 | if (likely(length == 0)) |
913 | length = count; | |
914 | ||
915 | out_free_page: | |
916 | free_page((unsigned long) page); | |
917 | return length; | |
918 | } | |
919 | ||
920 | static struct file_operations proc_loginuid_operations = { | |
921 | .read = proc_loginuid_read, | |
922 | .write = proc_loginuid_write, | |
923 | }; | |
924 | #endif | |
925 | ||
926 | #ifdef CONFIG_SECCOMP | |
927 | static ssize_t seccomp_read(struct file *file, char __user *buf, | |
928 | size_t count, loff_t *ppos) | |
929 | { | |
930 | struct task_struct *tsk = proc_task(file->f_dentry->d_inode); | |
931 | char __buf[20]; | |
932 | loff_t __ppos = *ppos; | |
933 | size_t len; | |
934 | ||
935 | /* no need to print the trailing zero, so use only len */ | |
936 | len = sprintf(__buf, "%u\n", tsk->seccomp.mode); | |
937 | if (__ppos >= len) | |
938 | return 0; | |
939 | if (count > len - __ppos) | |
940 | count = len - __ppos; | |
941 | if (copy_to_user(buf, __buf + __ppos, count)) | |
942 | return -EFAULT; | |
943 | *ppos = __ppos + count; | |
944 | return count; | |
945 | } | |
946 | ||
947 | static ssize_t seccomp_write(struct file *file, const char __user *buf, | |
948 | size_t count, loff_t *ppos) | |
949 | { | |
950 | struct task_struct *tsk = proc_task(file->f_dentry->d_inode); | |
951 | char __buf[20], *end; | |
952 | unsigned int seccomp_mode; | |
953 | ||
954 | /* can set it only once to be even more secure */ | |
955 | if (unlikely(tsk->seccomp.mode)) | |
956 | return -EPERM; | |
957 | ||
958 | memset(__buf, 0, sizeof(__buf)); | |
959 | count = min(count, sizeof(__buf) - 1); | |
960 | if (copy_from_user(__buf, buf, count)) | |
961 | return -EFAULT; | |
962 | seccomp_mode = simple_strtoul(__buf, &end, 0); | |
963 | if (*end == '\n') | |
964 | end++; | |
965 | if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { | |
966 | tsk->seccomp.mode = seccomp_mode; | |
967 | set_tsk_thread_flag(tsk, TIF_SECCOMP); | |
968 | } else | |
969 | return -EINVAL; | |
970 | if (unlikely(!(end - __buf))) | |
971 | return -EIO; | |
972 | return end - __buf; | |
973 | } | |
974 | ||
975 | static struct file_operations proc_seccomp_operations = { | |
976 | .read = seccomp_read, | |
977 | .write = seccomp_write, | |
978 | }; | |
979 | #endif /* CONFIG_SECCOMP */ | |
980 | ||
008b150a | 981 | static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
982 | { |
983 | struct inode *inode = dentry->d_inode; | |
984 | int error = -EACCES; | |
985 | ||
986 | /* We don't need a base pointer in the /proc filesystem */ | |
987 | path_release(nd); | |
988 | ||
989 | if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE)) | |
990 | goto out; | |
991 | error = proc_check_root(inode); | |
992 | if (error) | |
993 | goto out; | |
994 | ||
995 | error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); | |
996 | nd->last_type = LAST_BIND; | |
997 | out: | |
008b150a | 998 | return ERR_PTR(error); |
1da177e4 LT |
999 | } |
1000 | ||
1001 | static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, | |
1002 | char __user *buffer, int buflen) | |
1003 | { | |
1004 | struct inode * inode; | |
1005 | char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; | |
1006 | int len; | |
1007 | ||
1008 | if (!tmp) | |
1009 | return -ENOMEM; | |
1010 | ||
1011 | inode = dentry->d_inode; | |
1012 | path = d_path(dentry, mnt, tmp, PAGE_SIZE); | |
1013 | len = PTR_ERR(path); | |
1014 | if (IS_ERR(path)) | |
1015 | goto out; | |
1016 | len = tmp + PAGE_SIZE - 1 - path; | |
1017 | ||
1018 | if (len > buflen) | |
1019 | len = buflen; | |
1020 | if (copy_to_user(buffer, path, len)) | |
1021 | len = -EFAULT; | |
1022 | out: | |
1023 | free_page((unsigned long)tmp); | |
1024 | return len; | |
1025 | } | |
1026 | ||
1027 | static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) | |
1028 | { | |
1029 | int error = -EACCES; | |
1030 | struct inode *inode = dentry->d_inode; | |
1031 | struct dentry *de; | |
1032 | struct vfsmount *mnt = NULL; | |
1033 | ||
1034 | lock_kernel(); | |
1035 | ||
1036 | if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE)) | |
1037 | goto out; | |
1038 | error = proc_check_root(inode); | |
1039 | if (error) | |
1040 | goto out; | |
1041 | ||
1042 | error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); | |
1043 | if (error) | |
1044 | goto out; | |
1045 | ||
1046 | error = do_proc_readlink(de, mnt, buffer, buflen); | |
1047 | dput(de); | |
1048 | mntput(mnt); | |
1049 | out: | |
1050 | unlock_kernel(); | |
1051 | return error; | |
1052 | } | |
1053 | ||
1054 | static struct inode_operations proc_pid_link_inode_operations = { | |
1055 | .readlink = proc_pid_readlink, | |
1056 | .follow_link = proc_pid_follow_link | |
1057 | }; | |
1058 | ||
1059 | #define NUMBUF 10 | |
1060 | ||
1061 | static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) | |
1062 | { | |
1063 | struct inode *inode = filp->f_dentry->d_inode; | |
1064 | struct task_struct *p = proc_task(inode); | |
1065 | unsigned int fd, tid, ino; | |
1066 | int retval; | |
1067 | char buf[NUMBUF]; | |
1068 | struct files_struct * files; | |
1069 | ||
1070 | retval = -ENOENT; | |
1071 | if (!pid_alive(p)) | |
1072 | goto out; | |
1073 | retval = 0; | |
1074 | tid = p->pid; | |
1075 | ||
1076 | fd = filp->f_pos; | |
1077 | switch (fd) { | |
1078 | case 0: | |
1079 | if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) | |
1080 | goto out; | |
1081 | filp->f_pos++; | |
1082 | case 1: | |
1083 | ino = fake_ino(tid, PROC_TID_INO); | |
1084 | if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) | |
1085 | goto out; | |
1086 | filp->f_pos++; | |
1087 | default: | |
1088 | files = get_files_struct(p); | |
1089 | if (!files) | |
1090 | goto out; | |
1091 | spin_lock(&files->file_lock); | |
1092 | for (fd = filp->f_pos-2; | |
1093 | fd < files->max_fds; | |
1094 | fd++, filp->f_pos++) { | |
1095 | unsigned int i,j; | |
1096 | ||
1097 | if (!fcheck_files(files, fd)) | |
1098 | continue; | |
1099 | spin_unlock(&files->file_lock); | |
1100 | ||
1101 | j = NUMBUF; | |
1102 | i = fd; | |
1103 | do { | |
1104 | j--; | |
1105 | buf[j] = '0' + (i % 10); | |
1106 | i /= 10; | |
1107 | } while (i); | |
1108 | ||
1109 | ino = fake_ino(tid, PROC_TID_FD_DIR + fd); | |
1110 | if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) { | |
1111 | spin_lock(&files->file_lock); | |
1112 | break; | |
1113 | } | |
1114 | spin_lock(&files->file_lock); | |
1115 | } | |
1116 | spin_unlock(&files->file_lock); | |
1117 | put_files_struct(files); | |
1118 | } | |
1119 | out: | |
1120 | return retval; | |
1121 | } | |
1122 | ||
1123 | static int proc_pident_readdir(struct file *filp, | |
1124 | void *dirent, filldir_t filldir, | |
1125 | struct pid_entry *ents, unsigned int nents) | |
1126 | { | |
1127 | int i; | |
1128 | int pid; | |
1129 | struct dentry *dentry = filp->f_dentry; | |
1130 | struct inode *inode = dentry->d_inode; | |
1131 | struct pid_entry *p; | |
1132 | ino_t ino; | |
1133 | int ret; | |
1134 | ||
1135 | ret = -ENOENT; | |
1136 | if (!pid_alive(proc_task(inode))) | |
1137 | goto out; | |
1138 | ||
1139 | ret = 0; | |
1140 | pid = proc_task(inode)->pid; | |
1141 | i = filp->f_pos; | |
1142 | switch (i) { | |
1143 | case 0: | |
1144 | ino = inode->i_ino; | |
1145 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) | |
1146 | goto out; | |
1147 | i++; | |
1148 | filp->f_pos++; | |
1149 | /* fall through */ | |
1150 | case 1: | |
1151 | ino = parent_ino(dentry); | |
1152 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) | |
1153 | goto out; | |
1154 | i++; | |
1155 | filp->f_pos++; | |
1156 | /* fall through */ | |
1157 | default: | |
1158 | i -= 2; | |
1159 | if (i >= nents) { | |
1160 | ret = 1; | |
1161 | goto out; | |
1162 | } | |
1163 | p = ents + i; | |
1164 | while (p->name) { | |
1165 | if (filldir(dirent, p->name, p->len, filp->f_pos, | |
1166 | fake_ino(pid, p->type), p->mode >> 12) < 0) | |
1167 | goto out; | |
1168 | filp->f_pos++; | |
1169 | p++; | |
1170 | } | |
1171 | } | |
1172 | ||
1173 | ret = 1; | |
1174 | out: | |
1175 | return ret; | |
1176 | } | |
1177 | ||
1178 | static int proc_tgid_base_readdir(struct file * filp, | |
1179 | void * dirent, filldir_t filldir) | |
1180 | { | |
1181 | return proc_pident_readdir(filp,dirent,filldir, | |
1182 | tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); | |
1183 | } | |
1184 | ||
1185 | static int proc_tid_base_readdir(struct file * filp, | |
1186 | void * dirent, filldir_t filldir) | |
1187 | { | |
1188 | return proc_pident_readdir(filp,dirent,filldir, | |
1189 | tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); | |
1190 | } | |
1191 | ||
1192 | /* building an inode */ | |
1193 | ||
1194 | static int task_dumpable(struct task_struct *task) | |
1195 | { | |
1196 | int dumpable = 0; | |
1197 | struct mm_struct *mm; | |
1198 | ||
1199 | task_lock(task); | |
1200 | mm = task->mm; | |
1201 | if (mm) | |
1202 | dumpable = mm->dumpable; | |
1203 | task_unlock(task); | |
d6e71144 AC |
1204 | if(dumpable == 1) |
1205 | return 1; | |
1206 | return 0; | |
1da177e4 LT |
1207 | } |
1208 | ||
1209 | ||
1210 | static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino) | |
1211 | { | |
1212 | struct inode * inode; | |
1213 | struct proc_inode *ei; | |
1214 | ||
1215 | /* We need a new inode */ | |
1216 | ||
1217 | inode = new_inode(sb); | |
1218 | if (!inode) | |
1219 | goto out; | |
1220 | ||
1221 | /* Common stuff */ | |
1222 | ei = PROC_I(inode); | |
1223 | ei->task = NULL; | |
1224 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
1225 | inode->i_ino = fake_ino(task->pid, ino); | |
1226 | ||
1227 | if (!pid_alive(task)) | |
1228 | goto out_unlock; | |
1229 | ||
1230 | /* | |
1231 | * grab the reference to task. | |
1232 | */ | |
1233 | get_task_struct(task); | |
1234 | ei->task = task; | |
1235 | ei->type = ino; | |
1236 | inode->i_uid = 0; | |
1237 | inode->i_gid = 0; | |
1238 | if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) { | |
1239 | inode->i_uid = task->euid; | |
1240 | inode->i_gid = task->egid; | |
1241 | } | |
1242 | security_task_to_inode(task, inode); | |
1243 | ||
1244 | out: | |
1245 | return inode; | |
1246 | ||
1247 | out_unlock: | |
1248 | ei->pde = NULL; | |
1249 | iput(inode); | |
1250 | return NULL; | |
1251 | } | |
1252 | ||
1253 | /* dentry stuff */ | |
1254 | ||
1255 | /* | |
1256 | * Exceptional case: normally we are not allowed to unhash a busy | |
1257 | * directory. In this case, however, we can do it - no aliasing problems | |
1258 | * due to the way we treat inodes. | |
1259 | * | |
1260 | * Rewrite the inode's ownerships here because the owning task may have | |
1261 | * performed a setuid(), etc. | |
1262 | */ | |
1263 | static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) | |
1264 | { | |
1265 | struct inode *inode = dentry->d_inode; | |
1266 | struct task_struct *task = proc_task(inode); | |
1267 | if (pid_alive(task)) { | |
1268 | if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) { | |
1269 | inode->i_uid = task->euid; | |
1270 | inode->i_gid = task->egid; | |
1271 | } else { | |
1272 | inode->i_uid = 0; | |
1273 | inode->i_gid = 0; | |
1274 | } | |
1275 | security_task_to_inode(task, inode); | |
1276 | return 1; | |
1277 | } | |
1278 | d_drop(dentry); | |
1279 | return 0; | |
1280 | } | |
1281 | ||
1282 | static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) | |
1283 | { | |
1284 | struct inode *inode = dentry->d_inode; | |
1285 | struct task_struct *task = proc_task(inode); | |
1286 | int fd = proc_type(inode) - PROC_TID_FD_DIR; | |
1287 | struct files_struct *files; | |
1288 | ||
1289 | files = get_files_struct(task); | |
1290 | if (files) { | |
1291 | spin_lock(&files->file_lock); | |
1292 | if (fcheck_files(files, fd)) { | |
1293 | spin_unlock(&files->file_lock); | |
1294 | put_files_struct(files); | |
1295 | if (task_dumpable(task)) { | |
1296 | inode->i_uid = task->euid; | |
1297 | inode->i_gid = task->egid; | |
1298 | } else { | |
1299 | inode->i_uid = 0; | |
1300 | inode->i_gid = 0; | |
1301 | } | |
1302 | security_task_to_inode(task, inode); | |
1303 | return 1; | |
1304 | } | |
1305 | spin_unlock(&files->file_lock); | |
1306 | put_files_struct(files); | |
1307 | } | |
1308 | d_drop(dentry); | |
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | static void pid_base_iput(struct dentry *dentry, struct inode *inode) | |
1313 | { | |
1314 | struct task_struct *task = proc_task(inode); | |
1315 | spin_lock(&task->proc_lock); | |
1316 | if (task->proc_dentry == dentry) | |
1317 | task->proc_dentry = NULL; | |
1318 | spin_unlock(&task->proc_lock); | |
1319 | iput(inode); | |
1320 | } | |
1321 | ||
1322 | static int pid_delete_dentry(struct dentry * dentry) | |
1323 | { | |
1324 | /* Is the task we represent dead? | |
1325 | * If so, then don't put the dentry on the lru list, | |
1326 | * kill it immediately. | |
1327 | */ | |
1328 | return !pid_alive(proc_task(dentry->d_inode)); | |
1329 | } | |
1330 | ||
1331 | static struct dentry_operations tid_fd_dentry_operations = | |
1332 | { | |
1333 | .d_revalidate = tid_fd_revalidate, | |
1334 | .d_delete = pid_delete_dentry, | |
1335 | }; | |
1336 | ||
1337 | static struct dentry_operations pid_dentry_operations = | |
1338 | { | |
1339 | .d_revalidate = pid_revalidate, | |
1340 | .d_delete = pid_delete_dentry, | |
1341 | }; | |
1342 | ||
1343 | static struct dentry_operations pid_base_dentry_operations = | |
1344 | { | |
1345 | .d_revalidate = pid_revalidate, | |
1346 | .d_iput = pid_base_iput, | |
1347 | .d_delete = pid_delete_dentry, | |
1348 | }; | |
1349 | ||
1350 | /* Lookups */ | |
1351 | ||
1352 | static unsigned name_to_int(struct dentry *dentry) | |
1353 | { | |
1354 | const char *name = dentry->d_name.name; | |
1355 | int len = dentry->d_name.len; | |
1356 | unsigned n = 0; | |
1357 | ||
1358 | if (len > 1 && *name == '0') | |
1359 | goto out; | |
1360 | while (len-- > 0) { | |
1361 | unsigned c = *name++ - '0'; | |
1362 | if (c > 9) | |
1363 | goto out; | |
1364 | if (n >= (~0U-9)/10) | |
1365 | goto out; | |
1366 | n *= 10; | |
1367 | n += c; | |
1368 | } | |
1369 | return n; | |
1370 | out: | |
1371 | return ~0U; | |
1372 | } | |
1373 | ||
1374 | /* SMP-safe */ | |
1375 | static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd) | |
1376 | { | |
1377 | struct task_struct *task = proc_task(dir); | |
1378 | unsigned fd = name_to_int(dentry); | |
1379 | struct file * file; | |
1380 | struct files_struct * files; | |
1381 | struct inode *inode; | |
1382 | struct proc_inode *ei; | |
1383 | ||
1384 | if (fd == ~0U) | |
1385 | goto out; | |
1386 | if (!pid_alive(task)) | |
1387 | goto out; | |
1388 | ||
1389 | inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd); | |
1390 | if (!inode) | |
1391 | goto out; | |
1392 | ei = PROC_I(inode); | |
1393 | files = get_files_struct(task); | |
1394 | if (!files) | |
1395 | goto out_unlock; | |
1396 | inode->i_mode = S_IFLNK; | |
1397 | spin_lock(&files->file_lock); | |
1398 | file = fcheck_files(files, fd); | |
1399 | if (!file) | |
1400 | goto out_unlock2; | |
1401 | if (file->f_mode & 1) | |
1402 | inode->i_mode |= S_IRUSR | S_IXUSR; | |
1403 | if (file->f_mode & 2) | |
1404 | inode->i_mode |= S_IWUSR | S_IXUSR; | |
1405 | spin_unlock(&files->file_lock); | |
1406 | put_files_struct(files); | |
1407 | inode->i_op = &proc_pid_link_inode_operations; | |
1408 | inode->i_size = 64; | |
1409 | ei->op.proc_get_link = proc_fd_link; | |
1410 | dentry->d_op = &tid_fd_dentry_operations; | |
1411 | d_add(dentry, inode); | |
1412 | return NULL; | |
1413 | ||
1414 | out_unlock2: | |
1415 | spin_unlock(&files->file_lock); | |
1416 | put_files_struct(files); | |
1417 | out_unlock: | |
1418 | iput(inode); | |
1419 | out: | |
1420 | return ERR_PTR(-ENOENT); | |
1421 | } | |
1422 | ||
1423 | static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir); | |
1424 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd); | |
1425 | ||
1426 | static struct file_operations proc_fd_operations = { | |
1427 | .read = generic_read_dir, | |
1428 | .readdir = proc_readfd, | |
1429 | }; | |
1430 | ||
1431 | static struct file_operations proc_task_operations = { | |
1432 | .read = generic_read_dir, | |
1433 | .readdir = proc_task_readdir, | |
1434 | }; | |
1435 | ||
1436 | /* | |
1437 | * proc directories can do almost nothing.. | |
1438 | */ | |
1439 | static struct inode_operations proc_fd_inode_operations = { | |
1440 | .lookup = proc_lookupfd, | |
1441 | .permission = proc_permission, | |
1442 | }; | |
1443 | ||
1444 | static struct inode_operations proc_task_inode_operations = { | |
1445 | .lookup = proc_task_lookup, | |
1446 | .permission = proc_permission, | |
1447 | }; | |
1448 | ||
1449 | #ifdef CONFIG_SECURITY | |
1450 | static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, | |
1451 | size_t count, loff_t *ppos) | |
1452 | { | |
1453 | struct inode * inode = file->f_dentry->d_inode; | |
1454 | unsigned long page; | |
1455 | ssize_t length; | |
1456 | struct task_struct *task = proc_task(inode); | |
1457 | ||
1458 | if (count > PAGE_SIZE) | |
1459 | count = PAGE_SIZE; | |
1460 | if (!(page = __get_free_page(GFP_KERNEL))) | |
1461 | return -ENOMEM; | |
1462 | ||
1463 | length = security_getprocattr(task, | |
1464 | (char*)file->f_dentry->d_name.name, | |
1465 | (void*)page, count); | |
1466 | if (length >= 0) | |
1467 | length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); | |
1468 | free_page(page); | |
1469 | return length; | |
1470 | } | |
1471 | ||
1472 | static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, | |
1473 | size_t count, loff_t *ppos) | |
1474 | { | |
1475 | struct inode * inode = file->f_dentry->d_inode; | |
1476 | char *page; | |
1477 | ssize_t length; | |
1478 | struct task_struct *task = proc_task(inode); | |
1479 | ||
1480 | if (count > PAGE_SIZE) | |
1481 | count = PAGE_SIZE; | |
1482 | if (*ppos != 0) { | |
1483 | /* No partial writes. */ | |
1484 | return -EINVAL; | |
1485 | } | |
1486 | page = (char*)__get_free_page(GFP_USER); | |
1487 | if (!page) | |
1488 | return -ENOMEM; | |
1489 | length = -EFAULT; | |
1490 | if (copy_from_user(page, buf, count)) | |
1491 | goto out; | |
1492 | ||
1493 | length = security_setprocattr(task, | |
1494 | (char*)file->f_dentry->d_name.name, | |
1495 | (void*)page, count); | |
1496 | out: | |
1497 | free_page((unsigned long) page); | |
1498 | return length; | |
1499 | } | |
1500 | ||
1501 | static struct file_operations proc_pid_attr_operations = { | |
1502 | .read = proc_pid_attr_read, | |
1503 | .write = proc_pid_attr_write, | |
1504 | }; | |
1505 | ||
1506 | static struct file_operations proc_tid_attr_operations; | |
1507 | static struct inode_operations proc_tid_attr_inode_operations; | |
1508 | static struct file_operations proc_tgid_attr_operations; | |
1509 | static struct inode_operations proc_tgid_attr_inode_operations; | |
1510 | #endif | |
1511 | ||
f246315e DD |
1512 | static int get_tid_list(int index, unsigned int *tids, struct inode *dir); |
1513 | ||
1da177e4 LT |
1514 | /* SMP-safe */ |
1515 | static struct dentry *proc_pident_lookup(struct inode *dir, | |
1516 | struct dentry *dentry, | |
1517 | struct pid_entry *ents) | |
1518 | { | |
1519 | struct inode *inode; | |
1520 | int error; | |
1521 | struct task_struct *task = proc_task(dir); | |
1522 | struct pid_entry *p; | |
1523 | struct proc_inode *ei; | |
1524 | ||
1525 | error = -ENOENT; | |
1526 | inode = NULL; | |
1527 | ||
1528 | if (!pid_alive(task)) | |
1529 | goto out; | |
1530 | ||
1531 | for (p = ents; p->name; p++) { | |
1532 | if (p->len != dentry->d_name.len) | |
1533 | continue; | |
1534 | if (!memcmp(dentry->d_name.name, p->name, p->len)) | |
1535 | break; | |
1536 | } | |
1537 | if (!p->name) | |
1538 | goto out; | |
1539 | ||
1540 | error = -EINVAL; | |
1541 | inode = proc_pid_make_inode(dir->i_sb, task, p->type); | |
1542 | if (!inode) | |
1543 | goto out; | |
1544 | ||
1545 | ei = PROC_I(inode); | |
1546 | inode->i_mode = p->mode; | |
1547 | /* | |
1548 | * Yes, it does not scale. And it should not. Don't add | |
1549 | * new entries into /proc/<tgid>/ without very good reasons. | |
1550 | */ | |
1551 | switch(p->type) { | |
1552 | case PROC_TGID_TASK: | |
f246315e | 1553 | inode->i_nlink = 2 + get_tid_list(2, NULL, dir); |
1da177e4 LT |
1554 | inode->i_op = &proc_task_inode_operations; |
1555 | inode->i_fop = &proc_task_operations; | |
1556 | break; | |
1557 | case PROC_TID_FD: | |
1558 | case PROC_TGID_FD: | |
1559 | inode->i_nlink = 2; | |
1560 | inode->i_op = &proc_fd_inode_operations; | |
1561 | inode->i_fop = &proc_fd_operations; | |
1562 | break; | |
1563 | case PROC_TID_EXE: | |
1564 | case PROC_TGID_EXE: | |
1565 | inode->i_op = &proc_pid_link_inode_operations; | |
1566 | ei->op.proc_get_link = proc_exe_link; | |
1567 | break; | |
1568 | case PROC_TID_CWD: | |
1569 | case PROC_TGID_CWD: | |
1570 | inode->i_op = &proc_pid_link_inode_operations; | |
1571 | ei->op.proc_get_link = proc_cwd_link; | |
1572 | break; | |
1573 | case PROC_TID_ROOT: | |
1574 | case PROC_TGID_ROOT: | |
1575 | inode->i_op = &proc_pid_link_inode_operations; | |
1576 | ei->op.proc_get_link = proc_root_link; | |
1577 | break; | |
1578 | case PROC_TID_ENVIRON: | |
1579 | case PROC_TGID_ENVIRON: | |
1580 | inode->i_fop = &proc_info_file_operations; | |
1581 | ei->op.proc_read = proc_pid_environ; | |
1582 | break; | |
1583 | case PROC_TID_AUXV: | |
1584 | case PROC_TGID_AUXV: | |
1585 | inode->i_fop = &proc_info_file_operations; | |
1586 | ei->op.proc_read = proc_pid_auxv; | |
1587 | break; | |
1588 | case PROC_TID_STATUS: | |
1589 | case PROC_TGID_STATUS: | |
1590 | inode->i_fop = &proc_info_file_operations; | |
1591 | ei->op.proc_read = proc_pid_status; | |
1592 | break; | |
1593 | case PROC_TID_STAT: | |
1594 | inode->i_fop = &proc_info_file_operations; | |
1595 | ei->op.proc_read = proc_tid_stat; | |
1596 | break; | |
1597 | case PROC_TGID_STAT: | |
1598 | inode->i_fop = &proc_info_file_operations; | |
1599 | ei->op.proc_read = proc_tgid_stat; | |
1600 | break; | |
1601 | case PROC_TID_CMDLINE: | |
1602 | case PROC_TGID_CMDLINE: | |
1603 | inode->i_fop = &proc_info_file_operations; | |
1604 | ei->op.proc_read = proc_pid_cmdline; | |
1605 | break; | |
1606 | case PROC_TID_STATM: | |
1607 | case PROC_TGID_STATM: | |
1608 | inode->i_fop = &proc_info_file_operations; | |
1609 | ei->op.proc_read = proc_pid_statm; | |
1610 | break; | |
1611 | case PROC_TID_MAPS: | |
1612 | case PROC_TGID_MAPS: | |
1613 | inode->i_fop = &proc_maps_operations; | |
1614 | break; | |
6e21c8f1 CL |
1615 | #ifdef CONFIG_NUMA |
1616 | case PROC_TID_NUMA_MAPS: | |
1617 | case PROC_TGID_NUMA_MAPS: | |
1618 | inode->i_fop = &proc_numa_maps_operations; | |
1619 | break; | |
1620 | #endif | |
1da177e4 LT |
1621 | case PROC_TID_MEM: |
1622 | case PROC_TGID_MEM: | |
1623 | inode->i_op = &proc_mem_inode_operations; | |
1624 | inode->i_fop = &proc_mem_operations; | |
1625 | break; | |
1626 | #ifdef CONFIG_SECCOMP | |
1627 | case PROC_TID_SECCOMP: | |
1628 | case PROC_TGID_SECCOMP: | |
1629 | inode->i_fop = &proc_seccomp_operations; | |
1630 | break; | |
1631 | #endif /* CONFIG_SECCOMP */ | |
1632 | case PROC_TID_MOUNTS: | |
1633 | case PROC_TGID_MOUNTS: | |
1634 | inode->i_fop = &proc_mounts_operations; | |
1635 | break; | |
e070ad49 ML |
1636 | case PROC_TID_SMAPS: |
1637 | case PROC_TGID_SMAPS: | |
1638 | inode->i_fop = &proc_smaps_operations; | |
1639 | break; | |
1da177e4 LT |
1640 | #ifdef CONFIG_SECURITY |
1641 | case PROC_TID_ATTR: | |
1642 | inode->i_nlink = 2; | |
1643 | inode->i_op = &proc_tid_attr_inode_operations; | |
1644 | inode->i_fop = &proc_tid_attr_operations; | |
1645 | break; | |
1646 | case PROC_TGID_ATTR: | |
1647 | inode->i_nlink = 2; | |
1648 | inode->i_op = &proc_tgid_attr_inode_operations; | |
1649 | inode->i_fop = &proc_tgid_attr_operations; | |
1650 | break; | |
1651 | case PROC_TID_ATTR_CURRENT: | |
1652 | case PROC_TGID_ATTR_CURRENT: | |
1653 | case PROC_TID_ATTR_PREV: | |
1654 | case PROC_TGID_ATTR_PREV: | |
1655 | case PROC_TID_ATTR_EXEC: | |
1656 | case PROC_TGID_ATTR_EXEC: | |
1657 | case PROC_TID_ATTR_FSCREATE: | |
1658 | case PROC_TGID_ATTR_FSCREATE: | |
1659 | inode->i_fop = &proc_pid_attr_operations; | |
1660 | break; | |
1661 | #endif | |
1662 | #ifdef CONFIG_KALLSYMS | |
1663 | case PROC_TID_WCHAN: | |
1664 | case PROC_TGID_WCHAN: | |
1665 | inode->i_fop = &proc_info_file_operations; | |
1666 | ei->op.proc_read = proc_pid_wchan; | |
1667 | break; | |
1668 | #endif | |
1669 | #ifdef CONFIG_SCHEDSTATS | |
1670 | case PROC_TID_SCHEDSTAT: | |
1671 | case PROC_TGID_SCHEDSTAT: | |
1672 | inode->i_fop = &proc_info_file_operations; | |
1673 | ei->op.proc_read = proc_pid_schedstat; | |
1674 | break; | |
1675 | #endif | |
1676 | #ifdef CONFIG_CPUSETS | |
1677 | case PROC_TID_CPUSET: | |
1678 | case PROC_TGID_CPUSET: | |
1679 | inode->i_fop = &proc_cpuset_operations; | |
1680 | break; | |
1681 | #endif | |
1682 | case PROC_TID_OOM_SCORE: | |
1683 | case PROC_TGID_OOM_SCORE: | |
1684 | inode->i_fop = &proc_info_file_operations; | |
1685 | ei->op.proc_read = proc_oom_score; | |
1686 | break; | |
1687 | case PROC_TID_OOM_ADJUST: | |
1688 | case PROC_TGID_OOM_ADJUST: | |
1689 | inode->i_fop = &proc_oom_adjust_operations; | |
1690 | break; | |
1691 | #ifdef CONFIG_AUDITSYSCALL | |
1692 | case PROC_TID_LOGINUID: | |
1693 | case PROC_TGID_LOGINUID: | |
1694 | inode->i_fop = &proc_loginuid_operations; | |
1695 | break; | |
1696 | #endif | |
1697 | default: | |
1698 | printk("procfs: impossible type (%d)",p->type); | |
1699 | iput(inode); | |
1700 | return ERR_PTR(-EINVAL); | |
1701 | } | |
1702 | dentry->d_op = &pid_dentry_operations; | |
1703 | d_add(dentry, inode); | |
1704 | return NULL; | |
1705 | ||
1706 | out: | |
1707 | return ERR_PTR(error); | |
1708 | } | |
1709 | ||
1710 | static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ | |
1711 | return proc_pident_lookup(dir, dentry, tgid_base_stuff); | |
1712 | } | |
1713 | ||
1714 | static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ | |
1715 | return proc_pident_lookup(dir, dentry, tid_base_stuff); | |
1716 | } | |
1717 | ||
1718 | static struct file_operations proc_tgid_base_operations = { | |
1719 | .read = generic_read_dir, | |
1720 | .readdir = proc_tgid_base_readdir, | |
1721 | }; | |
1722 | ||
1723 | static struct file_operations proc_tid_base_operations = { | |
1724 | .read = generic_read_dir, | |
1725 | .readdir = proc_tid_base_readdir, | |
1726 | }; | |
1727 | ||
1728 | static struct inode_operations proc_tgid_base_inode_operations = { | |
1729 | .lookup = proc_tgid_base_lookup, | |
1730 | }; | |
1731 | ||
1732 | static struct inode_operations proc_tid_base_inode_operations = { | |
1733 | .lookup = proc_tid_base_lookup, | |
1734 | }; | |
1735 | ||
1736 | #ifdef CONFIG_SECURITY | |
1737 | static int proc_tgid_attr_readdir(struct file * filp, | |
1738 | void * dirent, filldir_t filldir) | |
1739 | { | |
1740 | return proc_pident_readdir(filp,dirent,filldir, | |
1741 | tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff)); | |
1742 | } | |
1743 | ||
1744 | static int proc_tid_attr_readdir(struct file * filp, | |
1745 | void * dirent, filldir_t filldir) | |
1746 | { | |
1747 | return proc_pident_readdir(filp,dirent,filldir, | |
1748 | tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff)); | |
1749 | } | |
1750 | ||
1751 | static struct file_operations proc_tgid_attr_operations = { | |
1752 | .read = generic_read_dir, | |
1753 | .readdir = proc_tgid_attr_readdir, | |
1754 | }; | |
1755 | ||
1756 | static struct file_operations proc_tid_attr_operations = { | |
1757 | .read = generic_read_dir, | |
1758 | .readdir = proc_tid_attr_readdir, | |
1759 | }; | |
1760 | ||
1761 | static struct dentry *proc_tgid_attr_lookup(struct inode *dir, | |
1762 | struct dentry *dentry, struct nameidata *nd) | |
1763 | { | |
1764 | return proc_pident_lookup(dir, dentry, tgid_attr_stuff); | |
1765 | } | |
1766 | ||
1767 | static struct dentry *proc_tid_attr_lookup(struct inode *dir, | |
1768 | struct dentry *dentry, struct nameidata *nd) | |
1769 | { | |
1770 | return proc_pident_lookup(dir, dentry, tid_attr_stuff); | |
1771 | } | |
1772 | ||
1773 | static struct inode_operations proc_tgid_attr_inode_operations = { | |
1774 | .lookup = proc_tgid_attr_lookup, | |
1775 | }; | |
1776 | ||
1777 | static struct inode_operations proc_tid_attr_inode_operations = { | |
1778 | .lookup = proc_tid_attr_lookup, | |
1779 | }; | |
1780 | #endif | |
1781 | ||
1782 | /* | |
1783 | * /proc/self: | |
1784 | */ | |
1785 | static int proc_self_readlink(struct dentry *dentry, char __user *buffer, | |
1786 | int buflen) | |
1787 | { | |
1788 | char tmp[30]; | |
1789 | sprintf(tmp, "%d", current->tgid); | |
1790 | return vfs_readlink(dentry,buffer,buflen,tmp); | |
1791 | } | |
1792 | ||
008b150a | 1793 | static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
1794 | { |
1795 | char tmp[30]; | |
1796 | sprintf(tmp, "%d", current->tgid); | |
008b150a | 1797 | return ERR_PTR(vfs_follow_link(nd,tmp)); |
1da177e4 LT |
1798 | } |
1799 | ||
1800 | static struct inode_operations proc_self_inode_operations = { | |
1801 | .readlink = proc_self_readlink, | |
1802 | .follow_link = proc_self_follow_link, | |
1803 | }; | |
1804 | ||
1805 | /** | |
4dc3b16b | 1806 | * proc_pid_unhash - Unhash /proc/@pid entry from the dcache. |
1da177e4 LT |
1807 | * @p: task that should be flushed. |
1808 | * | |
4dc3b16b | 1809 | * Drops the /proc/@pid dcache entry from the hash chains. |
1da177e4 | 1810 | * |
4dc3b16b PP |
1811 | * Dropping /proc/@pid entries and detach_pid must be synchroneous, |
1812 | * otherwise e.g. /proc/@pid/exe might point to the wrong executable, | |
1da177e4 LT |
1813 | * if the pid value is immediately reused. This is enforced by |
1814 | * - caller must acquire spin_lock(p->proc_lock) | |
1815 | * - must be called before detach_pid() | |
1816 | * - proc_pid_lookup acquires proc_lock, and checks that | |
1817 | * the target is not dead by looking at the attach count | |
1818 | * of PIDTYPE_PID. | |
1819 | */ | |
1820 | ||
1821 | struct dentry *proc_pid_unhash(struct task_struct *p) | |
1822 | { | |
1823 | struct dentry *proc_dentry; | |
1824 | ||
1825 | proc_dentry = p->proc_dentry; | |
1826 | if (proc_dentry != NULL) { | |
1827 | ||
1828 | spin_lock(&dcache_lock); | |
1829 | spin_lock(&proc_dentry->d_lock); | |
1830 | if (!d_unhashed(proc_dentry)) { | |
1831 | dget_locked(proc_dentry); | |
1832 | __d_drop(proc_dentry); | |
1833 | spin_unlock(&proc_dentry->d_lock); | |
1834 | } else { | |
1835 | spin_unlock(&proc_dentry->d_lock); | |
1836 | proc_dentry = NULL; | |
1837 | } | |
1838 | spin_unlock(&dcache_lock); | |
1839 | } | |
1840 | return proc_dentry; | |
1841 | } | |
1842 | ||
1843 | /** | |
4dc3b16b | 1844 | * proc_pid_flush - recover memory used by stale /proc/@pid/x entries |
67be2dd1 | 1845 | * @proc_dentry: directoy to prune. |
1da177e4 LT |
1846 | * |
1847 | * Shrink the /proc directory that was used by the just killed thread. | |
1848 | */ | |
1849 | ||
1850 | void proc_pid_flush(struct dentry *proc_dentry) | |
1851 | { | |
1852 | might_sleep(); | |
1853 | if(proc_dentry != NULL) { | |
1854 | shrink_dcache_parent(proc_dentry); | |
1855 | dput(proc_dentry); | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | /* SMP-safe */ | |
1860 | struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) | |
1861 | { | |
1862 | struct task_struct *task; | |
1863 | struct inode *inode; | |
1864 | struct proc_inode *ei; | |
1865 | unsigned tgid; | |
1866 | int died; | |
1867 | ||
1868 | if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) { | |
1869 | inode = new_inode(dir->i_sb); | |
1870 | if (!inode) | |
1871 | return ERR_PTR(-ENOMEM); | |
1872 | ei = PROC_I(inode); | |
1873 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
1874 | inode->i_ino = fake_ino(0, PROC_TGID_INO); | |
1875 | ei->pde = NULL; | |
1876 | inode->i_mode = S_IFLNK|S_IRWXUGO; | |
1877 | inode->i_uid = inode->i_gid = 0; | |
1878 | inode->i_size = 64; | |
1879 | inode->i_op = &proc_self_inode_operations; | |
1880 | d_add(dentry, inode); | |
1881 | return NULL; | |
1882 | } | |
1883 | tgid = name_to_int(dentry); | |
1884 | if (tgid == ~0U) | |
1885 | goto out; | |
1886 | ||
1887 | read_lock(&tasklist_lock); | |
1888 | task = find_task_by_pid(tgid); | |
1889 | if (task) | |
1890 | get_task_struct(task); | |
1891 | read_unlock(&tasklist_lock); | |
1892 | if (!task) | |
1893 | goto out; | |
1894 | ||
1895 | inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO); | |
1896 | ||
1897 | ||
1898 | if (!inode) { | |
1899 | put_task_struct(task); | |
1900 | goto out; | |
1901 | } | |
1902 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; | |
1903 | inode->i_op = &proc_tgid_base_inode_operations; | |
1904 | inode->i_fop = &proc_tgid_base_operations; | |
1da177e4 | 1905 | inode->i_flags|=S_IMMUTABLE; |
bcf88e11 DD |
1906 | #ifdef CONFIG_SECURITY |
1907 | inode->i_nlink = 5; | |
1908 | #else | |
1909 | inode->i_nlink = 4; | |
1910 | #endif | |
1da177e4 LT |
1911 | |
1912 | dentry->d_op = &pid_base_dentry_operations; | |
1913 | ||
1914 | died = 0; | |
1915 | d_add(dentry, inode); | |
1916 | spin_lock(&task->proc_lock); | |
1917 | task->proc_dentry = dentry; | |
1918 | if (!pid_alive(task)) { | |
1919 | dentry = proc_pid_unhash(task); | |
1920 | died = 1; | |
1921 | } | |
1922 | spin_unlock(&task->proc_lock); | |
1923 | ||
1924 | put_task_struct(task); | |
1925 | if (died) { | |
1926 | proc_pid_flush(dentry); | |
1927 | goto out; | |
1928 | } | |
1929 | return NULL; | |
1930 | out: | |
1931 | return ERR_PTR(-ENOENT); | |
1932 | } | |
1933 | ||
1934 | /* SMP-safe */ | |
1935 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) | |
1936 | { | |
1937 | struct task_struct *task; | |
1938 | struct task_struct *leader = proc_task(dir); | |
1939 | struct inode *inode; | |
1940 | unsigned tid; | |
1941 | ||
1942 | tid = name_to_int(dentry); | |
1943 | if (tid == ~0U) | |
1944 | goto out; | |
1945 | ||
1946 | read_lock(&tasklist_lock); | |
1947 | task = find_task_by_pid(tid); | |
1948 | if (task) | |
1949 | get_task_struct(task); | |
1950 | read_unlock(&tasklist_lock); | |
1951 | if (!task) | |
1952 | goto out; | |
1953 | if (leader->tgid != task->tgid) | |
1954 | goto out_drop_task; | |
1955 | ||
1956 | inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO); | |
1957 | ||
1958 | ||
1959 | if (!inode) | |
1960 | goto out_drop_task; | |
1961 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; | |
1962 | inode->i_op = &proc_tid_base_inode_operations; | |
1963 | inode->i_fop = &proc_tid_base_operations; | |
1da177e4 | 1964 | inode->i_flags|=S_IMMUTABLE; |
bcf88e11 DD |
1965 | #ifdef CONFIG_SECURITY |
1966 | inode->i_nlink = 4; | |
1967 | #else | |
1968 | inode->i_nlink = 3; | |
1969 | #endif | |
1da177e4 LT |
1970 | |
1971 | dentry->d_op = &pid_base_dentry_operations; | |
1972 | ||
1973 | d_add(dentry, inode); | |
1974 | ||
1975 | put_task_struct(task); | |
1976 | return NULL; | |
1977 | out_drop_task: | |
1978 | put_task_struct(task); | |
1979 | out: | |
1980 | return ERR_PTR(-ENOENT); | |
1981 | } | |
1982 | ||
1983 | #define PROC_NUMBUF 10 | |
1984 | #define PROC_MAXPIDS 20 | |
1985 | ||
1986 | /* | |
1987 | * Get a few tgid's to return for filldir - we need to hold the | |
1988 | * tasklist lock while doing this, and we must release it before | |
1989 | * we actually do the filldir itself, so we use a temp buffer.. | |
1990 | */ | |
1991 | static int get_tgid_list(int index, unsigned long version, unsigned int *tgids) | |
1992 | { | |
1993 | struct task_struct *p; | |
1994 | int nr_tgids = 0; | |
1995 | ||
1996 | index--; | |
1997 | read_lock(&tasklist_lock); | |
1998 | p = NULL; | |
1999 | if (version) { | |
2000 | p = find_task_by_pid(version); | |
2001 | if (p && !thread_group_leader(p)) | |
2002 | p = NULL; | |
2003 | } | |
2004 | ||
2005 | if (p) | |
2006 | index = 0; | |
2007 | else | |
2008 | p = next_task(&init_task); | |
2009 | ||
2010 | for ( ; p != &init_task; p = next_task(p)) { | |
2011 | int tgid = p->pid; | |
2012 | if (!pid_alive(p)) | |
2013 | continue; | |
2014 | if (--index >= 0) | |
2015 | continue; | |
2016 | tgids[nr_tgids] = tgid; | |
2017 | nr_tgids++; | |
2018 | if (nr_tgids >= PROC_MAXPIDS) | |
2019 | break; | |
2020 | } | |
2021 | read_unlock(&tasklist_lock); | |
2022 | return nr_tgids; | |
2023 | } | |
2024 | ||
2025 | /* | |
2026 | * Get a few tid's to return for filldir - we need to hold the | |
2027 | * tasklist lock while doing this, and we must release it before | |
2028 | * we actually do the filldir itself, so we use a temp buffer.. | |
2029 | */ | |
2030 | static int get_tid_list(int index, unsigned int *tids, struct inode *dir) | |
2031 | { | |
2032 | struct task_struct *leader_task = proc_task(dir); | |
2033 | struct task_struct *task = leader_task; | |
2034 | int nr_tids = 0; | |
2035 | ||
2036 | index -= 2; | |
2037 | read_lock(&tasklist_lock); | |
2038 | /* | |
2039 | * The starting point task (leader_task) might be an already | |
2040 | * unlinked task, which cannot be used to access the task-list | |
2041 | * via next_thread(). | |
2042 | */ | |
2043 | if (pid_alive(task)) do { | |
2044 | int tid = task->pid; | |
2045 | ||
2046 | if (--index >= 0) | |
2047 | continue; | |
f246315e DD |
2048 | if (tids != NULL) |
2049 | tids[nr_tids] = tid; | |
1da177e4 LT |
2050 | nr_tids++; |
2051 | if (nr_tids >= PROC_MAXPIDS) | |
2052 | break; | |
2053 | } while ((task = next_thread(task)) != leader_task); | |
2054 | read_unlock(&tasklist_lock); | |
2055 | return nr_tids; | |
2056 | } | |
2057 | ||
2058 | /* for the /proc/ directory itself, after non-process stuff has been done */ | |
2059 | int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
2060 | { | |
2061 | unsigned int tgid_array[PROC_MAXPIDS]; | |
2062 | char buf[PROC_NUMBUF]; | |
2063 | unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; | |
2064 | unsigned int nr_tgids, i; | |
2065 | int next_tgid; | |
2066 | ||
2067 | if (!nr) { | |
2068 | ino_t ino = fake_ino(0,PROC_TGID_INO); | |
2069 | if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0) | |
2070 | return 0; | |
2071 | filp->f_pos++; | |
2072 | nr++; | |
2073 | } | |
2074 | ||
2075 | /* f_version caches the tgid value that the last readdir call couldn't | |
2076 | * return. lseek aka telldir automagically resets f_version to 0. | |
2077 | */ | |
2078 | next_tgid = filp->f_version; | |
2079 | filp->f_version = 0; | |
2080 | for (;;) { | |
2081 | nr_tgids = get_tgid_list(nr, next_tgid, tgid_array); | |
2082 | if (!nr_tgids) { | |
2083 | /* no more entries ! */ | |
2084 | break; | |
2085 | } | |
2086 | next_tgid = 0; | |
2087 | ||
2088 | /* do not use the last found pid, reserve it for next_tgid */ | |
2089 | if (nr_tgids == PROC_MAXPIDS) { | |
2090 | nr_tgids--; | |
2091 | next_tgid = tgid_array[nr_tgids]; | |
2092 | } | |
2093 | ||
2094 | for (i=0;i<nr_tgids;i++) { | |
2095 | int tgid = tgid_array[i]; | |
2096 | ino_t ino = fake_ino(tgid,PROC_TGID_INO); | |
2097 | unsigned long j = PROC_NUMBUF; | |
2098 | ||
2099 | do | |
2100 | buf[--j] = '0' + (tgid % 10); | |
2101 | while ((tgid /= 10) != 0); | |
2102 | ||
2103 | if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) { | |
2104 | /* returning this tgid failed, save it as the first | |
2105 | * pid for the next readir call */ | |
2106 | filp->f_version = tgid_array[i]; | |
2107 | goto out; | |
2108 | } | |
2109 | filp->f_pos++; | |
2110 | nr++; | |
2111 | } | |
2112 | } | |
2113 | out: | |
2114 | return 0; | |
2115 | } | |
2116 | ||
2117 | /* for the /proc/TGID/task/ directories */ | |
2118 | static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
2119 | { | |
2120 | unsigned int tid_array[PROC_MAXPIDS]; | |
2121 | char buf[PROC_NUMBUF]; | |
2122 | unsigned int nr_tids, i; | |
2123 | struct dentry *dentry = filp->f_dentry; | |
2124 | struct inode *inode = dentry->d_inode; | |
2125 | int retval = -ENOENT; | |
2126 | ino_t ino; | |
2127 | unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ | |
2128 | ||
2129 | if (!pid_alive(proc_task(inode))) | |
2130 | goto out; | |
2131 | retval = 0; | |
2132 | ||
2133 | switch (pos) { | |
2134 | case 0: | |
2135 | ino = inode->i_ino; | |
2136 | if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) | |
2137 | goto out; | |
2138 | pos++; | |
2139 | /* fall through */ | |
2140 | case 1: | |
2141 | ino = parent_ino(dentry); | |
2142 | if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) | |
2143 | goto out; | |
2144 | pos++; | |
2145 | /* fall through */ | |
2146 | } | |
2147 | ||
2148 | nr_tids = get_tid_list(pos, tid_array, inode); | |
f246315e | 2149 | inode->i_nlink = pos + nr_tids; |
1da177e4 LT |
2150 | |
2151 | for (i = 0; i < nr_tids; i++) { | |
2152 | unsigned long j = PROC_NUMBUF; | |
2153 | int tid = tid_array[i]; | |
2154 | ||
2155 | ino = fake_ino(tid,PROC_TID_INO); | |
2156 | ||
2157 | do | |
2158 | buf[--j] = '0' + (tid % 10); | |
2159 | while ((tid /= 10) != 0); | |
2160 | ||
2161 | if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0) | |
2162 | break; | |
2163 | pos++; | |
2164 | } | |
2165 | out: | |
2166 | filp->f_pos = pos; | |
2167 | return retval; | |
2168 | } |