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/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
76 * For hysterical raisins we keep the same inumbers as in the old procfs.
77 * Feel free to change the macro below - just keep the range distinct from
78 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
79 * As soon as we'll get a separate superblock we will be able to forget
80 * about magical ranges too.
83 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
85 enum pid_directory_inos {
107 #ifdef CONFIG_SCHEDSTATS
110 #ifdef CONFIG_CPUSETS
113 #ifdef CONFIG_SECURITY
115 PROC_TGID_ATTR_CURRENT,
118 PROC_TGID_ATTR_FSCREATE,
120 #ifdef CONFIG_AUDITSYSCALL
124 PROC_TGID_OOM_ADJUST,
128 #ifdef CONFIG_SECCOMP
145 #ifdef CONFIG_SCHEDSTATS
148 #ifdef CONFIG_CPUSETS
151 #ifdef CONFIG_SECURITY
153 PROC_TID_ATTR_CURRENT,
156 PROC_TID_ATTR_FSCREATE,
158 #ifdef CONFIG_AUDITSYSCALL
164 /* Add new entries before this */
165 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
175 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
177 static struct pid_entry tgid_base_stuff[] = {
178 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
179 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
180 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
181 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
182 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
183 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
184 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
185 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
186 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
188 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
190 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
191 #ifdef CONFIG_SECCOMP
192 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
194 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
195 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
196 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
197 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
198 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
199 #ifdef CONFIG_SECURITY
200 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
202 #ifdef CONFIG_KALLSYMS
203 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
205 #ifdef CONFIG_SCHEDSTATS
206 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
208 #ifdef CONFIG_CPUSETS
209 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
211 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
212 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
213 #ifdef CONFIG_AUDITSYSCALL
214 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
218 static struct pid_entry tid_base_stuff[] = {
219 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
220 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
221 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
222 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
223 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
224 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
225 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
226 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
228 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
230 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
231 #ifdef CONFIG_SECCOMP
232 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
234 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
235 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
236 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
237 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
238 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
239 #ifdef CONFIG_SECURITY
240 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
242 #ifdef CONFIG_KALLSYMS
243 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
245 #ifdef CONFIG_SCHEDSTATS
246 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
248 #ifdef CONFIG_CPUSETS
249 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
251 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
252 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
253 #ifdef CONFIG_AUDITSYSCALL
254 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
259 #ifdef CONFIG_SECURITY
260 static struct pid_entry tgid_attr_stuff[] = {
261 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
262 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
263 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
264 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
267 static struct pid_entry tid_attr_stuff[] = {
268 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
269 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
270 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
271 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
278 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
280 struct task_struct *task = proc_task(inode);
281 struct files_struct *files;
283 int fd = proc_type(inode) - PROC_TID_FD_DIR;
285 files = get_files_struct(task);
288 file = fcheck_files(files, fd);
290 *mnt = mntget(file->f_vfsmnt);
291 *dentry = dget(file->f_dentry);
293 put_files_struct(files);
297 put_files_struct(files);
302 static struct fs_struct *get_fs_struct(struct task_struct *task)
304 struct fs_struct *fs;
308 atomic_inc(&fs->count);
313 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
315 struct fs_struct *fs = get_fs_struct(proc_task(inode));
316 int result = -ENOENT;
318 read_lock(&fs->lock);
319 *mnt = mntget(fs->pwdmnt);
320 *dentry = dget(fs->pwd);
321 read_unlock(&fs->lock);
328 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
330 struct fs_struct *fs = get_fs_struct(proc_task(inode));
331 int result = -ENOENT;
333 read_lock(&fs->lock);
334 *mnt = mntget(fs->rootmnt);
335 *dentry = dget(fs->root);
336 read_unlock(&fs->lock);
344 /* Same as proc_root_link, but this addionally tries to get fs from other
345 * threads in the group */
346 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
347 struct vfsmount **mnt)
349 struct fs_struct *fs;
350 int result = -ENOENT;
351 struct task_struct *leader = proc_task(inode);
356 atomic_inc(&fs->count);
359 /* Try to get fs from other threads */
361 read_lock(&tasklist_lock);
362 if (pid_alive(leader)) {
363 struct task_struct *task = leader;
365 while ((task = next_thread(task)) != leader) {
369 atomic_inc(&fs->count);
376 read_unlock(&tasklist_lock);
380 read_lock(&fs->lock);
381 *mnt = mntget(fs->rootmnt);
382 *dentry = dget(fs->root);
383 read_unlock(&fs->lock);
391 #define MAY_PTRACE(task) \
392 (task == current || \
393 (task->parent == current && \
394 (task->ptrace & PT_PTRACED) && \
395 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
396 security_ptrace(current,task) == 0))
398 static int proc_pid_environ(struct task_struct *task, char * buffer)
401 struct mm_struct *mm = get_task_mm(task);
403 unsigned int len = mm->env_end - mm->env_start;
406 res = access_process_vm(task, mm->env_start, buffer, len, 0);
407 if (!ptrace_may_attach(task))
414 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
418 struct mm_struct *mm = get_task_mm(task);
422 goto out_mm; /* Shh! No looking before we're done */
424 len = mm->arg_end - mm->arg_start;
429 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
431 // If the nul at the end of args has been overwritten, then
432 // assume application is using setproctitle(3).
433 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
434 len = strnlen(buffer, res);
438 len = mm->env_end - mm->env_start;
439 if (len > PAGE_SIZE - res)
440 len = PAGE_SIZE - res;
441 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
442 res = strnlen(buffer, res);
451 static int proc_pid_auxv(struct task_struct *task, char *buffer)
454 struct mm_struct *mm = get_task_mm(task);
456 unsigned int nwords = 0;
459 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
460 res = nwords * sizeof(mm->saved_auxv[0]);
463 memcpy(buffer, mm->saved_auxv, res);
470 #ifdef CONFIG_KALLSYMS
472 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
473 * Returns the resolved symbol. If that fails, simply return the address.
475 static int proc_pid_wchan(struct task_struct *task, char *buffer)
478 const char *sym_name;
479 unsigned long wchan, size, offset;
480 char namebuf[KSYM_NAME_LEN+1];
482 wchan = get_wchan(task);
484 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
486 return sprintf(buffer, "%s", sym_name);
487 return sprintf(buffer, "%lu", wchan);
489 #endif /* CONFIG_KALLSYMS */
491 #ifdef CONFIG_SCHEDSTATS
493 * Provides /proc/PID/schedstat
495 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
497 return sprintf(buffer, "%lu %lu %lu\n",
498 task->sched_info.cpu_time,
499 task->sched_info.run_delay,
500 task->sched_info.pcnt);
504 /* The badness from the OOM killer */
505 unsigned long badness(struct task_struct *p, unsigned long uptime);
506 static int proc_oom_score(struct task_struct *task, char *buffer)
508 unsigned long points;
509 struct timespec uptime;
511 do_posix_clock_monotonic_gettime(&uptime);
512 points = badness(task, uptime.tv_sec);
513 return sprintf(buffer, "%lu\n", points);
516 /************************************************************************/
517 /* Here the fs part begins */
518 /************************************************************************/
520 /* permission checks */
522 /* If the process being read is separated by chroot from the reading process,
523 * don't let the reader access the threads.
525 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
527 struct dentry *de, *base;
528 struct vfsmount *our_vfsmnt, *mnt;
530 read_lock(¤t->fs->lock);
531 our_vfsmnt = mntget(current->fs->rootmnt);
532 base = dget(current->fs->root);
533 read_unlock(¤t->fs->lock);
535 spin_lock(&vfsmount_lock);
539 while (vfsmnt != our_vfsmnt) {
540 if (vfsmnt == vfsmnt->mnt_parent)
542 de = vfsmnt->mnt_mountpoint;
543 vfsmnt = vfsmnt->mnt_parent;
546 if (!is_subdir(de, base))
548 spin_unlock(&vfsmount_lock);
557 spin_unlock(&vfsmount_lock);
562 static int proc_check_root(struct inode *inode)
565 struct vfsmount *vfsmnt;
567 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
569 return proc_check_chroot(root, vfsmnt);
572 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
574 if (generic_permission(inode, mask, NULL) != 0)
576 return proc_check_root(inode);
579 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
582 struct vfsmount *vfsmnt;
584 if (generic_permission(inode, mask, NULL) != 0)
587 if (proc_task_root_link(inode, &root, &vfsmnt))
590 return proc_check_chroot(root, vfsmnt);
593 extern struct seq_operations proc_pid_maps_op;
594 static int maps_open(struct inode *inode, struct file *file)
596 struct task_struct *task = proc_task(inode);
597 int ret = seq_open(file, &proc_pid_maps_op);
599 struct seq_file *m = file->private_data;
605 static struct file_operations proc_maps_operations = {
609 .release = seq_release,
613 extern struct seq_operations proc_pid_numa_maps_op;
614 static int numa_maps_open(struct inode *inode, struct file *file)
616 struct task_struct *task = proc_task(inode);
617 int ret = seq_open(file, &proc_pid_numa_maps_op);
619 struct seq_file *m = file->private_data;
625 static struct file_operations proc_numa_maps_operations = {
626 .open = numa_maps_open,
629 .release = seq_release,
633 extern struct seq_operations proc_pid_smaps_op;
634 static int smaps_open(struct inode *inode, struct file *file)
636 struct task_struct *task = proc_task(inode);
637 int ret = seq_open(file, &proc_pid_smaps_op);
639 struct seq_file *m = file->private_data;
645 static struct file_operations proc_smaps_operations = {
649 .release = seq_release,
652 extern struct seq_operations mounts_op;
653 static int mounts_open(struct inode *inode, struct file *file)
655 struct task_struct *task = proc_task(inode);
656 int ret = seq_open(file, &mounts_op);
659 struct seq_file *m = file->private_data;
660 struct namespace *namespace;
662 namespace = task->namespace;
664 get_namespace(namespace);
668 m->private = namespace;
670 seq_release(inode, file);
677 static int mounts_release(struct inode *inode, struct file *file)
679 struct seq_file *m = file->private_data;
680 struct namespace *namespace = m->private;
681 put_namespace(namespace);
682 return seq_release(inode, file);
685 static struct file_operations proc_mounts_operations = {
689 .release = mounts_release,
692 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
694 static ssize_t proc_info_read(struct file * file, char __user * buf,
695 size_t count, loff_t *ppos)
697 struct inode * inode = file->f_dentry->d_inode;
700 struct task_struct *task = proc_task(inode);
702 if (count > PROC_BLOCK_SIZE)
703 count = PROC_BLOCK_SIZE;
704 if (!(page = __get_free_page(GFP_KERNEL)))
707 length = PROC_I(inode)->op.proc_read(task, (char*)page);
710 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
715 static struct file_operations proc_info_file_operations = {
716 .read = proc_info_read,
719 static int mem_open(struct inode* inode, struct file* file)
721 file->private_data = (void*)((long)current->self_exec_id);
725 static ssize_t mem_read(struct file * file, char __user * buf,
726 size_t count, loff_t *ppos)
728 struct task_struct *task = proc_task(file->f_dentry->d_inode);
730 unsigned long src = *ppos;
732 struct mm_struct *mm;
734 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
738 page = (char *)__get_free_page(GFP_USER);
744 mm = get_task_mm(task);
750 if (file->private_data != (void*)((long)current->self_exec_id))
756 int this_len, retval;
758 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
759 retval = access_process_vm(task, src, page, this_len, 0);
760 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
766 if (copy_to_user(buf, page, retval)) {
781 free_page((unsigned long) page);
786 #define mem_write NULL
789 /* This is a security hazard */
790 static ssize_t mem_write(struct file * file, const char * buf,
791 size_t count, loff_t *ppos)
795 struct task_struct *task = proc_task(file->f_dentry->d_inode);
796 unsigned long dst = *ppos;
798 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
801 page = (char *)__get_free_page(GFP_USER);
806 int this_len, retval;
808 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
809 if (copy_from_user(page, buf, this_len)) {
813 retval = access_process_vm(task, dst, page, this_len, 1);
825 free_page((unsigned long) page);
830 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
834 file->f_pos = offset;
837 file->f_pos += offset;
842 force_successful_syscall_return();
846 static struct file_operations proc_mem_operations = {
853 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
854 size_t count, loff_t *ppos)
856 struct task_struct *task = proc_task(file->f_dentry->d_inode);
859 int oom_adjust = task->oomkilladj;
860 loff_t __ppos = *ppos;
862 len = sprintf(buffer, "%i\n", oom_adjust);
865 if (count > len-__ppos)
867 if (copy_to_user(buf, buffer + __ppos, count))
869 *ppos = __ppos + count;
873 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
874 size_t count, loff_t *ppos)
876 struct task_struct *task = proc_task(file->f_dentry->d_inode);
877 char buffer[8], *end;
880 if (!capable(CAP_SYS_RESOURCE))
882 memset(buffer, 0, 8);
885 if (copy_from_user(buffer, buf, count))
887 oom_adjust = simple_strtol(buffer, &end, 0);
888 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
892 task->oomkilladj = oom_adjust;
893 if (end - buffer == 0)
898 static struct file_operations proc_oom_adjust_operations = {
899 .read = oom_adjust_read,
900 .write = oom_adjust_write,
903 static struct inode_operations proc_mem_inode_operations = {
904 .permission = proc_permission,
907 #ifdef CONFIG_AUDITSYSCALL
909 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
910 size_t count, loff_t *ppos)
912 struct inode * inode = file->f_dentry->d_inode;
913 struct task_struct *task = proc_task(inode);
915 char tmpbuf[TMPBUFLEN];
917 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
918 audit_get_loginuid(task->audit_context));
919 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
922 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
923 size_t count, loff_t *ppos)
925 struct inode * inode = file->f_dentry->d_inode;
928 struct task_struct *task = proc_task(inode);
931 if (!capable(CAP_AUDIT_CONTROL))
937 if (count > PAGE_SIZE)
941 /* No partial writes. */
944 page = (char*)__get_free_page(GFP_USER);
948 if (copy_from_user(page, buf, count))
951 loginuid = simple_strtoul(page, &tmp, 10);
957 length = audit_set_loginuid(task, loginuid);
958 if (likely(length == 0))
962 free_page((unsigned long) page);
966 static struct file_operations proc_loginuid_operations = {
967 .read = proc_loginuid_read,
968 .write = proc_loginuid_write,
972 #ifdef CONFIG_SECCOMP
973 static ssize_t seccomp_read(struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
976 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
978 loff_t __ppos = *ppos;
981 /* no need to print the trailing zero, so use only len */
982 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
985 if (count > len - __ppos)
986 count = len - __ppos;
987 if (copy_to_user(buf, __buf + __ppos, count))
989 *ppos = __ppos + count;
993 static ssize_t seccomp_write(struct file *file, const char __user *buf,
994 size_t count, loff_t *ppos)
996 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
997 char __buf[20], *end;
998 unsigned int seccomp_mode;
1000 /* can set it only once to be even more secure */
1001 if (unlikely(tsk->seccomp.mode))
1004 memset(__buf, 0, sizeof(__buf));
1005 count = min(count, sizeof(__buf) - 1);
1006 if (copy_from_user(__buf, buf, count))
1008 seccomp_mode = simple_strtoul(__buf, &end, 0);
1011 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1012 tsk->seccomp.mode = seccomp_mode;
1013 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1016 if (unlikely(!(end - __buf)))
1021 static struct file_operations proc_seccomp_operations = {
1022 .read = seccomp_read,
1023 .write = seccomp_write,
1025 #endif /* CONFIG_SECCOMP */
1027 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1029 struct inode *inode = dentry->d_inode;
1030 int error = -EACCES;
1032 /* We don't need a base pointer in the /proc filesystem */
1035 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1037 error = proc_check_root(inode);
1041 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1042 nd->last_type = LAST_BIND;
1044 return ERR_PTR(error);
1047 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1048 char __user *buffer, int buflen)
1050 struct inode * inode;
1051 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1057 inode = dentry->d_inode;
1058 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1059 len = PTR_ERR(path);
1062 len = tmp + PAGE_SIZE - 1 - path;
1066 if (copy_to_user(buffer, path, len))
1069 free_page((unsigned long)tmp);
1073 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1075 int error = -EACCES;
1076 struct inode *inode = dentry->d_inode;
1078 struct vfsmount *mnt = NULL;
1082 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1084 error = proc_check_root(inode);
1088 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1092 error = do_proc_readlink(de, mnt, buffer, buflen);
1100 static struct inode_operations proc_pid_link_inode_operations = {
1101 .readlink = proc_pid_readlink,
1102 .follow_link = proc_pid_follow_link
1107 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1109 struct inode *inode = filp->f_dentry->d_inode;
1110 struct task_struct *p = proc_task(inode);
1111 unsigned int fd, tid, ino;
1114 struct files_struct * files;
1115 struct fdtable *fdt;
1126 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1130 ino = fake_ino(tid, PROC_TID_INO);
1131 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1135 files = get_files_struct(p);
1139 fdt = files_fdtable(files);
1140 for (fd = filp->f_pos-2;
1142 fd++, filp->f_pos++) {
1145 if (!fcheck_files(files, fd))
1153 buf[j] = '0' + (i % 10);
1157 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1158 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1165 put_files_struct(files);
1171 static int proc_pident_readdir(struct file *filp,
1172 void *dirent, filldir_t filldir,
1173 struct pid_entry *ents, unsigned int nents)
1177 struct dentry *dentry = filp->f_dentry;
1178 struct inode *inode = dentry->d_inode;
1179 struct pid_entry *p;
1184 if (!pid_alive(proc_task(inode)))
1188 pid = proc_task(inode)->pid;
1193 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1199 ino = parent_ino(dentry);
1200 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1213 if (filldir(dirent, p->name, p->len, filp->f_pos,
1214 fake_ino(pid, p->type), p->mode >> 12) < 0)
1226 static int proc_tgid_base_readdir(struct file * filp,
1227 void * dirent, filldir_t filldir)
1229 return proc_pident_readdir(filp,dirent,filldir,
1230 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1233 static int proc_tid_base_readdir(struct file * filp,
1234 void * dirent, filldir_t filldir)
1236 return proc_pident_readdir(filp,dirent,filldir,
1237 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1240 /* building an inode */
1242 static int task_dumpable(struct task_struct *task)
1245 struct mm_struct *mm;
1250 dumpable = mm->dumpable;
1258 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1260 struct inode * inode;
1261 struct proc_inode *ei;
1263 /* We need a new inode */
1265 inode = new_inode(sb);
1272 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1273 inode->i_ino = fake_ino(task->pid, ino);
1275 if (!pid_alive(task))
1279 * grab the reference to task.
1281 get_task_struct(task);
1286 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1287 inode->i_uid = task->euid;
1288 inode->i_gid = task->egid;
1290 security_task_to_inode(task, inode);
1304 * Exceptional case: normally we are not allowed to unhash a busy
1305 * directory. In this case, however, we can do it - no aliasing problems
1306 * due to the way we treat inodes.
1308 * Rewrite the inode's ownerships here because the owning task may have
1309 * performed a setuid(), etc.
1311 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1313 struct inode *inode = dentry->d_inode;
1314 struct task_struct *task = proc_task(inode);
1315 if (pid_alive(task)) {
1316 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1317 inode->i_uid = task->euid;
1318 inode->i_gid = task->egid;
1323 security_task_to_inode(task, inode);
1330 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1332 struct inode *inode = dentry->d_inode;
1333 struct task_struct *task = proc_task(inode);
1334 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1335 struct files_struct *files;
1337 files = get_files_struct(task);
1340 if (fcheck_files(files, fd)) {
1342 put_files_struct(files);
1343 if (task_dumpable(task)) {
1344 inode->i_uid = task->euid;
1345 inode->i_gid = task->egid;
1350 security_task_to_inode(task, inode);
1354 put_files_struct(files);
1360 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1362 struct task_struct *task = proc_task(inode);
1363 spin_lock(&task->proc_lock);
1364 if (task->proc_dentry == dentry)
1365 task->proc_dentry = NULL;
1366 spin_unlock(&task->proc_lock);
1370 static int pid_delete_dentry(struct dentry * dentry)
1372 /* Is the task we represent dead?
1373 * If so, then don't put the dentry on the lru list,
1374 * kill it immediately.
1376 return !pid_alive(proc_task(dentry->d_inode));
1379 static struct dentry_operations tid_fd_dentry_operations =
1381 .d_revalidate = tid_fd_revalidate,
1382 .d_delete = pid_delete_dentry,
1385 static struct dentry_operations pid_dentry_operations =
1387 .d_revalidate = pid_revalidate,
1388 .d_delete = pid_delete_dentry,
1391 static struct dentry_operations pid_base_dentry_operations =
1393 .d_revalidate = pid_revalidate,
1394 .d_iput = pid_base_iput,
1395 .d_delete = pid_delete_dentry,
1400 static unsigned name_to_int(struct dentry *dentry)
1402 const char *name = dentry->d_name.name;
1403 int len = dentry->d_name.len;
1406 if (len > 1 && *name == '0')
1409 unsigned c = *name++ - '0';
1412 if (n >= (~0U-9)/10)
1423 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1425 struct task_struct *task = proc_task(dir);
1426 unsigned fd = name_to_int(dentry);
1428 struct files_struct * files;
1429 struct inode *inode;
1430 struct proc_inode *ei;
1434 if (!pid_alive(task))
1437 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1441 files = get_files_struct(task);
1444 inode->i_mode = S_IFLNK;
1446 file = fcheck_files(files, fd);
1449 if (file->f_mode & 1)
1450 inode->i_mode |= S_IRUSR | S_IXUSR;
1451 if (file->f_mode & 2)
1452 inode->i_mode |= S_IWUSR | S_IXUSR;
1454 put_files_struct(files);
1455 inode->i_op = &proc_pid_link_inode_operations;
1457 ei->op.proc_get_link = proc_fd_link;
1458 dentry->d_op = &tid_fd_dentry_operations;
1459 d_add(dentry, inode);
1464 put_files_struct(files);
1468 return ERR_PTR(-ENOENT);
1471 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1472 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1474 static struct file_operations proc_fd_operations = {
1475 .read = generic_read_dir,
1476 .readdir = proc_readfd,
1479 static struct file_operations proc_task_operations = {
1480 .read = generic_read_dir,
1481 .readdir = proc_task_readdir,
1485 * proc directories can do almost nothing..
1487 static struct inode_operations proc_fd_inode_operations = {
1488 .lookup = proc_lookupfd,
1489 .permission = proc_permission,
1492 static struct inode_operations proc_task_inode_operations = {
1493 .lookup = proc_task_lookup,
1494 .permission = proc_task_permission,
1497 #ifdef CONFIG_SECURITY
1498 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1499 size_t count, loff_t *ppos)
1501 struct inode * inode = file->f_dentry->d_inode;
1504 struct task_struct *task = proc_task(inode);
1506 if (count > PAGE_SIZE)
1508 if (!(page = __get_free_page(GFP_KERNEL)))
1511 length = security_getprocattr(task,
1512 (char*)file->f_dentry->d_name.name,
1513 (void*)page, count);
1515 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1520 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1521 size_t count, loff_t *ppos)
1523 struct inode * inode = file->f_dentry->d_inode;
1526 struct task_struct *task = proc_task(inode);
1528 if (count > PAGE_SIZE)
1531 /* No partial writes. */
1534 page = (char*)__get_free_page(GFP_USER);
1538 if (copy_from_user(page, buf, count))
1541 length = security_setprocattr(task,
1542 (char*)file->f_dentry->d_name.name,
1543 (void*)page, count);
1545 free_page((unsigned long) page);
1549 static struct file_operations proc_pid_attr_operations = {
1550 .read = proc_pid_attr_read,
1551 .write = proc_pid_attr_write,
1554 static struct file_operations proc_tid_attr_operations;
1555 static struct inode_operations proc_tid_attr_inode_operations;
1556 static struct file_operations proc_tgid_attr_operations;
1557 static struct inode_operations proc_tgid_attr_inode_operations;
1560 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1563 static struct dentry *proc_pident_lookup(struct inode *dir,
1564 struct dentry *dentry,
1565 struct pid_entry *ents)
1567 struct inode *inode;
1569 struct task_struct *task = proc_task(dir);
1570 struct pid_entry *p;
1571 struct proc_inode *ei;
1576 if (!pid_alive(task))
1579 for (p = ents; p->name; p++) {
1580 if (p->len != dentry->d_name.len)
1582 if (!memcmp(dentry->d_name.name, p->name, p->len))
1589 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1594 inode->i_mode = p->mode;
1596 * Yes, it does not scale. And it should not. Don't add
1597 * new entries into /proc/<tgid>/ without very good reasons.
1600 case PROC_TGID_TASK:
1601 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1602 inode->i_op = &proc_task_inode_operations;
1603 inode->i_fop = &proc_task_operations;
1608 inode->i_op = &proc_fd_inode_operations;
1609 inode->i_fop = &proc_fd_operations;
1613 inode->i_op = &proc_pid_link_inode_operations;
1614 ei->op.proc_get_link = proc_exe_link;
1618 inode->i_op = &proc_pid_link_inode_operations;
1619 ei->op.proc_get_link = proc_cwd_link;
1622 case PROC_TGID_ROOT:
1623 inode->i_op = &proc_pid_link_inode_operations;
1624 ei->op.proc_get_link = proc_root_link;
1626 case PROC_TID_ENVIRON:
1627 case PROC_TGID_ENVIRON:
1628 inode->i_fop = &proc_info_file_operations;
1629 ei->op.proc_read = proc_pid_environ;
1632 case PROC_TGID_AUXV:
1633 inode->i_fop = &proc_info_file_operations;
1634 ei->op.proc_read = proc_pid_auxv;
1636 case PROC_TID_STATUS:
1637 case PROC_TGID_STATUS:
1638 inode->i_fop = &proc_info_file_operations;
1639 ei->op.proc_read = proc_pid_status;
1642 inode->i_fop = &proc_info_file_operations;
1643 ei->op.proc_read = proc_tid_stat;
1645 case PROC_TGID_STAT:
1646 inode->i_fop = &proc_info_file_operations;
1647 ei->op.proc_read = proc_tgid_stat;
1649 case PROC_TID_CMDLINE:
1650 case PROC_TGID_CMDLINE:
1651 inode->i_fop = &proc_info_file_operations;
1652 ei->op.proc_read = proc_pid_cmdline;
1654 case PROC_TID_STATM:
1655 case PROC_TGID_STATM:
1656 inode->i_fop = &proc_info_file_operations;
1657 ei->op.proc_read = proc_pid_statm;
1660 case PROC_TGID_MAPS:
1661 inode->i_fop = &proc_maps_operations;
1664 case PROC_TID_NUMA_MAPS:
1665 case PROC_TGID_NUMA_MAPS:
1666 inode->i_fop = &proc_numa_maps_operations;
1671 inode->i_op = &proc_mem_inode_operations;
1672 inode->i_fop = &proc_mem_operations;
1674 #ifdef CONFIG_SECCOMP
1675 case PROC_TID_SECCOMP:
1676 case PROC_TGID_SECCOMP:
1677 inode->i_fop = &proc_seccomp_operations;
1679 #endif /* CONFIG_SECCOMP */
1680 case PROC_TID_MOUNTS:
1681 case PROC_TGID_MOUNTS:
1682 inode->i_fop = &proc_mounts_operations;
1684 case PROC_TID_SMAPS:
1685 case PROC_TGID_SMAPS:
1686 inode->i_fop = &proc_smaps_operations;
1688 #ifdef CONFIG_SECURITY
1691 inode->i_op = &proc_tid_attr_inode_operations;
1692 inode->i_fop = &proc_tid_attr_operations;
1694 case PROC_TGID_ATTR:
1696 inode->i_op = &proc_tgid_attr_inode_operations;
1697 inode->i_fop = &proc_tgid_attr_operations;
1699 case PROC_TID_ATTR_CURRENT:
1700 case PROC_TGID_ATTR_CURRENT:
1701 case PROC_TID_ATTR_PREV:
1702 case PROC_TGID_ATTR_PREV:
1703 case PROC_TID_ATTR_EXEC:
1704 case PROC_TGID_ATTR_EXEC:
1705 case PROC_TID_ATTR_FSCREATE:
1706 case PROC_TGID_ATTR_FSCREATE:
1707 inode->i_fop = &proc_pid_attr_operations;
1710 #ifdef CONFIG_KALLSYMS
1711 case PROC_TID_WCHAN:
1712 case PROC_TGID_WCHAN:
1713 inode->i_fop = &proc_info_file_operations;
1714 ei->op.proc_read = proc_pid_wchan;
1717 #ifdef CONFIG_SCHEDSTATS
1718 case PROC_TID_SCHEDSTAT:
1719 case PROC_TGID_SCHEDSTAT:
1720 inode->i_fop = &proc_info_file_operations;
1721 ei->op.proc_read = proc_pid_schedstat;
1724 #ifdef CONFIG_CPUSETS
1725 case PROC_TID_CPUSET:
1726 case PROC_TGID_CPUSET:
1727 inode->i_fop = &proc_cpuset_operations;
1730 case PROC_TID_OOM_SCORE:
1731 case PROC_TGID_OOM_SCORE:
1732 inode->i_fop = &proc_info_file_operations;
1733 ei->op.proc_read = proc_oom_score;
1735 case PROC_TID_OOM_ADJUST:
1736 case PROC_TGID_OOM_ADJUST:
1737 inode->i_fop = &proc_oom_adjust_operations;
1739 #ifdef CONFIG_AUDITSYSCALL
1740 case PROC_TID_LOGINUID:
1741 case PROC_TGID_LOGINUID:
1742 inode->i_fop = &proc_loginuid_operations;
1746 printk("procfs: impossible type (%d)",p->type);
1748 return ERR_PTR(-EINVAL);
1750 dentry->d_op = &pid_dentry_operations;
1751 d_add(dentry, inode);
1755 return ERR_PTR(error);
1758 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1759 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1762 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1763 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1766 static struct file_operations proc_tgid_base_operations = {
1767 .read = generic_read_dir,
1768 .readdir = proc_tgid_base_readdir,
1771 static struct file_operations proc_tid_base_operations = {
1772 .read = generic_read_dir,
1773 .readdir = proc_tid_base_readdir,
1776 static struct inode_operations proc_tgid_base_inode_operations = {
1777 .lookup = proc_tgid_base_lookup,
1780 static struct inode_operations proc_tid_base_inode_operations = {
1781 .lookup = proc_tid_base_lookup,
1784 #ifdef CONFIG_SECURITY
1785 static int proc_tgid_attr_readdir(struct file * filp,
1786 void * dirent, filldir_t filldir)
1788 return proc_pident_readdir(filp,dirent,filldir,
1789 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1792 static int proc_tid_attr_readdir(struct file * filp,
1793 void * dirent, filldir_t filldir)
1795 return proc_pident_readdir(filp,dirent,filldir,
1796 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1799 static struct file_operations proc_tgid_attr_operations = {
1800 .read = generic_read_dir,
1801 .readdir = proc_tgid_attr_readdir,
1804 static struct file_operations proc_tid_attr_operations = {
1805 .read = generic_read_dir,
1806 .readdir = proc_tid_attr_readdir,
1809 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1810 struct dentry *dentry, struct nameidata *nd)
1812 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1815 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1816 struct dentry *dentry, struct nameidata *nd)
1818 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1821 static struct inode_operations proc_tgid_attr_inode_operations = {
1822 .lookup = proc_tgid_attr_lookup,
1825 static struct inode_operations proc_tid_attr_inode_operations = {
1826 .lookup = proc_tid_attr_lookup,
1833 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1837 sprintf(tmp, "%d", current->tgid);
1838 return vfs_readlink(dentry,buffer,buflen,tmp);
1841 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1844 sprintf(tmp, "%d", current->tgid);
1845 return ERR_PTR(vfs_follow_link(nd,tmp));
1848 static struct inode_operations proc_self_inode_operations = {
1849 .readlink = proc_self_readlink,
1850 .follow_link = proc_self_follow_link,
1854 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1855 * @p: task that should be flushed.
1857 * Drops the /proc/@pid dcache entry from the hash chains.
1859 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1860 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1861 * if the pid value is immediately reused. This is enforced by
1862 * - caller must acquire spin_lock(p->proc_lock)
1863 * - must be called before detach_pid()
1864 * - proc_pid_lookup acquires proc_lock, and checks that
1865 * the target is not dead by looking at the attach count
1869 struct dentry *proc_pid_unhash(struct task_struct *p)
1871 struct dentry *proc_dentry;
1873 proc_dentry = p->proc_dentry;
1874 if (proc_dentry != NULL) {
1876 spin_lock(&dcache_lock);
1877 spin_lock(&proc_dentry->d_lock);
1878 if (!d_unhashed(proc_dentry)) {
1879 dget_locked(proc_dentry);
1880 __d_drop(proc_dentry);
1881 spin_unlock(&proc_dentry->d_lock);
1883 spin_unlock(&proc_dentry->d_lock);
1886 spin_unlock(&dcache_lock);
1892 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1893 * @proc_dentry: directoy to prune.
1895 * Shrink the /proc directory that was used by the just killed thread.
1898 void proc_pid_flush(struct dentry *proc_dentry)
1901 if(proc_dentry != NULL) {
1902 shrink_dcache_parent(proc_dentry);
1908 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1910 struct task_struct *task;
1911 struct inode *inode;
1912 struct proc_inode *ei;
1916 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1917 inode = new_inode(dir->i_sb);
1919 return ERR_PTR(-ENOMEM);
1921 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1922 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1924 inode->i_mode = S_IFLNK|S_IRWXUGO;
1925 inode->i_uid = inode->i_gid = 0;
1927 inode->i_op = &proc_self_inode_operations;
1928 d_add(dentry, inode);
1931 tgid = name_to_int(dentry);
1935 read_lock(&tasklist_lock);
1936 task = find_task_by_pid(tgid);
1938 get_task_struct(task);
1939 read_unlock(&tasklist_lock);
1943 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1947 put_task_struct(task);
1950 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1951 inode->i_op = &proc_tgid_base_inode_operations;
1952 inode->i_fop = &proc_tgid_base_operations;
1953 inode->i_flags|=S_IMMUTABLE;
1954 #ifdef CONFIG_SECURITY
1960 dentry->d_op = &pid_base_dentry_operations;
1963 d_add(dentry, inode);
1964 spin_lock(&task->proc_lock);
1965 task->proc_dentry = dentry;
1966 if (!pid_alive(task)) {
1967 dentry = proc_pid_unhash(task);
1970 spin_unlock(&task->proc_lock);
1972 put_task_struct(task);
1974 proc_pid_flush(dentry);
1979 return ERR_PTR(-ENOENT);
1983 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1985 struct task_struct *task;
1986 struct task_struct *leader = proc_task(dir);
1987 struct inode *inode;
1990 tid = name_to_int(dentry);
1994 read_lock(&tasklist_lock);
1995 task = find_task_by_pid(tid);
1997 get_task_struct(task);
1998 read_unlock(&tasklist_lock);
2001 if (leader->tgid != task->tgid)
2004 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2009 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2010 inode->i_op = &proc_tid_base_inode_operations;
2011 inode->i_fop = &proc_tid_base_operations;
2012 inode->i_flags|=S_IMMUTABLE;
2013 #ifdef CONFIG_SECURITY
2019 dentry->d_op = &pid_base_dentry_operations;
2021 d_add(dentry, inode);
2023 put_task_struct(task);
2026 put_task_struct(task);
2028 return ERR_PTR(-ENOENT);
2031 #define PROC_NUMBUF 10
2032 #define PROC_MAXPIDS 20
2035 * Get a few tgid's to return for filldir - we need to hold the
2036 * tasklist lock while doing this, and we must release it before
2037 * we actually do the filldir itself, so we use a temp buffer..
2039 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2041 struct task_struct *p;
2045 read_lock(&tasklist_lock);
2048 p = find_task_by_pid(version);
2049 if (p && !thread_group_leader(p))
2056 p = next_task(&init_task);
2058 for ( ; p != &init_task; p = next_task(p)) {
2064 tgids[nr_tgids] = tgid;
2066 if (nr_tgids >= PROC_MAXPIDS)
2069 read_unlock(&tasklist_lock);
2074 * Get a few tid's to return for filldir - we need to hold the
2075 * tasklist lock while doing this, and we must release it before
2076 * we actually do the filldir itself, so we use a temp buffer..
2078 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2080 struct task_struct *leader_task = proc_task(dir);
2081 struct task_struct *task = leader_task;
2085 read_lock(&tasklist_lock);
2087 * The starting point task (leader_task) might be an already
2088 * unlinked task, which cannot be used to access the task-list
2089 * via next_thread().
2091 if (pid_alive(task)) do {
2092 int tid = task->pid;
2097 tids[nr_tids] = tid;
2099 if (nr_tids >= PROC_MAXPIDS)
2101 } while ((task = next_thread(task)) != leader_task);
2102 read_unlock(&tasklist_lock);
2106 /* for the /proc/ directory itself, after non-process stuff has been done */
2107 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2109 unsigned int tgid_array[PROC_MAXPIDS];
2110 char buf[PROC_NUMBUF];
2111 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2112 unsigned int nr_tgids, i;
2116 ino_t ino = fake_ino(0,PROC_TGID_INO);
2117 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2123 /* f_version caches the tgid value that the last readdir call couldn't
2124 * return. lseek aka telldir automagically resets f_version to 0.
2126 next_tgid = filp->f_version;
2127 filp->f_version = 0;
2129 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2131 /* no more entries ! */
2136 /* do not use the last found pid, reserve it for next_tgid */
2137 if (nr_tgids == PROC_MAXPIDS) {
2139 next_tgid = tgid_array[nr_tgids];
2142 for (i=0;i<nr_tgids;i++) {
2143 int tgid = tgid_array[i];
2144 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2145 unsigned long j = PROC_NUMBUF;
2148 buf[--j] = '0' + (tgid % 10);
2149 while ((tgid /= 10) != 0);
2151 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2152 /* returning this tgid failed, save it as the first
2153 * pid for the next readir call */
2154 filp->f_version = tgid_array[i];
2165 /* for the /proc/TGID/task/ directories */
2166 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2168 unsigned int tid_array[PROC_MAXPIDS];
2169 char buf[PROC_NUMBUF];
2170 unsigned int nr_tids, i;
2171 struct dentry *dentry = filp->f_dentry;
2172 struct inode *inode = dentry->d_inode;
2173 int retval = -ENOENT;
2175 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2177 if (!pid_alive(proc_task(inode)))
2184 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2189 ino = parent_ino(dentry);
2190 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2196 nr_tids = get_tid_list(pos, tid_array, inode);
2197 inode->i_nlink = pos + nr_tids;
2199 for (i = 0; i < nr_tids; i++) {
2200 unsigned long j = PROC_NUMBUF;
2201 int tid = tid_array[i];
2203 ino = fake_ino(tid,PROC_TID_INO);
2206 buf[--j] = '0' + (tid % 10);
2207 while ((tid /= 10) != 0);
2209 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)