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 static struct inode_operations proc_mem_inode_operations = {
931 .permission = proc_permission,
934 #ifdef CONFIG_AUDITSYSCALL
936 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
937 size_t count, loff_t *ppos)
939 struct inode * inode = file->f_dentry->d_inode;
940 struct task_struct *task = proc_task(inode);
942 char tmpbuf[TMPBUFLEN];
944 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
945 audit_get_loginuid(task->audit_context));
946 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
949 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
950 size_t count, loff_t *ppos)
952 struct inode * inode = file->f_dentry->d_inode;
955 struct task_struct *task = proc_task(inode);
958 if (!capable(CAP_AUDIT_CONTROL))
964 if (count >= PAGE_SIZE)
965 count = PAGE_SIZE - 1;
968 /* No partial writes. */
971 page = (char*)__get_free_page(GFP_USER);
975 if (copy_from_user(page, buf, count))
979 loginuid = simple_strtoul(page, &tmp, 10);
985 length = audit_set_loginuid(task, loginuid);
986 if (likely(length == 0))
990 free_page((unsigned long) page);
994 static struct file_operations proc_loginuid_operations = {
995 .read = proc_loginuid_read,
996 .write = proc_loginuid_write,
1000 #ifdef CONFIG_SECCOMP
1001 static ssize_t seccomp_read(struct file *file, char __user *buf,
1002 size_t count, loff_t *ppos)
1004 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1006 loff_t __ppos = *ppos;
1009 /* no need to print the trailing zero, so use only len */
1010 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1013 if (count > len - __ppos)
1014 count = len - __ppos;
1015 if (copy_to_user(buf, __buf + __ppos, count))
1017 *ppos = __ppos + count;
1021 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1022 size_t count, loff_t *ppos)
1024 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1025 char __buf[20], *end;
1026 unsigned int seccomp_mode;
1028 /* can set it only once to be even more secure */
1029 if (unlikely(tsk->seccomp.mode))
1032 memset(__buf, 0, sizeof(__buf));
1033 count = min(count, sizeof(__buf) - 1);
1034 if (copy_from_user(__buf, buf, count))
1036 seccomp_mode = simple_strtoul(__buf, &end, 0);
1039 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1040 tsk->seccomp.mode = seccomp_mode;
1041 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1044 if (unlikely(!(end - __buf)))
1049 static struct file_operations proc_seccomp_operations = {
1050 .read = seccomp_read,
1051 .write = seccomp_write,
1053 #endif /* CONFIG_SECCOMP */
1055 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1057 struct inode *inode = dentry->d_inode;
1058 int error = -EACCES;
1060 /* We don't need a base pointer in the /proc filesystem */
1063 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1065 error = proc_check_root(inode);
1069 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1070 nd->last_type = LAST_BIND;
1072 return ERR_PTR(error);
1075 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1076 char __user *buffer, int buflen)
1078 struct inode * inode;
1079 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1085 inode = dentry->d_inode;
1086 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1087 len = PTR_ERR(path);
1090 len = tmp + PAGE_SIZE - 1 - path;
1094 if (copy_to_user(buffer, path, len))
1097 free_page((unsigned long)tmp);
1101 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1103 int error = -EACCES;
1104 struct inode *inode = dentry->d_inode;
1106 struct vfsmount *mnt = NULL;
1109 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1111 error = proc_check_root(inode);
1115 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1119 error = do_proc_readlink(de, mnt, buffer, buflen);
1126 static struct inode_operations proc_pid_link_inode_operations = {
1127 .readlink = proc_pid_readlink,
1128 .follow_link = proc_pid_follow_link
1133 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1135 struct dentry *dentry = filp->f_dentry;
1136 struct inode *inode = dentry->d_inode;
1137 struct task_struct *p = proc_task(inode);
1138 unsigned int fd, tid, ino;
1141 struct files_struct * files;
1142 struct fdtable *fdt;
1153 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1157 ino = parent_ino(dentry);
1158 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1162 files = get_files_struct(p);
1166 fdt = files_fdtable(files);
1167 for (fd = filp->f_pos-2;
1169 fd++, filp->f_pos++) {
1172 if (!fcheck_files(files, fd))
1180 buf[j] = '0' + (i % 10);
1184 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1185 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1192 put_files_struct(files);
1198 static int proc_pident_readdir(struct file *filp,
1199 void *dirent, filldir_t filldir,
1200 struct pid_entry *ents, unsigned int nents)
1204 struct dentry *dentry = filp->f_dentry;
1205 struct inode *inode = dentry->d_inode;
1206 struct pid_entry *p;
1211 if (!pid_alive(proc_task(inode)))
1215 pid = proc_task(inode)->pid;
1220 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1226 ino = parent_ino(dentry);
1227 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1240 if (filldir(dirent, p->name, p->len, filp->f_pos,
1241 fake_ino(pid, p->type), p->mode >> 12) < 0)
1253 static int proc_tgid_base_readdir(struct file * filp,
1254 void * dirent, filldir_t filldir)
1256 return proc_pident_readdir(filp,dirent,filldir,
1257 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1260 static int proc_tid_base_readdir(struct file * filp,
1261 void * dirent, filldir_t filldir)
1263 return proc_pident_readdir(filp,dirent,filldir,
1264 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1267 /* building an inode */
1269 static int task_dumpable(struct task_struct *task)
1272 struct mm_struct *mm;
1277 dumpable = mm->dumpable;
1285 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1287 struct inode * inode;
1288 struct proc_inode *ei;
1290 /* We need a new inode */
1292 inode = new_inode(sb);
1298 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1299 inode->i_ino = fake_ino(task->pid, ino);
1301 if (!pid_alive(task))
1305 * grab the reference to task.
1307 get_task_struct(task);
1311 if (task_dumpable(task)) {
1312 inode->i_uid = task->euid;
1313 inode->i_gid = task->egid;
1315 security_task_to_inode(task, inode);
1328 * Exceptional case: normally we are not allowed to unhash a busy
1329 * directory. In this case, however, we can do it - no aliasing problems
1330 * due to the way we treat inodes.
1332 * Rewrite the inode's ownerships here because the owning task may have
1333 * performed a setuid(), etc.
1335 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1337 struct inode *inode = dentry->d_inode;
1338 struct task_struct *task = proc_task(inode);
1339 if (pid_alive(task)) {
1340 if (task_dumpable(task)) {
1341 inode->i_uid = task->euid;
1342 inode->i_gid = task->egid;
1347 security_task_to_inode(task, inode);
1354 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1356 struct inode *inode = dentry->d_inode;
1357 struct task_struct *task = proc_task(inode);
1358 int fd = proc_fd(inode);
1359 struct files_struct *files;
1361 files = get_files_struct(task);
1364 if (fcheck_files(files, fd)) {
1366 put_files_struct(files);
1367 if (task_dumpable(task)) {
1368 inode->i_uid = task->euid;
1369 inode->i_gid = task->egid;
1374 security_task_to_inode(task, inode);
1378 put_files_struct(files);
1384 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1386 struct task_struct *task = proc_task(inode);
1387 spin_lock(&task->proc_lock);
1388 if (task->proc_dentry == dentry)
1389 task->proc_dentry = NULL;
1390 spin_unlock(&task->proc_lock);
1394 static int pid_delete_dentry(struct dentry * dentry)
1396 /* Is the task we represent dead?
1397 * If so, then don't put the dentry on the lru list,
1398 * kill it immediately.
1400 return !pid_alive(proc_task(dentry->d_inode));
1403 static struct dentry_operations tid_fd_dentry_operations =
1405 .d_revalidate = tid_fd_revalidate,
1406 .d_delete = pid_delete_dentry,
1409 static struct dentry_operations pid_dentry_operations =
1411 .d_revalidate = pid_revalidate,
1412 .d_delete = pid_delete_dentry,
1415 static struct dentry_operations pid_base_dentry_operations =
1417 .d_revalidate = pid_revalidate,
1418 .d_iput = pid_base_iput,
1419 .d_delete = pid_delete_dentry,
1424 static unsigned name_to_int(struct dentry *dentry)
1426 const char *name = dentry->d_name.name;
1427 int len = dentry->d_name.len;
1430 if (len > 1 && *name == '0')
1433 unsigned c = *name++ - '0';
1436 if (n >= (~0U-9)/10)
1447 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1449 struct task_struct *task = proc_task(dir);
1450 unsigned fd = name_to_int(dentry);
1452 struct files_struct * files;
1453 struct inode *inode;
1454 struct proc_inode *ei;
1458 if (!pid_alive(task))
1461 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1466 files = get_files_struct(task);
1469 inode->i_mode = S_IFLNK;
1472 * We are not taking a ref to the file structure, so we must
1475 spin_lock(&files->file_lock);
1476 file = fcheck_files(files, fd);
1479 if (file->f_mode & 1)
1480 inode->i_mode |= S_IRUSR | S_IXUSR;
1481 if (file->f_mode & 2)
1482 inode->i_mode |= S_IWUSR | S_IXUSR;
1483 spin_unlock(&files->file_lock);
1484 put_files_struct(files);
1485 inode->i_op = &proc_pid_link_inode_operations;
1487 ei->op.proc_get_link = proc_fd_link;
1488 dentry->d_op = &tid_fd_dentry_operations;
1489 d_add(dentry, inode);
1493 spin_unlock(&files->file_lock);
1494 put_files_struct(files);
1498 return ERR_PTR(-ENOENT);
1501 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1502 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1504 static struct file_operations proc_fd_operations = {
1505 .read = generic_read_dir,
1506 .readdir = proc_readfd,
1509 static struct file_operations proc_task_operations = {
1510 .read = generic_read_dir,
1511 .readdir = proc_task_readdir,
1515 * proc directories can do almost nothing..
1517 static struct inode_operations proc_fd_inode_operations = {
1518 .lookup = proc_lookupfd,
1519 .permission = proc_permission,
1522 static struct inode_operations proc_task_inode_operations = {
1523 .lookup = proc_task_lookup,
1526 #ifdef CONFIG_SECURITY
1527 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1528 size_t count, loff_t *ppos)
1530 struct inode * inode = file->f_dentry->d_inode;
1533 struct task_struct *task = proc_task(inode);
1535 if (count > PAGE_SIZE)
1537 if (!(page = __get_free_page(GFP_KERNEL)))
1540 length = security_getprocattr(task,
1541 (char*)file->f_dentry->d_name.name,
1542 (void*)page, count);
1544 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1549 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1550 size_t count, loff_t *ppos)
1552 struct inode * inode = file->f_dentry->d_inode;
1555 struct task_struct *task = proc_task(inode);
1557 if (count > PAGE_SIZE)
1560 /* No partial writes. */
1563 page = (char*)__get_free_page(GFP_USER);
1567 if (copy_from_user(page, buf, count))
1570 length = security_setprocattr(task,
1571 (char*)file->f_dentry->d_name.name,
1572 (void*)page, count);
1574 free_page((unsigned long) page);
1578 static struct file_operations proc_pid_attr_operations = {
1579 .read = proc_pid_attr_read,
1580 .write = proc_pid_attr_write,
1583 static struct file_operations proc_tid_attr_operations;
1584 static struct inode_operations proc_tid_attr_inode_operations;
1585 static struct file_operations proc_tgid_attr_operations;
1586 static struct inode_operations proc_tgid_attr_inode_operations;
1589 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1592 static struct dentry *proc_pident_lookup(struct inode *dir,
1593 struct dentry *dentry,
1594 struct pid_entry *ents)
1596 struct inode *inode;
1598 struct task_struct *task = proc_task(dir);
1599 struct pid_entry *p;
1600 struct proc_inode *ei;
1605 if (!pid_alive(task))
1608 for (p = ents; p->name; p++) {
1609 if (p->len != dentry->d_name.len)
1611 if (!memcmp(dentry->d_name.name, p->name, p->len))
1618 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1623 inode->i_mode = p->mode;
1625 * Yes, it does not scale. And it should not. Don't add
1626 * new entries into /proc/<tgid>/ without very good reasons.
1629 case PROC_TGID_TASK:
1630 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1631 inode->i_op = &proc_task_inode_operations;
1632 inode->i_fop = &proc_task_operations;
1637 inode->i_op = &proc_fd_inode_operations;
1638 inode->i_fop = &proc_fd_operations;
1642 inode->i_op = &proc_pid_link_inode_operations;
1643 ei->op.proc_get_link = proc_exe_link;
1647 inode->i_op = &proc_pid_link_inode_operations;
1648 ei->op.proc_get_link = proc_cwd_link;
1651 case PROC_TGID_ROOT:
1652 inode->i_op = &proc_pid_link_inode_operations;
1653 ei->op.proc_get_link = proc_root_link;
1655 case PROC_TID_ENVIRON:
1656 case PROC_TGID_ENVIRON:
1657 inode->i_fop = &proc_info_file_operations;
1658 ei->op.proc_read = proc_pid_environ;
1661 case PROC_TGID_AUXV:
1662 inode->i_fop = &proc_info_file_operations;
1663 ei->op.proc_read = proc_pid_auxv;
1665 case PROC_TID_STATUS:
1666 case PROC_TGID_STATUS:
1667 inode->i_fop = &proc_info_file_operations;
1668 ei->op.proc_read = proc_pid_status;
1671 inode->i_fop = &proc_info_file_operations;
1672 ei->op.proc_read = proc_tid_stat;
1674 case PROC_TGID_STAT:
1675 inode->i_fop = &proc_info_file_operations;
1676 ei->op.proc_read = proc_tgid_stat;
1678 case PROC_TID_CMDLINE:
1679 case PROC_TGID_CMDLINE:
1680 inode->i_fop = &proc_info_file_operations;
1681 ei->op.proc_read = proc_pid_cmdline;
1683 case PROC_TID_STATM:
1684 case PROC_TGID_STATM:
1685 inode->i_fop = &proc_info_file_operations;
1686 ei->op.proc_read = proc_pid_statm;
1689 case PROC_TGID_MAPS:
1690 inode->i_fop = &proc_maps_operations;
1693 case PROC_TID_NUMA_MAPS:
1694 case PROC_TGID_NUMA_MAPS:
1695 inode->i_fop = &proc_numa_maps_operations;
1700 inode->i_op = &proc_mem_inode_operations;
1701 inode->i_fop = &proc_mem_operations;
1703 #ifdef CONFIG_SECCOMP
1704 case PROC_TID_SECCOMP:
1705 case PROC_TGID_SECCOMP:
1706 inode->i_fop = &proc_seccomp_operations;
1708 #endif /* CONFIG_SECCOMP */
1709 case PROC_TID_MOUNTS:
1710 case PROC_TGID_MOUNTS:
1711 inode->i_fop = &proc_mounts_operations;
1714 case PROC_TID_SMAPS:
1715 case PROC_TGID_SMAPS:
1716 inode->i_fop = &proc_smaps_operations;
1719 case PROC_TID_MOUNTSTATS:
1720 case PROC_TGID_MOUNTSTATS:
1721 inode->i_fop = &proc_mountstats_operations;
1723 #ifdef CONFIG_SECURITY
1726 inode->i_op = &proc_tid_attr_inode_operations;
1727 inode->i_fop = &proc_tid_attr_operations;
1729 case PROC_TGID_ATTR:
1731 inode->i_op = &proc_tgid_attr_inode_operations;
1732 inode->i_fop = &proc_tgid_attr_operations;
1734 case PROC_TID_ATTR_CURRENT:
1735 case PROC_TGID_ATTR_CURRENT:
1736 case PROC_TID_ATTR_PREV:
1737 case PROC_TGID_ATTR_PREV:
1738 case PROC_TID_ATTR_EXEC:
1739 case PROC_TGID_ATTR_EXEC:
1740 case PROC_TID_ATTR_FSCREATE:
1741 case PROC_TGID_ATTR_FSCREATE:
1742 case PROC_TID_ATTR_KEYCREATE:
1743 case PROC_TGID_ATTR_KEYCREATE:
1744 inode->i_fop = &proc_pid_attr_operations;
1747 #ifdef CONFIG_KALLSYMS
1748 case PROC_TID_WCHAN:
1749 case PROC_TGID_WCHAN:
1750 inode->i_fop = &proc_info_file_operations;
1751 ei->op.proc_read = proc_pid_wchan;
1754 #ifdef CONFIG_SCHEDSTATS
1755 case PROC_TID_SCHEDSTAT:
1756 case PROC_TGID_SCHEDSTAT:
1757 inode->i_fop = &proc_info_file_operations;
1758 ei->op.proc_read = proc_pid_schedstat;
1761 #ifdef CONFIG_CPUSETS
1762 case PROC_TID_CPUSET:
1763 case PROC_TGID_CPUSET:
1764 inode->i_fop = &proc_cpuset_operations;
1767 case PROC_TID_OOM_SCORE:
1768 case PROC_TGID_OOM_SCORE:
1769 inode->i_fop = &proc_info_file_operations;
1770 ei->op.proc_read = proc_oom_score;
1772 case PROC_TID_OOM_ADJUST:
1773 case PROC_TGID_OOM_ADJUST:
1774 inode->i_fop = &proc_oom_adjust_operations;
1776 #ifdef CONFIG_AUDITSYSCALL
1777 case PROC_TID_LOGINUID:
1778 case PROC_TGID_LOGINUID:
1779 inode->i_fop = &proc_loginuid_operations;
1783 printk("procfs: impossible type (%d)",p->type);
1785 return ERR_PTR(-EINVAL);
1787 dentry->d_op = &pid_dentry_operations;
1788 d_add(dentry, inode);
1792 return ERR_PTR(error);
1795 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1796 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1799 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1800 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1803 static struct file_operations proc_tgid_base_operations = {
1804 .read = generic_read_dir,
1805 .readdir = proc_tgid_base_readdir,
1808 static struct file_operations proc_tid_base_operations = {
1809 .read = generic_read_dir,
1810 .readdir = proc_tid_base_readdir,
1813 static struct inode_operations proc_tgid_base_inode_operations = {
1814 .lookup = proc_tgid_base_lookup,
1817 static struct inode_operations proc_tid_base_inode_operations = {
1818 .lookup = proc_tid_base_lookup,
1821 #ifdef CONFIG_SECURITY
1822 static int proc_tgid_attr_readdir(struct file * filp,
1823 void * dirent, filldir_t filldir)
1825 return proc_pident_readdir(filp,dirent,filldir,
1826 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1829 static int proc_tid_attr_readdir(struct file * filp,
1830 void * dirent, filldir_t filldir)
1832 return proc_pident_readdir(filp,dirent,filldir,
1833 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1836 static struct file_operations proc_tgid_attr_operations = {
1837 .read = generic_read_dir,
1838 .readdir = proc_tgid_attr_readdir,
1841 static struct file_operations proc_tid_attr_operations = {
1842 .read = generic_read_dir,
1843 .readdir = proc_tid_attr_readdir,
1846 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1847 struct dentry *dentry, struct nameidata *nd)
1849 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1852 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1853 struct dentry *dentry, struct nameidata *nd)
1855 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1858 static struct inode_operations proc_tgid_attr_inode_operations = {
1859 .lookup = proc_tgid_attr_lookup,
1862 static struct inode_operations proc_tid_attr_inode_operations = {
1863 .lookup = proc_tid_attr_lookup,
1870 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1874 sprintf(tmp, "%d", current->tgid);
1875 return vfs_readlink(dentry,buffer,buflen,tmp);
1878 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1881 sprintf(tmp, "%d", current->tgid);
1882 return ERR_PTR(vfs_follow_link(nd,tmp));
1885 static struct inode_operations proc_self_inode_operations = {
1886 .readlink = proc_self_readlink,
1887 .follow_link = proc_self_follow_link,
1891 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1892 * @p: task that should be flushed.
1894 * Drops the /proc/@pid dcache entry from the hash chains.
1896 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1897 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1898 * if the pid value is immediately reused. This is enforced by
1899 * - caller must acquire spin_lock(p->proc_lock)
1900 * - must be called before detach_pid()
1901 * - proc_pid_lookup acquires proc_lock, and checks that
1902 * the target is not dead by looking at the attach count
1906 struct dentry *proc_pid_unhash(struct task_struct *p)
1908 struct dentry *proc_dentry;
1910 proc_dentry = p->proc_dentry;
1911 if (proc_dentry != NULL) {
1913 spin_lock(&dcache_lock);
1914 spin_lock(&proc_dentry->d_lock);
1915 if (!d_unhashed(proc_dentry)) {
1916 dget_locked(proc_dentry);
1917 __d_drop(proc_dentry);
1918 spin_unlock(&proc_dentry->d_lock);
1920 spin_unlock(&proc_dentry->d_lock);
1923 spin_unlock(&dcache_lock);
1929 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1930 * @proc_dentry: directoy to prune.
1932 * Shrink the /proc directory that was used by the just killed thread.
1935 void proc_pid_flush(struct dentry *proc_dentry)
1938 if(proc_dentry != NULL) {
1939 shrink_dcache_parent(proc_dentry);
1945 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1947 struct task_struct *task;
1948 struct inode *inode;
1949 struct proc_inode *ei;
1953 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1954 inode = new_inode(dir->i_sb);
1956 return ERR_PTR(-ENOMEM);
1958 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1959 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1961 inode->i_mode = S_IFLNK|S_IRWXUGO;
1962 inode->i_uid = inode->i_gid = 0;
1964 inode->i_op = &proc_self_inode_operations;
1965 d_add(dentry, inode);
1968 tgid = name_to_int(dentry);
1972 read_lock(&tasklist_lock);
1973 task = find_task_by_pid(tgid);
1975 get_task_struct(task);
1976 read_unlock(&tasklist_lock);
1980 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1984 put_task_struct(task);
1987 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1988 inode->i_op = &proc_tgid_base_inode_operations;
1989 inode->i_fop = &proc_tgid_base_operations;
1990 inode->i_flags|=S_IMMUTABLE;
1991 #ifdef CONFIG_SECURITY
1997 dentry->d_op = &pid_base_dentry_operations;
2000 d_add(dentry, inode);
2001 spin_lock(&task->proc_lock);
2002 task->proc_dentry = dentry;
2003 if (!pid_alive(task)) {
2004 dentry = proc_pid_unhash(task);
2007 spin_unlock(&task->proc_lock);
2009 put_task_struct(task);
2011 proc_pid_flush(dentry);
2016 return ERR_PTR(-ENOENT);
2020 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2022 struct task_struct *task;
2023 struct task_struct *leader = proc_task(dir);
2024 struct inode *inode;
2027 tid = name_to_int(dentry);
2031 read_lock(&tasklist_lock);
2032 task = find_task_by_pid(tid);
2034 get_task_struct(task);
2035 read_unlock(&tasklist_lock);
2038 if (leader->tgid != task->tgid)
2041 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2046 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2047 inode->i_op = &proc_tid_base_inode_operations;
2048 inode->i_fop = &proc_tid_base_operations;
2049 inode->i_flags|=S_IMMUTABLE;
2050 #ifdef CONFIG_SECURITY
2056 dentry->d_op = &pid_base_dentry_operations;
2058 d_add(dentry, inode);
2060 put_task_struct(task);
2063 put_task_struct(task);
2065 return ERR_PTR(-ENOENT);
2068 #define PROC_NUMBUF 10
2069 #define PROC_MAXPIDS 20
2072 * Get a few tgid's to return for filldir - we need to hold the
2073 * tasklist lock while doing this, and we must release it before
2074 * we actually do the filldir itself, so we use a temp buffer..
2076 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2078 struct task_struct *p;
2082 read_lock(&tasklist_lock);
2085 p = find_task_by_pid(version);
2086 if (p && !thread_group_leader(p))
2093 p = next_task(&init_task);
2095 for ( ; p != &init_task; p = next_task(p)) {
2101 tgids[nr_tgids] = tgid;
2103 if (nr_tgids >= PROC_MAXPIDS)
2106 read_unlock(&tasklist_lock);
2111 * Get a few tid's to return for filldir - we need to hold the
2112 * tasklist lock while doing this, and we must release it before
2113 * we actually do the filldir itself, so we use a temp buffer..
2115 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2117 struct task_struct *leader_task = proc_task(dir);
2118 struct task_struct *task = leader_task;
2122 read_lock(&tasklist_lock);
2124 * The starting point task (leader_task) might be an already
2125 * unlinked task, which cannot be used to access the task-list
2126 * via next_thread().
2128 if (pid_alive(task)) do {
2129 int tid = task->pid;
2134 tids[nr_tids] = tid;
2136 if (nr_tids >= PROC_MAXPIDS)
2138 } while ((task = next_thread(task)) != leader_task);
2139 read_unlock(&tasklist_lock);
2143 /* for the /proc/ directory itself, after non-process stuff has been done */
2144 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2146 unsigned int tgid_array[PROC_MAXPIDS];
2147 char buf[PROC_NUMBUF];
2148 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2149 unsigned int nr_tgids, i;
2153 ino_t ino = fake_ino(0,PROC_TGID_INO);
2154 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2160 /* f_version caches the tgid value that the last readdir call couldn't
2161 * return. lseek aka telldir automagically resets f_version to 0.
2163 next_tgid = filp->f_version;
2164 filp->f_version = 0;
2166 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2168 /* no more entries ! */
2173 /* do not use the last found pid, reserve it for next_tgid */
2174 if (nr_tgids == PROC_MAXPIDS) {
2176 next_tgid = tgid_array[nr_tgids];
2179 for (i=0;i<nr_tgids;i++) {
2180 int tgid = tgid_array[i];
2181 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2182 unsigned long j = PROC_NUMBUF;
2185 buf[--j] = '0' + (tgid % 10);
2186 while ((tgid /= 10) != 0);
2188 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2189 /* returning this tgid failed, save it as the first
2190 * pid for the next readir call */
2191 filp->f_version = tgid_array[i];
2202 /* for the /proc/TGID/task/ directories */
2203 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2205 unsigned int tid_array[PROC_MAXPIDS];
2206 char buf[PROC_NUMBUF];
2207 unsigned int nr_tids, i;
2208 struct dentry *dentry = filp->f_dentry;
2209 struct inode *inode = dentry->d_inode;
2210 int retval = -ENOENT;
2212 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2214 if (!pid_alive(proc_task(inode)))
2221 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2226 ino = parent_ino(dentry);
2227 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2233 nr_tids = get_tid_list(pos, tid_array, inode);
2234 inode->i_nlink = pos + nr_tids;
2236 for (i = 0; i < nr_tids; i++) {
2237 unsigned long j = PROC_NUMBUF;
2238 int tid = tid_array[i];
2240 ino = fake_ino(tid,PROC_TID_INO);
2243 buf[--j] = '0' + (tid % 10);
2244 while ((tid /= 10) != 0);
2246 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)