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>
74 #include <linux/nsproxy.h>
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
95 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
97 enum pid_directory_inos {
102 #ifdef CONFIG_SECCOMP
117 PROC_TGID_MOUNTSTATS,
122 #ifdef CONFIG_SCHEDSTATS
125 #ifdef CONFIG_CPUSETS
128 #ifdef CONFIG_SECURITY
130 PROC_TGID_ATTR_CURRENT,
133 PROC_TGID_ATTR_FSCREATE,
134 PROC_TGID_ATTR_KEYCREATE,
135 PROC_TGID_ATTR_SOCKCREATE,
137 #ifdef CONFIG_AUDITSYSCALL
141 PROC_TGID_OOM_ADJUST,
145 #ifdef CONFIG_SECCOMP
165 #ifdef CONFIG_SCHEDSTATS
168 #ifdef CONFIG_CPUSETS
171 #ifdef CONFIG_SECURITY
173 PROC_TID_ATTR_CURRENT,
176 PROC_TID_ATTR_FSCREATE,
177 PROC_TID_ATTR_KEYCREATE,
178 PROC_TID_ATTR_SOCKCREATE,
180 #ifdef CONFIG_AUDITSYSCALL
186 /* Add new entries before this */
187 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
190 /* Worst case buffer size needed for holding an integer. */
191 #define PROC_NUMBUF 10
198 struct inode_operations *iop;
199 struct file_operations *fop;
203 #define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
205 .len = sizeof(NAME) - 1, \
213 #define DIR(TYPE, NAME, MODE, OTYPE) \
214 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
215 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
217 #define LNK(TYPE, NAME, OTYPE) \
218 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
219 &proc_pid_link_inode_operations, NULL, \
220 { .proc_get_link = &proc_##OTYPE##_link } )
221 #define REG(TYPE, NAME, MODE, OTYPE) \
222 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
223 &proc_##OTYPE##_operations, {})
224 #define INF(TYPE, NAME, MODE, OTYPE) \
225 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
226 NULL, &proc_info_file_operations, \
227 { .proc_read = &proc_##OTYPE } )
229 static struct fs_struct *get_fs_struct(struct task_struct *task)
231 struct fs_struct *fs;
235 atomic_inc(&fs->count);
240 static int get_nr_threads(struct task_struct *tsk)
242 /* Must be called with the rcu_read_lock held */
246 if (lock_task_sighand(tsk, &flags)) {
247 count = atomic_read(&tsk->signal->count);
248 unlock_task_sighand(tsk, &flags);
253 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
255 struct task_struct *task = get_proc_task(inode);
256 struct fs_struct *fs = NULL;
257 int result = -ENOENT;
260 fs = get_fs_struct(task);
261 put_task_struct(task);
264 read_lock(&fs->lock);
265 *mnt = mntget(fs->pwdmnt);
266 *dentry = dget(fs->pwd);
267 read_unlock(&fs->lock);
274 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
276 struct task_struct *task = get_proc_task(inode);
277 struct fs_struct *fs = NULL;
278 int result = -ENOENT;
281 fs = get_fs_struct(task);
282 put_task_struct(task);
285 read_lock(&fs->lock);
286 *mnt = mntget(fs->rootmnt);
287 *dentry = dget(fs->root);
288 read_unlock(&fs->lock);
295 #define MAY_PTRACE(task) \
296 (task == current || \
297 (task->parent == current && \
298 (task->ptrace & PT_PTRACED) && \
299 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
300 security_ptrace(current,task) == 0))
302 static int proc_pid_environ(struct task_struct *task, char * buffer)
305 struct mm_struct *mm = get_task_mm(task);
307 unsigned int len = mm->env_end - mm->env_start;
310 res = access_process_vm(task, mm->env_start, buffer, len, 0);
311 if (!ptrace_may_attach(task))
318 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
322 struct mm_struct *mm = get_task_mm(task);
326 goto out_mm; /* Shh! No looking before we're done */
328 len = mm->arg_end - mm->arg_start;
333 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
335 // If the nul at the end of args has been overwritten, then
336 // assume application is using setproctitle(3).
337 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
338 len = strnlen(buffer, res);
342 len = mm->env_end - mm->env_start;
343 if (len > PAGE_SIZE - res)
344 len = PAGE_SIZE - res;
345 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
346 res = strnlen(buffer, res);
355 static int proc_pid_auxv(struct task_struct *task, char *buffer)
358 struct mm_struct *mm = get_task_mm(task);
360 unsigned int nwords = 0;
363 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
364 res = nwords * sizeof(mm->saved_auxv[0]);
367 memcpy(buffer, mm->saved_auxv, res);
374 #ifdef CONFIG_KALLSYMS
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
379 static int proc_pid_wchan(struct task_struct *task, char *buffer)
382 const char *sym_name;
383 unsigned long wchan, size, offset;
384 char namebuf[KSYM_NAME_LEN+1];
386 wchan = get_wchan(task);
388 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
390 return sprintf(buffer, "%s", sym_name);
391 return sprintf(buffer, "%lu", wchan);
393 #endif /* CONFIG_KALLSYMS */
395 #ifdef CONFIG_SCHEDSTATS
397 * Provides /proc/PID/schedstat
399 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
401 return sprintf(buffer, "%lu %lu %lu\n",
402 task->sched_info.cpu_time,
403 task->sched_info.run_delay,
404 task->sched_info.pcnt);
408 /* The badness from the OOM killer */
409 unsigned long badness(struct task_struct *p, unsigned long uptime);
410 static int proc_oom_score(struct task_struct *task, char *buffer)
412 unsigned long points;
413 struct timespec uptime;
415 do_posix_clock_monotonic_gettime(&uptime);
416 points = badness(task, uptime.tv_sec);
417 return sprintf(buffer, "%lu\n", points);
420 /************************************************************************/
421 /* Here the fs part begins */
422 /************************************************************************/
424 /* permission checks */
425 static int proc_fd_access_allowed(struct inode *inode)
427 struct task_struct *task;
429 /* Allow access to a task's file descriptors if it is us or we
430 * may use ptrace attach to the process and find out that
433 task = get_proc_task(inode);
435 allowed = ptrace_may_attach(task);
436 put_task_struct(task);
441 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
444 struct inode *inode = dentry->d_inode;
446 if (attr->ia_valid & ATTR_MODE)
449 error = inode_change_ok(inode, attr);
451 error = security_inode_setattr(dentry, attr);
453 error = inode_setattr(inode, attr);
458 static struct inode_operations proc_def_inode_operations = {
459 .setattr = proc_setattr,
462 extern struct seq_operations mounts_op;
468 static int mounts_open(struct inode *inode, struct file *file)
470 struct task_struct *task = get_proc_task(inode);
471 struct namespace *namespace = NULL;
472 struct proc_mounts *p;
477 namespace = task->nsproxy->namespace;
479 get_namespace(namespace);
481 put_task_struct(task);
486 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
488 file->private_data = &p->m;
489 ret = seq_open(file, &mounts_op);
491 p->m.private = namespace;
492 p->event = namespace->event;
497 put_namespace(namespace);
502 static int mounts_release(struct inode *inode, struct file *file)
504 struct seq_file *m = file->private_data;
505 struct namespace *namespace = m->private;
506 put_namespace(namespace);
507 return seq_release(inode, file);
510 static unsigned mounts_poll(struct file *file, poll_table *wait)
512 struct proc_mounts *p = file->private_data;
513 struct namespace *ns = p->m.private;
516 poll_wait(file, &ns->poll, wait);
518 spin_lock(&vfsmount_lock);
519 if (p->event != ns->event) {
520 p->event = ns->event;
523 spin_unlock(&vfsmount_lock);
528 static struct file_operations proc_mounts_operations = {
532 .release = mounts_release,
536 extern struct seq_operations mountstats_op;
537 static int mountstats_open(struct inode *inode, struct file *file)
539 int ret = seq_open(file, &mountstats_op);
542 struct seq_file *m = file->private_data;
543 struct namespace *namespace = NULL;
544 struct task_struct *task = get_proc_task(inode);
548 namespace = task->nsproxy->namespace;
550 get_namespace(namespace);
552 put_task_struct(task);
556 m->private = namespace;
558 seq_release(inode, file);
565 static struct file_operations proc_mountstats_operations = {
566 .open = mountstats_open,
569 .release = mounts_release,
572 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
574 static ssize_t proc_info_read(struct file * file, char __user * buf,
575 size_t count, loff_t *ppos)
577 struct inode * inode = file->f_dentry->d_inode;
580 struct task_struct *task = get_proc_task(inode);
586 if (count > PROC_BLOCK_SIZE)
587 count = PROC_BLOCK_SIZE;
590 if (!(page = __get_free_page(GFP_KERNEL)))
593 length = PROC_I(inode)->op.proc_read(task, (char*)page);
596 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
599 put_task_struct(task);
604 static struct file_operations proc_info_file_operations = {
605 .read = proc_info_read,
608 static int mem_open(struct inode* inode, struct file* file)
610 file->private_data = (void*)((long)current->self_exec_id);
614 static ssize_t mem_read(struct file * file, char __user * buf,
615 size_t count, loff_t *ppos)
617 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
619 unsigned long src = *ppos;
621 struct mm_struct *mm;
626 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
630 page = (char *)__get_free_page(GFP_USER);
636 mm = get_task_mm(task);
642 if (file->private_data != (void*)((long)current->self_exec_id))
648 int this_len, retval;
650 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
651 retval = access_process_vm(task, src, page, this_len, 0);
652 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
658 if (copy_to_user(buf, page, retval)) {
673 free_page((unsigned long) page);
675 put_task_struct(task);
680 #define mem_write NULL
683 /* This is a security hazard */
684 static ssize_t mem_write(struct file * file, const char * buf,
685 size_t count, loff_t *ppos)
689 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
690 unsigned long dst = *ppos;
696 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
700 page = (char *)__get_free_page(GFP_USER);
706 int this_len, retval;
708 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
709 if (copy_from_user(page, buf, this_len)) {
713 retval = access_process_vm(task, dst, page, this_len, 1);
725 free_page((unsigned long) page);
727 put_task_struct(task);
733 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
737 file->f_pos = offset;
740 file->f_pos += offset;
745 force_successful_syscall_return();
749 static struct file_operations proc_mem_operations = {
756 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
757 size_t count, loff_t *ppos)
759 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
760 char buffer[PROC_NUMBUF];
763 loff_t __ppos = *ppos;
767 oom_adjust = task->oomkilladj;
768 put_task_struct(task);
770 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
773 if (count > len-__ppos)
775 if (copy_to_user(buf, buffer + __ppos, count))
777 *ppos = __ppos + count;
781 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
782 size_t count, loff_t *ppos)
784 struct task_struct *task;
785 char buffer[PROC_NUMBUF], *end;
788 if (!capable(CAP_SYS_RESOURCE))
790 memset(buffer, 0, sizeof(buffer));
791 if (count > sizeof(buffer) - 1)
792 count = sizeof(buffer) - 1;
793 if (copy_from_user(buffer, buf, count))
795 oom_adjust = simple_strtol(buffer, &end, 0);
796 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
800 task = get_proc_task(file->f_dentry->d_inode);
803 task->oomkilladj = oom_adjust;
804 put_task_struct(task);
805 if (end - buffer == 0)
810 static struct file_operations proc_oom_adjust_operations = {
811 .read = oom_adjust_read,
812 .write = oom_adjust_write,
815 #ifdef CONFIG_AUDITSYSCALL
817 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
818 size_t count, loff_t *ppos)
820 struct inode * inode = file->f_dentry->d_inode;
821 struct task_struct *task = get_proc_task(inode);
823 char tmpbuf[TMPBUFLEN];
827 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
828 audit_get_loginuid(task->audit_context));
829 put_task_struct(task);
830 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
833 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
834 size_t count, loff_t *ppos)
836 struct inode * inode = file->f_dentry->d_inode;
841 if (!capable(CAP_AUDIT_CONTROL))
844 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
847 if (count >= PAGE_SIZE)
848 count = PAGE_SIZE - 1;
851 /* No partial writes. */
854 page = (char*)__get_free_page(GFP_USER);
858 if (copy_from_user(page, buf, count))
862 loginuid = simple_strtoul(page, &tmp, 10);
868 length = audit_set_loginuid(current, loginuid);
869 if (likely(length == 0))
873 free_page((unsigned long) page);
877 static struct file_operations proc_loginuid_operations = {
878 .read = proc_loginuid_read,
879 .write = proc_loginuid_write,
883 #ifdef CONFIG_SECCOMP
884 static ssize_t seccomp_read(struct file *file, char __user *buf,
885 size_t count, loff_t *ppos)
887 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
889 loff_t __ppos = *ppos;
894 /* no need to print the trailing zero, so use only len */
895 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
896 put_task_struct(tsk);
899 if (count > len - __ppos)
900 count = len - __ppos;
901 if (copy_to_user(buf, __buf + __ppos, count))
903 *ppos = __ppos + count;
907 static ssize_t seccomp_write(struct file *file, const char __user *buf,
908 size_t count, loff_t *ppos)
910 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
911 char __buf[20], *end;
912 unsigned int seccomp_mode;
919 /* can set it only once to be even more secure */
921 if (unlikely(tsk->seccomp.mode))
925 memset(__buf, 0, sizeof(__buf));
926 count = min(count, sizeof(__buf) - 1);
927 if (copy_from_user(__buf, buf, count))
930 seccomp_mode = simple_strtoul(__buf, &end, 0);
934 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
935 tsk->seccomp.mode = seccomp_mode;
936 set_tsk_thread_flag(tsk, TIF_SECCOMP);
940 if (unlikely(!(end - __buf)))
942 result = end - __buf;
944 put_task_struct(tsk);
949 static struct file_operations proc_seccomp_operations = {
950 .read = seccomp_read,
951 .write = seccomp_write,
953 #endif /* CONFIG_SECCOMP */
955 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
957 struct inode *inode = dentry->d_inode;
960 /* We don't need a base pointer in the /proc filesystem */
963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
970 return ERR_PTR(error);
973 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974 char __user *buffer, int buflen)
976 struct inode * inode;
977 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
983 inode = dentry->d_inode;
984 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
988 len = tmp + PAGE_SIZE - 1 - path;
992 if (copy_to_user(buffer, path, len))
995 free_page((unsigned long)tmp);
999 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1001 int error = -EACCES;
1002 struct inode *inode = dentry->d_inode;
1004 struct vfsmount *mnt = NULL;
1006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
1010 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1014 error = do_proc_readlink(de, mnt, buffer, buflen);
1021 static struct inode_operations proc_pid_link_inode_operations = {
1022 .readlink = proc_pid_readlink,
1023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
1028 /* building an inode */
1030 static int task_dumpable(struct task_struct *task)
1033 struct mm_struct *mm;
1038 dumpable = mm->dumpable;
1046 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1048 struct inode * inode;
1049 struct proc_inode *ei;
1051 /* We need a new inode */
1053 inode = new_inode(sb);
1059 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060 inode->i_ino = fake_ino(task->pid, ino);
1061 inode->i_op = &proc_def_inode_operations;
1064 * grab the reference to task.
1066 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1072 if (task_dumpable(task)) {
1073 inode->i_uid = task->euid;
1074 inode->i_gid = task->egid;
1076 security_task_to_inode(task, inode);
1086 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1088 struct inode *inode = dentry->d_inode;
1089 struct task_struct *task;
1090 generic_fillattr(inode, stat);
1095 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1097 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1098 task_dumpable(task)) {
1099 stat->uid = task->euid;
1100 stat->gid = task->egid;
1110 * Exceptional case: normally we are not allowed to unhash a busy
1111 * directory. In this case, however, we can do it - no aliasing problems
1112 * due to the way we treat inodes.
1114 * Rewrite the inode's ownerships here because the owning task may have
1115 * performed a setuid(), etc.
1117 * Before the /proc/pid/status file was created the only way to read
1118 * the effective uid of a /process was to stat /proc/pid. Reading
1119 * /proc/pid/status is slow enough that procps and other packages
1120 * kept stating /proc/pid. To keep the rules in /proc simple I have
1121 * made this apply to all per process world readable and executable
1124 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1126 struct inode *inode = dentry->d_inode;
1127 struct task_struct *task = get_proc_task(inode);
1129 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1130 task_dumpable(task)) {
1131 inode->i_uid = task->euid;
1132 inode->i_gid = task->egid;
1137 inode->i_mode &= ~(S_ISUID | S_ISGID);
1138 security_task_to_inode(task, inode);
1139 put_task_struct(task);
1146 static int pid_delete_dentry(struct dentry * dentry)
1148 /* Is the task we represent dead?
1149 * If so, then don't put the dentry on the lru list,
1150 * kill it immediately.
1152 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1155 static struct dentry_operations pid_dentry_operations =
1157 .d_revalidate = pid_revalidate,
1158 .d_delete = pid_delete_dentry,
1163 static unsigned name_to_int(struct dentry *dentry)
1165 const char *name = dentry->d_name.name;
1166 int len = dentry->d_name.len;
1169 if (len > 1 && *name == '0')
1172 unsigned c = *name++ - '0';
1175 if (n >= (~0U-9)/10)
1185 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1187 struct task_struct *task = get_proc_task(inode);
1188 struct files_struct *files = NULL;
1190 int fd = proc_fd(inode);
1193 files = get_files_struct(task);
1194 put_task_struct(task);
1198 * We are not taking a ref to the file structure, so we must
1201 spin_lock(&files->file_lock);
1202 file = fcheck_files(files, fd);
1204 *mnt = mntget(file->f_vfsmnt);
1205 *dentry = dget(file->f_dentry);
1206 spin_unlock(&files->file_lock);
1207 put_files_struct(files);
1210 spin_unlock(&files->file_lock);
1211 put_files_struct(files);
1216 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1218 struct inode *inode = dentry->d_inode;
1219 struct task_struct *task = get_proc_task(inode);
1220 int fd = proc_fd(inode);
1221 struct files_struct *files;
1224 files = get_files_struct(task);
1227 if (fcheck_files(files, fd)) {
1229 put_files_struct(files);
1230 if (task_dumpable(task)) {
1231 inode->i_uid = task->euid;
1232 inode->i_gid = task->egid;
1237 inode->i_mode &= ~(S_ISUID | S_ISGID);
1238 security_task_to_inode(task, inode);
1239 put_task_struct(task);
1243 put_files_struct(files);
1245 put_task_struct(task);
1251 static struct dentry_operations tid_fd_dentry_operations =
1253 .d_revalidate = tid_fd_revalidate,
1254 .d_delete = pid_delete_dentry,
1258 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1260 struct task_struct *task = get_proc_task(dir);
1261 unsigned fd = name_to_int(dentry);
1262 struct dentry *result = ERR_PTR(-ENOENT);
1264 struct files_struct * files;
1265 struct inode *inode;
1266 struct proc_inode *ei;
1273 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1278 files = get_files_struct(task);
1281 inode->i_mode = S_IFLNK;
1284 * We are not taking a ref to the file structure, so we must
1287 spin_lock(&files->file_lock);
1288 file = fcheck_files(files, fd);
1291 if (file->f_mode & 1)
1292 inode->i_mode |= S_IRUSR | S_IXUSR;
1293 if (file->f_mode & 2)
1294 inode->i_mode |= S_IWUSR | S_IXUSR;
1295 spin_unlock(&files->file_lock);
1296 put_files_struct(files);
1297 inode->i_op = &proc_pid_link_inode_operations;
1299 ei->op.proc_get_link = proc_fd_link;
1300 dentry->d_op = &tid_fd_dentry_operations;
1301 d_add(dentry, inode);
1302 /* Close the race of the process dying before we return the dentry */
1303 if (tid_fd_revalidate(dentry, NULL))
1306 put_task_struct(task);
1311 spin_unlock(&files->file_lock);
1312 put_files_struct(files);
1318 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1320 struct dentry *dentry = filp->f_dentry;
1321 struct inode *inode = dentry->d_inode;
1322 struct task_struct *p = get_proc_task(inode);
1323 unsigned int fd, tid, ino;
1325 char buf[PROC_NUMBUF];
1326 struct files_struct * files;
1327 struct fdtable *fdt;
1338 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1342 ino = parent_ino(dentry);
1343 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1347 files = get_files_struct(p);
1351 fdt = files_fdtable(files);
1352 for (fd = filp->f_pos-2;
1354 fd++, filp->f_pos++) {
1357 if (!fcheck_files(files, fd))
1365 buf[j] = '0' + (i % 10);
1369 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1370 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1377 put_files_struct(files);
1385 static struct file_operations proc_fd_operations = {
1386 .read = generic_read_dir,
1387 .readdir = proc_readfd,
1391 * proc directories can do almost nothing..
1393 static struct inode_operations proc_fd_inode_operations = {
1394 .lookup = proc_lookupfd,
1395 .setattr = proc_setattr,
1399 static struct dentry *proc_pident_lookup(struct inode *dir,
1400 struct dentry *dentry,
1401 struct pid_entry *ents)
1403 struct inode *inode;
1404 struct dentry *error;
1405 struct task_struct *task = get_proc_task(dir);
1406 struct pid_entry *p;
1407 struct proc_inode *ei;
1409 error = ERR_PTR(-ENOENT);
1416 * Yes, it does not scale. And it should not. Don't add
1417 * new entries into /proc/<tgid>/ without very good reasons.
1419 for (p = ents; p->name; p++) {
1420 if (p->len != dentry->d_name.len)
1422 if (!memcmp(dentry->d_name.name, p->name, p->len))
1428 error = ERR_PTR(-EINVAL);
1429 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1434 inode->i_mode = p->mode;
1435 if (S_ISDIR(inode->i_mode))
1436 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1438 inode->i_op = p->iop;
1440 inode->i_fop = p->fop;
1442 dentry->d_op = &pid_dentry_operations;
1443 d_add(dentry, inode);
1444 /* Close the race of the process dying before we return the dentry */
1445 if (pid_revalidate(dentry, NULL))
1448 put_task_struct(task);
1453 static int proc_pident_readdir(struct file *filp,
1454 void *dirent, filldir_t filldir,
1455 struct pid_entry *ents, unsigned int nents)
1459 struct dentry *dentry = filp->f_dentry;
1460 struct inode *inode = dentry->d_inode;
1461 struct task_struct *task = get_proc_task(inode);
1462 struct pid_entry *p;
1472 put_task_struct(task);
1477 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1483 ino = parent_ino(dentry);
1484 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1497 if (filldir(dirent, p->name, p->len, filp->f_pos,
1498 fake_ino(pid, p->type), p->mode >> 12) < 0)
1510 #ifdef CONFIG_SECURITY
1511 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1512 size_t count, loff_t *ppos)
1514 struct inode * inode = file->f_dentry->d_inode;
1517 struct task_struct *task = get_proc_task(inode);
1523 if (count > PAGE_SIZE)
1526 if (!(page = __get_free_page(GFP_KERNEL)))
1529 length = security_getprocattr(task,
1530 (char*)file->f_dentry->d_name.name,
1531 (void*)page, count);
1533 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1536 put_task_struct(task);
1541 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1542 size_t count, loff_t *ppos)
1544 struct inode * inode = file->f_dentry->d_inode;
1547 struct task_struct *task = get_proc_task(inode);
1552 if (count > PAGE_SIZE)
1555 /* No partial writes. */
1561 page = (char*)__get_free_page(GFP_USER);
1566 if (copy_from_user(page, buf, count))
1569 length = security_setprocattr(task,
1570 (char*)file->f_dentry->d_name.name,
1571 (void*)page, count);
1573 free_page((unsigned long) page);
1575 put_task_struct(task);
1580 static struct file_operations proc_pid_attr_operations = {
1581 .read = proc_pid_attr_read,
1582 .write = proc_pid_attr_write,
1585 static struct pid_entry tgid_attr_stuff[] = {
1586 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1591 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1594 static struct pid_entry tid_attr_stuff[] = {
1595 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1596 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1597 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1600 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1604 static int proc_tgid_attr_readdir(struct file * filp,
1605 void * dirent, filldir_t filldir)
1607 return proc_pident_readdir(filp,dirent,filldir,
1608 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1611 static int proc_tid_attr_readdir(struct file * filp,
1612 void * dirent, filldir_t filldir)
1614 return proc_pident_readdir(filp,dirent,filldir,
1615 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1618 static struct file_operations proc_tgid_attr_operations = {
1619 .read = generic_read_dir,
1620 .readdir = proc_tgid_attr_readdir,
1623 static struct file_operations proc_tid_attr_operations = {
1624 .read = generic_read_dir,
1625 .readdir = proc_tid_attr_readdir,
1628 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1629 struct dentry *dentry, struct nameidata *nd)
1631 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1634 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1635 struct dentry *dentry, struct nameidata *nd)
1637 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1640 static struct inode_operations proc_tgid_attr_inode_operations = {
1641 .lookup = proc_tgid_attr_lookup,
1642 .getattr = pid_getattr,
1643 .setattr = proc_setattr,
1646 static struct inode_operations proc_tid_attr_inode_operations = {
1647 .lookup = proc_tid_attr_lookup,
1648 .getattr = pid_getattr,
1649 .setattr = proc_setattr,
1656 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1659 char tmp[PROC_NUMBUF];
1660 sprintf(tmp, "%d", current->tgid);
1661 return vfs_readlink(dentry,buffer,buflen,tmp);
1664 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1666 char tmp[PROC_NUMBUF];
1667 sprintf(tmp, "%d", current->tgid);
1668 return ERR_PTR(vfs_follow_link(nd,tmp));
1671 static struct inode_operations proc_self_inode_operations = {
1672 .readlink = proc_self_readlink,
1673 .follow_link = proc_self_follow_link,
1679 * These are the directory entries in the root directory of /proc
1680 * that properly belong to the /proc filesystem, as they describe
1681 * describe something that is process related.
1683 static struct pid_entry proc_base_stuff[] = {
1684 NOD(PROC_TGID_INO, "self", S_IFLNK|S_IRWXUGO,
1685 &proc_self_inode_operations, NULL, {}),
1690 * Exceptional case: normally we are not allowed to unhash a busy
1691 * directory. In this case, however, we can do it - no aliasing problems
1692 * due to the way we treat inodes.
1694 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1696 struct inode *inode = dentry->d_inode;
1697 struct task_struct *task = get_proc_task(inode);
1699 put_task_struct(task);
1706 static struct dentry_operations proc_base_dentry_operations =
1708 .d_revalidate = proc_base_revalidate,
1709 .d_delete = pid_delete_dentry,
1712 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1714 struct inode *inode;
1715 struct dentry *error;
1716 struct task_struct *task = get_proc_task(dir);
1717 struct pid_entry *p;
1718 struct proc_inode *ei;
1720 error = ERR_PTR(-ENOENT);
1726 /* Lookup the directory entry */
1727 for (p = proc_base_stuff; p->name; p++) {
1728 if (p->len != dentry->d_name.len)
1730 if (!memcmp(dentry->d_name.name, p->name, p->len))
1736 /* Allocate the inode */
1737 error = ERR_PTR(-ENOMEM);
1738 inode = new_inode(dir->i_sb);
1742 /* Initialize the inode */
1744 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1745 inode->i_ino = fake_ino(0, p->type);
1748 * grab the reference to the task.
1750 ei->pid = get_pid(task_pid(task));
1756 inode->i_mode = p->mode;
1757 if (S_ISDIR(inode->i_mode))
1759 if (S_ISLNK(inode->i_mode))
1762 inode->i_op = p->iop;
1764 inode->i_fop = p->fop;
1766 dentry->d_op = &proc_base_dentry_operations;
1767 d_add(dentry, inode);
1770 put_task_struct(task);
1781 static struct file_operations proc_task_operations;
1782 static struct inode_operations proc_task_inode_operations;
1784 static struct pid_entry tgid_base_stuff[] = {
1785 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1786 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1787 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1788 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1789 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1790 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1791 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1792 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1793 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
1795 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1797 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1798 #ifdef CONFIG_SECCOMP
1799 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1801 LNK(PROC_TGID_CWD, "cwd", cwd),
1802 LNK(PROC_TGID_ROOT, "root", root),
1803 LNK(PROC_TGID_EXE, "exe", exe),
1804 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1805 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
1807 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
1809 #ifdef CONFIG_SECURITY
1810 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
1812 #ifdef CONFIG_KALLSYMS
1813 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1815 #ifdef CONFIG_SCHEDSTATS
1816 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1818 #ifdef CONFIG_CPUSETS
1819 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
1821 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1822 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1823 #ifdef CONFIG_AUDITSYSCALL
1824 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1829 static int proc_tgid_base_readdir(struct file * filp,
1830 void * dirent, filldir_t filldir)
1832 return proc_pident_readdir(filp,dirent,filldir,
1833 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1836 static struct file_operations proc_tgid_base_operations = {
1837 .read = generic_read_dir,
1838 .readdir = proc_tgid_base_readdir,
1841 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1842 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1845 static struct inode_operations proc_tgid_base_inode_operations = {
1846 .lookup = proc_tgid_base_lookup,
1847 .getattr = pid_getattr,
1848 .setattr = proc_setattr,
1852 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1854 * @task: task that should be flushed.
1856 * Looks in the dcache for
1858 * /proc/@tgid/task/@pid
1859 * if either directory is present flushes it and all of it'ts children
1862 * It is safe and reasonable to cache /proc entries for a task until
1863 * that task exits. After that they just clog up the dcache with
1864 * useless entries, possibly causing useful dcache entries to be
1865 * flushed instead. This routine is proved to flush those useless
1866 * dcache entries at process exit time.
1868 * NOTE: This routine is just an optimization so it does not guarantee
1869 * that no dcache entries will exist at process exit time it
1870 * just makes it very unlikely that any will persist.
1872 void proc_flush_task(struct task_struct *task)
1874 struct dentry *dentry, *leader, *dir;
1875 char buf[PROC_NUMBUF];
1879 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1880 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1882 shrink_dcache_parent(dentry);
1887 if (thread_group_leader(task))
1891 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1892 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1897 name.len = strlen(name.name);
1898 dir = d_hash_and_lookup(leader, &name);
1900 goto out_put_leader;
1903 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1904 dentry = d_hash_and_lookup(dir, &name);
1906 shrink_dcache_parent(dentry);
1919 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1921 struct dentry *result = ERR_PTR(-ENOENT);
1922 struct task_struct *task;
1923 struct inode *inode;
1926 result = proc_base_lookup(dir, dentry);
1927 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1930 tgid = name_to_int(dentry);
1935 task = find_task_by_pid(tgid);
1937 get_task_struct(task);
1942 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1946 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1947 inode->i_op = &proc_tgid_base_inode_operations;
1948 inode->i_fop = &proc_tgid_base_operations;
1949 inode->i_flags|=S_IMMUTABLE;
1950 #ifdef CONFIG_SECURITY
1956 dentry->d_op = &pid_dentry_operations;
1958 d_add(dentry, inode);
1959 /* Close the race of the process dying before we return the dentry */
1960 if (pid_revalidate(dentry, NULL))
1964 put_task_struct(task);
1970 * Find the first task with tgid >= tgid
1973 static struct task_struct *next_tgid(unsigned int tgid)
1975 struct task_struct *task;
1981 pid = find_ge_pid(tgid);
1984 task = pid_task(pid, PIDTYPE_PID);
1985 /* What we to know is if the pid we have find is the
1986 * pid of a thread_group_leader. Testing for task
1987 * being a thread_group_leader is the obvious thing
1988 * todo but there is a window when it fails, due to
1989 * the pid transfer logic in de_thread.
1991 * So we perform the straight forward test of seeing
1992 * if the pid we have found is the pid of a thread
1993 * group leader, and don't worry if the task we have
1994 * found doesn't happen to be a thread group leader.
1995 * As we don't care in the case of readdir.
1997 if (!task || !has_group_leader_pid(task))
1999 get_task_struct(task);
2005 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
2007 /* for the /proc/ directory itself, after non-process stuff has been done */
2008 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2010 char buf[PROC_NUMBUF];
2011 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2012 struct task_struct *task;
2015 for (; nr < (ARRAY_SIZE(proc_base_stuff) - 1); filp->f_pos++, nr++) {
2016 struct pid_entry *p = &proc_base_stuff[nr];
2017 if (filldir(dirent, p->name, p->len, filp->f_pos,
2018 fake_ino(0, p->type), p->mode >> 12) < 0)
2022 tgid = filp->f_pos - TGID_OFFSET;
2023 for (task = next_tgid(tgid);
2025 put_task_struct(task), task = next_tgid(tgid + 1)) {
2029 filp->f_pos = tgid + TGID_OFFSET;
2030 len = snprintf(buf, sizeof(buf), "%d", tgid);
2031 ino = fake_ino(tgid, PROC_TGID_INO);
2032 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2033 put_task_struct(task);
2037 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2045 static struct pid_entry tid_base_stuff[] = {
2046 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
2047 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
2048 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
2049 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
2050 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
2051 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
2052 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
2053 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
2055 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
2057 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
2058 #ifdef CONFIG_SECCOMP
2059 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
2061 LNK(PROC_TID_CWD, "cwd", cwd),
2062 LNK(PROC_TID_ROOT, "root", root),
2063 LNK(PROC_TID_EXE, "exe", exe),
2064 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
2066 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
2068 #ifdef CONFIG_SECURITY
2069 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
2071 #ifdef CONFIG_KALLSYMS
2072 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
2074 #ifdef CONFIG_SCHEDSTATS
2075 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
2077 #ifdef CONFIG_CPUSETS
2078 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
2080 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
2081 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2082 #ifdef CONFIG_AUDITSYSCALL
2083 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
2088 static int proc_tid_base_readdir(struct file * filp,
2089 void * dirent, filldir_t filldir)
2091 return proc_pident_readdir(filp,dirent,filldir,
2092 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2095 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2096 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2099 static struct file_operations proc_tid_base_operations = {
2100 .read = generic_read_dir,
2101 .readdir = proc_tid_base_readdir,
2104 static struct inode_operations proc_tid_base_inode_operations = {
2105 .lookup = proc_tid_base_lookup,
2106 .getattr = pid_getattr,
2107 .setattr = proc_setattr,
2111 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2113 struct dentry *result = ERR_PTR(-ENOENT);
2114 struct task_struct *task;
2115 struct task_struct *leader = get_proc_task(dir);
2116 struct inode *inode;
2122 tid = name_to_int(dentry);
2127 task = find_task_by_pid(tid);
2129 get_task_struct(task);
2133 if (leader->tgid != task->tgid)
2136 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2141 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2142 inode->i_op = &proc_tid_base_inode_operations;
2143 inode->i_fop = &proc_tid_base_operations;
2144 inode->i_flags|=S_IMMUTABLE;
2145 #ifdef CONFIG_SECURITY
2151 dentry->d_op = &pid_dentry_operations;
2153 d_add(dentry, inode);
2154 /* Close the race of the process dying before we return the dentry */
2155 if (pid_revalidate(dentry, NULL))
2159 put_task_struct(task);
2161 put_task_struct(leader);
2167 * Find the first tid of a thread group to return to user space.
2169 * Usually this is just the thread group leader, but if the users
2170 * buffer was too small or there was a seek into the middle of the
2171 * directory we have more work todo.
2173 * In the case of a short read we start with find_task_by_pid.
2175 * In the case of a seek we start with the leader and walk nr
2178 static struct task_struct *first_tid(struct task_struct *leader,
2181 struct task_struct *pos;
2184 /* Attempt to start with the pid of a thread */
2185 if (tid && (nr > 0)) {
2186 pos = find_task_by_pid(tid);
2187 if (pos && (pos->group_leader == leader))
2191 /* If nr exceeds the number of threads there is nothing todo */
2193 if (nr && nr >= get_nr_threads(leader))
2196 /* If we haven't found our starting place yet start
2197 * with the leader and walk nr threads forward.
2199 for (pos = leader; nr > 0; --nr) {
2200 pos = next_thread(pos);
2201 if (pos == leader) {
2207 get_task_struct(pos);
2214 * Find the next thread in the thread list.
2215 * Return NULL if there is an error or no next thread.
2217 * The reference to the input task_struct is released.
2219 static struct task_struct *next_tid(struct task_struct *start)
2221 struct task_struct *pos = NULL;
2223 if (pid_alive(start)) {
2224 pos = next_thread(start);
2225 if (thread_group_leader(pos))
2228 get_task_struct(pos);
2231 put_task_struct(start);
2235 /* for the /proc/TGID/task/ directories */
2236 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2238 char buf[PROC_NUMBUF];
2239 struct dentry *dentry = filp->f_dentry;
2240 struct inode *inode = dentry->d_inode;
2241 struct task_struct *leader = get_proc_task(inode);
2242 struct task_struct *task;
2243 int retval = -ENOENT;
2246 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2255 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2260 ino = parent_ino(dentry);
2261 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2267 /* f_version caches the tgid value that the last readdir call couldn't
2268 * return. lseek aka telldir automagically resets f_version to 0.
2270 tid = filp->f_version;
2271 filp->f_version = 0;
2272 for (task = first_tid(leader, tid, pos - 2);
2274 task = next_tid(task), pos++) {
2277 len = snprintf(buf, sizeof(buf), "%d", tid);
2278 ino = fake_ino(tid, PROC_TID_INO);
2279 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2280 /* returning this tgid failed, save it as the first
2281 * pid for the next readir call */
2282 filp->f_version = tid;
2283 put_task_struct(task);
2289 put_task_struct(leader);
2294 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2296 struct inode *inode = dentry->d_inode;
2297 struct task_struct *p = get_proc_task(inode);
2298 generic_fillattr(inode, stat);
2302 stat->nlink += get_nr_threads(p);
2310 static struct inode_operations proc_task_inode_operations = {
2311 .lookup = proc_task_lookup,
2312 .getattr = proc_task_getattr,
2313 .setattr = proc_setattr,
2316 static struct file_operations proc_task_operations = {
2317 .read = generic_read_dir,
2318 .readdir = proc_task_readdir,