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/kallsyms.h>
66 #include <linux/mount.h>
67 #include <linux/security.h>
68 #include <linux/ptrace.h>
69 #include <linux/seccomp.h>
70 #include <linux/cpuset.h>
71 #include <linux/audit.h>
75 * For hysterical raisins we keep the same inumbers as in the old procfs.
76 * Feel free to change the macro below - just keep the range distinct from
77 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
78 * As soon as we'll get a separate superblock we will be able to forget
79 * about magical ranges too.
82 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
84 enum pid_directory_inos {
106 #ifdef CONFIG_SCHEDSTATS
109 #ifdef CONFIG_CPUSETS
112 #ifdef CONFIG_SECURITY
114 PROC_TGID_ATTR_CURRENT,
117 PROC_TGID_ATTR_FSCREATE,
119 #ifdef CONFIG_AUDITSYSCALL
123 PROC_TGID_OOM_ADJUST,
127 #ifdef CONFIG_SECCOMP
144 #ifdef CONFIG_SCHEDSTATS
147 #ifdef CONFIG_CPUSETS
150 #ifdef CONFIG_SECURITY
152 PROC_TID_ATTR_CURRENT,
155 PROC_TID_ATTR_FSCREATE,
157 #ifdef CONFIG_AUDITSYSCALL
163 /* Add new entries before this */
164 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
174 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
176 static struct pid_entry tgid_base_stuff[] = {
177 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
178 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
179 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
180 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
181 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
182 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
183 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
184 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
185 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
187 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
189 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
190 #ifdef CONFIG_SECCOMP
191 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
193 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
194 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
195 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
196 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
197 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
198 #ifdef CONFIG_SECURITY
199 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
201 #ifdef CONFIG_KALLSYMS
202 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
204 #ifdef CONFIG_SCHEDSTATS
205 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
207 #ifdef CONFIG_CPUSETS
208 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
210 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
211 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
212 #ifdef CONFIG_AUDITSYSCALL
213 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
217 static struct pid_entry tid_base_stuff[] = {
218 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
219 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
220 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
221 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
222 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
223 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
224 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
225 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
227 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
229 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
230 #ifdef CONFIG_SECCOMP
231 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
233 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
234 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
235 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
236 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
237 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
238 #ifdef CONFIG_SECURITY
239 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
241 #ifdef CONFIG_KALLSYMS
242 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
244 #ifdef CONFIG_SCHEDSTATS
245 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
247 #ifdef CONFIG_CPUSETS
248 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
250 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
251 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
252 #ifdef CONFIG_AUDITSYSCALL
253 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
258 #ifdef CONFIG_SECURITY
259 static struct pid_entry tgid_attr_stuff[] = {
260 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
261 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
262 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
263 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
266 static struct pid_entry tid_attr_stuff[] = {
267 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
268 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
269 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
270 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
277 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
279 struct task_struct *task = proc_task(inode);
280 struct files_struct *files;
282 int fd = proc_type(inode) - PROC_TID_FD_DIR;
284 files = get_files_struct(task);
286 spin_lock(&files->file_lock);
287 file = fcheck_files(files, fd);
289 *mnt = mntget(file->f_vfsmnt);
290 *dentry = dget(file->f_dentry);
291 spin_unlock(&files->file_lock);
292 put_files_struct(files);
295 spin_unlock(&files->file_lock);
296 put_files_struct(files);
301 static struct fs_struct *get_fs_struct(struct task_struct *task)
303 struct fs_struct *fs;
307 atomic_inc(&fs->count);
312 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
314 struct fs_struct *fs = get_fs_struct(proc_task(inode));
315 int result = -ENOENT;
317 read_lock(&fs->lock);
318 *mnt = mntget(fs->pwdmnt);
319 *dentry = dget(fs->pwd);
320 read_unlock(&fs->lock);
327 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
329 struct fs_struct *fs = get_fs_struct(proc_task(inode));
330 int result = -ENOENT;
332 read_lock(&fs->lock);
333 *mnt = mntget(fs->rootmnt);
334 *dentry = dget(fs->root);
335 read_unlock(&fs->lock);
342 #define MAY_PTRACE(task) \
343 (task == current || \
344 (task->parent == current && \
345 (task->ptrace & PT_PTRACED) && \
346 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
347 security_ptrace(current,task) == 0))
349 static int proc_pid_environ(struct task_struct *task, char * buffer)
352 struct mm_struct *mm = get_task_mm(task);
354 unsigned int len = mm->env_end - mm->env_start;
357 res = access_process_vm(task, mm->env_start, buffer, len, 0);
358 if (!ptrace_may_attach(task))
365 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
369 struct mm_struct *mm = get_task_mm(task);
373 goto out_mm; /* Shh! No looking before we're done */
375 len = mm->arg_end - mm->arg_start;
380 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
382 // If the nul at the end of args has been overwritten, then
383 // assume application is using setproctitle(3).
384 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
385 len = strnlen(buffer, res);
389 len = mm->env_end - mm->env_start;
390 if (len > PAGE_SIZE - res)
391 len = PAGE_SIZE - res;
392 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
393 res = strnlen(buffer, res);
402 static int proc_pid_auxv(struct task_struct *task, char *buffer)
405 struct mm_struct *mm = get_task_mm(task);
407 unsigned int nwords = 0;
410 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
411 res = nwords * sizeof(mm->saved_auxv[0]);
414 memcpy(buffer, mm->saved_auxv, res);
421 #ifdef CONFIG_KALLSYMS
423 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
424 * Returns the resolved symbol. If that fails, simply return the address.
426 static int proc_pid_wchan(struct task_struct *task, char *buffer)
429 const char *sym_name;
430 unsigned long wchan, size, offset;
431 char namebuf[KSYM_NAME_LEN+1];
433 wchan = get_wchan(task);
435 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
437 return sprintf(buffer, "%s", sym_name);
438 return sprintf(buffer, "%lu", wchan);
440 #endif /* CONFIG_KALLSYMS */
442 #ifdef CONFIG_SCHEDSTATS
444 * Provides /proc/PID/schedstat
446 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
448 return sprintf(buffer, "%lu %lu %lu\n",
449 task->sched_info.cpu_time,
450 task->sched_info.run_delay,
451 task->sched_info.pcnt);
455 /* The badness from the OOM killer */
456 unsigned long badness(struct task_struct *p, unsigned long uptime);
457 static int proc_oom_score(struct task_struct *task, char *buffer)
459 unsigned long points;
460 struct timespec uptime;
462 do_posix_clock_monotonic_gettime(&uptime);
463 points = badness(task, uptime.tv_sec);
464 return sprintf(buffer, "%lu\n", points);
467 /************************************************************************/
468 /* Here the fs part begins */
469 /************************************************************************/
471 /* permission checks */
473 static int proc_check_root(struct inode *inode)
475 struct dentry *de, *base, *root;
476 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
479 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
481 read_lock(¤t->fs->lock);
482 our_vfsmnt = mntget(current->fs->rootmnt);
483 base = dget(current->fs->root);
484 read_unlock(¤t->fs->lock);
486 spin_lock(&vfsmount_lock);
490 while (vfsmnt != our_vfsmnt) {
491 if (vfsmnt == vfsmnt->mnt_parent)
493 de = vfsmnt->mnt_mountpoint;
494 vfsmnt = vfsmnt->mnt_parent;
497 if (!is_subdir(de, base))
499 spin_unlock(&vfsmount_lock);
508 spin_unlock(&vfsmount_lock);
513 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
515 if (generic_permission(inode, mask, NULL) != 0)
517 return proc_check_root(inode);
520 extern struct seq_operations proc_pid_maps_op;
521 static int maps_open(struct inode *inode, struct file *file)
523 struct task_struct *task = proc_task(inode);
524 int ret = seq_open(file, &proc_pid_maps_op);
526 struct seq_file *m = file->private_data;
532 static struct file_operations proc_maps_operations = {
536 .release = seq_release,
540 extern struct seq_operations proc_pid_numa_maps_op;
541 static int numa_maps_open(struct inode *inode, struct file *file)
543 struct task_struct *task = proc_task(inode);
544 int ret = seq_open(file, &proc_pid_numa_maps_op);
546 struct seq_file *m = file->private_data;
552 static struct file_operations proc_numa_maps_operations = {
553 .open = numa_maps_open,
556 .release = seq_release,
560 extern struct seq_operations proc_pid_smaps_op;
561 static int smaps_open(struct inode *inode, struct file *file)
563 struct task_struct *task = proc_task(inode);
564 int ret = seq_open(file, &proc_pid_smaps_op);
566 struct seq_file *m = file->private_data;
572 static struct file_operations proc_smaps_operations = {
576 .release = seq_release,
579 extern struct seq_operations mounts_op;
580 static int mounts_open(struct inode *inode, struct file *file)
582 struct task_struct *task = proc_task(inode);
583 int ret = seq_open(file, &mounts_op);
586 struct seq_file *m = file->private_data;
587 struct namespace *namespace;
589 namespace = task->namespace;
591 get_namespace(namespace);
595 m->private = namespace;
597 seq_release(inode, file);
604 static int mounts_release(struct inode *inode, struct file *file)
606 struct seq_file *m = file->private_data;
607 struct namespace *namespace = m->private;
608 put_namespace(namespace);
609 return seq_release(inode, file);
612 static struct file_operations proc_mounts_operations = {
616 .release = mounts_release,
619 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
621 static ssize_t proc_info_read(struct file * file, char __user * buf,
622 size_t count, loff_t *ppos)
624 struct inode * inode = file->f_dentry->d_inode;
627 struct task_struct *task = proc_task(inode);
629 if (count > PROC_BLOCK_SIZE)
630 count = PROC_BLOCK_SIZE;
631 if (!(page = __get_free_page(GFP_KERNEL)))
634 length = PROC_I(inode)->op.proc_read(task, (char*)page);
637 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
642 static struct file_operations proc_info_file_operations = {
643 .read = proc_info_read,
646 static int mem_open(struct inode* inode, struct file* file)
648 file->private_data = (void*)((long)current->self_exec_id);
652 static ssize_t mem_read(struct file * file, char __user * buf,
653 size_t count, loff_t *ppos)
655 struct task_struct *task = proc_task(file->f_dentry->d_inode);
657 unsigned long src = *ppos;
659 struct mm_struct *mm;
661 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
665 page = (char *)__get_free_page(GFP_USER);
671 mm = get_task_mm(task);
677 if (file->private_data != (void*)((long)current->self_exec_id))
683 int this_len, retval;
685 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
686 retval = access_process_vm(task, src, page, this_len, 0);
687 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
693 if (copy_to_user(buf, page, retval)) {
708 free_page((unsigned long) page);
713 #define mem_write NULL
716 /* This is a security hazard */
717 static ssize_t mem_write(struct file * file, const char * buf,
718 size_t count, loff_t *ppos)
722 struct task_struct *task = proc_task(file->f_dentry->d_inode);
723 unsigned long dst = *ppos;
725 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
728 page = (char *)__get_free_page(GFP_USER);
733 int this_len, retval;
735 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
736 if (copy_from_user(page, buf, this_len)) {
740 retval = access_process_vm(task, dst, page, this_len, 1);
752 free_page((unsigned long) page);
757 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
761 file->f_pos = offset;
764 file->f_pos += offset;
769 force_successful_syscall_return();
773 static struct file_operations proc_mem_operations = {
780 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
783 struct task_struct *task = proc_task(file->f_dentry->d_inode);
786 int oom_adjust = task->oomkilladj;
787 loff_t __ppos = *ppos;
789 len = sprintf(buffer, "%i\n", oom_adjust);
792 if (count > len-__ppos)
794 if (copy_to_user(buf, buffer + __ppos, count))
796 *ppos = __ppos + count;
800 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
801 size_t count, loff_t *ppos)
803 struct task_struct *task = proc_task(file->f_dentry->d_inode);
804 char buffer[8], *end;
807 if (!capable(CAP_SYS_RESOURCE))
809 memset(buffer, 0, 8);
812 if (copy_from_user(buffer, buf, count))
814 oom_adjust = simple_strtol(buffer, &end, 0);
815 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
819 task->oomkilladj = oom_adjust;
820 if (end - buffer == 0)
825 static struct file_operations proc_oom_adjust_operations = {
826 .read = oom_adjust_read,
827 .write = oom_adjust_write,
830 static struct inode_operations proc_mem_inode_operations = {
831 .permission = proc_permission,
834 #ifdef CONFIG_AUDITSYSCALL
836 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
837 size_t count, loff_t *ppos)
839 struct inode * inode = file->f_dentry->d_inode;
840 struct task_struct *task = proc_task(inode);
842 char tmpbuf[TMPBUFLEN];
844 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
845 audit_get_loginuid(task->audit_context));
846 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
849 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
850 size_t count, loff_t *ppos)
852 struct inode * inode = file->f_dentry->d_inode;
855 struct task_struct *task = proc_task(inode);
858 if (!capable(CAP_AUDIT_CONTROL))
864 if (count > PAGE_SIZE)
868 /* No partial writes. */
871 page = (char*)__get_free_page(GFP_USER);
875 if (copy_from_user(page, buf, count))
878 loginuid = simple_strtoul(page, &tmp, 10);
884 length = audit_set_loginuid(task, loginuid);
885 if (likely(length == 0))
889 free_page((unsigned long) page);
893 static struct file_operations proc_loginuid_operations = {
894 .read = proc_loginuid_read,
895 .write = proc_loginuid_write,
899 #ifdef CONFIG_SECCOMP
900 static ssize_t seccomp_read(struct file *file, char __user *buf,
901 size_t count, loff_t *ppos)
903 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
905 loff_t __ppos = *ppos;
908 /* no need to print the trailing zero, so use only len */
909 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
912 if (count > len - __ppos)
913 count = len - __ppos;
914 if (copy_to_user(buf, __buf + __ppos, count))
916 *ppos = __ppos + count;
920 static ssize_t seccomp_write(struct file *file, const char __user *buf,
921 size_t count, loff_t *ppos)
923 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
924 char __buf[20], *end;
925 unsigned int seccomp_mode;
927 /* can set it only once to be even more secure */
928 if (unlikely(tsk->seccomp.mode))
931 memset(__buf, 0, sizeof(__buf));
932 count = min(count, sizeof(__buf) - 1);
933 if (copy_from_user(__buf, buf, count))
935 seccomp_mode = simple_strtoul(__buf, &end, 0);
938 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
939 tsk->seccomp.mode = seccomp_mode;
940 set_tsk_thread_flag(tsk, TIF_SECCOMP);
943 if (unlikely(!(end - __buf)))
948 static struct file_operations proc_seccomp_operations = {
949 .read = seccomp_read,
950 .write = seccomp_write,
952 #endif /* CONFIG_SECCOMP */
954 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
956 struct inode *inode = dentry->d_inode;
959 /* We don't need a base pointer in the /proc filesystem */
962 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
964 error = proc_check_root(inode);
968 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
969 nd->last_type = LAST_BIND;
971 return ERR_PTR(error);
974 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
975 char __user *buffer, int buflen)
977 struct inode * inode;
978 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
984 inode = dentry->d_inode;
985 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
989 len = tmp + PAGE_SIZE - 1 - path;
993 if (copy_to_user(buffer, path, len))
996 free_page((unsigned long)tmp);
1000 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1002 int error = -EACCES;
1003 struct inode *inode = dentry->d_inode;
1005 struct vfsmount *mnt = NULL;
1009 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1011 error = proc_check_root(inode);
1015 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1019 error = do_proc_readlink(de, mnt, buffer, buflen);
1027 static struct inode_operations proc_pid_link_inode_operations = {
1028 .readlink = proc_pid_readlink,
1029 .follow_link = proc_pid_follow_link
1034 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1036 struct inode *inode = filp->f_dentry->d_inode;
1037 struct task_struct *p = proc_task(inode);
1038 unsigned int fd, tid, ino;
1041 struct files_struct * files;
1042 struct fdtable *fdt;
1053 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1057 ino = fake_ino(tid, PROC_TID_INO);
1058 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1062 files = get_files_struct(p);
1065 spin_lock(&files->file_lock);
1066 fdt = files_fdtable(files);
1067 for (fd = filp->f_pos-2;
1069 fd++, filp->f_pos++) {
1072 if (!fcheck_files(files, fd))
1074 spin_unlock(&files->file_lock);
1080 buf[j] = '0' + (i % 10);
1084 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1085 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1086 spin_lock(&files->file_lock);
1089 spin_lock(&files->file_lock);
1091 spin_unlock(&files->file_lock);
1092 put_files_struct(files);
1098 static int proc_pident_readdir(struct file *filp,
1099 void *dirent, filldir_t filldir,
1100 struct pid_entry *ents, unsigned int nents)
1104 struct dentry *dentry = filp->f_dentry;
1105 struct inode *inode = dentry->d_inode;
1106 struct pid_entry *p;
1111 if (!pid_alive(proc_task(inode)))
1115 pid = proc_task(inode)->pid;
1120 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1126 ino = parent_ino(dentry);
1127 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1140 if (filldir(dirent, p->name, p->len, filp->f_pos,
1141 fake_ino(pid, p->type), p->mode >> 12) < 0)
1153 static int proc_tgid_base_readdir(struct file * filp,
1154 void * dirent, filldir_t filldir)
1156 return proc_pident_readdir(filp,dirent,filldir,
1157 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1160 static int proc_tid_base_readdir(struct file * filp,
1161 void * dirent, filldir_t filldir)
1163 return proc_pident_readdir(filp,dirent,filldir,
1164 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1167 /* building an inode */
1169 static int task_dumpable(struct task_struct *task)
1172 struct mm_struct *mm;
1177 dumpable = mm->dumpable;
1185 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1187 struct inode * inode;
1188 struct proc_inode *ei;
1190 /* We need a new inode */
1192 inode = new_inode(sb);
1199 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1200 inode->i_ino = fake_ino(task->pid, ino);
1202 if (!pid_alive(task))
1206 * grab the reference to task.
1208 get_task_struct(task);
1213 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1214 inode->i_uid = task->euid;
1215 inode->i_gid = task->egid;
1217 security_task_to_inode(task, inode);
1231 * Exceptional case: normally we are not allowed to unhash a busy
1232 * directory. In this case, however, we can do it - no aliasing problems
1233 * due to the way we treat inodes.
1235 * Rewrite the inode's ownerships here because the owning task may have
1236 * performed a setuid(), etc.
1238 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1240 struct inode *inode = dentry->d_inode;
1241 struct task_struct *task = proc_task(inode);
1242 if (pid_alive(task)) {
1243 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1244 inode->i_uid = task->euid;
1245 inode->i_gid = task->egid;
1250 security_task_to_inode(task, inode);
1257 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1259 struct inode *inode = dentry->d_inode;
1260 struct task_struct *task = proc_task(inode);
1261 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1262 struct files_struct *files;
1264 files = get_files_struct(task);
1266 spin_lock(&files->file_lock);
1267 if (fcheck_files(files, fd)) {
1268 spin_unlock(&files->file_lock);
1269 put_files_struct(files);
1270 if (task_dumpable(task)) {
1271 inode->i_uid = task->euid;
1272 inode->i_gid = task->egid;
1277 security_task_to_inode(task, inode);
1280 spin_unlock(&files->file_lock);
1281 put_files_struct(files);
1287 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1289 struct task_struct *task = proc_task(inode);
1290 spin_lock(&task->proc_lock);
1291 if (task->proc_dentry == dentry)
1292 task->proc_dentry = NULL;
1293 spin_unlock(&task->proc_lock);
1297 static int pid_delete_dentry(struct dentry * dentry)
1299 /* Is the task we represent dead?
1300 * If so, then don't put the dentry on the lru list,
1301 * kill it immediately.
1303 return !pid_alive(proc_task(dentry->d_inode));
1306 static struct dentry_operations tid_fd_dentry_operations =
1308 .d_revalidate = tid_fd_revalidate,
1309 .d_delete = pid_delete_dentry,
1312 static struct dentry_operations pid_dentry_operations =
1314 .d_revalidate = pid_revalidate,
1315 .d_delete = pid_delete_dentry,
1318 static struct dentry_operations pid_base_dentry_operations =
1320 .d_revalidate = pid_revalidate,
1321 .d_iput = pid_base_iput,
1322 .d_delete = pid_delete_dentry,
1327 static unsigned name_to_int(struct dentry *dentry)
1329 const char *name = dentry->d_name.name;
1330 int len = dentry->d_name.len;
1333 if (len > 1 && *name == '0')
1336 unsigned c = *name++ - '0';
1339 if (n >= (~0U-9)/10)
1350 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1352 struct task_struct *task = proc_task(dir);
1353 unsigned fd = name_to_int(dentry);
1355 struct files_struct * files;
1356 struct inode *inode;
1357 struct proc_inode *ei;
1361 if (!pid_alive(task))
1364 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1368 files = get_files_struct(task);
1371 inode->i_mode = S_IFLNK;
1372 spin_lock(&files->file_lock);
1373 file = fcheck_files(files, fd);
1376 if (file->f_mode & 1)
1377 inode->i_mode |= S_IRUSR | S_IXUSR;
1378 if (file->f_mode & 2)
1379 inode->i_mode |= S_IWUSR | S_IXUSR;
1380 spin_unlock(&files->file_lock);
1381 put_files_struct(files);
1382 inode->i_op = &proc_pid_link_inode_operations;
1384 ei->op.proc_get_link = proc_fd_link;
1385 dentry->d_op = &tid_fd_dentry_operations;
1386 d_add(dentry, inode);
1390 spin_unlock(&files->file_lock);
1391 put_files_struct(files);
1395 return ERR_PTR(-ENOENT);
1398 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1399 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1401 static struct file_operations proc_fd_operations = {
1402 .read = generic_read_dir,
1403 .readdir = proc_readfd,
1406 static struct file_operations proc_task_operations = {
1407 .read = generic_read_dir,
1408 .readdir = proc_task_readdir,
1412 * proc directories can do almost nothing..
1414 static struct inode_operations proc_fd_inode_operations = {
1415 .lookup = proc_lookupfd,
1416 .permission = proc_permission,
1419 static struct inode_operations proc_task_inode_operations = {
1420 .lookup = proc_task_lookup,
1421 .permission = proc_permission,
1424 #ifdef CONFIG_SECURITY
1425 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1426 size_t count, loff_t *ppos)
1428 struct inode * inode = file->f_dentry->d_inode;
1431 struct task_struct *task = proc_task(inode);
1433 if (count > PAGE_SIZE)
1435 if (!(page = __get_free_page(GFP_KERNEL)))
1438 length = security_getprocattr(task,
1439 (char*)file->f_dentry->d_name.name,
1440 (void*)page, count);
1442 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1447 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1448 size_t count, loff_t *ppos)
1450 struct inode * inode = file->f_dentry->d_inode;
1453 struct task_struct *task = proc_task(inode);
1455 if (count > PAGE_SIZE)
1458 /* No partial writes. */
1461 page = (char*)__get_free_page(GFP_USER);
1465 if (copy_from_user(page, buf, count))
1468 length = security_setprocattr(task,
1469 (char*)file->f_dentry->d_name.name,
1470 (void*)page, count);
1472 free_page((unsigned long) page);
1476 static struct file_operations proc_pid_attr_operations = {
1477 .read = proc_pid_attr_read,
1478 .write = proc_pid_attr_write,
1481 static struct file_operations proc_tid_attr_operations;
1482 static struct inode_operations proc_tid_attr_inode_operations;
1483 static struct file_operations proc_tgid_attr_operations;
1484 static struct inode_operations proc_tgid_attr_inode_operations;
1487 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1490 static struct dentry *proc_pident_lookup(struct inode *dir,
1491 struct dentry *dentry,
1492 struct pid_entry *ents)
1494 struct inode *inode;
1496 struct task_struct *task = proc_task(dir);
1497 struct pid_entry *p;
1498 struct proc_inode *ei;
1503 if (!pid_alive(task))
1506 for (p = ents; p->name; p++) {
1507 if (p->len != dentry->d_name.len)
1509 if (!memcmp(dentry->d_name.name, p->name, p->len))
1516 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1521 inode->i_mode = p->mode;
1523 * Yes, it does not scale. And it should not. Don't add
1524 * new entries into /proc/<tgid>/ without very good reasons.
1527 case PROC_TGID_TASK:
1528 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1529 inode->i_op = &proc_task_inode_operations;
1530 inode->i_fop = &proc_task_operations;
1535 inode->i_op = &proc_fd_inode_operations;
1536 inode->i_fop = &proc_fd_operations;
1540 inode->i_op = &proc_pid_link_inode_operations;
1541 ei->op.proc_get_link = proc_exe_link;
1545 inode->i_op = &proc_pid_link_inode_operations;
1546 ei->op.proc_get_link = proc_cwd_link;
1549 case PROC_TGID_ROOT:
1550 inode->i_op = &proc_pid_link_inode_operations;
1551 ei->op.proc_get_link = proc_root_link;
1553 case PROC_TID_ENVIRON:
1554 case PROC_TGID_ENVIRON:
1555 inode->i_fop = &proc_info_file_operations;
1556 ei->op.proc_read = proc_pid_environ;
1559 case PROC_TGID_AUXV:
1560 inode->i_fop = &proc_info_file_operations;
1561 ei->op.proc_read = proc_pid_auxv;
1563 case PROC_TID_STATUS:
1564 case PROC_TGID_STATUS:
1565 inode->i_fop = &proc_info_file_operations;
1566 ei->op.proc_read = proc_pid_status;
1569 inode->i_fop = &proc_info_file_operations;
1570 ei->op.proc_read = proc_tid_stat;
1572 case PROC_TGID_STAT:
1573 inode->i_fop = &proc_info_file_operations;
1574 ei->op.proc_read = proc_tgid_stat;
1576 case PROC_TID_CMDLINE:
1577 case PROC_TGID_CMDLINE:
1578 inode->i_fop = &proc_info_file_operations;
1579 ei->op.proc_read = proc_pid_cmdline;
1581 case PROC_TID_STATM:
1582 case PROC_TGID_STATM:
1583 inode->i_fop = &proc_info_file_operations;
1584 ei->op.proc_read = proc_pid_statm;
1587 case PROC_TGID_MAPS:
1588 inode->i_fop = &proc_maps_operations;
1591 case PROC_TID_NUMA_MAPS:
1592 case PROC_TGID_NUMA_MAPS:
1593 inode->i_fop = &proc_numa_maps_operations;
1598 inode->i_op = &proc_mem_inode_operations;
1599 inode->i_fop = &proc_mem_operations;
1601 #ifdef CONFIG_SECCOMP
1602 case PROC_TID_SECCOMP:
1603 case PROC_TGID_SECCOMP:
1604 inode->i_fop = &proc_seccomp_operations;
1606 #endif /* CONFIG_SECCOMP */
1607 case PROC_TID_MOUNTS:
1608 case PROC_TGID_MOUNTS:
1609 inode->i_fop = &proc_mounts_operations;
1611 case PROC_TID_SMAPS:
1612 case PROC_TGID_SMAPS:
1613 inode->i_fop = &proc_smaps_operations;
1615 #ifdef CONFIG_SECURITY
1618 inode->i_op = &proc_tid_attr_inode_operations;
1619 inode->i_fop = &proc_tid_attr_operations;
1621 case PROC_TGID_ATTR:
1623 inode->i_op = &proc_tgid_attr_inode_operations;
1624 inode->i_fop = &proc_tgid_attr_operations;
1626 case PROC_TID_ATTR_CURRENT:
1627 case PROC_TGID_ATTR_CURRENT:
1628 case PROC_TID_ATTR_PREV:
1629 case PROC_TGID_ATTR_PREV:
1630 case PROC_TID_ATTR_EXEC:
1631 case PROC_TGID_ATTR_EXEC:
1632 case PROC_TID_ATTR_FSCREATE:
1633 case PROC_TGID_ATTR_FSCREATE:
1634 inode->i_fop = &proc_pid_attr_operations;
1637 #ifdef CONFIG_KALLSYMS
1638 case PROC_TID_WCHAN:
1639 case PROC_TGID_WCHAN:
1640 inode->i_fop = &proc_info_file_operations;
1641 ei->op.proc_read = proc_pid_wchan;
1644 #ifdef CONFIG_SCHEDSTATS
1645 case PROC_TID_SCHEDSTAT:
1646 case PROC_TGID_SCHEDSTAT:
1647 inode->i_fop = &proc_info_file_operations;
1648 ei->op.proc_read = proc_pid_schedstat;
1651 #ifdef CONFIG_CPUSETS
1652 case PROC_TID_CPUSET:
1653 case PROC_TGID_CPUSET:
1654 inode->i_fop = &proc_cpuset_operations;
1657 case PROC_TID_OOM_SCORE:
1658 case PROC_TGID_OOM_SCORE:
1659 inode->i_fop = &proc_info_file_operations;
1660 ei->op.proc_read = proc_oom_score;
1662 case PROC_TID_OOM_ADJUST:
1663 case PROC_TGID_OOM_ADJUST:
1664 inode->i_fop = &proc_oom_adjust_operations;
1666 #ifdef CONFIG_AUDITSYSCALL
1667 case PROC_TID_LOGINUID:
1668 case PROC_TGID_LOGINUID:
1669 inode->i_fop = &proc_loginuid_operations;
1673 printk("procfs: impossible type (%d)",p->type);
1675 return ERR_PTR(-EINVAL);
1677 dentry->d_op = &pid_dentry_operations;
1678 d_add(dentry, inode);
1682 return ERR_PTR(error);
1685 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1686 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1689 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1690 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1693 static struct file_operations proc_tgid_base_operations = {
1694 .read = generic_read_dir,
1695 .readdir = proc_tgid_base_readdir,
1698 static struct file_operations proc_tid_base_operations = {
1699 .read = generic_read_dir,
1700 .readdir = proc_tid_base_readdir,
1703 static struct inode_operations proc_tgid_base_inode_operations = {
1704 .lookup = proc_tgid_base_lookup,
1707 static struct inode_operations proc_tid_base_inode_operations = {
1708 .lookup = proc_tid_base_lookup,
1711 #ifdef CONFIG_SECURITY
1712 static int proc_tgid_attr_readdir(struct file * filp,
1713 void * dirent, filldir_t filldir)
1715 return proc_pident_readdir(filp,dirent,filldir,
1716 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1719 static int proc_tid_attr_readdir(struct file * filp,
1720 void * dirent, filldir_t filldir)
1722 return proc_pident_readdir(filp,dirent,filldir,
1723 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1726 static struct file_operations proc_tgid_attr_operations = {
1727 .read = generic_read_dir,
1728 .readdir = proc_tgid_attr_readdir,
1731 static struct file_operations proc_tid_attr_operations = {
1732 .read = generic_read_dir,
1733 .readdir = proc_tid_attr_readdir,
1736 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1737 struct dentry *dentry, struct nameidata *nd)
1739 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1742 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1743 struct dentry *dentry, struct nameidata *nd)
1745 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1748 static struct inode_operations proc_tgid_attr_inode_operations = {
1749 .lookup = proc_tgid_attr_lookup,
1752 static struct inode_operations proc_tid_attr_inode_operations = {
1753 .lookup = proc_tid_attr_lookup,
1760 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1764 sprintf(tmp, "%d", current->tgid);
1765 return vfs_readlink(dentry,buffer,buflen,tmp);
1768 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1771 sprintf(tmp, "%d", current->tgid);
1772 return ERR_PTR(vfs_follow_link(nd,tmp));
1775 static struct inode_operations proc_self_inode_operations = {
1776 .readlink = proc_self_readlink,
1777 .follow_link = proc_self_follow_link,
1781 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1782 * @p: task that should be flushed.
1784 * Drops the /proc/@pid dcache entry from the hash chains.
1786 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1787 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1788 * if the pid value is immediately reused. This is enforced by
1789 * - caller must acquire spin_lock(p->proc_lock)
1790 * - must be called before detach_pid()
1791 * - proc_pid_lookup acquires proc_lock, and checks that
1792 * the target is not dead by looking at the attach count
1796 struct dentry *proc_pid_unhash(struct task_struct *p)
1798 struct dentry *proc_dentry;
1800 proc_dentry = p->proc_dentry;
1801 if (proc_dentry != NULL) {
1803 spin_lock(&dcache_lock);
1804 spin_lock(&proc_dentry->d_lock);
1805 if (!d_unhashed(proc_dentry)) {
1806 dget_locked(proc_dentry);
1807 __d_drop(proc_dentry);
1808 spin_unlock(&proc_dentry->d_lock);
1810 spin_unlock(&proc_dentry->d_lock);
1813 spin_unlock(&dcache_lock);
1819 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1820 * @proc_dentry: directoy to prune.
1822 * Shrink the /proc directory that was used by the just killed thread.
1825 void proc_pid_flush(struct dentry *proc_dentry)
1828 if(proc_dentry != NULL) {
1829 shrink_dcache_parent(proc_dentry);
1835 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1837 struct task_struct *task;
1838 struct inode *inode;
1839 struct proc_inode *ei;
1843 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1844 inode = new_inode(dir->i_sb);
1846 return ERR_PTR(-ENOMEM);
1848 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1849 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1851 inode->i_mode = S_IFLNK|S_IRWXUGO;
1852 inode->i_uid = inode->i_gid = 0;
1854 inode->i_op = &proc_self_inode_operations;
1855 d_add(dentry, inode);
1858 tgid = name_to_int(dentry);
1862 read_lock(&tasklist_lock);
1863 task = find_task_by_pid(tgid);
1865 get_task_struct(task);
1866 read_unlock(&tasklist_lock);
1870 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1874 put_task_struct(task);
1877 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1878 inode->i_op = &proc_tgid_base_inode_operations;
1879 inode->i_fop = &proc_tgid_base_operations;
1880 inode->i_flags|=S_IMMUTABLE;
1881 #ifdef CONFIG_SECURITY
1887 dentry->d_op = &pid_base_dentry_operations;
1890 d_add(dentry, inode);
1891 spin_lock(&task->proc_lock);
1892 task->proc_dentry = dentry;
1893 if (!pid_alive(task)) {
1894 dentry = proc_pid_unhash(task);
1897 spin_unlock(&task->proc_lock);
1899 put_task_struct(task);
1901 proc_pid_flush(dentry);
1906 return ERR_PTR(-ENOENT);
1910 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1912 struct task_struct *task;
1913 struct task_struct *leader = proc_task(dir);
1914 struct inode *inode;
1917 tid = name_to_int(dentry);
1921 read_lock(&tasklist_lock);
1922 task = find_task_by_pid(tid);
1924 get_task_struct(task);
1925 read_unlock(&tasklist_lock);
1928 if (leader->tgid != task->tgid)
1931 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1936 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1937 inode->i_op = &proc_tid_base_inode_operations;
1938 inode->i_fop = &proc_tid_base_operations;
1939 inode->i_flags|=S_IMMUTABLE;
1940 #ifdef CONFIG_SECURITY
1946 dentry->d_op = &pid_base_dentry_operations;
1948 d_add(dentry, inode);
1950 put_task_struct(task);
1953 put_task_struct(task);
1955 return ERR_PTR(-ENOENT);
1958 #define PROC_NUMBUF 10
1959 #define PROC_MAXPIDS 20
1962 * Get a few tgid's to return for filldir - we need to hold the
1963 * tasklist lock while doing this, and we must release it before
1964 * we actually do the filldir itself, so we use a temp buffer..
1966 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1968 struct task_struct *p;
1972 read_lock(&tasklist_lock);
1975 p = find_task_by_pid(version);
1976 if (p && !thread_group_leader(p))
1983 p = next_task(&init_task);
1985 for ( ; p != &init_task; p = next_task(p)) {
1991 tgids[nr_tgids] = tgid;
1993 if (nr_tgids >= PROC_MAXPIDS)
1996 read_unlock(&tasklist_lock);
2001 * Get a few tid's to return for filldir - we need to hold the
2002 * tasklist lock while doing this, and we must release it before
2003 * we actually do the filldir itself, so we use a temp buffer..
2005 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2007 struct task_struct *leader_task = proc_task(dir);
2008 struct task_struct *task = leader_task;
2012 read_lock(&tasklist_lock);
2014 * The starting point task (leader_task) might be an already
2015 * unlinked task, which cannot be used to access the task-list
2016 * via next_thread().
2018 if (pid_alive(task)) do {
2019 int tid = task->pid;
2024 tids[nr_tids] = tid;
2026 if (nr_tids >= PROC_MAXPIDS)
2028 } while ((task = next_thread(task)) != leader_task);
2029 read_unlock(&tasklist_lock);
2033 /* for the /proc/ directory itself, after non-process stuff has been done */
2034 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2036 unsigned int tgid_array[PROC_MAXPIDS];
2037 char buf[PROC_NUMBUF];
2038 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2039 unsigned int nr_tgids, i;
2043 ino_t ino = fake_ino(0,PROC_TGID_INO);
2044 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2050 /* f_version caches the tgid value that the last readdir call couldn't
2051 * return. lseek aka telldir automagically resets f_version to 0.
2053 next_tgid = filp->f_version;
2054 filp->f_version = 0;
2056 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2058 /* no more entries ! */
2063 /* do not use the last found pid, reserve it for next_tgid */
2064 if (nr_tgids == PROC_MAXPIDS) {
2066 next_tgid = tgid_array[nr_tgids];
2069 for (i=0;i<nr_tgids;i++) {
2070 int tgid = tgid_array[i];
2071 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2072 unsigned long j = PROC_NUMBUF;
2075 buf[--j] = '0' + (tgid % 10);
2076 while ((tgid /= 10) != 0);
2078 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2079 /* returning this tgid failed, save it as the first
2080 * pid for the next readir call */
2081 filp->f_version = tgid_array[i];
2092 /* for the /proc/TGID/task/ directories */
2093 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2095 unsigned int tid_array[PROC_MAXPIDS];
2096 char buf[PROC_NUMBUF];
2097 unsigned int nr_tids, i;
2098 struct dentry *dentry = filp->f_dentry;
2099 struct inode *inode = dentry->d_inode;
2100 int retval = -ENOENT;
2102 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2104 if (!pid_alive(proc_task(inode)))
2111 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2116 ino = parent_ino(dentry);
2117 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2123 nr_tids = get_tid_list(pos, tid_array, inode);
2124 inode->i_nlink = pos + nr_tids;
2126 for (i = 0; i < nr_tids; i++) {
2127 unsigned long j = PROC_NUMBUF;
2128 int tid = tid_array[i];
2130 ino = fake_ino(tid,PROC_TID_INO);
2133 buf[--j] = '0' + (tid % 10);
2134 while ((tid /= 10) != 0);
2136 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)