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