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, struct vfsmount **mnt)
348 struct fs_struct *fs;
349 int result = -ENOENT;
350 struct task_struct *leader = proc_task(inode);
355 atomic_inc(&fs->count);
358 /* Try to get fs from other threads */
360 struct task_struct *task = leader;
361 read_lock(&tasklist_lock);
362 if (pid_alive(task)) {
363 while ((task = next_thread(task)) != leader) {
367 atomic_inc(&fs->count);
374 read_unlock(&tasklist_lock);
378 read_lock(&fs->lock);
379 *mnt = mntget(fs->rootmnt);
380 *dentry = dget(fs->root);
381 read_unlock(&fs->lock);
389 #define MAY_PTRACE(task) \
390 (task == current || \
391 (task->parent == current && \
392 (task->ptrace & PT_PTRACED) && \
393 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
394 security_ptrace(current,task) == 0))
396 static int proc_pid_environ(struct task_struct *task, char * buffer)
399 struct mm_struct *mm = get_task_mm(task);
401 unsigned int len = mm->env_end - mm->env_start;
404 res = access_process_vm(task, mm->env_start, buffer, len, 0);
405 if (!ptrace_may_attach(task))
412 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
416 struct mm_struct *mm = get_task_mm(task);
420 goto out_mm; /* Shh! No looking before we're done */
422 len = mm->arg_end - mm->arg_start;
427 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
429 // If the nul at the end of args has been overwritten, then
430 // assume application is using setproctitle(3).
431 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
432 len = strnlen(buffer, res);
436 len = mm->env_end - mm->env_start;
437 if (len > PAGE_SIZE - res)
438 len = PAGE_SIZE - res;
439 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
440 res = strnlen(buffer, res);
449 static int proc_pid_auxv(struct task_struct *task, char *buffer)
452 struct mm_struct *mm = get_task_mm(task);
454 unsigned int nwords = 0;
457 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
458 res = nwords * sizeof(mm->saved_auxv[0]);
461 memcpy(buffer, mm->saved_auxv, res);
468 #ifdef CONFIG_KALLSYMS
470 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
471 * Returns the resolved symbol. If that fails, simply return the address.
473 static int proc_pid_wchan(struct task_struct *task, char *buffer)
476 const char *sym_name;
477 unsigned long wchan, size, offset;
478 char namebuf[KSYM_NAME_LEN+1];
480 wchan = get_wchan(task);
482 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
484 return sprintf(buffer, "%s", sym_name);
485 return sprintf(buffer, "%lu", wchan);
487 #endif /* CONFIG_KALLSYMS */
489 #ifdef CONFIG_SCHEDSTATS
491 * Provides /proc/PID/schedstat
493 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
495 return sprintf(buffer, "%lu %lu %lu\n",
496 task->sched_info.cpu_time,
497 task->sched_info.run_delay,
498 task->sched_info.pcnt);
502 /* The badness from the OOM killer */
503 unsigned long badness(struct task_struct *p, unsigned long uptime);
504 static int proc_oom_score(struct task_struct *task, char *buffer)
506 unsigned long points;
507 struct timespec uptime;
509 do_posix_clock_monotonic_gettime(&uptime);
510 points = badness(task, uptime.tv_sec);
511 return sprintf(buffer, "%lu\n", points);
514 /************************************************************************/
515 /* Here the fs part begins */
516 /************************************************************************/
518 /* permission checks */
520 /* If the process being read is separated by chroot from the reading process,
521 * don't let the reader access the threads.
523 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
525 struct dentry *de, *base;
526 struct vfsmount *our_vfsmnt, *mnt;
528 read_lock(¤t->fs->lock);
529 our_vfsmnt = mntget(current->fs->rootmnt);
530 base = dget(current->fs->root);
531 read_unlock(¤t->fs->lock);
533 spin_lock(&vfsmount_lock);
537 while (vfsmnt != our_vfsmnt) {
538 if (vfsmnt == vfsmnt->mnt_parent)
540 de = vfsmnt->mnt_mountpoint;
541 vfsmnt = vfsmnt->mnt_parent;
544 if (!is_subdir(de, base))
546 spin_unlock(&vfsmount_lock);
555 spin_unlock(&vfsmount_lock);
560 static int proc_check_root(struct inode *inode)
563 struct vfsmount *vfsmnt;
565 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
567 return proc_check_chroot(root, vfsmnt);
570 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
572 if (generic_permission(inode, mask, NULL) != 0)
574 return proc_check_root(inode);
577 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
580 struct vfsmount *vfsmnt;
582 if (generic_permission(inode, mask, NULL) != 0)
585 if (proc_task_root_link(inode, &root, &vfsmnt))
588 return proc_check_chroot(root, vfsmnt);
591 extern struct seq_operations proc_pid_maps_op;
592 static int maps_open(struct inode *inode, struct file *file)
594 struct task_struct *task = proc_task(inode);
595 int ret = seq_open(file, &proc_pid_maps_op);
597 struct seq_file *m = file->private_data;
603 static struct file_operations proc_maps_operations = {
607 .release = seq_release,
611 extern struct seq_operations proc_pid_numa_maps_op;
612 static int numa_maps_open(struct inode *inode, struct file *file)
614 struct task_struct *task = proc_task(inode);
615 int ret = seq_open(file, &proc_pid_numa_maps_op);
617 struct seq_file *m = file->private_data;
623 static struct file_operations proc_numa_maps_operations = {
624 .open = numa_maps_open,
627 .release = seq_release,
631 extern struct seq_operations proc_pid_smaps_op;
632 static int smaps_open(struct inode *inode, struct file *file)
634 struct task_struct *task = proc_task(inode);
635 int ret = seq_open(file, &proc_pid_smaps_op);
637 struct seq_file *m = file->private_data;
643 static struct file_operations proc_smaps_operations = {
647 .release = seq_release,
650 extern struct seq_operations mounts_op;
651 static int mounts_open(struct inode *inode, struct file *file)
653 struct task_struct *task = proc_task(inode);
654 int ret = seq_open(file, &mounts_op);
657 struct seq_file *m = file->private_data;
658 struct namespace *namespace;
660 namespace = task->namespace;
662 get_namespace(namespace);
666 m->private = namespace;
668 seq_release(inode, file);
675 static int mounts_release(struct inode *inode, struct file *file)
677 struct seq_file *m = file->private_data;
678 struct namespace *namespace = m->private;
679 put_namespace(namespace);
680 return seq_release(inode, file);
683 static struct file_operations proc_mounts_operations = {
687 .release = mounts_release,
690 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
692 static ssize_t proc_info_read(struct file * file, char __user * buf,
693 size_t count, loff_t *ppos)
695 struct inode * inode = file->f_dentry->d_inode;
698 struct task_struct *task = proc_task(inode);
700 if (count > PROC_BLOCK_SIZE)
701 count = PROC_BLOCK_SIZE;
702 if (!(page = __get_free_page(GFP_KERNEL)))
705 length = PROC_I(inode)->op.proc_read(task, (char*)page);
708 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
713 static struct file_operations proc_info_file_operations = {
714 .read = proc_info_read,
717 static int mem_open(struct inode* inode, struct file* file)
719 file->private_data = (void*)((long)current->self_exec_id);
723 static ssize_t mem_read(struct file * file, char __user * buf,
724 size_t count, loff_t *ppos)
726 struct task_struct *task = proc_task(file->f_dentry->d_inode);
728 unsigned long src = *ppos;
730 struct mm_struct *mm;
732 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
736 page = (char *)__get_free_page(GFP_USER);
742 mm = get_task_mm(task);
748 if (file->private_data != (void*)((long)current->self_exec_id))
754 int this_len, retval;
756 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
757 retval = access_process_vm(task, src, page, this_len, 0);
758 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
764 if (copy_to_user(buf, page, retval)) {
779 free_page((unsigned long) page);
784 #define mem_write NULL
787 /* This is a security hazard */
788 static ssize_t mem_write(struct file * file, const char * buf,
789 size_t count, loff_t *ppos)
793 struct task_struct *task = proc_task(file->f_dentry->d_inode);
794 unsigned long dst = *ppos;
796 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
799 page = (char *)__get_free_page(GFP_USER);
804 int this_len, retval;
806 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
807 if (copy_from_user(page, buf, this_len)) {
811 retval = access_process_vm(task, dst, page, this_len, 1);
823 free_page((unsigned long) page);
828 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
832 file->f_pos = offset;
835 file->f_pos += offset;
840 force_successful_syscall_return();
844 static struct file_operations proc_mem_operations = {
851 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
852 size_t count, loff_t *ppos)
854 struct task_struct *task = proc_task(file->f_dentry->d_inode);
857 int oom_adjust = task->oomkilladj;
858 loff_t __ppos = *ppos;
860 len = sprintf(buffer, "%i\n", oom_adjust);
863 if (count > len-__ppos)
865 if (copy_to_user(buf, buffer + __ppos, count))
867 *ppos = __ppos + count;
871 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
872 size_t count, loff_t *ppos)
874 struct task_struct *task = proc_task(file->f_dentry->d_inode);
875 char buffer[8], *end;
878 if (!capable(CAP_SYS_RESOURCE))
880 memset(buffer, 0, 8);
883 if (copy_from_user(buffer, buf, count))
885 oom_adjust = simple_strtol(buffer, &end, 0);
886 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
890 task->oomkilladj = oom_adjust;
891 if (end - buffer == 0)
896 static struct file_operations proc_oom_adjust_operations = {
897 .read = oom_adjust_read,
898 .write = oom_adjust_write,
901 static struct inode_operations proc_mem_inode_operations = {
902 .permission = proc_permission,
905 #ifdef CONFIG_AUDITSYSCALL
907 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
908 size_t count, loff_t *ppos)
910 struct inode * inode = file->f_dentry->d_inode;
911 struct task_struct *task = proc_task(inode);
913 char tmpbuf[TMPBUFLEN];
915 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
916 audit_get_loginuid(task->audit_context));
917 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
920 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
921 size_t count, loff_t *ppos)
923 struct inode * inode = file->f_dentry->d_inode;
926 struct task_struct *task = proc_task(inode);
929 if (!capable(CAP_AUDIT_CONTROL))
935 if (count > PAGE_SIZE)
939 /* No partial writes. */
942 page = (char*)__get_free_page(GFP_USER);
946 if (copy_from_user(page, buf, count))
949 loginuid = simple_strtoul(page, &tmp, 10);
955 length = audit_set_loginuid(task, loginuid);
956 if (likely(length == 0))
960 free_page((unsigned long) page);
964 static struct file_operations proc_loginuid_operations = {
965 .read = proc_loginuid_read,
966 .write = proc_loginuid_write,
970 #ifdef CONFIG_SECCOMP
971 static ssize_t seccomp_read(struct file *file, char __user *buf,
972 size_t count, loff_t *ppos)
974 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
976 loff_t __ppos = *ppos;
979 /* no need to print the trailing zero, so use only len */
980 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
983 if (count > len - __ppos)
984 count = len - __ppos;
985 if (copy_to_user(buf, __buf + __ppos, count))
987 *ppos = __ppos + count;
991 static ssize_t seccomp_write(struct file *file, const char __user *buf,
992 size_t count, loff_t *ppos)
994 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
995 char __buf[20], *end;
996 unsigned int seccomp_mode;
998 /* can set it only once to be even more secure */
999 if (unlikely(tsk->seccomp.mode))
1002 memset(__buf, 0, sizeof(__buf));
1003 count = min(count, sizeof(__buf) - 1);
1004 if (copy_from_user(__buf, buf, count))
1006 seccomp_mode = simple_strtoul(__buf, &end, 0);
1009 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1010 tsk->seccomp.mode = seccomp_mode;
1011 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1014 if (unlikely(!(end - __buf)))
1019 static struct file_operations proc_seccomp_operations = {
1020 .read = seccomp_read,
1021 .write = seccomp_write,
1023 #endif /* CONFIG_SECCOMP */
1025 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1027 struct inode *inode = dentry->d_inode;
1028 int error = -EACCES;
1030 /* We don't need a base pointer in the /proc filesystem */
1033 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1035 error = proc_check_root(inode);
1039 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1040 nd->last_type = LAST_BIND;
1042 return ERR_PTR(error);
1045 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1046 char __user *buffer, int buflen)
1048 struct inode * inode;
1049 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1055 inode = dentry->d_inode;
1056 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1057 len = PTR_ERR(path);
1060 len = tmp + PAGE_SIZE - 1 - path;
1064 if (copy_to_user(buffer, path, len))
1067 free_page((unsigned long)tmp);
1071 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1073 int error = -EACCES;
1074 struct inode *inode = dentry->d_inode;
1076 struct vfsmount *mnt = NULL;
1080 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1082 error = proc_check_root(inode);
1086 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1090 error = do_proc_readlink(de, mnt, buffer, buflen);
1098 static struct inode_operations proc_pid_link_inode_operations = {
1099 .readlink = proc_pid_readlink,
1100 .follow_link = proc_pid_follow_link
1105 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1107 struct inode *inode = filp->f_dentry->d_inode;
1108 struct task_struct *p = proc_task(inode);
1109 unsigned int fd, tid, ino;
1112 struct files_struct * files;
1113 struct fdtable *fdt;
1124 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1128 ino = fake_ino(tid, PROC_TID_INO);
1129 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1133 files = get_files_struct(p);
1137 fdt = files_fdtable(files);
1138 for (fd = filp->f_pos-2;
1140 fd++, filp->f_pos++) {
1143 if (!fcheck_files(files, fd))
1151 buf[j] = '0' + (i % 10);
1155 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1156 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1163 put_files_struct(files);
1169 static int proc_pident_readdir(struct file *filp,
1170 void *dirent, filldir_t filldir,
1171 struct pid_entry *ents, unsigned int nents)
1175 struct dentry *dentry = filp->f_dentry;
1176 struct inode *inode = dentry->d_inode;
1177 struct pid_entry *p;
1182 if (!pid_alive(proc_task(inode)))
1186 pid = proc_task(inode)->pid;
1191 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1197 ino = parent_ino(dentry);
1198 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1211 if (filldir(dirent, p->name, p->len, filp->f_pos,
1212 fake_ino(pid, p->type), p->mode >> 12) < 0)
1224 static int proc_tgid_base_readdir(struct file * filp,
1225 void * dirent, filldir_t filldir)
1227 return proc_pident_readdir(filp,dirent,filldir,
1228 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1231 static int proc_tid_base_readdir(struct file * filp,
1232 void * dirent, filldir_t filldir)
1234 return proc_pident_readdir(filp,dirent,filldir,
1235 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1238 /* building an inode */
1240 static int task_dumpable(struct task_struct *task)
1243 struct mm_struct *mm;
1248 dumpable = mm->dumpable;
1256 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1258 struct inode * inode;
1259 struct proc_inode *ei;
1261 /* We need a new inode */
1263 inode = new_inode(sb);
1270 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1271 inode->i_ino = fake_ino(task->pid, ino);
1273 if (!pid_alive(task))
1277 * grab the reference to task.
1279 get_task_struct(task);
1284 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1285 inode->i_uid = task->euid;
1286 inode->i_gid = task->egid;
1288 security_task_to_inode(task, inode);
1302 * Exceptional case: normally we are not allowed to unhash a busy
1303 * directory. In this case, however, we can do it - no aliasing problems
1304 * due to the way we treat inodes.
1306 * Rewrite the inode's ownerships here because the owning task may have
1307 * performed a setuid(), etc.
1309 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1311 struct inode *inode = dentry->d_inode;
1312 struct task_struct *task = proc_task(inode);
1313 if (pid_alive(task)) {
1314 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1315 inode->i_uid = task->euid;
1316 inode->i_gid = task->egid;
1321 security_task_to_inode(task, inode);
1328 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1330 struct inode *inode = dentry->d_inode;
1331 struct task_struct *task = proc_task(inode);
1332 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1333 struct files_struct *files;
1335 files = get_files_struct(task);
1338 if (fcheck_files(files, fd)) {
1340 put_files_struct(files);
1341 if (task_dumpable(task)) {
1342 inode->i_uid = task->euid;
1343 inode->i_gid = task->egid;
1348 security_task_to_inode(task, inode);
1352 put_files_struct(files);
1358 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1360 struct task_struct *task = proc_task(inode);
1361 spin_lock(&task->proc_lock);
1362 if (task->proc_dentry == dentry)
1363 task->proc_dentry = NULL;
1364 spin_unlock(&task->proc_lock);
1368 static int pid_delete_dentry(struct dentry * dentry)
1370 /* Is the task we represent dead?
1371 * If so, then don't put the dentry on the lru list,
1372 * kill it immediately.
1374 return !pid_alive(proc_task(dentry->d_inode));
1377 static struct dentry_operations tid_fd_dentry_operations =
1379 .d_revalidate = tid_fd_revalidate,
1380 .d_delete = pid_delete_dentry,
1383 static struct dentry_operations pid_dentry_operations =
1385 .d_revalidate = pid_revalidate,
1386 .d_delete = pid_delete_dentry,
1389 static struct dentry_operations pid_base_dentry_operations =
1391 .d_revalidate = pid_revalidate,
1392 .d_iput = pid_base_iput,
1393 .d_delete = pid_delete_dentry,
1398 static unsigned name_to_int(struct dentry *dentry)
1400 const char *name = dentry->d_name.name;
1401 int len = dentry->d_name.len;
1404 if (len > 1 && *name == '0')
1407 unsigned c = *name++ - '0';
1410 if (n >= (~0U-9)/10)
1421 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1423 struct task_struct *task = proc_task(dir);
1424 unsigned fd = name_to_int(dentry);
1426 struct files_struct * files;
1427 struct inode *inode;
1428 struct proc_inode *ei;
1432 if (!pid_alive(task))
1435 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1439 files = get_files_struct(task);
1442 inode->i_mode = S_IFLNK;
1444 file = fcheck_files(files, fd);
1447 if (file->f_mode & 1)
1448 inode->i_mode |= S_IRUSR | S_IXUSR;
1449 if (file->f_mode & 2)
1450 inode->i_mode |= S_IWUSR | S_IXUSR;
1452 put_files_struct(files);
1453 inode->i_op = &proc_pid_link_inode_operations;
1455 ei->op.proc_get_link = proc_fd_link;
1456 dentry->d_op = &tid_fd_dentry_operations;
1457 d_add(dentry, inode);
1462 put_files_struct(files);
1466 return ERR_PTR(-ENOENT);
1469 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1470 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1472 static struct file_operations proc_fd_operations = {
1473 .read = generic_read_dir,
1474 .readdir = proc_readfd,
1477 static struct file_operations proc_task_operations = {
1478 .read = generic_read_dir,
1479 .readdir = proc_task_readdir,
1483 * proc directories can do almost nothing..
1485 static struct inode_operations proc_fd_inode_operations = {
1486 .lookup = proc_lookupfd,
1487 .permission = proc_permission,
1490 static struct inode_operations proc_task_inode_operations = {
1491 .lookup = proc_task_lookup,
1492 .permission = proc_task_permission,
1495 #ifdef CONFIG_SECURITY
1496 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1497 size_t count, loff_t *ppos)
1499 struct inode * inode = file->f_dentry->d_inode;
1502 struct task_struct *task = proc_task(inode);
1504 if (count > PAGE_SIZE)
1506 if (!(page = __get_free_page(GFP_KERNEL)))
1509 length = security_getprocattr(task,
1510 (char*)file->f_dentry->d_name.name,
1511 (void*)page, count);
1513 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1518 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1519 size_t count, loff_t *ppos)
1521 struct inode * inode = file->f_dentry->d_inode;
1524 struct task_struct *task = proc_task(inode);
1526 if (count > PAGE_SIZE)
1529 /* No partial writes. */
1532 page = (char*)__get_free_page(GFP_USER);
1536 if (copy_from_user(page, buf, count))
1539 length = security_setprocattr(task,
1540 (char*)file->f_dentry->d_name.name,
1541 (void*)page, count);
1543 free_page((unsigned long) page);
1547 static struct file_operations proc_pid_attr_operations = {
1548 .read = proc_pid_attr_read,
1549 .write = proc_pid_attr_write,
1552 static struct file_operations proc_tid_attr_operations;
1553 static struct inode_operations proc_tid_attr_inode_operations;
1554 static struct file_operations proc_tgid_attr_operations;
1555 static struct inode_operations proc_tgid_attr_inode_operations;
1558 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1561 static struct dentry *proc_pident_lookup(struct inode *dir,
1562 struct dentry *dentry,
1563 struct pid_entry *ents)
1565 struct inode *inode;
1567 struct task_struct *task = proc_task(dir);
1568 struct pid_entry *p;
1569 struct proc_inode *ei;
1574 if (!pid_alive(task))
1577 for (p = ents; p->name; p++) {
1578 if (p->len != dentry->d_name.len)
1580 if (!memcmp(dentry->d_name.name, p->name, p->len))
1587 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1592 inode->i_mode = p->mode;
1594 * Yes, it does not scale. And it should not. Don't add
1595 * new entries into /proc/<tgid>/ without very good reasons.
1598 case PROC_TGID_TASK:
1599 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1600 inode->i_op = &proc_task_inode_operations;
1601 inode->i_fop = &proc_task_operations;
1606 inode->i_op = &proc_fd_inode_operations;
1607 inode->i_fop = &proc_fd_operations;
1611 inode->i_op = &proc_pid_link_inode_operations;
1612 ei->op.proc_get_link = proc_exe_link;
1616 inode->i_op = &proc_pid_link_inode_operations;
1617 ei->op.proc_get_link = proc_cwd_link;
1620 case PROC_TGID_ROOT:
1621 inode->i_op = &proc_pid_link_inode_operations;
1622 ei->op.proc_get_link = proc_root_link;
1624 case PROC_TID_ENVIRON:
1625 case PROC_TGID_ENVIRON:
1626 inode->i_fop = &proc_info_file_operations;
1627 ei->op.proc_read = proc_pid_environ;
1630 case PROC_TGID_AUXV:
1631 inode->i_fop = &proc_info_file_operations;
1632 ei->op.proc_read = proc_pid_auxv;
1634 case PROC_TID_STATUS:
1635 case PROC_TGID_STATUS:
1636 inode->i_fop = &proc_info_file_operations;
1637 ei->op.proc_read = proc_pid_status;
1640 inode->i_fop = &proc_info_file_operations;
1641 ei->op.proc_read = proc_tid_stat;
1643 case PROC_TGID_STAT:
1644 inode->i_fop = &proc_info_file_operations;
1645 ei->op.proc_read = proc_tgid_stat;
1647 case PROC_TID_CMDLINE:
1648 case PROC_TGID_CMDLINE:
1649 inode->i_fop = &proc_info_file_operations;
1650 ei->op.proc_read = proc_pid_cmdline;
1652 case PROC_TID_STATM:
1653 case PROC_TGID_STATM:
1654 inode->i_fop = &proc_info_file_operations;
1655 ei->op.proc_read = proc_pid_statm;
1658 case PROC_TGID_MAPS:
1659 inode->i_fop = &proc_maps_operations;
1662 case PROC_TID_NUMA_MAPS:
1663 case PROC_TGID_NUMA_MAPS:
1664 inode->i_fop = &proc_numa_maps_operations;
1669 inode->i_op = &proc_mem_inode_operations;
1670 inode->i_fop = &proc_mem_operations;
1672 #ifdef CONFIG_SECCOMP
1673 case PROC_TID_SECCOMP:
1674 case PROC_TGID_SECCOMP:
1675 inode->i_fop = &proc_seccomp_operations;
1677 #endif /* CONFIG_SECCOMP */
1678 case PROC_TID_MOUNTS:
1679 case PROC_TGID_MOUNTS:
1680 inode->i_fop = &proc_mounts_operations;
1682 case PROC_TID_SMAPS:
1683 case PROC_TGID_SMAPS:
1684 inode->i_fop = &proc_smaps_operations;
1686 #ifdef CONFIG_SECURITY
1689 inode->i_op = &proc_tid_attr_inode_operations;
1690 inode->i_fop = &proc_tid_attr_operations;
1692 case PROC_TGID_ATTR:
1694 inode->i_op = &proc_tgid_attr_inode_operations;
1695 inode->i_fop = &proc_tgid_attr_operations;
1697 case PROC_TID_ATTR_CURRENT:
1698 case PROC_TGID_ATTR_CURRENT:
1699 case PROC_TID_ATTR_PREV:
1700 case PROC_TGID_ATTR_PREV:
1701 case PROC_TID_ATTR_EXEC:
1702 case PROC_TGID_ATTR_EXEC:
1703 case PROC_TID_ATTR_FSCREATE:
1704 case PROC_TGID_ATTR_FSCREATE:
1705 inode->i_fop = &proc_pid_attr_operations;
1708 #ifdef CONFIG_KALLSYMS
1709 case PROC_TID_WCHAN:
1710 case PROC_TGID_WCHAN:
1711 inode->i_fop = &proc_info_file_operations;
1712 ei->op.proc_read = proc_pid_wchan;
1715 #ifdef CONFIG_SCHEDSTATS
1716 case PROC_TID_SCHEDSTAT:
1717 case PROC_TGID_SCHEDSTAT:
1718 inode->i_fop = &proc_info_file_operations;
1719 ei->op.proc_read = proc_pid_schedstat;
1722 #ifdef CONFIG_CPUSETS
1723 case PROC_TID_CPUSET:
1724 case PROC_TGID_CPUSET:
1725 inode->i_fop = &proc_cpuset_operations;
1728 case PROC_TID_OOM_SCORE:
1729 case PROC_TGID_OOM_SCORE:
1730 inode->i_fop = &proc_info_file_operations;
1731 ei->op.proc_read = proc_oom_score;
1733 case PROC_TID_OOM_ADJUST:
1734 case PROC_TGID_OOM_ADJUST:
1735 inode->i_fop = &proc_oom_adjust_operations;
1737 #ifdef CONFIG_AUDITSYSCALL
1738 case PROC_TID_LOGINUID:
1739 case PROC_TGID_LOGINUID:
1740 inode->i_fop = &proc_loginuid_operations;
1744 printk("procfs: impossible type (%d)",p->type);
1746 return ERR_PTR(-EINVAL);
1748 dentry->d_op = &pid_dentry_operations;
1749 d_add(dentry, inode);
1753 return ERR_PTR(error);
1756 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1757 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1760 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1761 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1764 static struct file_operations proc_tgid_base_operations = {
1765 .read = generic_read_dir,
1766 .readdir = proc_tgid_base_readdir,
1769 static struct file_operations proc_tid_base_operations = {
1770 .read = generic_read_dir,
1771 .readdir = proc_tid_base_readdir,
1774 static struct inode_operations proc_tgid_base_inode_operations = {
1775 .lookup = proc_tgid_base_lookup,
1778 static struct inode_operations proc_tid_base_inode_operations = {
1779 .lookup = proc_tid_base_lookup,
1782 #ifdef CONFIG_SECURITY
1783 static int proc_tgid_attr_readdir(struct file * filp,
1784 void * dirent, filldir_t filldir)
1786 return proc_pident_readdir(filp,dirent,filldir,
1787 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1790 static int proc_tid_attr_readdir(struct file * filp,
1791 void * dirent, filldir_t filldir)
1793 return proc_pident_readdir(filp,dirent,filldir,
1794 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1797 static struct file_operations proc_tgid_attr_operations = {
1798 .read = generic_read_dir,
1799 .readdir = proc_tgid_attr_readdir,
1802 static struct file_operations proc_tid_attr_operations = {
1803 .read = generic_read_dir,
1804 .readdir = proc_tid_attr_readdir,
1807 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1808 struct dentry *dentry, struct nameidata *nd)
1810 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1813 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1814 struct dentry *dentry, struct nameidata *nd)
1816 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1819 static struct inode_operations proc_tgid_attr_inode_operations = {
1820 .lookup = proc_tgid_attr_lookup,
1823 static struct inode_operations proc_tid_attr_inode_operations = {
1824 .lookup = proc_tid_attr_lookup,
1831 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1835 sprintf(tmp, "%d", current->tgid);
1836 return vfs_readlink(dentry,buffer,buflen,tmp);
1839 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1842 sprintf(tmp, "%d", current->tgid);
1843 return ERR_PTR(vfs_follow_link(nd,tmp));
1846 static struct inode_operations proc_self_inode_operations = {
1847 .readlink = proc_self_readlink,
1848 .follow_link = proc_self_follow_link,
1852 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1853 * @p: task that should be flushed.
1855 * Drops the /proc/@pid dcache entry from the hash chains.
1857 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1858 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1859 * if the pid value is immediately reused. This is enforced by
1860 * - caller must acquire spin_lock(p->proc_lock)
1861 * - must be called before detach_pid()
1862 * - proc_pid_lookup acquires proc_lock, and checks that
1863 * the target is not dead by looking at the attach count
1867 struct dentry *proc_pid_unhash(struct task_struct *p)
1869 struct dentry *proc_dentry;
1871 proc_dentry = p->proc_dentry;
1872 if (proc_dentry != NULL) {
1874 spin_lock(&dcache_lock);
1875 spin_lock(&proc_dentry->d_lock);
1876 if (!d_unhashed(proc_dentry)) {
1877 dget_locked(proc_dentry);
1878 __d_drop(proc_dentry);
1879 spin_unlock(&proc_dentry->d_lock);
1881 spin_unlock(&proc_dentry->d_lock);
1884 spin_unlock(&dcache_lock);
1890 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1891 * @proc_dentry: directoy to prune.
1893 * Shrink the /proc directory that was used by the just killed thread.
1896 void proc_pid_flush(struct dentry *proc_dentry)
1899 if(proc_dentry != NULL) {
1900 shrink_dcache_parent(proc_dentry);
1906 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1908 struct task_struct *task;
1909 struct inode *inode;
1910 struct proc_inode *ei;
1914 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1915 inode = new_inode(dir->i_sb);
1917 return ERR_PTR(-ENOMEM);
1919 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1920 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1922 inode->i_mode = S_IFLNK|S_IRWXUGO;
1923 inode->i_uid = inode->i_gid = 0;
1925 inode->i_op = &proc_self_inode_operations;
1926 d_add(dentry, inode);
1929 tgid = name_to_int(dentry);
1933 read_lock(&tasklist_lock);
1934 task = find_task_by_pid(tgid);
1936 get_task_struct(task);
1937 read_unlock(&tasklist_lock);
1941 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1945 put_task_struct(task);
1948 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1949 inode->i_op = &proc_tgid_base_inode_operations;
1950 inode->i_fop = &proc_tgid_base_operations;
1951 inode->i_flags|=S_IMMUTABLE;
1952 #ifdef CONFIG_SECURITY
1958 dentry->d_op = &pid_base_dentry_operations;
1961 d_add(dentry, inode);
1962 spin_lock(&task->proc_lock);
1963 task->proc_dentry = dentry;
1964 if (!pid_alive(task)) {
1965 dentry = proc_pid_unhash(task);
1968 spin_unlock(&task->proc_lock);
1970 put_task_struct(task);
1972 proc_pid_flush(dentry);
1977 return ERR_PTR(-ENOENT);
1981 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1983 struct task_struct *task;
1984 struct task_struct *leader = proc_task(dir);
1985 struct inode *inode;
1988 tid = name_to_int(dentry);
1992 read_lock(&tasklist_lock);
1993 task = find_task_by_pid(tid);
1995 get_task_struct(task);
1996 read_unlock(&tasklist_lock);
1999 if (leader->tgid != task->tgid)
2002 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2007 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2008 inode->i_op = &proc_tid_base_inode_operations;
2009 inode->i_fop = &proc_tid_base_operations;
2010 inode->i_flags|=S_IMMUTABLE;
2011 #ifdef CONFIG_SECURITY
2017 dentry->d_op = &pid_base_dentry_operations;
2019 d_add(dentry, inode);
2021 put_task_struct(task);
2024 put_task_struct(task);
2026 return ERR_PTR(-ENOENT);
2029 #define PROC_NUMBUF 10
2030 #define PROC_MAXPIDS 20
2033 * Get a few tgid's to return for filldir - we need to hold the
2034 * tasklist lock while doing this, and we must release it before
2035 * we actually do the filldir itself, so we use a temp buffer..
2037 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2039 struct task_struct *p;
2043 read_lock(&tasklist_lock);
2046 p = find_task_by_pid(version);
2047 if (p && !thread_group_leader(p))
2054 p = next_task(&init_task);
2056 for ( ; p != &init_task; p = next_task(p)) {
2062 tgids[nr_tgids] = tgid;
2064 if (nr_tgids >= PROC_MAXPIDS)
2067 read_unlock(&tasklist_lock);
2072 * Get a few tid'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_tid_list(int index, unsigned int *tids, struct inode *dir)
2078 struct task_struct *leader_task = proc_task(dir);
2079 struct task_struct *task = leader_task;
2083 read_lock(&tasklist_lock);
2085 * The starting point task (leader_task) might be an already
2086 * unlinked task, which cannot be used to access the task-list
2087 * via next_thread().
2089 if (pid_alive(task)) do {
2090 int tid = task->pid;
2095 tids[nr_tids] = tid;
2097 if (nr_tids >= PROC_MAXPIDS)
2099 } while ((task = next_thread(task)) != leader_task);
2100 read_unlock(&tasklist_lock);
2104 /* for the /proc/ directory itself, after non-process stuff has been done */
2105 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2107 unsigned int tgid_array[PROC_MAXPIDS];
2108 char buf[PROC_NUMBUF];
2109 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2110 unsigned int nr_tgids, i;
2114 ino_t ino = fake_ino(0,PROC_TGID_INO);
2115 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2121 /* f_version caches the tgid value that the last readdir call couldn't
2122 * return. lseek aka telldir automagically resets f_version to 0.
2124 next_tgid = filp->f_version;
2125 filp->f_version = 0;
2127 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2129 /* no more entries ! */
2134 /* do not use the last found pid, reserve it for next_tgid */
2135 if (nr_tgids == PROC_MAXPIDS) {
2137 next_tgid = tgid_array[nr_tgids];
2140 for (i=0;i<nr_tgids;i++) {
2141 int tgid = tgid_array[i];
2142 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2143 unsigned long j = PROC_NUMBUF;
2146 buf[--j] = '0' + (tgid % 10);
2147 while ((tgid /= 10) != 0);
2149 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2150 /* returning this tgid failed, save it as the first
2151 * pid for the next readir call */
2152 filp->f_version = tgid_array[i];
2163 /* for the /proc/TGID/task/ directories */
2164 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2166 unsigned int tid_array[PROC_MAXPIDS];
2167 char buf[PROC_NUMBUF];
2168 unsigned int nr_tids, i;
2169 struct dentry *dentry = filp->f_dentry;
2170 struct inode *inode = dentry->d_inode;
2171 int retval = -ENOENT;
2173 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2175 if (!pid_alive(proc_task(inode)))
2182 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2187 ino = parent_ino(dentry);
2188 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2194 nr_tids = get_tid_list(pos, tid_array, inode);
2195 inode->i_nlink = pos + nr_tids;
2197 for (i = 0; i < nr_tids; i++) {
2198 unsigned long j = PROC_NUMBUF;
2199 int tid = tid_array[i];
2201 ino = fake_ino(tid,PROC_TID_INO);
2204 buf[--j] = '0' + (tid % 10);
2205 while ((tid /= 10) != 0);
2207 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)