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
197 struct inode_operations *iop;
198 struct file_operations *fop;
202 #define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
204 .len = sizeof(NAME) - 1, \
212 #define DIR(TYPE, NAME, MODE, OTYPE) \
213 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
214 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
216 #define LNK(TYPE, NAME, OTYPE) \
217 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
218 &proc_pid_link_inode_operations, NULL, \
219 { .proc_get_link = &proc_##OTYPE##_link } )
220 #define REG(TYPE, NAME, MODE, OTYPE) \
221 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
222 &proc_##OTYPE##_operations, {})
223 #define INF(TYPE, NAME, MODE, OTYPE) \
224 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
225 NULL, &proc_info_file_operations, \
226 { .proc_read = &proc_##OTYPE } )
228 static struct fs_struct *get_fs_struct(struct task_struct *task)
230 struct fs_struct *fs;
234 atomic_inc(&fs->count);
239 static int get_nr_threads(struct task_struct *tsk)
241 /* Must be called with the rcu_read_lock held */
245 if (lock_task_sighand(tsk, &flags)) {
246 count = atomic_read(&tsk->signal->count);
247 unlock_task_sighand(tsk, &flags);
252 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
254 struct task_struct *task = get_proc_task(inode);
255 struct fs_struct *fs = NULL;
256 int result = -ENOENT;
259 fs = get_fs_struct(task);
260 put_task_struct(task);
263 read_lock(&fs->lock);
264 *mnt = mntget(fs->pwdmnt);
265 *dentry = dget(fs->pwd);
266 read_unlock(&fs->lock);
273 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
275 struct task_struct *task = get_proc_task(inode);
276 struct fs_struct *fs = NULL;
277 int result = -ENOENT;
280 fs = get_fs_struct(task);
281 put_task_struct(task);
284 read_lock(&fs->lock);
285 *mnt = mntget(fs->rootmnt);
286 *dentry = dget(fs->root);
287 read_unlock(&fs->lock);
294 #define MAY_PTRACE(task) \
295 (task == current || \
296 (task->parent == current && \
297 (task->ptrace & PT_PTRACED) && \
298 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
299 security_ptrace(current,task) == 0))
301 static int proc_pid_environ(struct task_struct *task, char * buffer)
304 struct mm_struct *mm = get_task_mm(task);
306 unsigned int len = mm->env_end - mm->env_start;
309 res = access_process_vm(task, mm->env_start, buffer, len, 0);
310 if (!ptrace_may_attach(task))
317 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
321 struct mm_struct *mm = get_task_mm(task);
325 goto out_mm; /* Shh! No looking before we're done */
327 len = mm->arg_end - mm->arg_start;
332 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
334 // If the nul at the end of args has been overwritten, then
335 // assume application is using setproctitle(3).
336 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
337 len = strnlen(buffer, res);
341 len = mm->env_end - mm->env_start;
342 if (len > PAGE_SIZE - res)
343 len = PAGE_SIZE - res;
344 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
345 res = strnlen(buffer, res);
354 static int proc_pid_auxv(struct task_struct *task, char *buffer)
357 struct mm_struct *mm = get_task_mm(task);
359 unsigned int nwords = 0;
362 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
363 res = nwords * sizeof(mm->saved_auxv[0]);
366 memcpy(buffer, mm->saved_auxv, res);
373 #ifdef CONFIG_KALLSYMS
375 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
376 * Returns the resolved symbol. If that fails, simply return the address.
378 static int proc_pid_wchan(struct task_struct *task, char *buffer)
381 const char *sym_name;
382 unsigned long wchan, size, offset;
383 char namebuf[KSYM_NAME_LEN+1];
385 wchan = get_wchan(task);
387 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
389 return sprintf(buffer, "%s", sym_name);
390 return sprintf(buffer, "%lu", wchan);
392 #endif /* CONFIG_KALLSYMS */
394 #ifdef CONFIG_SCHEDSTATS
396 * Provides /proc/PID/schedstat
398 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
400 return sprintf(buffer, "%lu %lu %lu\n",
401 task->sched_info.cpu_time,
402 task->sched_info.run_delay,
403 task->sched_info.pcnt);
407 /* The badness from the OOM killer */
408 unsigned long badness(struct task_struct *p, unsigned long uptime);
409 static int proc_oom_score(struct task_struct *task, char *buffer)
411 unsigned long points;
412 struct timespec uptime;
414 do_posix_clock_monotonic_gettime(&uptime);
415 points = badness(task, uptime.tv_sec);
416 return sprintf(buffer, "%lu\n", points);
419 /************************************************************************/
420 /* Here the fs part begins */
421 /************************************************************************/
423 /* permission checks */
424 static int proc_fd_access_allowed(struct inode *inode)
426 struct task_struct *task;
428 /* Allow access to a task's file descriptors if it is us or we
429 * may use ptrace attach to the process and find out that
432 task = get_proc_task(inode);
434 allowed = ptrace_may_attach(task);
435 put_task_struct(task);
440 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
443 struct inode *inode = dentry->d_inode;
445 if (attr->ia_valid & ATTR_MODE)
448 error = inode_change_ok(inode, attr);
450 error = security_inode_setattr(dentry, attr);
452 error = inode_setattr(inode, attr);
457 static struct inode_operations proc_def_inode_operations = {
458 .setattr = proc_setattr,
461 extern struct seq_operations mounts_op;
467 static int mounts_open(struct inode *inode, struct file *file)
469 struct task_struct *task = get_proc_task(inode);
470 struct namespace *namespace = NULL;
471 struct proc_mounts *p;
476 namespace = task->namespace;
478 get_namespace(namespace);
480 put_task_struct(task);
485 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
487 file->private_data = &p->m;
488 ret = seq_open(file, &mounts_op);
490 p->m.private = namespace;
491 p->event = namespace->event;
496 put_namespace(namespace);
501 static int mounts_release(struct inode *inode, struct file *file)
503 struct seq_file *m = file->private_data;
504 struct namespace *namespace = m->private;
505 put_namespace(namespace);
506 return seq_release(inode, file);
509 static unsigned mounts_poll(struct file *file, poll_table *wait)
511 struct proc_mounts *p = file->private_data;
512 struct namespace *ns = p->m.private;
515 poll_wait(file, &ns->poll, wait);
517 spin_lock(&vfsmount_lock);
518 if (p->event != ns->event) {
519 p->event = ns->event;
522 spin_unlock(&vfsmount_lock);
527 static struct file_operations proc_mounts_operations = {
531 .release = mounts_release,
535 extern struct seq_operations mountstats_op;
536 static int mountstats_open(struct inode *inode, struct file *file)
538 int ret = seq_open(file, &mountstats_op);
541 struct seq_file *m = file->private_data;
542 struct namespace *namespace = NULL;
543 struct task_struct *task = get_proc_task(inode);
547 namespace = task->namespace;
549 get_namespace(namespace);
551 put_task_struct(task);
555 m->private = namespace;
557 seq_release(inode, file);
564 static struct file_operations proc_mountstats_operations = {
565 .open = mountstats_open,
568 .release = mounts_release,
571 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
573 static ssize_t proc_info_read(struct file * file, char __user * buf,
574 size_t count, loff_t *ppos)
576 struct inode * inode = file->f_dentry->d_inode;
579 struct task_struct *task = get_proc_task(inode);
585 if (count > PROC_BLOCK_SIZE)
586 count = PROC_BLOCK_SIZE;
589 if (!(page = __get_free_page(GFP_KERNEL)))
592 length = PROC_I(inode)->op.proc_read(task, (char*)page);
595 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
598 put_task_struct(task);
603 static struct file_operations proc_info_file_operations = {
604 .read = proc_info_read,
607 static int mem_open(struct inode* inode, struct file* file)
609 file->private_data = (void*)((long)current->self_exec_id);
613 static ssize_t mem_read(struct file * file, char __user * buf,
614 size_t count, loff_t *ppos)
616 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
618 unsigned long src = *ppos;
620 struct mm_struct *mm;
625 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
629 page = (char *)__get_free_page(GFP_USER);
635 mm = get_task_mm(task);
641 if (file->private_data != (void*)((long)current->self_exec_id))
647 int this_len, retval;
649 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
650 retval = access_process_vm(task, src, page, this_len, 0);
651 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
657 if (copy_to_user(buf, page, retval)) {
672 free_page((unsigned long) page);
674 put_task_struct(task);
679 #define mem_write NULL
682 /* This is a security hazard */
683 static ssize_t mem_write(struct file * file, const char * buf,
684 size_t count, loff_t *ppos)
688 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
689 unsigned long dst = *ppos;
695 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
699 page = (char *)__get_free_page(GFP_USER);
705 int this_len, retval;
707 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
708 if (copy_from_user(page, buf, this_len)) {
712 retval = access_process_vm(task, dst, page, this_len, 1);
724 free_page((unsigned long) page);
726 put_task_struct(task);
732 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
736 file->f_pos = offset;
739 file->f_pos += offset;
744 force_successful_syscall_return();
748 static struct file_operations proc_mem_operations = {
755 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
756 size_t count, loff_t *ppos)
758 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
759 char buffer[PROC_NUMBUF];
762 loff_t __ppos = *ppos;
766 oom_adjust = task->oomkilladj;
767 put_task_struct(task);
769 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
772 if (count > len-__ppos)
774 if (copy_to_user(buf, buffer + __ppos, count))
776 *ppos = __ppos + count;
780 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
781 size_t count, loff_t *ppos)
783 struct task_struct *task;
784 char buffer[PROC_NUMBUF], *end;
787 if (!capable(CAP_SYS_RESOURCE))
789 memset(buffer, 0, sizeof(buffer));
790 if (count > sizeof(buffer) - 1)
791 count = sizeof(buffer) - 1;
792 if (copy_from_user(buffer, buf, count))
794 oom_adjust = simple_strtol(buffer, &end, 0);
795 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
799 task = get_proc_task(file->f_dentry->d_inode);
802 task->oomkilladj = oom_adjust;
803 put_task_struct(task);
804 if (end - buffer == 0)
809 static struct file_operations proc_oom_adjust_operations = {
810 .read = oom_adjust_read,
811 .write = oom_adjust_write,
814 #ifdef CONFIG_AUDITSYSCALL
816 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
817 size_t count, loff_t *ppos)
819 struct inode * inode = file->f_dentry->d_inode;
820 struct task_struct *task = get_proc_task(inode);
822 char tmpbuf[TMPBUFLEN];
826 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
827 audit_get_loginuid(task->audit_context));
828 put_task_struct(task);
829 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
832 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
833 size_t count, loff_t *ppos)
835 struct inode * inode = file->f_dentry->d_inode;
840 if (!capable(CAP_AUDIT_CONTROL))
843 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
846 if (count >= PAGE_SIZE)
847 count = PAGE_SIZE - 1;
850 /* No partial writes. */
853 page = (char*)__get_free_page(GFP_USER);
857 if (copy_from_user(page, buf, count))
861 loginuid = simple_strtoul(page, &tmp, 10);
867 length = audit_set_loginuid(current, loginuid);
868 if (likely(length == 0))
872 free_page((unsigned long) page);
876 static struct file_operations proc_loginuid_operations = {
877 .read = proc_loginuid_read,
878 .write = proc_loginuid_write,
882 #ifdef CONFIG_SECCOMP
883 static ssize_t seccomp_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
886 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
888 loff_t __ppos = *ppos;
893 /* no need to print the trailing zero, so use only len */
894 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
895 put_task_struct(tsk);
898 if (count > len - __ppos)
899 count = len - __ppos;
900 if (copy_to_user(buf, __buf + __ppos, count))
902 *ppos = __ppos + count;
906 static ssize_t seccomp_write(struct file *file, const char __user *buf,
907 size_t count, loff_t *ppos)
909 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
910 char __buf[20], *end;
911 unsigned int seccomp_mode;
918 /* can set it only once to be even more secure */
920 if (unlikely(tsk->seccomp.mode))
924 memset(__buf, 0, sizeof(__buf));
925 count = min(count, sizeof(__buf) - 1);
926 if (copy_from_user(__buf, buf, count))
929 seccomp_mode = simple_strtoul(__buf, &end, 0);
933 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
934 tsk->seccomp.mode = seccomp_mode;
935 set_tsk_thread_flag(tsk, TIF_SECCOMP);
939 if (unlikely(!(end - __buf)))
941 result = end - __buf;
943 put_task_struct(tsk);
948 static struct file_operations proc_seccomp_operations = {
949 .read = seccomp_read,
950 .write = seccomp_write,
952 #endif /* CONFIG_SECCOMP */
954 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
956 struct inode *inode = dentry->d_inode;
959 /* We don't need a base pointer in the /proc filesystem */
962 /* Are we allowed to snoop on the tasks file descriptors? */
963 if (!proc_fd_access_allowed(inode))
966 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
967 nd->last_type = LAST_BIND;
969 return ERR_PTR(error);
972 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
973 char __user *buffer, int buflen)
975 struct inode * inode;
976 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
982 inode = dentry->d_inode;
983 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
987 len = tmp + PAGE_SIZE - 1 - path;
991 if (copy_to_user(buffer, path, len))
994 free_page((unsigned long)tmp);
998 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000 int error = -EACCES;
1001 struct inode *inode = dentry->d_inode;
1003 struct vfsmount *mnt = NULL;
1005 /* Are we allowed to snoop on the tasks file descriptors? */
1006 if (!proc_fd_access_allowed(inode))
1009 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1013 error = do_proc_readlink(de, mnt, buffer, buflen);
1020 static struct inode_operations proc_pid_link_inode_operations = {
1021 .readlink = proc_pid_readlink,
1022 .follow_link = proc_pid_follow_link,
1023 .setattr = proc_setattr,
1027 /* building an inode */
1029 static int task_dumpable(struct task_struct *task)
1032 struct mm_struct *mm;
1037 dumpable = mm->dumpable;
1045 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1047 struct inode * inode;
1048 struct proc_inode *ei;
1050 /* We need a new inode */
1052 inode = new_inode(sb);
1058 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1059 inode->i_ino = fake_ino(task->pid, ino);
1060 inode->i_op = &proc_def_inode_operations;
1063 * grab the reference to task.
1065 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1071 if (task_dumpable(task)) {
1072 inode->i_uid = task->euid;
1073 inode->i_gid = task->egid;
1075 security_task_to_inode(task, inode);
1085 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task;
1089 generic_fillattr(inode, stat);
1094 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1096 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097 task_dumpable(task)) {
1098 stat->uid = task->euid;
1099 stat->gid = task->egid;
1109 * Exceptional case: normally we are not allowed to unhash a busy
1110 * directory. In this case, however, we can do it - no aliasing problems
1111 * due to the way we treat inodes.
1113 * Rewrite the inode's ownerships here because the owning task may have
1114 * performed a setuid(), etc.
1116 * Before the /proc/pid/status file was created the only way to read
1117 * the effective uid of a /process was to stat /proc/pid. Reading
1118 * /proc/pid/status is slow enough that procps and other packages
1119 * kept stating /proc/pid. To keep the rules in /proc simple I have
1120 * made this apply to all per process world readable and executable
1123 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125 struct inode *inode = dentry->d_inode;
1126 struct task_struct *task = get_proc_task(inode);
1128 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129 task_dumpable(task)) {
1130 inode->i_uid = task->euid;
1131 inode->i_gid = task->egid;
1136 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137 security_task_to_inode(task, inode);
1138 put_task_struct(task);
1145 static int pid_delete_dentry(struct dentry * dentry)
1147 /* Is the task we represent dead?
1148 * If so, then don't put the dentry on the lru list,
1149 * kill it immediately.
1151 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1154 static struct dentry_operations pid_dentry_operations =
1156 .d_revalidate = pid_revalidate,
1157 .d_delete = pid_delete_dentry,
1162 static unsigned name_to_int(struct dentry *dentry)
1164 const char *name = dentry->d_name.name;
1165 int len = dentry->d_name.len;
1168 if (len > 1 && *name == '0')
1171 unsigned c = *name++ - '0';
1174 if (n >= (~0U-9)/10)
1184 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1186 struct task_struct *task = get_proc_task(inode);
1187 struct files_struct *files = NULL;
1189 int fd = proc_fd(inode);
1192 files = get_files_struct(task);
1193 put_task_struct(task);
1197 * We are not taking a ref to the file structure, so we must
1200 spin_lock(&files->file_lock);
1201 file = fcheck_files(files, fd);
1203 *mnt = mntget(file->f_vfsmnt);
1204 *dentry = dget(file->f_dentry);
1205 spin_unlock(&files->file_lock);
1206 put_files_struct(files);
1209 spin_unlock(&files->file_lock);
1210 put_files_struct(files);
1215 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1217 struct inode *inode = dentry->d_inode;
1218 struct task_struct *task = get_proc_task(inode);
1219 int fd = proc_fd(inode);
1220 struct files_struct *files;
1223 files = get_files_struct(task);
1226 if (fcheck_files(files, fd)) {
1228 put_files_struct(files);
1229 if (task_dumpable(task)) {
1230 inode->i_uid = task->euid;
1231 inode->i_gid = task->egid;
1236 inode->i_mode &= ~(S_ISUID | S_ISGID);
1237 security_task_to_inode(task, inode);
1238 put_task_struct(task);
1242 put_files_struct(files);
1244 put_task_struct(task);
1250 static struct dentry_operations tid_fd_dentry_operations =
1252 .d_revalidate = tid_fd_revalidate,
1253 .d_delete = pid_delete_dentry,
1257 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1259 struct task_struct *task = get_proc_task(dir);
1260 unsigned fd = name_to_int(dentry);
1261 struct dentry *result = ERR_PTR(-ENOENT);
1263 struct files_struct * files;
1264 struct inode *inode;
1265 struct proc_inode *ei;
1272 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1277 files = get_files_struct(task);
1280 inode->i_mode = S_IFLNK;
1283 * We are not taking a ref to the file structure, so we must
1286 spin_lock(&files->file_lock);
1287 file = fcheck_files(files, fd);
1290 if (file->f_mode & 1)
1291 inode->i_mode |= S_IRUSR | S_IXUSR;
1292 if (file->f_mode & 2)
1293 inode->i_mode |= S_IWUSR | S_IXUSR;
1294 spin_unlock(&files->file_lock);
1295 put_files_struct(files);
1296 inode->i_op = &proc_pid_link_inode_operations;
1298 ei->op.proc_get_link = proc_fd_link;
1299 dentry->d_op = &tid_fd_dentry_operations;
1300 d_add(dentry, inode);
1301 /* Close the race of the process dying before we return the dentry */
1302 if (tid_fd_revalidate(dentry, NULL))
1305 put_task_struct(task);
1310 spin_unlock(&files->file_lock);
1311 put_files_struct(files);
1317 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1319 struct dentry *dentry = filp->f_dentry;
1320 struct inode *inode = dentry->d_inode;
1321 struct task_struct *p = get_proc_task(inode);
1322 unsigned int fd, tid, ino;
1324 char buf[PROC_NUMBUF];
1325 struct files_struct * files;
1326 struct fdtable *fdt;
1337 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1341 ino = parent_ino(dentry);
1342 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1346 files = get_files_struct(p);
1350 fdt = files_fdtable(files);
1351 for (fd = filp->f_pos-2;
1353 fd++, filp->f_pos++) {
1356 if (!fcheck_files(files, fd))
1364 buf[j] = '0' + (i % 10);
1368 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1369 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1376 put_files_struct(files);
1384 static struct file_operations proc_fd_operations = {
1385 .read = generic_read_dir,
1386 .readdir = proc_readfd,
1390 * proc directories can do almost nothing..
1392 static struct inode_operations proc_fd_inode_operations = {
1393 .lookup = proc_lookupfd,
1394 .setattr = proc_setattr,
1398 static struct dentry *proc_pident_lookup(struct inode *dir,
1399 struct dentry *dentry,
1400 struct pid_entry *ents)
1402 struct inode *inode;
1403 struct dentry *error;
1404 struct task_struct *task = get_proc_task(dir);
1405 struct pid_entry *p;
1406 struct proc_inode *ei;
1408 error = ERR_PTR(-ENOENT);
1415 * Yes, it does not scale. And it should not. Don't add
1416 * new entries into /proc/<tgid>/ without very good reasons.
1418 for (p = ents; p->name; p++) {
1419 if (p->len != dentry->d_name.len)
1421 if (!memcmp(dentry->d_name.name, p->name, p->len))
1427 error = ERR_PTR(-EINVAL);
1428 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1433 inode->i_mode = p->mode;
1434 if (S_ISDIR(inode->i_mode))
1435 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1437 inode->i_op = p->iop;
1439 inode->i_fop = p->fop;
1441 dentry->d_op = &pid_dentry_operations;
1442 d_add(dentry, inode);
1443 /* Close the race of the process dying before we return the dentry */
1444 if (pid_revalidate(dentry, NULL))
1447 put_task_struct(task);
1452 static int proc_pident_readdir(struct file *filp,
1453 void *dirent, filldir_t filldir,
1454 struct pid_entry *ents, unsigned int nents)
1458 struct dentry *dentry = filp->f_dentry;
1459 struct inode *inode = dentry->d_inode;
1460 struct task_struct *task = get_proc_task(inode);
1461 struct pid_entry *p;
1471 put_task_struct(task);
1476 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1482 ino = parent_ino(dentry);
1483 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1496 if (filldir(dirent, p->name, p->len, filp->f_pos,
1497 fake_ino(pid, p->type), p->mode >> 12) < 0)
1509 #ifdef CONFIG_SECURITY
1510 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1511 size_t count, loff_t *ppos)
1513 struct inode * inode = file->f_dentry->d_inode;
1516 struct task_struct *task = get_proc_task(inode);
1522 if (count > PAGE_SIZE)
1525 if (!(page = __get_free_page(GFP_KERNEL)))
1528 length = security_getprocattr(task,
1529 (char*)file->f_dentry->d_name.name,
1530 (void*)page, count);
1532 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1535 put_task_struct(task);
1540 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1541 size_t count, loff_t *ppos)
1543 struct inode * inode = file->f_dentry->d_inode;
1546 struct task_struct *task = get_proc_task(inode);
1551 if (count > PAGE_SIZE)
1554 /* No partial writes. */
1560 page = (char*)__get_free_page(GFP_USER);
1565 if (copy_from_user(page, buf, count))
1568 length = security_setprocattr(task,
1569 (char*)file->f_dentry->d_name.name,
1570 (void*)page, count);
1572 free_page((unsigned long) page);
1574 put_task_struct(task);
1579 static struct file_operations proc_pid_attr_operations = {
1580 .read = proc_pid_attr_read,
1581 .write = proc_pid_attr_write,
1584 static struct pid_entry tgid_attr_stuff[] = {
1585 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1586 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1593 static struct pid_entry tid_attr_stuff[] = {
1594 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1595 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1596 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1597 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1603 static int proc_tgid_attr_readdir(struct file * filp,
1604 void * dirent, filldir_t filldir)
1606 return proc_pident_readdir(filp,dirent,filldir,
1607 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1610 static int proc_tid_attr_readdir(struct file * filp,
1611 void * dirent, filldir_t filldir)
1613 return proc_pident_readdir(filp,dirent,filldir,
1614 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1617 static struct file_operations proc_tgid_attr_operations = {
1618 .read = generic_read_dir,
1619 .readdir = proc_tgid_attr_readdir,
1622 static struct file_operations proc_tid_attr_operations = {
1623 .read = generic_read_dir,
1624 .readdir = proc_tid_attr_readdir,
1627 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1628 struct dentry *dentry, struct nameidata *nd)
1630 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1633 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1634 struct dentry *dentry, struct nameidata *nd)
1636 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1639 static struct inode_operations proc_tgid_attr_inode_operations = {
1640 .lookup = proc_tgid_attr_lookup,
1641 .getattr = pid_getattr,
1642 .setattr = proc_setattr,
1645 static struct inode_operations proc_tid_attr_inode_operations = {
1646 .lookup = proc_tid_attr_lookup,
1647 .getattr = pid_getattr,
1648 .setattr = proc_setattr,
1655 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1658 char tmp[PROC_NUMBUF];
1659 sprintf(tmp, "%d", current->tgid);
1660 return vfs_readlink(dentry,buffer,buflen,tmp);
1663 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1665 char tmp[PROC_NUMBUF];
1666 sprintf(tmp, "%d", current->tgid);
1667 return ERR_PTR(vfs_follow_link(nd,tmp));
1670 static struct inode_operations proc_self_inode_operations = {
1671 .readlink = proc_self_readlink,
1672 .follow_link = proc_self_follow_link,
1678 static struct file_operations proc_task_operations;
1679 static struct inode_operations proc_task_inode_operations;
1681 static struct pid_entry tgid_base_stuff[] = {
1682 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1683 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1684 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1685 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1686 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1687 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1688 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1689 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1690 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
1692 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1694 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1695 #ifdef CONFIG_SECCOMP
1696 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1698 LNK(PROC_TGID_CWD, "cwd", cwd),
1699 LNK(PROC_TGID_ROOT, "root", root),
1700 LNK(PROC_TGID_EXE, "exe", exe),
1701 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1702 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
1704 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
1706 #ifdef CONFIG_SECURITY
1707 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
1709 #ifdef CONFIG_KALLSYMS
1710 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1712 #ifdef CONFIG_SCHEDSTATS
1713 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1715 #ifdef CONFIG_CPUSETS
1716 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
1718 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1719 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1720 #ifdef CONFIG_AUDITSYSCALL
1721 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1726 static int proc_tgid_base_readdir(struct file * filp,
1727 void * dirent, filldir_t filldir)
1729 return proc_pident_readdir(filp,dirent,filldir,
1730 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1733 static struct file_operations proc_tgid_base_operations = {
1734 .read = generic_read_dir,
1735 .readdir = proc_tgid_base_readdir,
1738 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1739 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1742 static struct inode_operations proc_tgid_base_inode_operations = {
1743 .lookup = proc_tgid_base_lookup,
1744 .getattr = pid_getattr,
1745 .setattr = proc_setattr,
1749 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1751 * @task: task that should be flushed.
1753 * Looks in the dcache for
1755 * /proc/@tgid/task/@pid
1756 * if either directory is present flushes it and all of it'ts children
1759 * It is safe and reasonable to cache /proc entries for a task until
1760 * that task exits. After that they just clog up the dcache with
1761 * useless entries, possibly causing useful dcache entries to be
1762 * flushed instead. This routine is proved to flush those useless
1763 * dcache entries at process exit time.
1765 * NOTE: This routine is just an optimization so it does not guarantee
1766 * that no dcache entries will exist at process exit time it
1767 * just makes it very unlikely that any will persist.
1769 void proc_flush_task(struct task_struct *task)
1771 struct dentry *dentry, *leader, *dir;
1772 char buf[PROC_NUMBUF];
1776 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1777 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1779 shrink_dcache_parent(dentry);
1784 if (thread_group_leader(task))
1788 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1789 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1794 name.len = strlen(name.name);
1795 dir = d_hash_and_lookup(leader, &name);
1797 goto out_put_leader;
1800 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1801 dentry = d_hash_and_lookup(dir, &name);
1803 shrink_dcache_parent(dentry);
1816 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1818 struct dentry *result = ERR_PTR(-ENOENT);
1819 struct task_struct *task;
1820 struct inode *inode;
1821 struct proc_inode *ei;
1824 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1825 inode = new_inode(dir->i_sb);
1827 return ERR_PTR(-ENOMEM);
1829 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1830 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1832 inode->i_mode = S_IFLNK|S_IRWXUGO;
1833 inode->i_uid = inode->i_gid = 0;
1835 inode->i_op = &proc_self_inode_operations;
1836 d_add(dentry, inode);
1839 tgid = name_to_int(dentry);
1844 task = find_task_by_pid(tgid);
1846 get_task_struct(task);
1851 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1855 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1856 inode->i_op = &proc_tgid_base_inode_operations;
1857 inode->i_fop = &proc_tgid_base_operations;
1858 inode->i_flags|=S_IMMUTABLE;
1859 #ifdef CONFIG_SECURITY
1865 dentry->d_op = &pid_dentry_operations;
1867 d_add(dentry, inode);
1868 /* Close the race of the process dying before we return the dentry */
1869 if (pid_revalidate(dentry, NULL))
1873 put_task_struct(task);
1879 * Find the first task with tgid >= tgid
1882 static struct task_struct *next_tgid(unsigned int tgid)
1884 struct task_struct *task;
1890 pid = find_ge_pid(tgid);
1893 task = pid_task(pid, PIDTYPE_PID);
1894 /* What we to know is if the pid we have find is the
1895 * pid of a thread_group_leader. Testing for task
1896 * being a thread_group_leader is the obvious thing
1897 * todo but there is a window when it fails, due to
1898 * the pid transfer logic in de_thread.
1900 * So we perform the straight forward test of seeing
1901 * if the pid we have found is the pid of a thread
1902 * group leader, and don't worry if the task we have
1903 * found doesn't happen to be a thread group leader.
1904 * As we don't care in the case of readdir.
1906 if (!task || !has_group_leader_pid(task))
1908 get_task_struct(task);
1914 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
1916 /* for the /proc/ directory itself, after non-process stuff has been done */
1917 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1919 char buf[PROC_NUMBUF];
1920 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1921 struct task_struct *task;
1925 ino_t ino = fake_ino(0,PROC_TGID_INO);
1926 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1932 tgid = filp->f_pos - TGID_OFFSET;
1933 for (task = next_tgid(tgid);
1935 put_task_struct(task), task = next_tgid(tgid + 1)) {
1939 filp->f_pos = tgid + TGID_OFFSET;
1940 len = snprintf(buf, sizeof(buf), "%d", tgid);
1941 ino = fake_ino(tgid, PROC_TGID_INO);
1942 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
1943 put_task_struct(task);
1947 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
1955 static struct pid_entry tid_base_stuff[] = {
1956 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1957 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
1958 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
1959 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
1960 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1961 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
1962 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
1963 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
1965 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1967 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1968 #ifdef CONFIG_SECCOMP
1969 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1971 LNK(PROC_TID_CWD, "cwd", cwd),
1972 LNK(PROC_TID_ROOT, "root", root),
1973 LNK(PROC_TID_EXE, "exe", exe),
1974 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
1976 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
1978 #ifdef CONFIG_SECURITY
1979 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
1981 #ifdef CONFIG_KALLSYMS
1982 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1984 #ifdef CONFIG_SCHEDSTATS
1985 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1987 #ifdef CONFIG_CPUSETS
1988 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
1990 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1991 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1992 #ifdef CONFIG_AUDITSYSCALL
1993 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1998 static int proc_tid_base_readdir(struct file * filp,
1999 void * dirent, filldir_t filldir)
2001 return proc_pident_readdir(filp,dirent,filldir,
2002 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2005 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2006 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2009 static struct file_operations proc_tid_base_operations = {
2010 .read = generic_read_dir,
2011 .readdir = proc_tid_base_readdir,
2014 static struct inode_operations proc_tid_base_inode_operations = {
2015 .lookup = proc_tid_base_lookup,
2016 .getattr = pid_getattr,
2017 .setattr = proc_setattr,
2021 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2023 struct dentry *result = ERR_PTR(-ENOENT);
2024 struct task_struct *task;
2025 struct task_struct *leader = get_proc_task(dir);
2026 struct inode *inode;
2032 tid = name_to_int(dentry);
2037 task = find_task_by_pid(tid);
2039 get_task_struct(task);
2043 if (leader->tgid != task->tgid)
2046 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2051 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2052 inode->i_op = &proc_tid_base_inode_operations;
2053 inode->i_fop = &proc_tid_base_operations;
2054 inode->i_flags|=S_IMMUTABLE;
2055 #ifdef CONFIG_SECURITY
2061 dentry->d_op = &pid_dentry_operations;
2063 d_add(dentry, inode);
2064 /* Close the race of the process dying before we return the dentry */
2065 if (pid_revalidate(dentry, NULL))
2069 put_task_struct(task);
2071 put_task_struct(leader);
2077 * Find the first tid of a thread group to return to user space.
2079 * Usually this is just the thread group leader, but if the users
2080 * buffer was too small or there was a seek into the middle of the
2081 * directory we have more work todo.
2083 * In the case of a short read we start with find_task_by_pid.
2085 * In the case of a seek we start with the leader and walk nr
2088 static struct task_struct *first_tid(struct task_struct *leader,
2091 struct task_struct *pos;
2094 /* Attempt to start with the pid of a thread */
2095 if (tid && (nr > 0)) {
2096 pos = find_task_by_pid(tid);
2097 if (pos && (pos->group_leader == leader))
2101 /* If nr exceeds the number of threads there is nothing todo */
2103 if (nr && nr >= get_nr_threads(leader))
2106 /* If we haven't found our starting place yet start
2107 * with the leader and walk nr threads forward.
2109 for (pos = leader; nr > 0; --nr) {
2110 pos = next_thread(pos);
2111 if (pos == leader) {
2117 get_task_struct(pos);
2124 * Find the next thread in the thread list.
2125 * Return NULL if there is an error or no next thread.
2127 * The reference to the input task_struct is released.
2129 static struct task_struct *next_tid(struct task_struct *start)
2131 struct task_struct *pos = NULL;
2133 if (pid_alive(start)) {
2134 pos = next_thread(start);
2135 if (thread_group_leader(pos))
2138 get_task_struct(pos);
2141 put_task_struct(start);
2145 /* for the /proc/TGID/task/ directories */
2146 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2148 char buf[PROC_NUMBUF];
2149 struct dentry *dentry = filp->f_dentry;
2150 struct inode *inode = dentry->d_inode;
2151 struct task_struct *leader = get_proc_task(inode);
2152 struct task_struct *task;
2153 int retval = -ENOENT;
2156 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2165 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2170 ino = parent_ino(dentry);
2171 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2177 /* f_version caches the tgid value that the last readdir call couldn't
2178 * return. lseek aka telldir automagically resets f_version to 0.
2180 tid = filp->f_version;
2181 filp->f_version = 0;
2182 for (task = first_tid(leader, tid, pos - 2);
2184 task = next_tid(task), pos++) {
2187 len = snprintf(buf, sizeof(buf), "%d", tid);
2188 ino = fake_ino(tid, PROC_TID_INO);
2189 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2190 /* returning this tgid failed, save it as the first
2191 * pid for the next readir call */
2192 filp->f_version = tid;
2193 put_task_struct(task);
2199 put_task_struct(leader);
2204 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2206 struct inode *inode = dentry->d_inode;
2207 struct task_struct *p = get_proc_task(inode);
2208 generic_fillattr(inode, stat);
2212 stat->nlink += get_nr_threads(p);
2220 static struct inode_operations proc_task_inode_operations = {
2221 .lookup = proc_task_lookup,
2222 .getattr = proc_task_getattr,
2223 .setattr = proc_setattr,
2226 static struct file_operations proc_task_operations = {
2227 .read = generic_read_dir,
2228 .readdir = proc_task_readdir,