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.
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.
44 * Smaps information related to shared, private, clean and dirty pages.
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>
73 #include <linux/poll.h>
77 * For hysterical raisins we keep the same inumbers as in the old procfs.
78 * Feel free to change the macro below - just keep the range distinct from
79 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
80 * As soon as we'll get a separate superblock we will be able to forget
81 * about magical ranges too.
84 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
86 enum pid_directory_inos {
110 #ifdef CONFIG_SCHEDSTATS
113 #ifdef CONFIG_CPUSETS
116 #ifdef CONFIG_SECURITY
118 PROC_TGID_ATTR_CURRENT,
121 PROC_TGID_ATTR_FSCREATE,
123 #ifdef CONFIG_AUDITSYSCALL
127 PROC_TGID_OOM_ADJUST,
131 #ifdef CONFIG_SECCOMP
150 #ifdef CONFIG_SCHEDSTATS
153 #ifdef CONFIG_CPUSETS
156 #ifdef CONFIG_SECURITY
158 PROC_TID_ATTR_CURRENT,
161 PROC_TID_ATTR_FSCREATE,
163 #ifdef CONFIG_AUDITSYSCALL
169 /* Add new entries before this */
170 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
180 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
182 static struct pid_entry tgid_base_stuff[] = {
183 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
184 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
185 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
186 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
187 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
188 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
189 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
190 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
191 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
193 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
195 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
196 #ifdef CONFIG_SECCOMP
197 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
199 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
200 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
201 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
202 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
204 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
206 #ifdef CONFIG_SECURITY
207 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
209 #ifdef CONFIG_KALLSYMS
210 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
212 #ifdef CONFIG_SCHEDSTATS
213 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
215 #ifdef CONFIG_CPUSETS
216 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
218 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
219 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
220 #ifdef CONFIG_AUDITSYSCALL
221 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
225 static struct pid_entry tid_base_stuff[] = {
226 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
227 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
228 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
229 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
230 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
231 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
232 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
233 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
235 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
237 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
238 #ifdef CONFIG_SECCOMP
239 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
241 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
242 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
243 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
244 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
246 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
248 #ifdef CONFIG_SECURITY
249 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
251 #ifdef CONFIG_KALLSYMS
252 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
254 #ifdef CONFIG_SCHEDSTATS
255 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
257 #ifdef CONFIG_CPUSETS
258 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
260 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
261 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
262 #ifdef CONFIG_AUDITSYSCALL
263 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
268 #ifdef CONFIG_SECURITY
269 static struct pid_entry tgid_attr_stuff[] = {
270 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
271 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
272 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
273 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
276 static struct pid_entry tid_attr_stuff[] = {
277 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
278 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
279 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
280 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
287 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
289 struct task_struct *task = proc_task(inode);
290 struct files_struct *files;
292 int fd = proc_type(inode) - PROC_TID_FD_DIR;
294 files = get_files_struct(task);
297 file = fcheck_files(files, fd);
299 *mnt = mntget(file->f_vfsmnt);
300 *dentry = dget(file->f_dentry);
302 put_files_struct(files);
306 put_files_struct(files);
311 static struct fs_struct *get_fs_struct(struct task_struct *task)
313 struct fs_struct *fs;
317 atomic_inc(&fs->count);
322 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
324 struct fs_struct *fs = get_fs_struct(proc_task(inode));
325 int result = -ENOENT;
327 read_lock(&fs->lock);
328 *mnt = mntget(fs->pwdmnt);
329 *dentry = dget(fs->pwd);
330 read_unlock(&fs->lock);
337 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
339 struct fs_struct *fs = get_fs_struct(proc_task(inode));
340 int result = -ENOENT;
342 read_lock(&fs->lock);
343 *mnt = mntget(fs->rootmnt);
344 *dentry = dget(fs->root);
345 read_unlock(&fs->lock);
353 /* Same as proc_root_link, but this addionally tries to get fs from other
354 * threads in the group */
355 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
356 struct vfsmount **mnt)
358 struct fs_struct *fs;
359 int result = -ENOENT;
360 struct task_struct *leader = proc_task(inode);
365 atomic_inc(&fs->count);
368 /* Try to get fs from other threads */
370 read_lock(&tasklist_lock);
371 if (pid_alive(leader)) {
372 struct task_struct *task = leader;
374 while ((task = next_thread(task)) != leader) {
378 atomic_inc(&fs->count);
385 read_unlock(&tasklist_lock);
389 read_lock(&fs->lock);
390 *mnt = mntget(fs->rootmnt);
391 *dentry = dget(fs->root);
392 read_unlock(&fs->lock);
400 #define MAY_PTRACE(task) \
401 (task == current || \
402 (task->parent == current && \
403 (task->ptrace & PT_PTRACED) && \
404 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
405 security_ptrace(current,task) == 0))
407 static int proc_pid_environ(struct task_struct *task, char * buffer)
410 struct mm_struct *mm = get_task_mm(task);
412 unsigned int len = mm->env_end - mm->env_start;
415 res = access_process_vm(task, mm->env_start, buffer, len, 0);
416 if (!ptrace_may_attach(task))
423 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
427 struct mm_struct *mm = get_task_mm(task);
431 goto out_mm; /* Shh! No looking before we're done */
433 len = mm->arg_end - mm->arg_start;
438 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
440 // If the nul at the end of args has been overwritten, then
441 // assume application is using setproctitle(3).
442 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
443 len = strnlen(buffer, res);
447 len = mm->env_end - mm->env_start;
448 if (len > PAGE_SIZE - res)
449 len = PAGE_SIZE - res;
450 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
451 res = strnlen(buffer, res);
460 static int proc_pid_auxv(struct task_struct *task, char *buffer)
463 struct mm_struct *mm = get_task_mm(task);
465 unsigned int nwords = 0;
468 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
469 res = nwords * sizeof(mm->saved_auxv[0]);
472 memcpy(buffer, mm->saved_auxv, res);
479 #ifdef CONFIG_KALLSYMS
481 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
482 * Returns the resolved symbol. If that fails, simply return the address.
484 static int proc_pid_wchan(struct task_struct *task, char *buffer)
487 const char *sym_name;
488 unsigned long wchan, size, offset;
489 char namebuf[KSYM_NAME_LEN+1];
491 wchan = get_wchan(task);
493 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
495 return sprintf(buffer, "%s", sym_name);
496 return sprintf(buffer, "%lu", wchan);
498 #endif /* CONFIG_KALLSYMS */
500 #ifdef CONFIG_SCHEDSTATS
502 * Provides /proc/PID/schedstat
504 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
506 return sprintf(buffer, "%lu %lu %lu\n",
507 task->sched_info.cpu_time,
508 task->sched_info.run_delay,
509 task->sched_info.pcnt);
513 /* The badness from the OOM killer */
514 unsigned long badness(struct task_struct *p, unsigned long uptime);
515 static int proc_oom_score(struct task_struct *task, char *buffer)
517 unsigned long points;
518 struct timespec uptime;
520 do_posix_clock_monotonic_gettime(&uptime);
521 points = badness(task, uptime.tv_sec);
522 return sprintf(buffer, "%lu\n", points);
525 /************************************************************************/
526 /* Here the fs part begins */
527 /************************************************************************/
529 /* permission checks */
531 /* If the process being read is separated by chroot from the reading process,
532 * don't let the reader access the threads.
534 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
536 struct dentry *de, *base;
537 struct vfsmount *our_vfsmnt, *mnt;
539 read_lock(¤t->fs->lock);
540 our_vfsmnt = mntget(current->fs->rootmnt);
541 base = dget(current->fs->root);
542 read_unlock(¤t->fs->lock);
544 spin_lock(&vfsmount_lock);
548 while (vfsmnt != our_vfsmnt) {
549 if (vfsmnt == vfsmnt->mnt_parent)
551 de = vfsmnt->mnt_mountpoint;
552 vfsmnt = vfsmnt->mnt_parent;
555 if (!is_subdir(de, base))
557 spin_unlock(&vfsmount_lock);
566 spin_unlock(&vfsmount_lock);
571 static int proc_check_root(struct inode *inode)
574 struct vfsmount *vfsmnt;
576 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
578 return proc_check_chroot(root, vfsmnt);
581 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
583 if (generic_permission(inode, mask, NULL) != 0)
585 return proc_check_root(inode);
588 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
591 struct vfsmount *vfsmnt;
593 if (generic_permission(inode, mask, NULL) != 0)
596 if (proc_task_root_link(inode, &root, &vfsmnt))
599 return proc_check_chroot(root, vfsmnt);
602 extern struct seq_operations proc_pid_maps_op;
603 static int maps_open(struct inode *inode, struct file *file)
605 struct task_struct *task = proc_task(inode);
606 int ret = seq_open(file, &proc_pid_maps_op);
608 struct seq_file *m = file->private_data;
614 static struct file_operations proc_maps_operations = {
618 .release = seq_release,
622 extern struct seq_operations proc_pid_numa_maps_op;
623 static int numa_maps_open(struct inode *inode, struct file *file)
625 struct task_struct *task = proc_task(inode);
626 int ret = seq_open(file, &proc_pid_numa_maps_op);
628 struct seq_file *m = file->private_data;
634 static struct file_operations proc_numa_maps_operations = {
635 .open = numa_maps_open,
638 .release = seq_release,
643 extern struct seq_operations proc_pid_smaps_op;
644 static int smaps_open(struct inode *inode, struct file *file)
646 struct task_struct *task = proc_task(inode);
647 int ret = seq_open(file, &proc_pid_smaps_op);
649 struct seq_file *m = file->private_data;
655 static struct file_operations proc_smaps_operations = {
659 .release = seq_release,
663 extern struct seq_operations mounts_op;
669 static int mounts_open(struct inode *inode, struct file *file)
671 struct task_struct *task = proc_task(inode);
672 struct namespace *namespace;
673 struct proc_mounts *p;
677 namespace = task->namespace;
679 get_namespace(namespace);
684 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
686 file->private_data = &p->m;
687 ret = seq_open(file, &mounts_op);
689 p->m.private = namespace;
690 p->event = namespace->event;
695 put_namespace(namespace);
700 static int mounts_release(struct inode *inode, struct file *file)
702 struct seq_file *m = file->private_data;
703 struct namespace *namespace = m->private;
704 put_namespace(namespace);
705 return seq_release(inode, file);
708 static unsigned mounts_poll(struct file *file, poll_table *wait)
710 struct proc_mounts *p = file->private_data;
711 struct namespace *ns = p->m.private;
714 poll_wait(file, &ns->poll, wait);
716 spin_lock(&vfsmount_lock);
717 if (p->event != ns->event) {
718 p->event = ns->event;
721 spin_unlock(&vfsmount_lock);
726 static struct file_operations proc_mounts_operations = {
730 .release = mounts_release,
734 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
736 static ssize_t proc_info_read(struct file * file, char __user * buf,
737 size_t count, loff_t *ppos)
739 struct inode * inode = file->f_dentry->d_inode;
742 struct task_struct *task = proc_task(inode);
744 if (count > PROC_BLOCK_SIZE)
745 count = PROC_BLOCK_SIZE;
746 if (!(page = __get_free_page(GFP_KERNEL)))
749 length = PROC_I(inode)->op.proc_read(task, (char*)page);
752 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
757 static struct file_operations proc_info_file_operations = {
758 .read = proc_info_read,
761 static int mem_open(struct inode* inode, struct file* file)
763 file->private_data = (void*)((long)current->self_exec_id);
767 static ssize_t mem_read(struct file * file, char __user * buf,
768 size_t count, loff_t *ppos)
770 struct task_struct *task = proc_task(file->f_dentry->d_inode);
772 unsigned long src = *ppos;
774 struct mm_struct *mm;
776 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
780 page = (char *)__get_free_page(GFP_USER);
786 mm = get_task_mm(task);
792 if (file->private_data != (void*)((long)current->self_exec_id))
798 int this_len, retval;
800 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
801 retval = access_process_vm(task, src, page, this_len, 0);
802 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
808 if (copy_to_user(buf, page, retval)) {
823 free_page((unsigned long) page);
828 #define mem_write NULL
831 /* This is a security hazard */
832 static ssize_t mem_write(struct file * file, const char * buf,
833 size_t count, loff_t *ppos)
837 struct task_struct *task = proc_task(file->f_dentry->d_inode);
838 unsigned long dst = *ppos;
840 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
843 page = (char *)__get_free_page(GFP_USER);
848 int this_len, retval;
850 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
851 if (copy_from_user(page, buf, this_len)) {
855 retval = access_process_vm(task, dst, page, this_len, 1);
867 free_page((unsigned long) page);
872 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
876 file->f_pos = offset;
879 file->f_pos += offset;
884 force_successful_syscall_return();
888 static struct file_operations proc_mem_operations = {
895 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
896 size_t count, loff_t *ppos)
898 struct task_struct *task = proc_task(file->f_dentry->d_inode);
901 int oom_adjust = task->oomkilladj;
902 loff_t __ppos = *ppos;
904 len = sprintf(buffer, "%i\n", oom_adjust);
907 if (count > len-__ppos)
909 if (copy_to_user(buf, buffer + __ppos, count))
911 *ppos = __ppos + count;
915 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
916 size_t count, loff_t *ppos)
918 struct task_struct *task = proc_task(file->f_dentry->d_inode);
919 char buffer[8], *end;
922 if (!capable(CAP_SYS_RESOURCE))
924 memset(buffer, 0, 8);
927 if (copy_from_user(buffer, buf, count))
929 oom_adjust = simple_strtol(buffer, &end, 0);
930 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
934 task->oomkilladj = oom_adjust;
935 if (end - buffer == 0)
940 static struct file_operations proc_oom_adjust_operations = {
941 .read = oom_adjust_read,
942 .write = oom_adjust_write,
945 static struct inode_operations proc_mem_inode_operations = {
946 .permission = proc_permission,
949 #ifdef CONFIG_AUDITSYSCALL
951 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
952 size_t count, loff_t *ppos)
954 struct inode * inode = file->f_dentry->d_inode;
955 struct task_struct *task = proc_task(inode);
957 char tmpbuf[TMPBUFLEN];
959 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
960 audit_get_loginuid(task->audit_context));
961 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
964 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
965 size_t count, loff_t *ppos)
967 struct inode * inode = file->f_dentry->d_inode;
970 struct task_struct *task = proc_task(inode);
973 if (!capable(CAP_AUDIT_CONTROL))
979 if (count > PAGE_SIZE)
983 /* No partial writes. */
986 page = (char*)__get_free_page(GFP_USER);
990 if (copy_from_user(page, buf, count))
993 loginuid = simple_strtoul(page, &tmp, 10);
999 length = audit_set_loginuid(task, loginuid);
1000 if (likely(length == 0))
1004 free_page((unsigned long) page);
1008 static struct file_operations proc_loginuid_operations = {
1009 .read = proc_loginuid_read,
1010 .write = proc_loginuid_write,
1014 #ifdef CONFIG_SECCOMP
1015 static ssize_t seccomp_read(struct file *file, char __user *buf,
1016 size_t count, loff_t *ppos)
1018 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1020 loff_t __ppos = *ppos;
1023 /* no need to print the trailing zero, so use only len */
1024 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1027 if (count > len - __ppos)
1028 count = len - __ppos;
1029 if (copy_to_user(buf, __buf + __ppos, count))
1031 *ppos = __ppos + count;
1035 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1036 size_t count, loff_t *ppos)
1038 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1039 char __buf[20], *end;
1040 unsigned int seccomp_mode;
1042 /* can set it only once to be even more secure */
1043 if (unlikely(tsk->seccomp.mode))
1046 memset(__buf, 0, sizeof(__buf));
1047 count = min(count, sizeof(__buf) - 1);
1048 if (copy_from_user(__buf, buf, count))
1050 seccomp_mode = simple_strtoul(__buf, &end, 0);
1053 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1054 tsk->seccomp.mode = seccomp_mode;
1055 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1058 if (unlikely(!(end - __buf)))
1063 static struct file_operations proc_seccomp_operations = {
1064 .read = seccomp_read,
1065 .write = seccomp_write,
1067 #endif /* CONFIG_SECCOMP */
1069 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1071 struct inode *inode = dentry->d_inode;
1072 int error = -EACCES;
1074 /* We don't need a base pointer in the /proc filesystem */
1077 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1079 error = proc_check_root(inode);
1083 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1084 nd->last_type = LAST_BIND;
1086 return ERR_PTR(error);
1089 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1090 char __user *buffer, int buflen)
1092 struct inode * inode;
1093 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1099 inode = dentry->d_inode;
1100 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1101 len = PTR_ERR(path);
1104 len = tmp + PAGE_SIZE - 1 - path;
1108 if (copy_to_user(buffer, path, len))
1111 free_page((unsigned long)tmp);
1115 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1117 int error = -EACCES;
1118 struct inode *inode = dentry->d_inode;
1120 struct vfsmount *mnt = NULL;
1124 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1126 error = proc_check_root(inode);
1130 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1134 error = do_proc_readlink(de, mnt, buffer, buflen);
1142 static struct inode_operations proc_pid_link_inode_operations = {
1143 .readlink = proc_pid_readlink,
1144 .follow_link = proc_pid_follow_link
1149 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1151 struct inode *inode = filp->f_dentry->d_inode;
1152 struct task_struct *p = proc_task(inode);
1153 unsigned int fd, tid, ino;
1156 struct files_struct * files;
1157 struct fdtable *fdt;
1168 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1172 ino = fake_ino(tid, PROC_TID_INO);
1173 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1177 files = get_files_struct(p);
1181 fdt = files_fdtable(files);
1182 for (fd = filp->f_pos-2;
1184 fd++, filp->f_pos++) {
1187 if (!fcheck_files(files, fd))
1195 buf[j] = '0' + (i % 10);
1199 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1200 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1207 put_files_struct(files);
1213 static int proc_pident_readdir(struct file *filp,
1214 void *dirent, filldir_t filldir,
1215 struct pid_entry *ents, unsigned int nents)
1219 struct dentry *dentry = filp->f_dentry;
1220 struct inode *inode = dentry->d_inode;
1221 struct pid_entry *p;
1226 if (!pid_alive(proc_task(inode)))
1230 pid = proc_task(inode)->pid;
1235 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1241 ino = parent_ino(dentry);
1242 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1255 if (filldir(dirent, p->name, p->len, filp->f_pos,
1256 fake_ino(pid, p->type), p->mode >> 12) < 0)
1268 static int proc_tgid_base_readdir(struct file * filp,
1269 void * dirent, filldir_t filldir)
1271 return proc_pident_readdir(filp,dirent,filldir,
1272 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1275 static int proc_tid_base_readdir(struct file * filp,
1276 void * dirent, filldir_t filldir)
1278 return proc_pident_readdir(filp,dirent,filldir,
1279 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1282 /* building an inode */
1284 static int task_dumpable(struct task_struct *task)
1287 struct mm_struct *mm;
1292 dumpable = mm->dumpable;
1300 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1302 struct inode * inode;
1303 struct proc_inode *ei;
1305 /* We need a new inode */
1307 inode = new_inode(sb);
1314 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1315 inode->i_ino = fake_ino(task->pid, ino);
1317 if (!pid_alive(task))
1321 * grab the reference to task.
1323 get_task_struct(task);
1328 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1329 inode->i_uid = task->euid;
1330 inode->i_gid = task->egid;
1332 security_task_to_inode(task, inode);
1346 * Exceptional case: normally we are not allowed to unhash a busy
1347 * directory. In this case, however, we can do it - no aliasing problems
1348 * due to the way we treat inodes.
1350 * Rewrite the inode's ownerships here because the owning task may have
1351 * performed a setuid(), etc.
1353 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1355 struct inode *inode = dentry->d_inode;
1356 struct task_struct *task = proc_task(inode);
1357 if (pid_alive(task)) {
1358 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1359 inode->i_uid = task->euid;
1360 inode->i_gid = task->egid;
1365 security_task_to_inode(task, inode);
1372 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1374 struct inode *inode = dentry->d_inode;
1375 struct task_struct *task = proc_task(inode);
1376 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1377 struct files_struct *files;
1379 files = get_files_struct(task);
1382 if (fcheck_files(files, fd)) {
1384 put_files_struct(files);
1385 if (task_dumpable(task)) {
1386 inode->i_uid = task->euid;
1387 inode->i_gid = task->egid;
1392 security_task_to_inode(task, inode);
1396 put_files_struct(files);
1402 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1404 struct task_struct *task = proc_task(inode);
1405 spin_lock(&task->proc_lock);
1406 if (task->proc_dentry == dentry)
1407 task->proc_dentry = NULL;
1408 spin_unlock(&task->proc_lock);
1412 static int pid_delete_dentry(struct dentry * dentry)
1414 /* Is the task we represent dead?
1415 * If so, then don't put the dentry on the lru list,
1416 * kill it immediately.
1418 return !pid_alive(proc_task(dentry->d_inode));
1421 static struct dentry_operations tid_fd_dentry_operations =
1423 .d_revalidate = tid_fd_revalidate,
1424 .d_delete = pid_delete_dentry,
1427 static struct dentry_operations pid_dentry_operations =
1429 .d_revalidate = pid_revalidate,
1430 .d_delete = pid_delete_dentry,
1433 static struct dentry_operations pid_base_dentry_operations =
1435 .d_revalidate = pid_revalidate,
1436 .d_iput = pid_base_iput,
1437 .d_delete = pid_delete_dentry,
1442 static unsigned name_to_int(struct dentry *dentry)
1444 const char *name = dentry->d_name.name;
1445 int len = dentry->d_name.len;
1448 if (len > 1 && *name == '0')
1451 unsigned c = *name++ - '0';
1454 if (n >= (~0U-9)/10)
1465 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1467 struct task_struct *task = proc_task(dir);
1468 unsigned fd = name_to_int(dentry);
1470 struct files_struct * files;
1471 struct inode *inode;
1472 struct proc_inode *ei;
1476 if (!pid_alive(task))
1479 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1483 files = get_files_struct(task);
1486 inode->i_mode = S_IFLNK;
1488 file = fcheck_files(files, fd);
1491 if (file->f_mode & 1)
1492 inode->i_mode |= S_IRUSR | S_IXUSR;
1493 if (file->f_mode & 2)
1494 inode->i_mode |= S_IWUSR | S_IXUSR;
1496 put_files_struct(files);
1497 inode->i_op = &proc_pid_link_inode_operations;
1499 ei->op.proc_get_link = proc_fd_link;
1500 dentry->d_op = &tid_fd_dentry_operations;
1501 d_add(dentry, inode);
1506 put_files_struct(files);
1510 return ERR_PTR(-ENOENT);
1513 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1514 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1516 static struct file_operations proc_fd_operations = {
1517 .read = generic_read_dir,
1518 .readdir = proc_readfd,
1521 static struct file_operations proc_task_operations = {
1522 .read = generic_read_dir,
1523 .readdir = proc_task_readdir,
1527 * proc directories can do almost nothing..
1529 static struct inode_operations proc_fd_inode_operations = {
1530 .lookup = proc_lookupfd,
1531 .permission = proc_permission,
1534 static struct inode_operations proc_task_inode_operations = {
1535 .lookup = proc_task_lookup,
1536 .permission = proc_task_permission,
1539 #ifdef CONFIG_SECURITY
1540 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1541 size_t count, loff_t *ppos)
1543 struct inode * inode = file->f_dentry->d_inode;
1546 struct task_struct *task = proc_task(inode);
1548 if (count > PAGE_SIZE)
1550 if (!(page = __get_free_page(GFP_KERNEL)))
1553 length = security_getprocattr(task,
1554 (char*)file->f_dentry->d_name.name,
1555 (void*)page, count);
1557 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1562 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1563 size_t count, loff_t *ppos)
1565 struct inode * inode = file->f_dentry->d_inode;
1568 struct task_struct *task = proc_task(inode);
1570 if (count > PAGE_SIZE)
1573 /* No partial writes. */
1576 page = (char*)__get_free_page(GFP_USER);
1580 if (copy_from_user(page, buf, count))
1583 length = security_setprocattr(task,
1584 (char*)file->f_dentry->d_name.name,
1585 (void*)page, count);
1587 free_page((unsigned long) page);
1591 static struct file_operations proc_pid_attr_operations = {
1592 .read = proc_pid_attr_read,
1593 .write = proc_pid_attr_write,
1596 static struct file_operations proc_tid_attr_operations;
1597 static struct inode_operations proc_tid_attr_inode_operations;
1598 static struct file_operations proc_tgid_attr_operations;
1599 static struct inode_operations proc_tgid_attr_inode_operations;
1602 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1605 static struct dentry *proc_pident_lookup(struct inode *dir,
1606 struct dentry *dentry,
1607 struct pid_entry *ents)
1609 struct inode *inode;
1611 struct task_struct *task = proc_task(dir);
1612 struct pid_entry *p;
1613 struct proc_inode *ei;
1618 if (!pid_alive(task))
1621 for (p = ents; p->name; p++) {
1622 if (p->len != dentry->d_name.len)
1624 if (!memcmp(dentry->d_name.name, p->name, p->len))
1631 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1636 inode->i_mode = p->mode;
1638 * Yes, it does not scale. And it should not. Don't add
1639 * new entries into /proc/<tgid>/ without very good reasons.
1642 case PROC_TGID_TASK:
1643 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1644 inode->i_op = &proc_task_inode_operations;
1645 inode->i_fop = &proc_task_operations;
1650 inode->i_op = &proc_fd_inode_operations;
1651 inode->i_fop = &proc_fd_operations;
1655 inode->i_op = &proc_pid_link_inode_operations;
1656 ei->op.proc_get_link = proc_exe_link;
1660 inode->i_op = &proc_pid_link_inode_operations;
1661 ei->op.proc_get_link = proc_cwd_link;
1664 case PROC_TGID_ROOT:
1665 inode->i_op = &proc_pid_link_inode_operations;
1666 ei->op.proc_get_link = proc_root_link;
1668 case PROC_TID_ENVIRON:
1669 case PROC_TGID_ENVIRON:
1670 inode->i_fop = &proc_info_file_operations;
1671 ei->op.proc_read = proc_pid_environ;
1674 case PROC_TGID_AUXV:
1675 inode->i_fop = &proc_info_file_operations;
1676 ei->op.proc_read = proc_pid_auxv;
1678 case PROC_TID_STATUS:
1679 case PROC_TGID_STATUS:
1680 inode->i_fop = &proc_info_file_operations;
1681 ei->op.proc_read = proc_pid_status;
1684 inode->i_fop = &proc_info_file_operations;
1685 ei->op.proc_read = proc_tid_stat;
1687 case PROC_TGID_STAT:
1688 inode->i_fop = &proc_info_file_operations;
1689 ei->op.proc_read = proc_tgid_stat;
1691 case PROC_TID_CMDLINE:
1692 case PROC_TGID_CMDLINE:
1693 inode->i_fop = &proc_info_file_operations;
1694 ei->op.proc_read = proc_pid_cmdline;
1696 case PROC_TID_STATM:
1697 case PROC_TGID_STATM:
1698 inode->i_fop = &proc_info_file_operations;
1699 ei->op.proc_read = proc_pid_statm;
1702 case PROC_TGID_MAPS:
1703 inode->i_fop = &proc_maps_operations;
1706 case PROC_TID_NUMA_MAPS:
1707 case PROC_TGID_NUMA_MAPS:
1708 inode->i_fop = &proc_numa_maps_operations;
1713 inode->i_op = &proc_mem_inode_operations;
1714 inode->i_fop = &proc_mem_operations;
1716 #ifdef CONFIG_SECCOMP
1717 case PROC_TID_SECCOMP:
1718 case PROC_TGID_SECCOMP:
1719 inode->i_fop = &proc_seccomp_operations;
1721 #endif /* CONFIG_SECCOMP */
1722 case PROC_TID_MOUNTS:
1723 case PROC_TGID_MOUNTS:
1724 inode->i_fop = &proc_mounts_operations;
1727 case PROC_TID_SMAPS:
1728 case PROC_TGID_SMAPS:
1729 inode->i_fop = &proc_smaps_operations;
1732 #ifdef CONFIG_SECURITY
1735 inode->i_op = &proc_tid_attr_inode_operations;
1736 inode->i_fop = &proc_tid_attr_operations;
1738 case PROC_TGID_ATTR:
1740 inode->i_op = &proc_tgid_attr_inode_operations;
1741 inode->i_fop = &proc_tgid_attr_operations;
1743 case PROC_TID_ATTR_CURRENT:
1744 case PROC_TGID_ATTR_CURRENT:
1745 case PROC_TID_ATTR_PREV:
1746 case PROC_TGID_ATTR_PREV:
1747 case PROC_TID_ATTR_EXEC:
1748 case PROC_TGID_ATTR_EXEC:
1749 case PROC_TID_ATTR_FSCREATE:
1750 case PROC_TGID_ATTR_FSCREATE:
1751 inode->i_fop = &proc_pid_attr_operations;
1754 #ifdef CONFIG_KALLSYMS
1755 case PROC_TID_WCHAN:
1756 case PROC_TGID_WCHAN:
1757 inode->i_fop = &proc_info_file_operations;
1758 ei->op.proc_read = proc_pid_wchan;
1761 #ifdef CONFIG_SCHEDSTATS
1762 case PROC_TID_SCHEDSTAT:
1763 case PROC_TGID_SCHEDSTAT:
1764 inode->i_fop = &proc_info_file_operations;
1765 ei->op.proc_read = proc_pid_schedstat;
1768 #ifdef CONFIG_CPUSETS
1769 case PROC_TID_CPUSET:
1770 case PROC_TGID_CPUSET:
1771 inode->i_fop = &proc_cpuset_operations;
1774 case PROC_TID_OOM_SCORE:
1775 case PROC_TGID_OOM_SCORE:
1776 inode->i_fop = &proc_info_file_operations;
1777 ei->op.proc_read = proc_oom_score;
1779 case PROC_TID_OOM_ADJUST:
1780 case PROC_TGID_OOM_ADJUST:
1781 inode->i_fop = &proc_oom_adjust_operations;
1783 #ifdef CONFIG_AUDITSYSCALL
1784 case PROC_TID_LOGINUID:
1785 case PROC_TGID_LOGINUID:
1786 inode->i_fop = &proc_loginuid_operations;
1790 printk("procfs: impossible type (%d)",p->type);
1792 return ERR_PTR(-EINVAL);
1794 dentry->d_op = &pid_dentry_operations;
1795 d_add(dentry, inode);
1799 return ERR_PTR(error);
1802 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1803 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1806 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1807 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1810 static struct file_operations proc_tgid_base_operations = {
1811 .read = generic_read_dir,
1812 .readdir = proc_tgid_base_readdir,
1815 static struct file_operations proc_tid_base_operations = {
1816 .read = generic_read_dir,
1817 .readdir = proc_tid_base_readdir,
1820 static struct inode_operations proc_tgid_base_inode_operations = {
1821 .lookup = proc_tgid_base_lookup,
1824 static struct inode_operations proc_tid_base_inode_operations = {
1825 .lookup = proc_tid_base_lookup,
1828 #ifdef CONFIG_SECURITY
1829 static int proc_tgid_attr_readdir(struct file * filp,
1830 void * dirent, filldir_t filldir)
1832 return proc_pident_readdir(filp,dirent,filldir,
1833 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1836 static int proc_tid_attr_readdir(struct file * filp,
1837 void * dirent, filldir_t filldir)
1839 return proc_pident_readdir(filp,dirent,filldir,
1840 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1843 static struct file_operations proc_tgid_attr_operations = {
1844 .read = generic_read_dir,
1845 .readdir = proc_tgid_attr_readdir,
1848 static struct file_operations proc_tid_attr_operations = {
1849 .read = generic_read_dir,
1850 .readdir = proc_tid_attr_readdir,
1853 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1854 struct dentry *dentry, struct nameidata *nd)
1856 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1859 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1860 struct dentry *dentry, struct nameidata *nd)
1862 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1865 static struct inode_operations proc_tgid_attr_inode_operations = {
1866 .lookup = proc_tgid_attr_lookup,
1869 static struct inode_operations proc_tid_attr_inode_operations = {
1870 .lookup = proc_tid_attr_lookup,
1877 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1881 sprintf(tmp, "%d", current->tgid);
1882 return vfs_readlink(dentry,buffer,buflen,tmp);
1885 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1888 sprintf(tmp, "%d", current->tgid);
1889 return ERR_PTR(vfs_follow_link(nd,tmp));
1892 static struct inode_operations proc_self_inode_operations = {
1893 .readlink = proc_self_readlink,
1894 .follow_link = proc_self_follow_link,
1898 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1899 * @p: task that should be flushed.
1901 * Drops the /proc/@pid dcache entry from the hash chains.
1903 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1904 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1905 * if the pid value is immediately reused. This is enforced by
1906 * - caller must acquire spin_lock(p->proc_lock)
1907 * - must be called before detach_pid()
1908 * - proc_pid_lookup acquires proc_lock, and checks that
1909 * the target is not dead by looking at the attach count
1913 struct dentry *proc_pid_unhash(struct task_struct *p)
1915 struct dentry *proc_dentry;
1917 proc_dentry = p->proc_dentry;
1918 if (proc_dentry != NULL) {
1920 spin_lock(&dcache_lock);
1921 spin_lock(&proc_dentry->d_lock);
1922 if (!d_unhashed(proc_dentry)) {
1923 dget_locked(proc_dentry);
1924 __d_drop(proc_dentry);
1925 spin_unlock(&proc_dentry->d_lock);
1927 spin_unlock(&proc_dentry->d_lock);
1930 spin_unlock(&dcache_lock);
1936 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1937 * @proc_dentry: directoy to prune.
1939 * Shrink the /proc directory that was used by the just killed thread.
1942 void proc_pid_flush(struct dentry *proc_dentry)
1945 if(proc_dentry != NULL) {
1946 shrink_dcache_parent(proc_dentry);
1952 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1954 struct task_struct *task;
1955 struct inode *inode;
1956 struct proc_inode *ei;
1960 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1961 inode = new_inode(dir->i_sb);
1963 return ERR_PTR(-ENOMEM);
1965 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1966 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1968 inode->i_mode = S_IFLNK|S_IRWXUGO;
1969 inode->i_uid = inode->i_gid = 0;
1971 inode->i_op = &proc_self_inode_operations;
1972 d_add(dentry, inode);
1975 tgid = name_to_int(dentry);
1979 read_lock(&tasklist_lock);
1980 task = find_task_by_pid(tgid);
1982 get_task_struct(task);
1983 read_unlock(&tasklist_lock);
1987 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1991 put_task_struct(task);
1994 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1995 inode->i_op = &proc_tgid_base_inode_operations;
1996 inode->i_fop = &proc_tgid_base_operations;
1997 inode->i_flags|=S_IMMUTABLE;
1998 #ifdef CONFIG_SECURITY
2004 dentry->d_op = &pid_base_dentry_operations;
2007 d_add(dentry, inode);
2008 spin_lock(&task->proc_lock);
2009 task->proc_dentry = dentry;
2010 if (!pid_alive(task)) {
2011 dentry = proc_pid_unhash(task);
2014 spin_unlock(&task->proc_lock);
2016 put_task_struct(task);
2018 proc_pid_flush(dentry);
2023 return ERR_PTR(-ENOENT);
2027 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2029 struct task_struct *task;
2030 struct task_struct *leader = proc_task(dir);
2031 struct inode *inode;
2034 tid = name_to_int(dentry);
2038 read_lock(&tasklist_lock);
2039 task = find_task_by_pid(tid);
2041 get_task_struct(task);
2042 read_unlock(&tasklist_lock);
2045 if (leader->tgid != task->tgid)
2048 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2053 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2054 inode->i_op = &proc_tid_base_inode_operations;
2055 inode->i_fop = &proc_tid_base_operations;
2056 inode->i_flags|=S_IMMUTABLE;
2057 #ifdef CONFIG_SECURITY
2063 dentry->d_op = &pid_base_dentry_operations;
2065 d_add(dentry, inode);
2067 put_task_struct(task);
2070 put_task_struct(task);
2072 return ERR_PTR(-ENOENT);
2075 #define PROC_NUMBUF 10
2076 #define PROC_MAXPIDS 20
2079 * Get a few tgid's to return for filldir - we need to hold the
2080 * tasklist lock while doing this, and we must release it before
2081 * we actually do the filldir itself, so we use a temp buffer..
2083 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2085 struct task_struct *p;
2089 read_lock(&tasklist_lock);
2092 p = find_task_by_pid(version);
2093 if (p && !thread_group_leader(p))
2100 p = next_task(&init_task);
2102 for ( ; p != &init_task; p = next_task(p)) {
2108 tgids[nr_tgids] = tgid;
2110 if (nr_tgids >= PROC_MAXPIDS)
2113 read_unlock(&tasklist_lock);
2118 * Get a few tid's to return for filldir - we need to hold the
2119 * tasklist lock while doing this, and we must release it before
2120 * we actually do the filldir itself, so we use a temp buffer..
2122 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2124 struct task_struct *leader_task = proc_task(dir);
2125 struct task_struct *task = leader_task;
2129 read_lock(&tasklist_lock);
2131 * The starting point task (leader_task) might be an already
2132 * unlinked task, which cannot be used to access the task-list
2133 * via next_thread().
2135 if (pid_alive(task)) do {
2136 int tid = task->pid;
2141 tids[nr_tids] = tid;
2143 if (nr_tids >= PROC_MAXPIDS)
2145 } while ((task = next_thread(task)) != leader_task);
2146 read_unlock(&tasklist_lock);
2150 /* for the /proc/ directory itself, after non-process stuff has been done */
2151 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2153 unsigned int tgid_array[PROC_MAXPIDS];
2154 char buf[PROC_NUMBUF];
2155 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2156 unsigned int nr_tgids, i;
2160 ino_t ino = fake_ino(0,PROC_TGID_INO);
2161 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2167 /* f_version caches the tgid value that the last readdir call couldn't
2168 * return. lseek aka telldir automagically resets f_version to 0.
2170 next_tgid = filp->f_version;
2171 filp->f_version = 0;
2173 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2175 /* no more entries ! */
2180 /* do not use the last found pid, reserve it for next_tgid */
2181 if (nr_tgids == PROC_MAXPIDS) {
2183 next_tgid = tgid_array[nr_tgids];
2186 for (i=0;i<nr_tgids;i++) {
2187 int tgid = tgid_array[i];
2188 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2189 unsigned long j = PROC_NUMBUF;
2192 buf[--j] = '0' + (tgid % 10);
2193 while ((tgid /= 10) != 0);
2195 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2196 /* returning this tgid failed, save it as the first
2197 * pid for the next readir call */
2198 filp->f_version = tgid_array[i];
2209 /* for the /proc/TGID/task/ directories */
2210 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2212 unsigned int tid_array[PROC_MAXPIDS];
2213 char buf[PROC_NUMBUF];
2214 unsigned int nr_tids, i;
2215 struct dentry *dentry = filp->f_dentry;
2216 struct inode *inode = dentry->d_inode;
2217 int retval = -ENOENT;
2219 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2221 if (!pid_alive(proc_task(inode)))
2228 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2233 ino = parent_ino(dentry);
2234 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2240 nr_tids = get_tid_list(pos, tid_array, inode);
2241 inode->i_nlink = pos + nr_tids;
2243 for (i = 0; i < nr_tids; i++) {
2244 unsigned long j = PROC_NUMBUF;
2245 int tid = tid_array[i];
2247 ino = fake_ino(tid,PROC_TID_INO);
2250 buf[--j] = '0' + (tid % 10);
2251 while ((tid /= 10) != 0);
2253 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)