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);
343 #define MAY_PTRACE(task) \
344 (task == current || \
345 (task->parent == current && \
346 (task->ptrace & PT_PTRACED) && \
347 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
348 security_ptrace(current,task) == 0))
350 static int proc_pid_environ(struct task_struct *task, char * buffer)
353 struct mm_struct *mm = get_task_mm(task);
355 unsigned int len = mm->env_end - mm->env_start;
358 res = access_process_vm(task, mm->env_start, buffer, len, 0);
359 if (!ptrace_may_attach(task))
366 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
370 struct mm_struct *mm = get_task_mm(task);
374 goto out_mm; /* Shh! No looking before we're done */
376 len = mm->arg_end - mm->arg_start;
381 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
383 // If the nul at the end of args has been overwritten, then
384 // assume application is using setproctitle(3).
385 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
386 len = strnlen(buffer, res);
390 len = mm->env_end - mm->env_start;
391 if (len > PAGE_SIZE - res)
392 len = PAGE_SIZE - res;
393 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
394 res = strnlen(buffer, res);
403 static int proc_pid_auxv(struct task_struct *task, char *buffer)
406 struct mm_struct *mm = get_task_mm(task);
408 unsigned int nwords = 0;
411 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
412 res = nwords * sizeof(mm->saved_auxv[0]);
415 memcpy(buffer, mm->saved_auxv, res);
422 #ifdef CONFIG_KALLSYMS
424 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
425 * Returns the resolved symbol. If that fails, simply return the address.
427 static int proc_pid_wchan(struct task_struct *task, char *buffer)
430 const char *sym_name;
431 unsigned long wchan, size, offset;
432 char namebuf[KSYM_NAME_LEN+1];
434 wchan = get_wchan(task);
436 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
438 return sprintf(buffer, "%s", sym_name);
439 return sprintf(buffer, "%lu", wchan);
441 #endif /* CONFIG_KALLSYMS */
443 #ifdef CONFIG_SCHEDSTATS
445 * Provides /proc/PID/schedstat
447 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
449 return sprintf(buffer, "%lu %lu %lu\n",
450 task->sched_info.cpu_time,
451 task->sched_info.run_delay,
452 task->sched_info.pcnt);
456 /* The badness from the OOM killer */
457 unsigned long badness(struct task_struct *p, unsigned long uptime);
458 static int proc_oom_score(struct task_struct *task, char *buffer)
460 unsigned long points;
461 struct timespec uptime;
463 do_posix_clock_monotonic_gettime(&uptime);
464 points = badness(task, uptime.tv_sec);
465 return sprintf(buffer, "%lu\n", points);
468 /************************************************************************/
469 /* Here the fs part begins */
470 /************************************************************************/
472 /* permission checks */
474 static int proc_check_root(struct inode *inode)
476 struct dentry *de, *base, *root;
477 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
480 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
482 read_lock(¤t->fs->lock);
483 our_vfsmnt = mntget(current->fs->rootmnt);
484 base = dget(current->fs->root);
485 read_unlock(¤t->fs->lock);
487 spin_lock(&vfsmount_lock);
491 while (vfsmnt != our_vfsmnt) {
492 if (vfsmnt == vfsmnt->mnt_parent)
494 de = vfsmnt->mnt_mountpoint;
495 vfsmnt = vfsmnt->mnt_parent;
498 if (!is_subdir(de, base))
500 spin_unlock(&vfsmount_lock);
509 spin_unlock(&vfsmount_lock);
514 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
516 if (generic_permission(inode, mask, NULL) != 0)
518 return proc_check_root(inode);
521 extern struct seq_operations proc_pid_maps_op;
522 static int maps_open(struct inode *inode, struct file *file)
524 struct task_struct *task = proc_task(inode);
525 int ret = seq_open(file, &proc_pid_maps_op);
527 struct seq_file *m = file->private_data;
533 static struct file_operations proc_maps_operations = {
537 .release = seq_release,
541 extern struct seq_operations proc_pid_numa_maps_op;
542 static int numa_maps_open(struct inode *inode, struct file *file)
544 struct task_struct *task = proc_task(inode);
545 int ret = seq_open(file, &proc_pid_numa_maps_op);
547 struct seq_file *m = file->private_data;
553 static struct file_operations proc_numa_maps_operations = {
554 .open = numa_maps_open,
557 .release = seq_release,
561 extern struct seq_operations proc_pid_smaps_op;
562 static int smaps_open(struct inode *inode, struct file *file)
564 struct task_struct *task = proc_task(inode);
565 int ret = seq_open(file, &proc_pid_smaps_op);
567 struct seq_file *m = file->private_data;
573 static struct file_operations proc_smaps_operations = {
577 .release = seq_release,
580 extern struct seq_operations mounts_op;
581 static int mounts_open(struct inode *inode, struct file *file)
583 struct task_struct *task = proc_task(inode);
584 int ret = seq_open(file, &mounts_op);
587 struct seq_file *m = file->private_data;
588 struct namespace *namespace;
590 namespace = task->namespace;
592 get_namespace(namespace);
596 m->private = namespace;
598 seq_release(inode, file);
605 static int mounts_release(struct inode *inode, struct file *file)
607 struct seq_file *m = file->private_data;
608 struct namespace *namespace = m->private;
609 put_namespace(namespace);
610 return seq_release(inode, file);
613 static struct file_operations proc_mounts_operations = {
617 .release = mounts_release,
620 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
622 static ssize_t proc_info_read(struct file * file, char __user * buf,
623 size_t count, loff_t *ppos)
625 struct inode * inode = file->f_dentry->d_inode;
628 struct task_struct *task = proc_task(inode);
630 if (count > PROC_BLOCK_SIZE)
631 count = PROC_BLOCK_SIZE;
632 if (!(page = __get_free_page(GFP_KERNEL)))
635 length = PROC_I(inode)->op.proc_read(task, (char*)page);
638 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
643 static struct file_operations proc_info_file_operations = {
644 .read = proc_info_read,
647 static int mem_open(struct inode* inode, struct file* file)
649 file->private_data = (void*)((long)current->self_exec_id);
653 static ssize_t mem_read(struct file * file, char __user * buf,
654 size_t count, loff_t *ppos)
656 struct task_struct *task = proc_task(file->f_dentry->d_inode);
658 unsigned long src = *ppos;
660 struct mm_struct *mm;
662 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
666 page = (char *)__get_free_page(GFP_USER);
672 mm = get_task_mm(task);
678 if (file->private_data != (void*)((long)current->self_exec_id))
684 int this_len, retval;
686 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
687 retval = access_process_vm(task, src, page, this_len, 0);
688 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
694 if (copy_to_user(buf, page, retval)) {
709 free_page((unsigned long) page);
714 #define mem_write NULL
717 /* This is a security hazard */
718 static ssize_t mem_write(struct file * file, const char * buf,
719 size_t count, loff_t *ppos)
723 struct task_struct *task = proc_task(file->f_dentry->d_inode);
724 unsigned long dst = *ppos;
726 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
729 page = (char *)__get_free_page(GFP_USER);
734 int this_len, retval;
736 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
737 if (copy_from_user(page, buf, this_len)) {
741 retval = access_process_vm(task, dst, page, this_len, 1);
753 free_page((unsigned long) page);
758 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
762 file->f_pos = offset;
765 file->f_pos += offset;
770 force_successful_syscall_return();
774 static struct file_operations proc_mem_operations = {
781 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
782 size_t count, loff_t *ppos)
784 struct task_struct *task = proc_task(file->f_dentry->d_inode);
787 int oom_adjust = task->oomkilladj;
788 loff_t __ppos = *ppos;
790 len = sprintf(buffer, "%i\n", oom_adjust);
793 if (count > len-__ppos)
795 if (copy_to_user(buf, buffer + __ppos, count))
797 *ppos = __ppos + count;
801 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
802 size_t count, loff_t *ppos)
804 struct task_struct *task = proc_task(file->f_dentry->d_inode);
805 char buffer[8], *end;
808 if (!capable(CAP_SYS_RESOURCE))
810 memset(buffer, 0, 8);
813 if (copy_from_user(buffer, buf, count))
815 oom_adjust = simple_strtol(buffer, &end, 0);
816 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
820 task->oomkilladj = oom_adjust;
821 if (end - buffer == 0)
826 static struct file_operations proc_oom_adjust_operations = {
827 .read = oom_adjust_read,
828 .write = oom_adjust_write,
831 static struct inode_operations proc_mem_inode_operations = {
832 .permission = proc_permission,
835 #ifdef CONFIG_AUDITSYSCALL
837 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
838 size_t count, loff_t *ppos)
840 struct inode * inode = file->f_dentry->d_inode;
841 struct task_struct *task = proc_task(inode);
843 char tmpbuf[TMPBUFLEN];
845 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
846 audit_get_loginuid(task->audit_context));
847 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
850 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
851 size_t count, loff_t *ppos)
853 struct inode * inode = file->f_dentry->d_inode;
856 struct task_struct *task = proc_task(inode);
859 if (!capable(CAP_AUDIT_CONTROL))
865 if (count > PAGE_SIZE)
869 /* No partial writes. */
872 page = (char*)__get_free_page(GFP_USER);
876 if (copy_from_user(page, buf, count))
879 loginuid = simple_strtoul(page, &tmp, 10);
885 length = audit_set_loginuid(task, loginuid);
886 if (likely(length == 0))
890 free_page((unsigned long) page);
894 static struct file_operations proc_loginuid_operations = {
895 .read = proc_loginuid_read,
896 .write = proc_loginuid_write,
900 #ifdef CONFIG_SECCOMP
901 static ssize_t seccomp_read(struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
904 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
906 loff_t __ppos = *ppos;
909 /* no need to print the trailing zero, so use only len */
910 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
913 if (count > len - __ppos)
914 count = len - __ppos;
915 if (copy_to_user(buf, __buf + __ppos, count))
917 *ppos = __ppos + count;
921 static ssize_t seccomp_write(struct file *file, const char __user *buf,
922 size_t count, loff_t *ppos)
924 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
925 char __buf[20], *end;
926 unsigned int seccomp_mode;
928 /* can set it only once to be even more secure */
929 if (unlikely(tsk->seccomp.mode))
932 memset(__buf, 0, sizeof(__buf));
933 count = min(count, sizeof(__buf) - 1);
934 if (copy_from_user(__buf, buf, count))
936 seccomp_mode = simple_strtoul(__buf, &end, 0);
939 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
940 tsk->seccomp.mode = seccomp_mode;
941 set_tsk_thread_flag(tsk, TIF_SECCOMP);
944 if (unlikely(!(end - __buf)))
949 static struct file_operations proc_seccomp_operations = {
950 .read = seccomp_read,
951 .write = seccomp_write,
953 #endif /* CONFIG_SECCOMP */
955 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
957 struct inode *inode = dentry->d_inode;
960 /* We don't need a base pointer in the /proc filesystem */
963 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
965 error = proc_check_root(inode);
969 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
970 nd->last_type = LAST_BIND;
972 return ERR_PTR(error);
975 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
976 char __user *buffer, int buflen)
978 struct inode * inode;
979 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
985 inode = dentry->d_inode;
986 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
990 len = tmp + PAGE_SIZE - 1 - path;
994 if (copy_to_user(buffer, path, len))
997 free_page((unsigned long)tmp);
1001 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1003 int error = -EACCES;
1004 struct inode *inode = dentry->d_inode;
1006 struct vfsmount *mnt = NULL;
1010 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1012 error = proc_check_root(inode);
1016 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1020 error = do_proc_readlink(de, mnt, buffer, buflen);
1028 static struct inode_operations proc_pid_link_inode_operations = {
1029 .readlink = proc_pid_readlink,
1030 .follow_link = proc_pid_follow_link
1035 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1037 struct inode *inode = filp->f_dentry->d_inode;
1038 struct task_struct *p = proc_task(inode);
1039 unsigned int fd, tid, ino;
1042 struct files_struct * files;
1043 struct fdtable *fdt;
1054 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1058 ino = fake_ino(tid, PROC_TID_INO);
1059 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1063 files = get_files_struct(p);
1067 fdt = files_fdtable(files);
1068 for (fd = filp->f_pos-2;
1070 fd++, filp->f_pos++) {
1073 if (!fcheck_files(files, fd))
1081 buf[j] = '0' + (i % 10);
1085 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1086 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1093 put_files_struct(files);
1099 static int proc_pident_readdir(struct file *filp,
1100 void *dirent, filldir_t filldir,
1101 struct pid_entry *ents, unsigned int nents)
1105 struct dentry *dentry = filp->f_dentry;
1106 struct inode *inode = dentry->d_inode;
1107 struct pid_entry *p;
1112 if (!pid_alive(proc_task(inode)))
1116 pid = proc_task(inode)->pid;
1121 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1127 ino = parent_ino(dentry);
1128 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1141 if (filldir(dirent, p->name, p->len, filp->f_pos,
1142 fake_ino(pid, p->type), p->mode >> 12) < 0)
1154 static int proc_tgid_base_readdir(struct file * filp,
1155 void * dirent, filldir_t filldir)
1157 return proc_pident_readdir(filp,dirent,filldir,
1158 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1161 static int proc_tid_base_readdir(struct file * filp,
1162 void * dirent, filldir_t filldir)
1164 return proc_pident_readdir(filp,dirent,filldir,
1165 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1168 /* building an inode */
1170 static int task_dumpable(struct task_struct *task)
1173 struct mm_struct *mm;
1178 dumpable = mm->dumpable;
1186 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1188 struct inode * inode;
1189 struct proc_inode *ei;
1191 /* We need a new inode */
1193 inode = new_inode(sb);
1200 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1201 inode->i_ino = fake_ino(task->pid, ino);
1203 if (!pid_alive(task))
1207 * grab the reference to task.
1209 get_task_struct(task);
1214 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1215 inode->i_uid = task->euid;
1216 inode->i_gid = task->egid;
1218 security_task_to_inode(task, inode);
1232 * Exceptional case: normally we are not allowed to unhash a busy
1233 * directory. In this case, however, we can do it - no aliasing problems
1234 * due to the way we treat inodes.
1236 * Rewrite the inode's ownerships here because the owning task may have
1237 * performed a setuid(), etc.
1239 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1241 struct inode *inode = dentry->d_inode;
1242 struct task_struct *task = proc_task(inode);
1243 if (pid_alive(task)) {
1244 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1245 inode->i_uid = task->euid;
1246 inode->i_gid = task->egid;
1251 security_task_to_inode(task, inode);
1258 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1260 struct inode *inode = dentry->d_inode;
1261 struct task_struct *task = proc_task(inode);
1262 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1263 struct files_struct *files;
1265 files = get_files_struct(task);
1268 if (fcheck_files(files, fd)) {
1270 put_files_struct(files);
1271 if (task_dumpable(task)) {
1272 inode->i_uid = task->euid;
1273 inode->i_gid = task->egid;
1278 security_task_to_inode(task, inode);
1282 put_files_struct(files);
1288 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1290 struct task_struct *task = proc_task(inode);
1291 spin_lock(&task->proc_lock);
1292 if (task->proc_dentry == dentry)
1293 task->proc_dentry = NULL;
1294 spin_unlock(&task->proc_lock);
1298 static int pid_delete_dentry(struct dentry * dentry)
1300 /* Is the task we represent dead?
1301 * If so, then don't put the dentry on the lru list,
1302 * kill it immediately.
1304 return !pid_alive(proc_task(dentry->d_inode));
1307 static struct dentry_operations tid_fd_dentry_operations =
1309 .d_revalidate = tid_fd_revalidate,
1310 .d_delete = pid_delete_dentry,
1313 static struct dentry_operations pid_dentry_operations =
1315 .d_revalidate = pid_revalidate,
1316 .d_delete = pid_delete_dentry,
1319 static struct dentry_operations pid_base_dentry_operations =
1321 .d_revalidate = pid_revalidate,
1322 .d_iput = pid_base_iput,
1323 .d_delete = pid_delete_dentry,
1328 static unsigned name_to_int(struct dentry *dentry)
1330 const char *name = dentry->d_name.name;
1331 int len = dentry->d_name.len;
1334 if (len > 1 && *name == '0')
1337 unsigned c = *name++ - '0';
1340 if (n >= (~0U-9)/10)
1351 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1353 struct task_struct *task = proc_task(dir);
1354 unsigned fd = name_to_int(dentry);
1356 struct files_struct * files;
1357 struct inode *inode;
1358 struct proc_inode *ei;
1362 if (!pid_alive(task))
1365 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1369 files = get_files_struct(task);
1372 inode->i_mode = S_IFLNK;
1374 file = fcheck_files(files, fd);
1377 if (file->f_mode & 1)
1378 inode->i_mode |= S_IRUSR | S_IXUSR;
1379 if (file->f_mode & 2)
1380 inode->i_mode |= S_IWUSR | S_IXUSR;
1382 put_files_struct(files);
1383 inode->i_op = &proc_pid_link_inode_operations;
1385 ei->op.proc_get_link = proc_fd_link;
1386 dentry->d_op = &tid_fd_dentry_operations;
1387 d_add(dentry, inode);
1392 put_files_struct(files);
1396 return ERR_PTR(-ENOENT);
1399 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1400 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1402 static struct file_operations proc_fd_operations = {
1403 .read = generic_read_dir,
1404 .readdir = proc_readfd,
1407 static struct file_operations proc_task_operations = {
1408 .read = generic_read_dir,
1409 .readdir = proc_task_readdir,
1413 * proc directories can do almost nothing..
1415 static struct inode_operations proc_fd_inode_operations = {
1416 .lookup = proc_lookupfd,
1417 .permission = proc_permission,
1420 static struct inode_operations proc_task_inode_operations = {
1421 .lookup = proc_task_lookup,
1422 .permission = proc_permission,
1425 #ifdef CONFIG_SECURITY
1426 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1427 size_t count, loff_t *ppos)
1429 struct inode * inode = file->f_dentry->d_inode;
1432 struct task_struct *task = proc_task(inode);
1434 if (count > PAGE_SIZE)
1436 if (!(page = __get_free_page(GFP_KERNEL)))
1439 length = security_getprocattr(task,
1440 (char*)file->f_dentry->d_name.name,
1441 (void*)page, count);
1443 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1448 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1449 size_t count, loff_t *ppos)
1451 struct inode * inode = file->f_dentry->d_inode;
1454 struct task_struct *task = proc_task(inode);
1456 if (count > PAGE_SIZE)
1459 /* No partial writes. */
1462 page = (char*)__get_free_page(GFP_USER);
1466 if (copy_from_user(page, buf, count))
1469 length = security_setprocattr(task,
1470 (char*)file->f_dentry->d_name.name,
1471 (void*)page, count);
1473 free_page((unsigned long) page);
1477 static struct file_operations proc_pid_attr_operations = {
1478 .read = proc_pid_attr_read,
1479 .write = proc_pid_attr_write,
1482 static struct file_operations proc_tid_attr_operations;
1483 static struct inode_operations proc_tid_attr_inode_operations;
1484 static struct file_operations proc_tgid_attr_operations;
1485 static struct inode_operations proc_tgid_attr_inode_operations;
1488 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1491 static struct dentry *proc_pident_lookup(struct inode *dir,
1492 struct dentry *dentry,
1493 struct pid_entry *ents)
1495 struct inode *inode;
1497 struct task_struct *task = proc_task(dir);
1498 struct pid_entry *p;
1499 struct proc_inode *ei;
1504 if (!pid_alive(task))
1507 for (p = ents; p->name; p++) {
1508 if (p->len != dentry->d_name.len)
1510 if (!memcmp(dentry->d_name.name, p->name, p->len))
1517 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1522 inode->i_mode = p->mode;
1524 * Yes, it does not scale. And it should not. Don't add
1525 * new entries into /proc/<tgid>/ without very good reasons.
1528 case PROC_TGID_TASK:
1529 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1530 inode->i_op = &proc_task_inode_operations;
1531 inode->i_fop = &proc_task_operations;
1536 inode->i_op = &proc_fd_inode_operations;
1537 inode->i_fop = &proc_fd_operations;
1541 inode->i_op = &proc_pid_link_inode_operations;
1542 ei->op.proc_get_link = proc_exe_link;
1546 inode->i_op = &proc_pid_link_inode_operations;
1547 ei->op.proc_get_link = proc_cwd_link;
1550 case PROC_TGID_ROOT:
1551 inode->i_op = &proc_pid_link_inode_operations;
1552 ei->op.proc_get_link = proc_root_link;
1554 case PROC_TID_ENVIRON:
1555 case PROC_TGID_ENVIRON:
1556 inode->i_fop = &proc_info_file_operations;
1557 ei->op.proc_read = proc_pid_environ;
1560 case PROC_TGID_AUXV:
1561 inode->i_fop = &proc_info_file_operations;
1562 ei->op.proc_read = proc_pid_auxv;
1564 case PROC_TID_STATUS:
1565 case PROC_TGID_STATUS:
1566 inode->i_fop = &proc_info_file_operations;
1567 ei->op.proc_read = proc_pid_status;
1570 inode->i_fop = &proc_info_file_operations;
1571 ei->op.proc_read = proc_tid_stat;
1573 case PROC_TGID_STAT:
1574 inode->i_fop = &proc_info_file_operations;
1575 ei->op.proc_read = proc_tgid_stat;
1577 case PROC_TID_CMDLINE:
1578 case PROC_TGID_CMDLINE:
1579 inode->i_fop = &proc_info_file_operations;
1580 ei->op.proc_read = proc_pid_cmdline;
1582 case PROC_TID_STATM:
1583 case PROC_TGID_STATM:
1584 inode->i_fop = &proc_info_file_operations;
1585 ei->op.proc_read = proc_pid_statm;
1588 case PROC_TGID_MAPS:
1589 inode->i_fop = &proc_maps_operations;
1592 case PROC_TID_NUMA_MAPS:
1593 case PROC_TGID_NUMA_MAPS:
1594 inode->i_fop = &proc_numa_maps_operations;
1599 inode->i_op = &proc_mem_inode_operations;
1600 inode->i_fop = &proc_mem_operations;
1602 #ifdef CONFIG_SECCOMP
1603 case PROC_TID_SECCOMP:
1604 case PROC_TGID_SECCOMP:
1605 inode->i_fop = &proc_seccomp_operations;
1607 #endif /* CONFIG_SECCOMP */
1608 case PROC_TID_MOUNTS:
1609 case PROC_TGID_MOUNTS:
1610 inode->i_fop = &proc_mounts_operations;
1612 case PROC_TID_SMAPS:
1613 case PROC_TGID_SMAPS:
1614 inode->i_fop = &proc_smaps_operations;
1616 #ifdef CONFIG_SECURITY
1619 inode->i_op = &proc_tid_attr_inode_operations;
1620 inode->i_fop = &proc_tid_attr_operations;
1622 case PROC_TGID_ATTR:
1624 inode->i_op = &proc_tgid_attr_inode_operations;
1625 inode->i_fop = &proc_tgid_attr_operations;
1627 case PROC_TID_ATTR_CURRENT:
1628 case PROC_TGID_ATTR_CURRENT:
1629 case PROC_TID_ATTR_PREV:
1630 case PROC_TGID_ATTR_PREV:
1631 case PROC_TID_ATTR_EXEC:
1632 case PROC_TGID_ATTR_EXEC:
1633 case PROC_TID_ATTR_FSCREATE:
1634 case PROC_TGID_ATTR_FSCREATE:
1635 inode->i_fop = &proc_pid_attr_operations;
1638 #ifdef CONFIG_KALLSYMS
1639 case PROC_TID_WCHAN:
1640 case PROC_TGID_WCHAN:
1641 inode->i_fop = &proc_info_file_operations;
1642 ei->op.proc_read = proc_pid_wchan;
1645 #ifdef CONFIG_SCHEDSTATS
1646 case PROC_TID_SCHEDSTAT:
1647 case PROC_TGID_SCHEDSTAT:
1648 inode->i_fop = &proc_info_file_operations;
1649 ei->op.proc_read = proc_pid_schedstat;
1652 #ifdef CONFIG_CPUSETS
1653 case PROC_TID_CPUSET:
1654 case PROC_TGID_CPUSET:
1655 inode->i_fop = &proc_cpuset_operations;
1658 case PROC_TID_OOM_SCORE:
1659 case PROC_TGID_OOM_SCORE:
1660 inode->i_fop = &proc_info_file_operations;
1661 ei->op.proc_read = proc_oom_score;
1663 case PROC_TID_OOM_ADJUST:
1664 case PROC_TGID_OOM_ADJUST:
1665 inode->i_fop = &proc_oom_adjust_operations;
1667 #ifdef CONFIG_AUDITSYSCALL
1668 case PROC_TID_LOGINUID:
1669 case PROC_TGID_LOGINUID:
1670 inode->i_fop = &proc_loginuid_operations;
1674 printk("procfs: impossible type (%d)",p->type);
1676 return ERR_PTR(-EINVAL);
1678 dentry->d_op = &pid_dentry_operations;
1679 d_add(dentry, inode);
1683 return ERR_PTR(error);
1686 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1687 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1690 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1691 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1694 static struct file_operations proc_tgid_base_operations = {
1695 .read = generic_read_dir,
1696 .readdir = proc_tgid_base_readdir,
1699 static struct file_operations proc_tid_base_operations = {
1700 .read = generic_read_dir,
1701 .readdir = proc_tid_base_readdir,
1704 static struct inode_operations proc_tgid_base_inode_operations = {
1705 .lookup = proc_tgid_base_lookup,
1708 static struct inode_operations proc_tid_base_inode_operations = {
1709 .lookup = proc_tid_base_lookup,
1712 #ifdef CONFIG_SECURITY
1713 static int proc_tgid_attr_readdir(struct file * filp,
1714 void * dirent, filldir_t filldir)
1716 return proc_pident_readdir(filp,dirent,filldir,
1717 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1720 static int proc_tid_attr_readdir(struct file * filp,
1721 void * dirent, filldir_t filldir)
1723 return proc_pident_readdir(filp,dirent,filldir,
1724 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1727 static struct file_operations proc_tgid_attr_operations = {
1728 .read = generic_read_dir,
1729 .readdir = proc_tgid_attr_readdir,
1732 static struct file_operations proc_tid_attr_operations = {
1733 .read = generic_read_dir,
1734 .readdir = proc_tid_attr_readdir,
1737 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1738 struct dentry *dentry, struct nameidata *nd)
1740 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1743 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1744 struct dentry *dentry, struct nameidata *nd)
1746 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1749 static struct inode_operations proc_tgid_attr_inode_operations = {
1750 .lookup = proc_tgid_attr_lookup,
1753 static struct inode_operations proc_tid_attr_inode_operations = {
1754 .lookup = proc_tid_attr_lookup,
1761 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1765 sprintf(tmp, "%d", current->tgid);
1766 return vfs_readlink(dentry,buffer,buflen,tmp);
1769 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1772 sprintf(tmp, "%d", current->tgid);
1773 return ERR_PTR(vfs_follow_link(nd,tmp));
1776 static struct inode_operations proc_self_inode_operations = {
1777 .readlink = proc_self_readlink,
1778 .follow_link = proc_self_follow_link,
1782 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1783 * @p: task that should be flushed.
1785 * Drops the /proc/@pid dcache entry from the hash chains.
1787 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1788 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1789 * if the pid value is immediately reused. This is enforced by
1790 * - caller must acquire spin_lock(p->proc_lock)
1791 * - must be called before detach_pid()
1792 * - proc_pid_lookup acquires proc_lock, and checks that
1793 * the target is not dead by looking at the attach count
1797 struct dentry *proc_pid_unhash(struct task_struct *p)
1799 struct dentry *proc_dentry;
1801 proc_dentry = p->proc_dentry;
1802 if (proc_dentry != NULL) {
1804 spin_lock(&dcache_lock);
1805 spin_lock(&proc_dentry->d_lock);
1806 if (!d_unhashed(proc_dentry)) {
1807 dget_locked(proc_dentry);
1808 __d_drop(proc_dentry);
1809 spin_unlock(&proc_dentry->d_lock);
1811 spin_unlock(&proc_dentry->d_lock);
1814 spin_unlock(&dcache_lock);
1820 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1821 * @proc_dentry: directoy to prune.
1823 * Shrink the /proc directory that was used by the just killed thread.
1826 void proc_pid_flush(struct dentry *proc_dentry)
1829 if(proc_dentry != NULL) {
1830 shrink_dcache_parent(proc_dentry);
1836 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1838 struct task_struct *task;
1839 struct inode *inode;
1840 struct proc_inode *ei;
1844 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1845 inode = new_inode(dir->i_sb);
1847 return ERR_PTR(-ENOMEM);
1849 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1850 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1852 inode->i_mode = S_IFLNK|S_IRWXUGO;
1853 inode->i_uid = inode->i_gid = 0;
1855 inode->i_op = &proc_self_inode_operations;
1856 d_add(dentry, inode);
1859 tgid = name_to_int(dentry);
1863 read_lock(&tasklist_lock);
1864 task = find_task_by_pid(tgid);
1866 get_task_struct(task);
1867 read_unlock(&tasklist_lock);
1871 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1875 put_task_struct(task);
1878 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1879 inode->i_op = &proc_tgid_base_inode_operations;
1880 inode->i_fop = &proc_tgid_base_operations;
1881 inode->i_flags|=S_IMMUTABLE;
1882 #ifdef CONFIG_SECURITY
1888 dentry->d_op = &pid_base_dentry_operations;
1891 d_add(dentry, inode);
1892 spin_lock(&task->proc_lock);
1893 task->proc_dentry = dentry;
1894 if (!pid_alive(task)) {
1895 dentry = proc_pid_unhash(task);
1898 spin_unlock(&task->proc_lock);
1900 put_task_struct(task);
1902 proc_pid_flush(dentry);
1907 return ERR_PTR(-ENOENT);
1911 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1913 struct task_struct *task;
1914 struct task_struct *leader = proc_task(dir);
1915 struct inode *inode;
1918 tid = name_to_int(dentry);
1922 read_lock(&tasklist_lock);
1923 task = find_task_by_pid(tid);
1925 get_task_struct(task);
1926 read_unlock(&tasklist_lock);
1929 if (leader->tgid != task->tgid)
1932 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1937 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1938 inode->i_op = &proc_tid_base_inode_operations;
1939 inode->i_fop = &proc_tid_base_operations;
1940 inode->i_flags|=S_IMMUTABLE;
1941 #ifdef CONFIG_SECURITY
1947 dentry->d_op = &pid_base_dentry_operations;
1949 d_add(dentry, inode);
1951 put_task_struct(task);
1954 put_task_struct(task);
1956 return ERR_PTR(-ENOENT);
1959 #define PROC_NUMBUF 10
1960 #define PROC_MAXPIDS 20
1963 * Get a few tgid's to return for filldir - we need to hold the
1964 * tasklist lock while doing this, and we must release it before
1965 * we actually do the filldir itself, so we use a temp buffer..
1967 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1969 struct task_struct *p;
1973 read_lock(&tasklist_lock);
1976 p = find_task_by_pid(version);
1977 if (p && !thread_group_leader(p))
1984 p = next_task(&init_task);
1986 for ( ; p != &init_task; p = next_task(p)) {
1992 tgids[nr_tgids] = tgid;
1994 if (nr_tgids >= PROC_MAXPIDS)
1997 read_unlock(&tasklist_lock);
2002 * Get a few tid's to return for filldir - we need to hold the
2003 * tasklist lock while doing this, and we must release it before
2004 * we actually do the filldir itself, so we use a temp buffer..
2006 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2008 struct task_struct *leader_task = proc_task(dir);
2009 struct task_struct *task = leader_task;
2013 read_lock(&tasklist_lock);
2015 * The starting point task (leader_task) might be an already
2016 * unlinked task, which cannot be used to access the task-list
2017 * via next_thread().
2019 if (pid_alive(task)) do {
2020 int tid = task->pid;
2025 tids[nr_tids] = tid;
2027 if (nr_tids >= PROC_MAXPIDS)
2029 } while ((task = next_thread(task)) != leader_task);
2030 read_unlock(&tasklist_lock);
2034 /* for the /proc/ directory itself, after non-process stuff has been done */
2035 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2037 unsigned int tgid_array[PROC_MAXPIDS];
2038 char buf[PROC_NUMBUF];
2039 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2040 unsigned int nr_tgids, i;
2044 ino_t ino = fake_ino(0,PROC_TGID_INO);
2045 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2051 /* f_version caches the tgid value that the last readdir call couldn't
2052 * return. lseek aka telldir automagically resets f_version to 0.
2054 next_tgid = filp->f_version;
2055 filp->f_version = 0;
2057 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2059 /* no more entries ! */
2064 /* do not use the last found pid, reserve it for next_tgid */
2065 if (nr_tgids == PROC_MAXPIDS) {
2067 next_tgid = tgid_array[nr_tgids];
2070 for (i=0;i<nr_tgids;i++) {
2071 int tgid = tgid_array[i];
2072 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2073 unsigned long j = PROC_NUMBUF;
2076 buf[--j] = '0' + (tgid % 10);
2077 while ((tgid /= 10) != 0);
2079 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2080 /* returning this tgid failed, save it as the first
2081 * pid for the next readir call */
2082 filp->f_version = tgid_array[i];
2093 /* for the /proc/TGID/task/ directories */
2094 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2096 unsigned int tid_array[PROC_MAXPIDS];
2097 char buf[PROC_NUMBUF];
2098 unsigned int nr_tids, i;
2099 struct dentry *dentry = filp->f_dentry;
2100 struct inode *inode = dentry->d_inode;
2101 int retval = -ENOENT;
2103 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2105 if (!pid_alive(proc_task(inode)))
2112 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2117 ino = parent_ino(dentry);
2118 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2124 nr_tids = get_tid_list(pos, tid_array, inode);
2125 inode->i_nlink = pos + nr_tids;
2127 for (i = 0; i < nr_tids; i++) {
2128 unsigned long j = PROC_NUMBUF;
2129 int tid = tid_array[i];
2131 ino = fake_ino(tid,PROC_TID_INO);
2134 buf[--j] = '0' + (tid % 10);
2135 while ((tid /= 10) != 0);
2137 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)