4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
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.
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>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
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.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
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/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
78 * For hysterical raisins we keep the same inumbers as in the old procfs.
79 * Feel free to change the macro below - just keep the range distinct from
80 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
81 * As soon as we'll get a separate superblock we will be able to forget
82 * about magical ranges too.
85 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
87 enum pid_directory_inos {
107 PROC_TGID_MOUNTSTATS,
112 #ifdef CONFIG_SCHEDSTATS
115 #ifdef CONFIG_CPUSETS
118 #ifdef CONFIG_SECURITY
120 PROC_TGID_ATTR_CURRENT,
123 PROC_TGID_ATTR_FSCREATE,
124 PROC_TGID_ATTR_KEYCREATE,
126 #ifdef CONFIG_AUDITSYSCALL
130 PROC_TGID_OOM_ADJUST,
134 #ifdef CONFIG_SECCOMP
154 #ifdef CONFIG_SCHEDSTATS
157 #ifdef CONFIG_CPUSETS
160 #ifdef CONFIG_SECURITY
162 PROC_TID_ATTR_CURRENT,
165 PROC_TID_ATTR_FSCREATE,
166 PROC_TID_ATTR_KEYCREATE,
168 #ifdef CONFIG_AUDITSYSCALL
174 /* Add new entries before this */
175 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
185 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
187 static struct pid_entry tgid_base_stuff[] = {
188 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
189 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
190 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
191 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
192 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
193 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
194 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
195 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
196 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
198 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
200 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
201 #ifdef CONFIG_SECCOMP
202 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
204 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
205 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
206 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
207 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
208 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
210 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
212 #ifdef CONFIG_SECURITY
213 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
215 #ifdef CONFIG_KALLSYMS
216 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
218 #ifdef CONFIG_SCHEDSTATS
219 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
221 #ifdef CONFIG_CPUSETS
222 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
224 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
225 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
226 #ifdef CONFIG_AUDITSYSCALL
227 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
231 static struct pid_entry tid_base_stuff[] = {
232 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
233 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
234 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
235 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
236 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
237 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
238 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
239 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
241 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
243 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
244 #ifdef CONFIG_SECCOMP
245 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
247 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
248 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
249 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
250 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
252 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
254 #ifdef CONFIG_SECURITY
255 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
257 #ifdef CONFIG_KALLSYMS
258 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
260 #ifdef CONFIG_SCHEDSTATS
261 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
263 #ifdef CONFIG_CPUSETS
264 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
266 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
267 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
268 #ifdef CONFIG_AUDITSYSCALL
269 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
274 #ifdef CONFIG_SECURITY
275 static struct pid_entry tgid_attr_stuff[] = {
276 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
277 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
278 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
279 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
280 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
283 static struct pid_entry tid_attr_stuff[] = {
284 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
285 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
286 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
287 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
288 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
295 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
297 struct task_struct *task = proc_task(inode);
298 struct files_struct *files;
300 int fd = proc_fd(inode);
302 files = get_files_struct(task);
305 * We are not taking a ref to the file structure, so we must
308 spin_lock(&files->file_lock);
309 file = fcheck_files(files, fd);
311 *mnt = mntget(file->f_vfsmnt);
312 *dentry = dget(file->f_dentry);
313 spin_unlock(&files->file_lock);
314 put_files_struct(files);
317 spin_unlock(&files->file_lock);
318 put_files_struct(files);
323 static struct fs_struct *get_fs_struct(struct task_struct *task)
325 struct fs_struct *fs;
329 atomic_inc(&fs->count);
334 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
336 struct fs_struct *fs = get_fs_struct(proc_task(inode));
337 int result = -ENOENT;
339 read_lock(&fs->lock);
340 *mnt = mntget(fs->pwdmnt);
341 *dentry = dget(fs->pwd);
342 read_unlock(&fs->lock);
349 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
351 struct fs_struct *fs = get_fs_struct(proc_task(inode));
352 int result = -ENOENT;
354 read_lock(&fs->lock);
355 *mnt = mntget(fs->rootmnt);
356 *dentry = dget(fs->root);
357 read_unlock(&fs->lock);
364 #define MAY_PTRACE(task) \
365 (task == current || \
366 (task->parent == current && \
367 (task->ptrace & PT_PTRACED) && \
368 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
369 security_ptrace(current,task) == 0))
371 static int proc_pid_environ(struct task_struct *task, char * buffer)
374 struct mm_struct *mm = get_task_mm(task);
376 unsigned int len = mm->env_end - mm->env_start;
379 res = access_process_vm(task, mm->env_start, buffer, len, 0);
380 if (!ptrace_may_attach(task))
387 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
391 struct mm_struct *mm = get_task_mm(task);
395 goto out_mm; /* Shh! No looking before we're done */
397 len = mm->arg_end - mm->arg_start;
402 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
404 // If the nul at the end of args has been overwritten, then
405 // assume application is using setproctitle(3).
406 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
407 len = strnlen(buffer, res);
411 len = mm->env_end - mm->env_start;
412 if (len > PAGE_SIZE - res)
413 len = PAGE_SIZE - res;
414 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
415 res = strnlen(buffer, res);
424 static int proc_pid_auxv(struct task_struct *task, char *buffer)
427 struct mm_struct *mm = get_task_mm(task);
429 unsigned int nwords = 0;
432 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
433 res = nwords * sizeof(mm->saved_auxv[0]);
436 memcpy(buffer, mm->saved_auxv, res);
443 #ifdef CONFIG_KALLSYMS
445 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
446 * Returns the resolved symbol. If that fails, simply return the address.
448 static int proc_pid_wchan(struct task_struct *task, char *buffer)
451 const char *sym_name;
452 unsigned long wchan, size, offset;
453 char namebuf[KSYM_NAME_LEN+1];
455 wchan = get_wchan(task);
457 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
459 return sprintf(buffer, "%s", sym_name);
460 return sprintf(buffer, "%lu", wchan);
462 #endif /* CONFIG_KALLSYMS */
464 #ifdef CONFIG_SCHEDSTATS
466 * Provides /proc/PID/schedstat
468 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
470 return sprintf(buffer, "%lu %lu %lu\n",
471 task->sched_info.cpu_time,
472 task->sched_info.run_delay,
473 task->sched_info.pcnt);
477 /* The badness from the OOM killer */
478 unsigned long badness(struct task_struct *p, unsigned long uptime);
479 static int proc_oom_score(struct task_struct *task, char *buffer)
481 unsigned long points;
482 struct timespec uptime;
484 do_posix_clock_monotonic_gettime(&uptime);
485 points = badness(task, uptime.tv_sec);
486 return sprintf(buffer, "%lu\n", points);
489 /************************************************************************/
490 /* Here the fs part begins */
491 /************************************************************************/
493 /* permission checks */
495 /* If the process being read is separated by chroot from the reading process,
496 * don't let the reader access the threads.
498 * note: this does dput(root) and mntput(vfsmnt) on exit.
500 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
502 struct dentry *de, *base;
503 struct vfsmount *our_vfsmnt, *mnt;
506 read_lock(¤t->fs->lock);
507 our_vfsmnt = mntget(current->fs->rootmnt);
508 base = dget(current->fs->root);
509 read_unlock(¤t->fs->lock);
511 spin_lock(&vfsmount_lock);
515 while (mnt != our_vfsmnt) {
516 if (mnt == mnt->mnt_parent)
518 de = mnt->mnt_mountpoint;
519 mnt = mnt->mnt_parent;
522 if (!is_subdir(de, base))
524 spin_unlock(&vfsmount_lock);
533 spin_unlock(&vfsmount_lock);
538 static int proc_check_root(struct inode *inode)
541 struct vfsmount *vfsmnt;
543 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
545 return proc_check_chroot(root, vfsmnt);
548 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
550 if (generic_permission(inode, mask, NULL) != 0)
552 return proc_check_root(inode);
555 extern struct seq_operations proc_pid_maps_op;
556 static int maps_open(struct inode *inode, struct file *file)
558 struct task_struct *task = proc_task(inode);
559 int ret = seq_open(file, &proc_pid_maps_op);
561 struct seq_file *m = file->private_data;
567 static struct file_operations proc_maps_operations = {
571 .release = seq_release,
575 extern struct seq_operations proc_pid_numa_maps_op;
576 static int numa_maps_open(struct inode *inode, struct file *file)
578 struct task_struct *task = proc_task(inode);
579 int ret = seq_open(file, &proc_pid_numa_maps_op);
581 struct seq_file *m = file->private_data;
587 static struct file_operations proc_numa_maps_operations = {
588 .open = numa_maps_open,
591 .release = seq_release,
596 extern struct seq_operations proc_pid_smaps_op;
597 static int smaps_open(struct inode *inode, struct file *file)
599 struct task_struct *task = proc_task(inode);
600 int ret = seq_open(file, &proc_pid_smaps_op);
602 struct seq_file *m = file->private_data;
608 static struct file_operations proc_smaps_operations = {
612 .release = seq_release,
616 extern struct seq_operations mounts_op;
622 static int mounts_open(struct inode *inode, struct file *file)
624 struct task_struct *task = proc_task(inode);
625 struct namespace *namespace;
626 struct proc_mounts *p;
630 namespace = task->namespace;
632 get_namespace(namespace);
637 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
639 file->private_data = &p->m;
640 ret = seq_open(file, &mounts_op);
642 p->m.private = namespace;
643 p->event = namespace->event;
648 put_namespace(namespace);
653 static int mounts_release(struct inode *inode, struct file *file)
655 struct seq_file *m = file->private_data;
656 struct namespace *namespace = m->private;
657 put_namespace(namespace);
658 return seq_release(inode, file);
661 static unsigned mounts_poll(struct file *file, poll_table *wait)
663 struct proc_mounts *p = file->private_data;
664 struct namespace *ns = p->m.private;
667 poll_wait(file, &ns->poll, wait);
669 spin_lock(&vfsmount_lock);
670 if (p->event != ns->event) {
671 p->event = ns->event;
674 spin_unlock(&vfsmount_lock);
679 static struct file_operations proc_mounts_operations = {
683 .release = mounts_release,
687 extern struct seq_operations mountstats_op;
688 static int mountstats_open(struct inode *inode, struct file *file)
690 struct task_struct *task = proc_task(inode);
691 int ret = seq_open(file, &mountstats_op);
694 struct seq_file *m = file->private_data;
695 struct namespace *namespace;
697 namespace = task->namespace;
699 get_namespace(namespace);
703 m->private = namespace;
705 seq_release(inode, file);
712 static struct file_operations proc_mountstats_operations = {
713 .open = mountstats_open,
716 .release = mounts_release,
719 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
721 static ssize_t proc_info_read(struct file * file, char __user * buf,
722 size_t count, loff_t *ppos)
724 struct inode * inode = file->f_dentry->d_inode;
727 struct task_struct *task = proc_task(inode);
729 if (count > PROC_BLOCK_SIZE)
730 count = PROC_BLOCK_SIZE;
731 if (!(page = __get_free_page(GFP_KERNEL)))
734 length = PROC_I(inode)->op.proc_read(task, (char*)page);
737 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
742 static struct file_operations proc_info_file_operations = {
743 .read = proc_info_read,
746 static int mem_open(struct inode* inode, struct file* file)
748 file->private_data = (void*)((long)current->self_exec_id);
752 static ssize_t mem_read(struct file * file, char __user * buf,
753 size_t count, loff_t *ppos)
755 struct task_struct *task = proc_task(file->f_dentry->d_inode);
757 unsigned long src = *ppos;
759 struct mm_struct *mm;
761 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
765 page = (char *)__get_free_page(GFP_USER);
771 mm = get_task_mm(task);
777 if (file->private_data != (void*)((long)current->self_exec_id))
783 int this_len, retval;
785 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
786 retval = access_process_vm(task, src, page, this_len, 0);
787 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
793 if (copy_to_user(buf, page, retval)) {
808 free_page((unsigned long) page);
813 #define mem_write NULL
816 /* This is a security hazard */
817 static ssize_t mem_write(struct file * file, const char * buf,
818 size_t count, loff_t *ppos)
822 struct task_struct *task = proc_task(file->f_dentry->d_inode);
823 unsigned long dst = *ppos;
825 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
828 page = (char *)__get_free_page(GFP_USER);
833 int this_len, retval;
835 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
836 if (copy_from_user(page, buf, this_len)) {
840 retval = access_process_vm(task, dst, page, this_len, 1);
852 free_page((unsigned long) page);
857 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
861 file->f_pos = offset;
864 file->f_pos += offset;
869 force_successful_syscall_return();
873 static struct file_operations proc_mem_operations = {
880 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
881 size_t count, loff_t *ppos)
883 struct task_struct *task = proc_task(file->f_dentry->d_inode);
886 int oom_adjust = task->oomkilladj;
887 loff_t __ppos = *ppos;
889 len = sprintf(buffer, "%i\n", oom_adjust);
892 if (count > len-__ppos)
894 if (copy_to_user(buf, buffer + __ppos, count))
896 *ppos = __ppos + count;
900 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
903 struct task_struct *task = proc_task(file->f_dentry->d_inode);
904 char buffer[8], *end;
907 if (!capable(CAP_SYS_RESOURCE))
909 memset(buffer, 0, 8);
912 if (copy_from_user(buffer, buf, count))
914 oom_adjust = simple_strtol(buffer, &end, 0);
915 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
919 task->oomkilladj = oom_adjust;
920 if (end - buffer == 0)
925 static struct file_operations proc_oom_adjust_operations = {
926 .read = oom_adjust_read,
927 .write = oom_adjust_write,
930 #ifdef CONFIG_AUDITSYSCALL
932 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
933 size_t count, loff_t *ppos)
935 struct inode * inode = file->f_dentry->d_inode;
936 struct task_struct *task = proc_task(inode);
938 char tmpbuf[TMPBUFLEN];
940 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
941 audit_get_loginuid(task->audit_context));
942 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
945 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
946 size_t count, loff_t *ppos)
948 struct inode * inode = file->f_dentry->d_inode;
951 struct task_struct *task = proc_task(inode);
954 if (!capable(CAP_AUDIT_CONTROL))
960 if (count >= PAGE_SIZE)
961 count = PAGE_SIZE - 1;
964 /* No partial writes. */
967 page = (char*)__get_free_page(GFP_USER);
971 if (copy_from_user(page, buf, count))
975 loginuid = simple_strtoul(page, &tmp, 10);
981 length = audit_set_loginuid(task, loginuid);
982 if (likely(length == 0))
986 free_page((unsigned long) page);
990 static struct file_operations proc_loginuid_operations = {
991 .read = proc_loginuid_read,
992 .write = proc_loginuid_write,
996 #ifdef CONFIG_SECCOMP
997 static ssize_t seccomp_read(struct file *file, char __user *buf,
998 size_t count, loff_t *ppos)
1000 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1002 loff_t __ppos = *ppos;
1005 /* no need to print the trailing zero, so use only len */
1006 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1009 if (count > len - __ppos)
1010 count = len - __ppos;
1011 if (copy_to_user(buf, __buf + __ppos, count))
1013 *ppos = __ppos + count;
1017 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1018 size_t count, loff_t *ppos)
1020 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1021 char __buf[20], *end;
1022 unsigned int seccomp_mode;
1024 /* can set it only once to be even more secure */
1025 if (unlikely(tsk->seccomp.mode))
1028 memset(__buf, 0, sizeof(__buf));
1029 count = min(count, sizeof(__buf) - 1);
1030 if (copy_from_user(__buf, buf, count))
1032 seccomp_mode = simple_strtoul(__buf, &end, 0);
1035 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1036 tsk->seccomp.mode = seccomp_mode;
1037 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1040 if (unlikely(!(end - __buf)))
1045 static struct file_operations proc_seccomp_operations = {
1046 .read = seccomp_read,
1047 .write = seccomp_write,
1049 #endif /* CONFIG_SECCOMP */
1051 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1053 struct inode *inode = dentry->d_inode;
1054 int error = -EACCES;
1056 /* We don't need a base pointer in the /proc filesystem */
1059 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1061 error = proc_check_root(inode);
1065 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1066 nd->last_type = LAST_BIND;
1068 return ERR_PTR(error);
1071 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1072 char __user *buffer, int buflen)
1074 struct inode * inode;
1075 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1081 inode = dentry->d_inode;
1082 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1083 len = PTR_ERR(path);
1086 len = tmp + PAGE_SIZE - 1 - path;
1090 if (copy_to_user(buffer, path, len))
1093 free_page((unsigned long)tmp);
1097 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1099 int error = -EACCES;
1100 struct inode *inode = dentry->d_inode;
1102 struct vfsmount *mnt = NULL;
1105 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1107 error = proc_check_root(inode);
1111 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1115 error = do_proc_readlink(de, mnt, buffer, buflen);
1122 static struct inode_operations proc_pid_link_inode_operations = {
1123 .readlink = proc_pid_readlink,
1124 .follow_link = proc_pid_follow_link
1129 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1131 struct dentry *dentry = filp->f_dentry;
1132 struct inode *inode = dentry->d_inode;
1133 struct task_struct *p = proc_task(inode);
1134 unsigned int fd, tid, ino;
1137 struct files_struct * files;
1138 struct fdtable *fdt;
1149 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1153 ino = parent_ino(dentry);
1154 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1158 files = get_files_struct(p);
1162 fdt = files_fdtable(files);
1163 for (fd = filp->f_pos-2;
1165 fd++, filp->f_pos++) {
1168 if (!fcheck_files(files, fd))
1176 buf[j] = '0' + (i % 10);
1180 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1181 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1188 put_files_struct(files);
1194 static int proc_pident_readdir(struct file *filp,
1195 void *dirent, filldir_t filldir,
1196 struct pid_entry *ents, unsigned int nents)
1200 struct dentry *dentry = filp->f_dentry;
1201 struct inode *inode = dentry->d_inode;
1202 struct pid_entry *p;
1207 if (!pid_alive(proc_task(inode)))
1211 pid = proc_task(inode)->pid;
1216 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1222 ino = parent_ino(dentry);
1223 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1236 if (filldir(dirent, p->name, p->len, filp->f_pos,
1237 fake_ino(pid, p->type), p->mode >> 12) < 0)
1249 static int proc_tgid_base_readdir(struct file * filp,
1250 void * dirent, filldir_t filldir)
1252 return proc_pident_readdir(filp,dirent,filldir,
1253 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1256 static int proc_tid_base_readdir(struct file * filp,
1257 void * dirent, filldir_t filldir)
1259 return proc_pident_readdir(filp,dirent,filldir,
1260 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1263 /* building an inode */
1265 static int task_dumpable(struct task_struct *task)
1268 struct mm_struct *mm;
1273 dumpable = mm->dumpable;
1281 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1283 struct inode * inode;
1284 struct proc_inode *ei;
1286 /* We need a new inode */
1288 inode = new_inode(sb);
1294 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1295 inode->i_ino = fake_ino(task->pid, ino);
1297 if (!pid_alive(task))
1301 * grab the reference to task.
1303 get_task_struct(task);
1307 if (task_dumpable(task)) {
1308 inode->i_uid = task->euid;
1309 inode->i_gid = task->egid;
1311 security_task_to_inode(task, inode);
1324 * Exceptional case: normally we are not allowed to unhash a busy
1325 * directory. In this case, however, we can do it - no aliasing problems
1326 * due to the way we treat inodes.
1328 * Rewrite the inode's ownerships here because the owning task may have
1329 * performed a setuid(), etc.
1331 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1333 struct inode *inode = dentry->d_inode;
1334 struct task_struct *task = proc_task(inode);
1335 if (pid_alive(task)) {
1336 if (task_dumpable(task)) {
1337 inode->i_uid = task->euid;
1338 inode->i_gid = task->egid;
1343 security_task_to_inode(task, inode);
1350 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1352 struct inode *inode = dentry->d_inode;
1353 struct task_struct *task = proc_task(inode);
1354 int fd = proc_fd(inode);
1355 struct files_struct *files;
1357 files = get_files_struct(task);
1360 if (fcheck_files(files, fd)) {
1362 put_files_struct(files);
1363 if (task_dumpable(task)) {
1364 inode->i_uid = task->euid;
1365 inode->i_gid = task->egid;
1370 security_task_to_inode(task, inode);
1374 put_files_struct(files);
1380 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1382 struct task_struct *task = proc_task(inode);
1383 spin_lock(&task->proc_lock);
1384 if (task->proc_dentry == dentry)
1385 task->proc_dentry = NULL;
1386 spin_unlock(&task->proc_lock);
1390 static int pid_delete_dentry(struct dentry * dentry)
1392 /* Is the task we represent dead?
1393 * If so, then don't put the dentry on the lru list,
1394 * kill it immediately.
1396 return !pid_alive(proc_task(dentry->d_inode));
1399 static struct dentry_operations tid_fd_dentry_operations =
1401 .d_revalidate = tid_fd_revalidate,
1402 .d_delete = pid_delete_dentry,
1405 static struct dentry_operations pid_dentry_operations =
1407 .d_revalidate = pid_revalidate,
1408 .d_delete = pid_delete_dentry,
1411 static struct dentry_operations pid_base_dentry_operations =
1413 .d_revalidate = pid_revalidate,
1414 .d_iput = pid_base_iput,
1415 .d_delete = pid_delete_dentry,
1420 static unsigned name_to_int(struct dentry *dentry)
1422 const char *name = dentry->d_name.name;
1423 int len = dentry->d_name.len;
1426 if (len > 1 && *name == '0')
1429 unsigned c = *name++ - '0';
1432 if (n >= (~0U-9)/10)
1443 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1445 struct task_struct *task = proc_task(dir);
1446 unsigned fd = name_to_int(dentry);
1448 struct files_struct * files;
1449 struct inode *inode;
1450 struct proc_inode *ei;
1454 if (!pid_alive(task))
1457 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1462 files = get_files_struct(task);
1465 inode->i_mode = S_IFLNK;
1468 * We are not taking a ref to the file structure, so we must
1471 spin_lock(&files->file_lock);
1472 file = fcheck_files(files, fd);
1475 if (file->f_mode & 1)
1476 inode->i_mode |= S_IRUSR | S_IXUSR;
1477 if (file->f_mode & 2)
1478 inode->i_mode |= S_IWUSR | S_IXUSR;
1479 spin_unlock(&files->file_lock);
1480 put_files_struct(files);
1481 inode->i_op = &proc_pid_link_inode_operations;
1483 ei->op.proc_get_link = proc_fd_link;
1484 dentry->d_op = &tid_fd_dentry_operations;
1485 d_add(dentry, inode);
1489 spin_unlock(&files->file_lock);
1490 put_files_struct(files);
1494 return ERR_PTR(-ENOENT);
1497 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1498 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1500 static struct file_operations proc_fd_operations = {
1501 .read = generic_read_dir,
1502 .readdir = proc_readfd,
1505 static struct file_operations proc_task_operations = {
1506 .read = generic_read_dir,
1507 .readdir = proc_task_readdir,
1511 * proc directories can do almost nothing..
1513 static struct inode_operations proc_fd_inode_operations = {
1514 .lookup = proc_lookupfd,
1515 .permission = proc_permission,
1518 static struct inode_operations proc_task_inode_operations = {
1519 .lookup = proc_task_lookup,
1522 #ifdef CONFIG_SECURITY
1523 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1524 size_t count, loff_t *ppos)
1526 struct inode * inode = file->f_dentry->d_inode;
1529 struct task_struct *task = proc_task(inode);
1531 if (count > PAGE_SIZE)
1533 if (!(page = __get_free_page(GFP_KERNEL)))
1536 length = security_getprocattr(task,
1537 (char*)file->f_dentry->d_name.name,
1538 (void*)page, count);
1540 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1545 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1546 size_t count, loff_t *ppos)
1548 struct inode * inode = file->f_dentry->d_inode;
1551 struct task_struct *task = proc_task(inode);
1553 if (count > PAGE_SIZE)
1556 /* No partial writes. */
1559 page = (char*)__get_free_page(GFP_USER);
1563 if (copy_from_user(page, buf, count))
1566 length = security_setprocattr(task,
1567 (char*)file->f_dentry->d_name.name,
1568 (void*)page, count);
1570 free_page((unsigned long) page);
1574 static struct file_operations proc_pid_attr_operations = {
1575 .read = proc_pid_attr_read,
1576 .write = proc_pid_attr_write,
1579 static struct file_operations proc_tid_attr_operations;
1580 static struct inode_operations proc_tid_attr_inode_operations;
1581 static struct file_operations proc_tgid_attr_operations;
1582 static struct inode_operations proc_tgid_attr_inode_operations;
1585 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1588 static struct dentry *proc_pident_lookup(struct inode *dir,
1589 struct dentry *dentry,
1590 struct pid_entry *ents)
1592 struct inode *inode;
1594 struct task_struct *task = proc_task(dir);
1595 struct pid_entry *p;
1596 struct proc_inode *ei;
1601 if (!pid_alive(task))
1604 for (p = ents; p->name; p++) {
1605 if (p->len != dentry->d_name.len)
1607 if (!memcmp(dentry->d_name.name, p->name, p->len))
1614 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1619 inode->i_mode = p->mode;
1621 * Yes, it does not scale. And it should not. Don't add
1622 * new entries into /proc/<tgid>/ without very good reasons.
1625 case PROC_TGID_TASK:
1626 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1627 inode->i_op = &proc_task_inode_operations;
1628 inode->i_fop = &proc_task_operations;
1633 inode->i_op = &proc_fd_inode_operations;
1634 inode->i_fop = &proc_fd_operations;
1638 inode->i_op = &proc_pid_link_inode_operations;
1639 ei->op.proc_get_link = proc_exe_link;
1643 inode->i_op = &proc_pid_link_inode_operations;
1644 ei->op.proc_get_link = proc_cwd_link;
1647 case PROC_TGID_ROOT:
1648 inode->i_op = &proc_pid_link_inode_operations;
1649 ei->op.proc_get_link = proc_root_link;
1651 case PROC_TID_ENVIRON:
1652 case PROC_TGID_ENVIRON:
1653 inode->i_fop = &proc_info_file_operations;
1654 ei->op.proc_read = proc_pid_environ;
1657 case PROC_TGID_AUXV:
1658 inode->i_fop = &proc_info_file_operations;
1659 ei->op.proc_read = proc_pid_auxv;
1661 case PROC_TID_STATUS:
1662 case PROC_TGID_STATUS:
1663 inode->i_fop = &proc_info_file_operations;
1664 ei->op.proc_read = proc_pid_status;
1667 inode->i_fop = &proc_info_file_operations;
1668 ei->op.proc_read = proc_tid_stat;
1670 case PROC_TGID_STAT:
1671 inode->i_fop = &proc_info_file_operations;
1672 ei->op.proc_read = proc_tgid_stat;
1674 case PROC_TID_CMDLINE:
1675 case PROC_TGID_CMDLINE:
1676 inode->i_fop = &proc_info_file_operations;
1677 ei->op.proc_read = proc_pid_cmdline;
1679 case PROC_TID_STATM:
1680 case PROC_TGID_STATM:
1681 inode->i_fop = &proc_info_file_operations;
1682 ei->op.proc_read = proc_pid_statm;
1685 case PROC_TGID_MAPS:
1686 inode->i_fop = &proc_maps_operations;
1689 case PROC_TID_NUMA_MAPS:
1690 case PROC_TGID_NUMA_MAPS:
1691 inode->i_fop = &proc_numa_maps_operations;
1696 inode->i_fop = &proc_mem_operations;
1698 #ifdef CONFIG_SECCOMP
1699 case PROC_TID_SECCOMP:
1700 case PROC_TGID_SECCOMP:
1701 inode->i_fop = &proc_seccomp_operations;
1703 #endif /* CONFIG_SECCOMP */
1704 case PROC_TID_MOUNTS:
1705 case PROC_TGID_MOUNTS:
1706 inode->i_fop = &proc_mounts_operations;
1709 case PROC_TID_SMAPS:
1710 case PROC_TGID_SMAPS:
1711 inode->i_fop = &proc_smaps_operations;
1714 case PROC_TID_MOUNTSTATS:
1715 case PROC_TGID_MOUNTSTATS:
1716 inode->i_fop = &proc_mountstats_operations;
1718 #ifdef CONFIG_SECURITY
1721 inode->i_op = &proc_tid_attr_inode_operations;
1722 inode->i_fop = &proc_tid_attr_operations;
1724 case PROC_TGID_ATTR:
1726 inode->i_op = &proc_tgid_attr_inode_operations;
1727 inode->i_fop = &proc_tgid_attr_operations;
1729 case PROC_TID_ATTR_CURRENT:
1730 case PROC_TGID_ATTR_CURRENT:
1731 case PROC_TID_ATTR_PREV:
1732 case PROC_TGID_ATTR_PREV:
1733 case PROC_TID_ATTR_EXEC:
1734 case PROC_TGID_ATTR_EXEC:
1735 case PROC_TID_ATTR_FSCREATE:
1736 case PROC_TGID_ATTR_FSCREATE:
1737 case PROC_TID_ATTR_KEYCREATE:
1738 case PROC_TGID_ATTR_KEYCREATE:
1739 inode->i_fop = &proc_pid_attr_operations;
1742 #ifdef CONFIG_KALLSYMS
1743 case PROC_TID_WCHAN:
1744 case PROC_TGID_WCHAN:
1745 inode->i_fop = &proc_info_file_operations;
1746 ei->op.proc_read = proc_pid_wchan;
1749 #ifdef CONFIG_SCHEDSTATS
1750 case PROC_TID_SCHEDSTAT:
1751 case PROC_TGID_SCHEDSTAT:
1752 inode->i_fop = &proc_info_file_operations;
1753 ei->op.proc_read = proc_pid_schedstat;
1756 #ifdef CONFIG_CPUSETS
1757 case PROC_TID_CPUSET:
1758 case PROC_TGID_CPUSET:
1759 inode->i_fop = &proc_cpuset_operations;
1762 case PROC_TID_OOM_SCORE:
1763 case PROC_TGID_OOM_SCORE:
1764 inode->i_fop = &proc_info_file_operations;
1765 ei->op.proc_read = proc_oom_score;
1767 case PROC_TID_OOM_ADJUST:
1768 case PROC_TGID_OOM_ADJUST:
1769 inode->i_fop = &proc_oom_adjust_operations;
1771 #ifdef CONFIG_AUDITSYSCALL
1772 case PROC_TID_LOGINUID:
1773 case PROC_TGID_LOGINUID:
1774 inode->i_fop = &proc_loginuid_operations;
1778 printk("procfs: impossible type (%d)",p->type);
1780 return ERR_PTR(-EINVAL);
1782 dentry->d_op = &pid_dentry_operations;
1783 d_add(dentry, inode);
1787 return ERR_PTR(error);
1790 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1791 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1794 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1795 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1798 static struct file_operations proc_tgid_base_operations = {
1799 .read = generic_read_dir,
1800 .readdir = proc_tgid_base_readdir,
1803 static struct file_operations proc_tid_base_operations = {
1804 .read = generic_read_dir,
1805 .readdir = proc_tid_base_readdir,
1808 static struct inode_operations proc_tgid_base_inode_operations = {
1809 .lookup = proc_tgid_base_lookup,
1812 static struct inode_operations proc_tid_base_inode_operations = {
1813 .lookup = proc_tid_base_lookup,
1816 #ifdef CONFIG_SECURITY
1817 static int proc_tgid_attr_readdir(struct file * filp,
1818 void * dirent, filldir_t filldir)
1820 return proc_pident_readdir(filp,dirent,filldir,
1821 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1824 static int proc_tid_attr_readdir(struct file * filp,
1825 void * dirent, filldir_t filldir)
1827 return proc_pident_readdir(filp,dirent,filldir,
1828 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1831 static struct file_operations proc_tgid_attr_operations = {
1832 .read = generic_read_dir,
1833 .readdir = proc_tgid_attr_readdir,
1836 static struct file_operations proc_tid_attr_operations = {
1837 .read = generic_read_dir,
1838 .readdir = proc_tid_attr_readdir,
1841 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1842 struct dentry *dentry, struct nameidata *nd)
1844 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1847 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1848 struct dentry *dentry, struct nameidata *nd)
1850 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1853 static struct inode_operations proc_tgid_attr_inode_operations = {
1854 .lookup = proc_tgid_attr_lookup,
1857 static struct inode_operations proc_tid_attr_inode_operations = {
1858 .lookup = proc_tid_attr_lookup,
1865 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1869 sprintf(tmp, "%d", current->tgid);
1870 return vfs_readlink(dentry,buffer,buflen,tmp);
1873 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1876 sprintf(tmp, "%d", current->tgid);
1877 return ERR_PTR(vfs_follow_link(nd,tmp));
1880 static struct inode_operations proc_self_inode_operations = {
1881 .readlink = proc_self_readlink,
1882 .follow_link = proc_self_follow_link,
1886 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1887 * @p: task that should be flushed.
1889 * Drops the /proc/@pid dcache entry from the hash chains.
1891 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1892 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1893 * if the pid value is immediately reused. This is enforced by
1894 * - caller must acquire spin_lock(p->proc_lock)
1895 * - must be called before detach_pid()
1896 * - proc_pid_lookup acquires proc_lock, and checks that
1897 * the target is not dead by looking at the attach count
1901 struct dentry *proc_pid_unhash(struct task_struct *p)
1903 struct dentry *proc_dentry;
1905 proc_dentry = p->proc_dentry;
1906 if (proc_dentry != NULL) {
1908 spin_lock(&dcache_lock);
1909 spin_lock(&proc_dentry->d_lock);
1910 if (!d_unhashed(proc_dentry)) {
1911 dget_locked(proc_dentry);
1912 __d_drop(proc_dentry);
1913 spin_unlock(&proc_dentry->d_lock);
1915 spin_unlock(&proc_dentry->d_lock);
1918 spin_unlock(&dcache_lock);
1924 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1925 * @proc_dentry: directoy to prune.
1927 * Shrink the /proc directory that was used by the just killed thread.
1930 void proc_pid_flush(struct dentry *proc_dentry)
1933 if(proc_dentry != NULL) {
1934 shrink_dcache_parent(proc_dentry);
1940 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1942 struct task_struct *task;
1943 struct inode *inode;
1944 struct proc_inode *ei;
1948 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1949 inode = new_inode(dir->i_sb);
1951 return ERR_PTR(-ENOMEM);
1953 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1954 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1956 inode->i_mode = S_IFLNK|S_IRWXUGO;
1957 inode->i_uid = inode->i_gid = 0;
1959 inode->i_op = &proc_self_inode_operations;
1960 d_add(dentry, inode);
1963 tgid = name_to_int(dentry);
1967 read_lock(&tasklist_lock);
1968 task = find_task_by_pid(tgid);
1970 get_task_struct(task);
1971 read_unlock(&tasklist_lock);
1975 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1979 put_task_struct(task);
1982 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1983 inode->i_op = &proc_tgid_base_inode_operations;
1984 inode->i_fop = &proc_tgid_base_operations;
1985 inode->i_flags|=S_IMMUTABLE;
1986 #ifdef CONFIG_SECURITY
1992 dentry->d_op = &pid_base_dentry_operations;
1995 d_add(dentry, inode);
1996 spin_lock(&task->proc_lock);
1997 task->proc_dentry = dentry;
1998 if (!pid_alive(task)) {
1999 dentry = proc_pid_unhash(task);
2002 spin_unlock(&task->proc_lock);
2004 put_task_struct(task);
2006 proc_pid_flush(dentry);
2011 return ERR_PTR(-ENOENT);
2015 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2017 struct task_struct *task;
2018 struct task_struct *leader = proc_task(dir);
2019 struct inode *inode;
2022 tid = name_to_int(dentry);
2026 read_lock(&tasklist_lock);
2027 task = find_task_by_pid(tid);
2029 get_task_struct(task);
2030 read_unlock(&tasklist_lock);
2033 if (leader->tgid != task->tgid)
2036 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2041 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2042 inode->i_op = &proc_tid_base_inode_operations;
2043 inode->i_fop = &proc_tid_base_operations;
2044 inode->i_flags|=S_IMMUTABLE;
2045 #ifdef CONFIG_SECURITY
2051 dentry->d_op = &pid_base_dentry_operations;
2053 d_add(dentry, inode);
2055 put_task_struct(task);
2058 put_task_struct(task);
2060 return ERR_PTR(-ENOENT);
2063 #define PROC_NUMBUF 10
2064 #define PROC_MAXPIDS 20
2067 * Get a few tgid's to return for filldir - we need to hold the
2068 * tasklist lock while doing this, and we must release it before
2069 * we actually do the filldir itself, so we use a temp buffer..
2071 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2073 struct task_struct *p;
2077 read_lock(&tasklist_lock);
2080 p = find_task_by_pid(version);
2081 if (p && !thread_group_leader(p))
2088 p = next_task(&init_task);
2090 for ( ; p != &init_task; p = next_task(p)) {
2096 tgids[nr_tgids] = tgid;
2098 if (nr_tgids >= PROC_MAXPIDS)
2101 read_unlock(&tasklist_lock);
2106 * Get a few tid's to return for filldir - we need to hold the
2107 * tasklist lock while doing this, and we must release it before
2108 * we actually do the filldir itself, so we use a temp buffer..
2110 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2112 struct task_struct *leader_task = proc_task(dir);
2113 struct task_struct *task = leader_task;
2117 read_lock(&tasklist_lock);
2119 * The starting point task (leader_task) might be an already
2120 * unlinked task, which cannot be used to access the task-list
2121 * via next_thread().
2123 if (pid_alive(task)) do {
2124 int tid = task->pid;
2129 tids[nr_tids] = tid;
2131 if (nr_tids >= PROC_MAXPIDS)
2133 } while ((task = next_thread(task)) != leader_task);
2134 read_unlock(&tasklist_lock);
2138 /* for the /proc/ directory itself, after non-process stuff has been done */
2139 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2141 unsigned int tgid_array[PROC_MAXPIDS];
2142 char buf[PROC_NUMBUF];
2143 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2144 unsigned int nr_tgids, i;
2148 ino_t ino = fake_ino(0,PROC_TGID_INO);
2149 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2155 /* f_version caches the tgid value that the last readdir call couldn't
2156 * return. lseek aka telldir automagically resets f_version to 0.
2158 next_tgid = filp->f_version;
2159 filp->f_version = 0;
2161 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2163 /* no more entries ! */
2168 /* do not use the last found pid, reserve it for next_tgid */
2169 if (nr_tgids == PROC_MAXPIDS) {
2171 next_tgid = tgid_array[nr_tgids];
2174 for (i=0;i<nr_tgids;i++) {
2175 int tgid = tgid_array[i];
2176 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2177 unsigned long j = PROC_NUMBUF;
2180 buf[--j] = '0' + (tgid % 10);
2181 while ((tgid /= 10) != 0);
2183 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2184 /* returning this tgid failed, save it as the first
2185 * pid for the next readir call */
2186 filp->f_version = tgid_array[i];
2197 /* for the /proc/TGID/task/ directories */
2198 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2200 unsigned int tid_array[PROC_MAXPIDS];
2201 char buf[PROC_NUMBUF];
2202 unsigned int nr_tids, i;
2203 struct dentry *dentry = filp->f_dentry;
2204 struct inode *inode = dentry->d_inode;
2205 int retval = -ENOENT;
2207 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2209 if (!pid_alive(proc_task(inode)))
2216 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2221 ino = parent_ino(dentry);
2222 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2228 nr_tids = get_tid_list(pos, tid_array, inode);
2229 inode->i_nlink = pos + nr_tids;
2231 for (i = 0; i < nr_tids; i++) {
2232 unsigned long j = PROC_NUMBUF;
2233 int tid = tid_array[i];
2235 ino = fake_ino(tid,PROC_TID_INO);
2238 buf[--j] = '0' + (tid % 10);
2239 while ((tid /= 10) != 0);
2241 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)