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/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.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 * Implementing inode permission operations in /proc is almost
78 * certainly an error. Permission checks need to happen during
79 * each system call not at open time. The reason is that most of
80 * what we wish to check for permissions in /proc varies at runtime.
82 * The classic example of a problem is opening file descriptors
83 * in /proc for a task before it execs a suid executable.
87 * For hysterical raisins we keep the same inumbers as in the old procfs.
88 * Feel free to change the macro below - just keep the range distinct from
89 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
90 * As soon as we'll get a separate superblock we will be able to forget
91 * about magical ranges too.
94 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
96 enum pid_directory_inos {
101 #ifdef CONFIG_SECCOMP
116 PROC_TGID_MOUNTSTATS,
121 #ifdef CONFIG_SCHEDSTATS
124 #ifdef CONFIG_CPUSETS
127 #ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR_CURRENT,
132 PROC_TGID_ATTR_FSCREATE,
133 PROC_TGID_ATTR_KEYCREATE,
134 PROC_TGID_ATTR_SOCKCREATE,
136 #ifdef CONFIG_AUDITSYSCALL
140 PROC_TGID_OOM_ADJUST,
144 #ifdef CONFIG_SECCOMP
164 #ifdef CONFIG_SCHEDSTATS
167 #ifdef CONFIG_CPUSETS
170 #ifdef CONFIG_SECURITY
172 PROC_TID_ATTR_CURRENT,
175 PROC_TID_ATTR_FSCREATE,
176 PROC_TID_ATTR_KEYCREATE,
177 PROC_TID_ATTR_SOCKCREATE,
179 #ifdef CONFIG_AUDITSYSCALL
185 /* Add new entries before this */
186 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
189 /* Worst case buffer size needed for holding an integer. */
190 #define PROC_NUMBUF 10
199 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
201 static struct fs_struct *get_fs_struct(struct task_struct *task)
203 struct fs_struct *fs;
207 atomic_inc(&fs->count);
212 static int get_nr_threads(struct task_struct *tsk)
214 /* Must be called with the rcu_read_lock held */
218 if (lock_task_sighand(tsk, &flags)) {
219 count = atomic_read(&tsk->signal->count);
220 unlock_task_sighand(tsk, &flags);
225 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
227 struct task_struct *task = get_proc_task(inode);
228 struct fs_struct *fs = NULL;
229 int result = -ENOENT;
232 fs = get_fs_struct(task);
233 put_task_struct(task);
236 read_lock(&fs->lock);
237 *mnt = mntget(fs->pwdmnt);
238 *dentry = dget(fs->pwd);
239 read_unlock(&fs->lock);
246 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
248 struct task_struct *task = get_proc_task(inode);
249 struct fs_struct *fs = NULL;
250 int result = -ENOENT;
253 fs = get_fs_struct(task);
254 put_task_struct(task);
257 read_lock(&fs->lock);
258 *mnt = mntget(fs->rootmnt);
259 *dentry = dget(fs->root);
260 read_unlock(&fs->lock);
267 #define MAY_PTRACE(task) \
268 (task == current || \
269 (task->parent == current && \
270 (task->ptrace & PT_PTRACED) && \
271 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
272 security_ptrace(current,task) == 0))
274 static int proc_pid_environ(struct task_struct *task, char * buffer)
277 struct mm_struct *mm = get_task_mm(task);
279 unsigned int len = mm->env_end - mm->env_start;
282 res = access_process_vm(task, mm->env_start, buffer, len, 0);
283 if (!ptrace_may_attach(task))
290 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
294 struct mm_struct *mm = get_task_mm(task);
298 goto out_mm; /* Shh! No looking before we're done */
300 len = mm->arg_end - mm->arg_start;
305 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
307 // If the nul at the end of args has been overwritten, then
308 // assume application is using setproctitle(3).
309 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
310 len = strnlen(buffer, res);
314 len = mm->env_end - mm->env_start;
315 if (len > PAGE_SIZE - res)
316 len = PAGE_SIZE - res;
317 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
318 res = strnlen(buffer, res);
327 static int proc_pid_auxv(struct task_struct *task, char *buffer)
330 struct mm_struct *mm = get_task_mm(task);
332 unsigned int nwords = 0;
335 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
336 res = nwords * sizeof(mm->saved_auxv[0]);
339 memcpy(buffer, mm->saved_auxv, res);
346 #ifdef CONFIG_KALLSYMS
348 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
349 * Returns the resolved symbol. If that fails, simply return the address.
351 static int proc_pid_wchan(struct task_struct *task, char *buffer)
354 const char *sym_name;
355 unsigned long wchan, size, offset;
356 char namebuf[KSYM_NAME_LEN+1];
358 wchan = get_wchan(task);
360 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
362 return sprintf(buffer, "%s", sym_name);
363 return sprintf(buffer, "%lu", wchan);
365 #endif /* CONFIG_KALLSYMS */
367 #ifdef CONFIG_SCHEDSTATS
369 * Provides /proc/PID/schedstat
371 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
373 return sprintf(buffer, "%lu %lu %lu\n",
374 task->sched_info.cpu_time,
375 task->sched_info.run_delay,
376 task->sched_info.pcnt);
380 /* The badness from the OOM killer */
381 unsigned long badness(struct task_struct *p, unsigned long uptime);
382 static int proc_oom_score(struct task_struct *task, char *buffer)
384 unsigned long points;
385 struct timespec uptime;
387 do_posix_clock_monotonic_gettime(&uptime);
388 points = badness(task, uptime.tv_sec);
389 return sprintf(buffer, "%lu\n", points);
392 /************************************************************************/
393 /* Here the fs part begins */
394 /************************************************************************/
396 /* permission checks */
397 static int proc_fd_access_allowed(struct inode *inode)
399 struct task_struct *task;
401 /* Allow access to a task's file descriptors if it is us or we
402 * may use ptrace attach to the process and find out that
405 task = get_proc_task(inode);
407 allowed = ptrace_may_attach(task);
408 put_task_struct(task);
413 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
416 struct inode *inode = dentry->d_inode;
418 if (attr->ia_valid & ATTR_MODE)
421 error = inode_change_ok(inode, attr);
423 error = security_inode_setattr(dentry, attr);
425 error = inode_setattr(inode, attr);
430 static struct inode_operations proc_def_inode_operations = {
431 .setattr = proc_setattr,
434 extern struct seq_operations mounts_op;
440 static int mounts_open(struct inode *inode, struct file *file)
442 struct task_struct *task = get_proc_task(inode);
443 struct namespace *namespace = NULL;
444 struct proc_mounts *p;
449 namespace = task->namespace;
451 get_namespace(namespace);
453 put_task_struct(task);
458 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
460 file->private_data = &p->m;
461 ret = seq_open(file, &mounts_op);
463 p->m.private = namespace;
464 p->event = namespace->event;
469 put_namespace(namespace);
474 static int mounts_release(struct inode *inode, struct file *file)
476 struct seq_file *m = file->private_data;
477 struct namespace *namespace = m->private;
478 put_namespace(namespace);
479 return seq_release(inode, file);
482 static unsigned mounts_poll(struct file *file, poll_table *wait)
484 struct proc_mounts *p = file->private_data;
485 struct namespace *ns = p->m.private;
488 poll_wait(file, &ns->poll, wait);
490 spin_lock(&vfsmount_lock);
491 if (p->event != ns->event) {
492 p->event = ns->event;
495 spin_unlock(&vfsmount_lock);
500 static struct file_operations proc_mounts_operations = {
504 .release = mounts_release,
508 extern struct seq_operations mountstats_op;
509 static int mountstats_open(struct inode *inode, struct file *file)
511 int ret = seq_open(file, &mountstats_op);
514 struct seq_file *m = file->private_data;
515 struct namespace *namespace = NULL;
516 struct task_struct *task = get_proc_task(inode);
520 namespace = task->namespace;
522 get_namespace(namespace);
524 put_task_struct(task);
528 m->private = namespace;
530 seq_release(inode, file);
537 static struct file_operations proc_mountstats_operations = {
538 .open = mountstats_open,
541 .release = mounts_release,
544 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
546 static ssize_t proc_info_read(struct file * file, char __user * buf,
547 size_t count, loff_t *ppos)
549 struct inode * inode = file->f_dentry->d_inode;
552 struct task_struct *task = get_proc_task(inode);
558 if (count > PROC_BLOCK_SIZE)
559 count = PROC_BLOCK_SIZE;
562 if (!(page = __get_free_page(GFP_KERNEL)))
565 length = PROC_I(inode)->op.proc_read(task, (char*)page);
568 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
571 put_task_struct(task);
576 static struct file_operations proc_info_file_operations = {
577 .read = proc_info_read,
580 static int mem_open(struct inode* inode, struct file* file)
582 file->private_data = (void*)((long)current->self_exec_id);
586 static ssize_t mem_read(struct file * file, char __user * buf,
587 size_t count, loff_t *ppos)
589 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
591 unsigned long src = *ppos;
593 struct mm_struct *mm;
598 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
602 page = (char *)__get_free_page(GFP_USER);
608 mm = get_task_mm(task);
614 if (file->private_data != (void*)((long)current->self_exec_id))
620 int this_len, retval;
622 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
623 retval = access_process_vm(task, src, page, this_len, 0);
624 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
630 if (copy_to_user(buf, page, retval)) {
645 free_page((unsigned long) page);
647 put_task_struct(task);
652 #define mem_write NULL
655 /* This is a security hazard */
656 static ssize_t mem_write(struct file * file, const char * buf,
657 size_t count, loff_t *ppos)
661 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
662 unsigned long dst = *ppos;
668 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
672 page = (char *)__get_free_page(GFP_USER);
678 int this_len, retval;
680 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
681 if (copy_from_user(page, buf, this_len)) {
685 retval = access_process_vm(task, dst, page, this_len, 1);
697 free_page((unsigned long) page);
699 put_task_struct(task);
705 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
709 file->f_pos = offset;
712 file->f_pos += offset;
717 force_successful_syscall_return();
721 static struct file_operations proc_mem_operations = {
728 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
729 size_t count, loff_t *ppos)
731 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
732 char buffer[PROC_NUMBUF];
735 loff_t __ppos = *ppos;
739 oom_adjust = task->oomkilladj;
740 put_task_struct(task);
742 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
745 if (count > len-__ppos)
747 if (copy_to_user(buf, buffer + __ppos, count))
749 *ppos = __ppos + count;
753 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
754 size_t count, loff_t *ppos)
756 struct task_struct *task;
757 char buffer[PROC_NUMBUF], *end;
760 if (!capable(CAP_SYS_RESOURCE))
762 memset(buffer, 0, sizeof(buffer));
763 if (count > sizeof(buffer) - 1)
764 count = sizeof(buffer) - 1;
765 if (copy_from_user(buffer, buf, count))
767 oom_adjust = simple_strtol(buffer, &end, 0);
768 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
772 task = get_proc_task(file->f_dentry->d_inode);
775 task->oomkilladj = oom_adjust;
776 put_task_struct(task);
777 if (end - buffer == 0)
782 static struct file_operations proc_oom_adjust_operations = {
783 .read = oom_adjust_read,
784 .write = oom_adjust_write,
787 #ifdef CONFIG_AUDITSYSCALL
789 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
790 size_t count, loff_t *ppos)
792 struct inode * inode = file->f_dentry->d_inode;
793 struct task_struct *task = get_proc_task(inode);
795 char tmpbuf[TMPBUFLEN];
799 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
800 audit_get_loginuid(task->audit_context));
801 put_task_struct(task);
802 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
805 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
806 size_t count, loff_t *ppos)
808 struct inode * inode = file->f_dentry->d_inode;
813 if (!capable(CAP_AUDIT_CONTROL))
816 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
819 if (count >= PAGE_SIZE)
820 count = PAGE_SIZE - 1;
823 /* No partial writes. */
826 page = (char*)__get_free_page(GFP_USER);
830 if (copy_from_user(page, buf, count))
834 loginuid = simple_strtoul(page, &tmp, 10);
840 length = audit_set_loginuid(current, loginuid);
841 if (likely(length == 0))
845 free_page((unsigned long) page);
849 static struct file_operations proc_loginuid_operations = {
850 .read = proc_loginuid_read,
851 .write = proc_loginuid_write,
855 #ifdef CONFIG_SECCOMP
856 static ssize_t seccomp_read(struct file *file, char __user *buf,
857 size_t count, loff_t *ppos)
859 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
861 loff_t __ppos = *ppos;
866 /* no need to print the trailing zero, so use only len */
867 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
868 put_task_struct(tsk);
871 if (count > len - __ppos)
872 count = len - __ppos;
873 if (copy_to_user(buf, __buf + __ppos, count))
875 *ppos = __ppos + count;
879 static ssize_t seccomp_write(struct file *file, const char __user *buf,
880 size_t count, loff_t *ppos)
882 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
883 char __buf[20], *end;
884 unsigned int seccomp_mode;
891 /* can set it only once to be even more secure */
893 if (unlikely(tsk->seccomp.mode))
897 memset(__buf, 0, sizeof(__buf));
898 count = min(count, sizeof(__buf) - 1);
899 if (copy_from_user(__buf, buf, count))
902 seccomp_mode = simple_strtoul(__buf, &end, 0);
906 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
907 tsk->seccomp.mode = seccomp_mode;
908 set_tsk_thread_flag(tsk, TIF_SECCOMP);
912 if (unlikely(!(end - __buf)))
914 result = end - __buf;
916 put_task_struct(tsk);
921 static struct file_operations proc_seccomp_operations = {
922 .read = seccomp_read,
923 .write = seccomp_write,
925 #endif /* CONFIG_SECCOMP */
927 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
929 struct inode *inode = dentry->d_inode;
932 /* We don't need a base pointer in the /proc filesystem */
935 /* Are we allowed to snoop on the tasks file descriptors? */
936 if (!proc_fd_access_allowed(inode))
939 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
940 nd->last_type = LAST_BIND;
942 return ERR_PTR(error);
945 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
946 char __user *buffer, int buflen)
948 struct inode * inode;
949 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
955 inode = dentry->d_inode;
956 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
960 len = tmp + PAGE_SIZE - 1 - path;
964 if (copy_to_user(buffer, path, len))
967 free_page((unsigned long)tmp);
971 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
974 struct inode *inode = dentry->d_inode;
976 struct vfsmount *mnt = NULL;
978 /* Are we allowed to snoop on the tasks file descriptors? */
979 if (!proc_fd_access_allowed(inode))
982 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
986 error = do_proc_readlink(de, mnt, buffer, buflen);
993 static struct inode_operations proc_pid_link_inode_operations = {
994 .readlink = proc_pid_readlink,
995 .follow_link = proc_pid_follow_link,
996 .setattr = proc_setattr,
1000 /* building an inode */
1002 static int task_dumpable(struct task_struct *task)
1005 struct mm_struct *mm;
1010 dumpable = mm->dumpable;
1018 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1020 struct inode * inode;
1021 struct proc_inode *ei;
1023 /* We need a new inode */
1025 inode = new_inode(sb);
1031 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1032 inode->i_ino = fake_ino(task->pid, ino);
1033 inode->i_op = &proc_def_inode_operations;
1036 * grab the reference to task.
1038 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1044 if (task_dumpable(task)) {
1045 inode->i_uid = task->euid;
1046 inode->i_gid = task->egid;
1048 security_task_to_inode(task, inode);
1058 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1060 struct inode *inode = dentry->d_inode;
1061 struct task_struct *task;
1062 generic_fillattr(inode, stat);
1067 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1069 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1070 task_dumpable(task)) {
1071 stat->uid = task->euid;
1072 stat->gid = task->egid;
1082 * Exceptional case: normally we are not allowed to unhash a busy
1083 * directory. In this case, however, we can do it - no aliasing problems
1084 * due to the way we treat inodes.
1086 * Rewrite the inode's ownerships here because the owning task may have
1087 * performed a setuid(), etc.
1089 * Before the /proc/pid/status file was created the only way to read
1090 * the effective uid of a /process was to stat /proc/pid. Reading
1091 * /proc/pid/status is slow enough that procps and other packages
1092 * kept stating /proc/pid. To keep the rules in /proc simple I have
1093 * made this apply to all per process world readable and executable
1096 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1098 struct inode *inode = dentry->d_inode;
1099 struct task_struct *task = get_proc_task(inode);
1101 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1102 task_dumpable(task)) {
1103 inode->i_uid = task->euid;
1104 inode->i_gid = task->egid;
1109 inode->i_mode &= ~(S_ISUID | S_ISGID);
1110 security_task_to_inode(task, inode);
1111 put_task_struct(task);
1118 static int pid_delete_dentry(struct dentry * dentry)
1120 /* Is the task we represent dead?
1121 * If so, then don't put the dentry on the lru list,
1122 * kill it immediately.
1124 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1127 static struct dentry_operations pid_dentry_operations =
1129 .d_revalidate = pid_revalidate,
1130 .d_delete = pid_delete_dentry,
1135 static unsigned name_to_int(struct dentry *dentry)
1137 const char *name = dentry->d_name.name;
1138 int len = dentry->d_name.len;
1141 if (len > 1 && *name == '0')
1144 unsigned c = *name++ - '0';
1147 if (n >= (~0U-9)/10)
1157 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1159 struct task_struct *task = get_proc_task(inode);
1160 struct files_struct *files = NULL;
1162 int fd = proc_fd(inode);
1165 files = get_files_struct(task);
1166 put_task_struct(task);
1170 * We are not taking a ref to the file structure, so we must
1173 spin_lock(&files->file_lock);
1174 file = fcheck_files(files, fd);
1176 *mnt = mntget(file->f_vfsmnt);
1177 *dentry = dget(file->f_dentry);
1178 spin_unlock(&files->file_lock);
1179 put_files_struct(files);
1182 spin_unlock(&files->file_lock);
1183 put_files_struct(files);
1188 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1190 struct inode *inode = dentry->d_inode;
1191 struct task_struct *task = get_proc_task(inode);
1192 int fd = proc_fd(inode);
1193 struct files_struct *files;
1196 files = get_files_struct(task);
1199 if (fcheck_files(files, fd)) {
1201 put_files_struct(files);
1202 if (task_dumpable(task)) {
1203 inode->i_uid = task->euid;
1204 inode->i_gid = task->egid;
1209 inode->i_mode &= ~(S_ISUID | S_ISGID);
1210 security_task_to_inode(task, inode);
1211 put_task_struct(task);
1215 put_files_struct(files);
1217 put_task_struct(task);
1223 static struct dentry_operations tid_fd_dentry_operations =
1225 .d_revalidate = tid_fd_revalidate,
1226 .d_delete = pid_delete_dentry,
1230 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1232 struct task_struct *task = get_proc_task(dir);
1233 unsigned fd = name_to_int(dentry);
1234 struct dentry *result = ERR_PTR(-ENOENT);
1236 struct files_struct * files;
1237 struct inode *inode;
1238 struct proc_inode *ei;
1245 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1250 files = get_files_struct(task);
1253 inode->i_mode = S_IFLNK;
1256 * We are not taking a ref to the file structure, so we must
1259 spin_lock(&files->file_lock);
1260 file = fcheck_files(files, fd);
1263 if (file->f_mode & 1)
1264 inode->i_mode |= S_IRUSR | S_IXUSR;
1265 if (file->f_mode & 2)
1266 inode->i_mode |= S_IWUSR | S_IXUSR;
1267 spin_unlock(&files->file_lock);
1268 put_files_struct(files);
1269 inode->i_op = &proc_pid_link_inode_operations;
1271 ei->op.proc_get_link = proc_fd_link;
1272 dentry->d_op = &tid_fd_dentry_operations;
1273 d_add(dentry, inode);
1274 /* Close the race of the process dying before we return the dentry */
1275 if (tid_fd_revalidate(dentry, NULL))
1278 put_task_struct(task);
1283 spin_unlock(&files->file_lock);
1284 put_files_struct(files);
1290 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1292 struct dentry *dentry = filp->f_dentry;
1293 struct inode *inode = dentry->d_inode;
1294 struct task_struct *p = get_proc_task(inode);
1295 unsigned int fd, tid, ino;
1297 char buf[PROC_NUMBUF];
1298 struct files_struct * files;
1299 struct fdtable *fdt;
1310 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1314 ino = parent_ino(dentry);
1315 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1319 files = get_files_struct(p);
1323 fdt = files_fdtable(files);
1324 for (fd = filp->f_pos-2;
1326 fd++, filp->f_pos++) {
1329 if (!fcheck_files(files, fd))
1337 buf[j] = '0' + (i % 10);
1341 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1342 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1349 put_files_struct(files);
1357 static struct file_operations proc_fd_operations = {
1358 .read = generic_read_dir,
1359 .readdir = proc_readfd,
1363 * proc directories can do almost nothing..
1365 static struct inode_operations proc_fd_inode_operations = {
1366 .lookup = proc_lookupfd,
1367 .setattr = proc_setattr,
1370 static struct file_operations proc_task_operations;
1371 static struct inode_operations proc_task_inode_operations;
1373 #ifdef CONFIG_SECURITY
1374 static struct file_operations proc_pid_attr_operations;
1375 static struct file_operations proc_tid_attr_operations;
1376 static struct inode_operations proc_tid_attr_inode_operations;
1377 static struct file_operations proc_tgid_attr_operations;
1378 static struct inode_operations proc_tgid_attr_inode_operations;
1382 static struct dentry *proc_pident_lookup(struct inode *dir,
1383 struct dentry *dentry,
1384 struct pid_entry *ents)
1386 struct inode *inode;
1387 struct dentry *error;
1388 struct task_struct *task = get_proc_task(dir);
1389 struct pid_entry *p;
1390 struct proc_inode *ei;
1392 error = ERR_PTR(-ENOENT);
1398 for (p = ents; p->name; p++) {
1399 if (p->len != dentry->d_name.len)
1401 if (!memcmp(dentry->d_name.name, p->name, p->len))
1407 error = ERR_PTR(-EINVAL);
1408 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1413 inode->i_mode = p->mode;
1415 * Yes, it does not scale. And it should not. Don't add
1416 * new entries into /proc/<tgid>/ without very good reasons.
1419 case PROC_TGID_TASK:
1421 inode->i_op = &proc_task_inode_operations;
1422 inode->i_fop = &proc_task_operations;
1427 inode->i_op = &proc_fd_inode_operations;
1428 inode->i_fop = &proc_fd_operations;
1432 inode->i_op = &proc_pid_link_inode_operations;
1433 ei->op.proc_get_link = proc_exe_link;
1437 inode->i_op = &proc_pid_link_inode_operations;
1438 ei->op.proc_get_link = proc_cwd_link;
1441 case PROC_TGID_ROOT:
1442 inode->i_op = &proc_pid_link_inode_operations;
1443 ei->op.proc_get_link = proc_root_link;
1445 case PROC_TID_ENVIRON:
1446 case PROC_TGID_ENVIRON:
1447 inode->i_fop = &proc_info_file_operations;
1448 ei->op.proc_read = proc_pid_environ;
1451 case PROC_TGID_AUXV:
1452 inode->i_fop = &proc_info_file_operations;
1453 ei->op.proc_read = proc_pid_auxv;
1455 case PROC_TID_STATUS:
1456 case PROC_TGID_STATUS:
1457 inode->i_fop = &proc_info_file_operations;
1458 ei->op.proc_read = proc_pid_status;
1461 inode->i_fop = &proc_info_file_operations;
1462 ei->op.proc_read = proc_tid_stat;
1464 case PROC_TGID_STAT:
1465 inode->i_fop = &proc_info_file_operations;
1466 ei->op.proc_read = proc_tgid_stat;
1468 case PROC_TID_CMDLINE:
1469 case PROC_TGID_CMDLINE:
1470 inode->i_fop = &proc_info_file_operations;
1471 ei->op.proc_read = proc_pid_cmdline;
1473 case PROC_TID_STATM:
1474 case PROC_TGID_STATM:
1475 inode->i_fop = &proc_info_file_operations;
1476 ei->op.proc_read = proc_pid_statm;
1479 case PROC_TGID_MAPS:
1480 inode->i_fop = &proc_maps_operations;
1483 case PROC_TID_NUMA_MAPS:
1484 case PROC_TGID_NUMA_MAPS:
1485 inode->i_fop = &proc_numa_maps_operations;
1490 inode->i_fop = &proc_mem_operations;
1492 #ifdef CONFIG_SECCOMP
1493 case PROC_TID_SECCOMP:
1494 case PROC_TGID_SECCOMP:
1495 inode->i_fop = &proc_seccomp_operations;
1497 #endif /* CONFIG_SECCOMP */
1498 case PROC_TID_MOUNTS:
1499 case PROC_TGID_MOUNTS:
1500 inode->i_fop = &proc_mounts_operations;
1503 case PROC_TID_SMAPS:
1504 case PROC_TGID_SMAPS:
1505 inode->i_fop = &proc_smaps_operations;
1508 case PROC_TID_MOUNTSTATS:
1509 case PROC_TGID_MOUNTSTATS:
1510 inode->i_fop = &proc_mountstats_operations;
1512 #ifdef CONFIG_SECURITY
1515 inode->i_op = &proc_tid_attr_inode_operations;
1516 inode->i_fop = &proc_tid_attr_operations;
1518 case PROC_TGID_ATTR:
1520 inode->i_op = &proc_tgid_attr_inode_operations;
1521 inode->i_fop = &proc_tgid_attr_operations;
1523 case PROC_TID_ATTR_CURRENT:
1524 case PROC_TGID_ATTR_CURRENT:
1525 case PROC_TID_ATTR_PREV:
1526 case PROC_TGID_ATTR_PREV:
1527 case PROC_TID_ATTR_EXEC:
1528 case PROC_TGID_ATTR_EXEC:
1529 case PROC_TID_ATTR_FSCREATE:
1530 case PROC_TGID_ATTR_FSCREATE:
1531 case PROC_TID_ATTR_KEYCREATE:
1532 case PROC_TGID_ATTR_KEYCREATE:
1533 case PROC_TID_ATTR_SOCKCREATE:
1534 case PROC_TGID_ATTR_SOCKCREATE:
1535 inode->i_fop = &proc_pid_attr_operations;
1538 #ifdef CONFIG_KALLSYMS
1539 case PROC_TID_WCHAN:
1540 case PROC_TGID_WCHAN:
1541 inode->i_fop = &proc_info_file_operations;
1542 ei->op.proc_read = proc_pid_wchan;
1545 #ifdef CONFIG_SCHEDSTATS
1546 case PROC_TID_SCHEDSTAT:
1547 case PROC_TGID_SCHEDSTAT:
1548 inode->i_fop = &proc_info_file_operations;
1549 ei->op.proc_read = proc_pid_schedstat;
1552 #ifdef CONFIG_CPUSETS
1553 case PROC_TID_CPUSET:
1554 case PROC_TGID_CPUSET:
1555 inode->i_fop = &proc_cpuset_operations;
1558 case PROC_TID_OOM_SCORE:
1559 case PROC_TGID_OOM_SCORE:
1560 inode->i_fop = &proc_info_file_operations;
1561 ei->op.proc_read = proc_oom_score;
1563 case PROC_TID_OOM_ADJUST:
1564 case PROC_TGID_OOM_ADJUST:
1565 inode->i_fop = &proc_oom_adjust_operations;
1567 #ifdef CONFIG_AUDITSYSCALL
1568 case PROC_TID_LOGINUID:
1569 case PROC_TGID_LOGINUID:
1570 inode->i_fop = &proc_loginuid_operations;
1574 printk("procfs: impossible type (%d)",p->type);
1576 error = ERR_PTR(-EINVAL);
1579 dentry->d_op = &pid_dentry_operations;
1580 d_add(dentry, inode);
1581 /* Close the race of the process dying before we return the dentry */
1582 if (pid_revalidate(dentry, NULL))
1585 put_task_struct(task);
1590 static int proc_pident_readdir(struct file *filp,
1591 void *dirent, filldir_t filldir,
1592 struct pid_entry *ents, unsigned int nents)
1596 struct dentry *dentry = filp->f_dentry;
1597 struct inode *inode = dentry->d_inode;
1598 struct task_struct *task = get_proc_task(inode);
1599 struct pid_entry *p;
1609 put_task_struct(task);
1614 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1620 ino = parent_ino(dentry);
1621 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1634 if (filldir(dirent, p->name, p->len, filp->f_pos,
1635 fake_ino(pid, p->type), p->mode >> 12) < 0)
1647 #ifdef CONFIG_SECURITY
1648 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1649 size_t count, loff_t *ppos)
1651 struct inode * inode = file->f_dentry->d_inode;
1654 struct task_struct *task = get_proc_task(inode);
1660 if (count > PAGE_SIZE)
1663 if (!(page = __get_free_page(GFP_KERNEL)))
1666 length = security_getprocattr(task,
1667 (char*)file->f_dentry->d_name.name,
1668 (void*)page, count);
1670 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1673 put_task_struct(task);
1678 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1679 size_t count, loff_t *ppos)
1681 struct inode * inode = file->f_dentry->d_inode;
1684 struct task_struct *task = get_proc_task(inode);
1689 if (count > PAGE_SIZE)
1692 /* No partial writes. */
1698 page = (char*)__get_free_page(GFP_USER);
1703 if (copy_from_user(page, buf, count))
1706 length = security_setprocattr(task,
1707 (char*)file->f_dentry->d_name.name,
1708 (void*)page, count);
1710 free_page((unsigned long) page);
1712 put_task_struct(task);
1717 static struct file_operations proc_pid_attr_operations = {
1718 .read = proc_pid_attr_read,
1719 .write = proc_pid_attr_write,
1722 static struct pid_entry tgid_attr_stuff[] = {
1723 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
1724 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
1725 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
1726 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
1727 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
1728 E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
1731 static struct pid_entry tid_attr_stuff[] = {
1732 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
1733 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
1734 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
1735 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
1736 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
1737 E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
1741 static int proc_tgid_attr_readdir(struct file * filp,
1742 void * dirent, filldir_t filldir)
1744 return proc_pident_readdir(filp,dirent,filldir,
1745 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1748 static int proc_tid_attr_readdir(struct file * filp,
1749 void * dirent, filldir_t filldir)
1751 return proc_pident_readdir(filp,dirent,filldir,
1752 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1755 static struct file_operations proc_tgid_attr_operations = {
1756 .read = generic_read_dir,
1757 .readdir = proc_tgid_attr_readdir,
1760 static struct file_operations proc_tid_attr_operations = {
1761 .read = generic_read_dir,
1762 .readdir = proc_tid_attr_readdir,
1765 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1766 struct dentry *dentry, struct nameidata *nd)
1768 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1771 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1772 struct dentry *dentry, struct nameidata *nd)
1774 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1777 static struct inode_operations proc_tgid_attr_inode_operations = {
1778 .lookup = proc_tgid_attr_lookup,
1779 .getattr = pid_getattr,
1780 .setattr = proc_setattr,
1783 static struct inode_operations proc_tid_attr_inode_operations = {
1784 .lookup = proc_tid_attr_lookup,
1785 .getattr = pid_getattr,
1786 .setattr = proc_setattr,
1793 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1796 char tmp[PROC_NUMBUF];
1797 sprintf(tmp, "%d", current->tgid);
1798 return vfs_readlink(dentry,buffer,buflen,tmp);
1801 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1803 char tmp[PROC_NUMBUF];
1804 sprintf(tmp, "%d", current->tgid);
1805 return ERR_PTR(vfs_follow_link(nd,tmp));
1808 static struct inode_operations proc_self_inode_operations = {
1809 .readlink = proc_self_readlink,
1810 .follow_link = proc_self_follow_link,
1816 static struct pid_entry tgid_base_stuff[] = {
1817 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
1818 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
1819 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
1820 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
1821 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
1822 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
1823 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
1824 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
1825 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
1827 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
1829 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
1830 #ifdef CONFIG_SECCOMP
1831 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
1833 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
1834 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
1835 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
1836 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
1837 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
1839 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
1841 #ifdef CONFIG_SECURITY
1842 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
1844 #ifdef CONFIG_KALLSYMS
1845 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
1847 #ifdef CONFIG_SCHEDSTATS
1848 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
1850 #ifdef CONFIG_CPUSETS
1851 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
1853 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
1854 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
1855 #ifdef CONFIG_AUDITSYSCALL
1856 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
1861 static int proc_tgid_base_readdir(struct file * filp,
1862 void * dirent, filldir_t filldir)
1864 return proc_pident_readdir(filp,dirent,filldir,
1865 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1868 static struct file_operations proc_tgid_base_operations = {
1869 .read = generic_read_dir,
1870 .readdir = proc_tgid_base_readdir,
1873 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1874 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1877 static struct inode_operations proc_tgid_base_inode_operations = {
1878 .lookup = proc_tgid_base_lookup,
1879 .getattr = pid_getattr,
1880 .setattr = proc_setattr,
1884 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1886 * @task: task that should be flushed.
1888 * Looks in the dcache for
1890 * /proc/@tgid/task/@pid
1891 * if either directory is present flushes it and all of it'ts children
1894 * It is safe and reasonable to cache /proc entries for a task until
1895 * that task exits. After that they just clog up the dcache with
1896 * useless entries, possibly causing useful dcache entries to be
1897 * flushed instead. This routine is proved to flush those useless
1898 * dcache entries at process exit time.
1900 * NOTE: This routine is just an optimization so it does not guarantee
1901 * that no dcache entries will exist at process exit time it
1902 * just makes it very unlikely that any will persist.
1904 void proc_flush_task(struct task_struct *task)
1906 struct dentry *dentry, *leader, *dir;
1907 char buf[PROC_NUMBUF];
1911 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1912 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1914 shrink_dcache_parent(dentry);
1919 if (thread_group_leader(task))
1923 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1924 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1929 name.len = strlen(name.name);
1930 dir = d_hash_and_lookup(leader, &name);
1932 goto out_put_leader;
1935 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1936 dentry = d_hash_and_lookup(dir, &name);
1938 shrink_dcache_parent(dentry);
1951 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1953 struct dentry *result = ERR_PTR(-ENOENT);
1954 struct task_struct *task;
1955 struct inode *inode;
1956 struct proc_inode *ei;
1959 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1960 inode = new_inode(dir->i_sb);
1962 return ERR_PTR(-ENOMEM);
1964 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1965 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1967 inode->i_mode = S_IFLNK|S_IRWXUGO;
1968 inode->i_uid = inode->i_gid = 0;
1970 inode->i_op = &proc_self_inode_operations;
1971 d_add(dentry, inode);
1974 tgid = name_to_int(dentry);
1979 task = find_task_by_pid(tgid);
1981 get_task_struct(task);
1986 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1990 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1991 inode->i_op = &proc_tgid_base_inode_operations;
1992 inode->i_fop = &proc_tgid_base_operations;
1993 inode->i_flags|=S_IMMUTABLE;
1994 #ifdef CONFIG_SECURITY
2000 dentry->d_op = &pid_dentry_operations;
2002 d_add(dentry, inode);
2003 /* Close the race of the process dying before we return the dentry */
2004 if (pid_revalidate(dentry, NULL))
2008 put_task_struct(task);
2014 * Find the first task with tgid >= tgid
2017 static struct task_struct *next_tgid(unsigned int tgid)
2019 struct task_struct *task;
2025 pid = find_ge_pid(tgid);
2028 task = pid_task(pid, PIDTYPE_PID);
2029 /* What we to know is if the pid we have find is the
2030 * pid of a thread_group_leader. Testing for task
2031 * being a thread_group_leader is the obvious thing
2032 * todo but there is a window when it fails, due to
2033 * the pid transfer logic in de_thread.
2035 * So we perform the straight forward test of seeing
2036 * if the pid we have found is the pid of a thread
2037 * group leader, and don't worry if the task we have
2038 * found doesn't happen to be a thread group leader.
2039 * As we don't care in the case of readdir.
2041 if (!task || !has_group_leader_pid(task))
2043 get_task_struct(task);
2049 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
2051 /* for the /proc/ directory itself, after non-process stuff has been done */
2052 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2054 char buf[PROC_NUMBUF];
2055 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2056 struct task_struct *task;
2060 ino_t ino = fake_ino(0,PROC_TGID_INO);
2061 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2067 tgid = filp->f_pos - TGID_OFFSET;
2068 for (task = next_tgid(tgid);
2070 put_task_struct(task), task = next_tgid(tgid + 1)) {
2074 filp->f_pos = tgid + TGID_OFFSET;
2075 len = snprintf(buf, sizeof(buf), "%d", tgid);
2076 ino = fake_ino(tgid, PROC_TGID_INO);
2077 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2078 put_task_struct(task);
2082 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2090 static struct pid_entry tid_base_stuff[] = {
2091 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
2092 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
2093 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
2094 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
2095 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
2096 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
2097 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
2098 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
2100 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
2102 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
2103 #ifdef CONFIG_SECCOMP
2104 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
2106 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
2107 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
2108 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
2109 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
2111 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
2113 #ifdef CONFIG_SECURITY
2114 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
2116 #ifdef CONFIG_KALLSYMS
2117 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
2119 #ifdef CONFIG_SCHEDSTATS
2120 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
2122 #ifdef CONFIG_CPUSETS
2123 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
2125 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
2126 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
2127 #ifdef CONFIG_AUDITSYSCALL
2128 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
2133 static int proc_tid_base_readdir(struct file * filp,
2134 void * dirent, filldir_t filldir)
2136 return proc_pident_readdir(filp,dirent,filldir,
2137 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2140 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2141 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2144 static struct file_operations proc_tid_base_operations = {
2145 .read = generic_read_dir,
2146 .readdir = proc_tid_base_readdir,
2149 static struct inode_operations proc_tid_base_inode_operations = {
2150 .lookup = proc_tid_base_lookup,
2151 .getattr = pid_getattr,
2152 .setattr = proc_setattr,
2156 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2158 struct dentry *result = ERR_PTR(-ENOENT);
2159 struct task_struct *task;
2160 struct task_struct *leader = get_proc_task(dir);
2161 struct inode *inode;
2167 tid = name_to_int(dentry);
2172 task = find_task_by_pid(tid);
2174 get_task_struct(task);
2178 if (leader->tgid != task->tgid)
2181 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2186 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2187 inode->i_op = &proc_tid_base_inode_operations;
2188 inode->i_fop = &proc_tid_base_operations;
2189 inode->i_flags|=S_IMMUTABLE;
2190 #ifdef CONFIG_SECURITY
2196 dentry->d_op = &pid_dentry_operations;
2198 d_add(dentry, inode);
2199 /* Close the race of the process dying before we return the dentry */
2200 if (pid_revalidate(dentry, NULL))
2204 put_task_struct(task);
2206 put_task_struct(leader);
2212 * Find the first tid of a thread group to return to user space.
2214 * Usually this is just the thread group leader, but if the users
2215 * buffer was too small or there was a seek into the middle of the
2216 * directory we have more work todo.
2218 * In the case of a short read we start with find_task_by_pid.
2220 * In the case of a seek we start with the leader and walk nr
2223 static struct task_struct *first_tid(struct task_struct *leader,
2226 struct task_struct *pos;
2229 /* Attempt to start with the pid of a thread */
2230 if (tid && (nr > 0)) {
2231 pos = find_task_by_pid(tid);
2232 if (pos && (pos->group_leader == leader))
2236 /* If nr exceeds the number of threads there is nothing todo */
2238 if (nr && nr >= get_nr_threads(leader))
2241 /* If we haven't found our starting place yet start
2242 * with the leader and walk nr threads forward.
2244 for (pos = leader; nr > 0; --nr) {
2245 pos = next_thread(pos);
2246 if (pos == leader) {
2252 get_task_struct(pos);
2259 * Find the next thread in the thread list.
2260 * Return NULL if there is an error or no next thread.
2262 * The reference to the input task_struct is released.
2264 static struct task_struct *next_tid(struct task_struct *start)
2266 struct task_struct *pos = NULL;
2268 if (pid_alive(start)) {
2269 pos = next_thread(start);
2270 if (thread_group_leader(pos))
2273 get_task_struct(pos);
2276 put_task_struct(start);
2280 /* for the /proc/TGID/task/ directories */
2281 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2283 char buf[PROC_NUMBUF];
2284 struct dentry *dentry = filp->f_dentry;
2285 struct inode *inode = dentry->d_inode;
2286 struct task_struct *leader = get_proc_task(inode);
2287 struct task_struct *task;
2288 int retval = -ENOENT;
2291 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2300 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2305 ino = parent_ino(dentry);
2306 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2312 /* f_version caches the tgid value that the last readdir call couldn't
2313 * return. lseek aka telldir automagically resets f_version to 0.
2315 tid = filp->f_version;
2316 filp->f_version = 0;
2317 for (task = first_tid(leader, tid, pos - 2);
2319 task = next_tid(task), pos++) {
2322 len = snprintf(buf, sizeof(buf), "%d", tid);
2323 ino = fake_ino(tid, PROC_TID_INO);
2324 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2325 /* returning this tgid failed, save it as the first
2326 * pid for the next readir call */
2327 filp->f_version = tid;
2328 put_task_struct(task);
2334 put_task_struct(leader);
2339 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2341 struct inode *inode = dentry->d_inode;
2342 struct task_struct *p = get_proc_task(inode);
2343 generic_fillattr(inode, stat);
2347 stat->nlink += get_nr_threads(p);
2355 static struct inode_operations proc_task_inode_operations = {
2356 .lookup = proc_task_lookup,
2357 .getattr = proc_task_getattr,
2358 .setattr = proc_setattr,
2361 static struct file_operations proc_task_operations = {
2362 .read = generic_read_dir,
2363 .readdir = proc_task_readdir,