4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/config.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
78 * For hysterical raisins we keep the same inumbers as in the old procfs.
79 * Feel free to change the macro below - just keep the range distinct from
80 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
81 * As soon as we'll get a separate superblock we will be able to forget
82 * about magical ranges too.
85 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
87 enum pid_directory_inos {
111 #ifdef CONFIG_SCHEDSTATS
114 #ifdef CONFIG_CPUSETS
117 #ifdef CONFIG_SECURITY
119 PROC_TGID_ATTR_CURRENT,
122 PROC_TGID_ATTR_FSCREATE,
124 #ifdef CONFIG_AUDITSYSCALL
128 PROC_TGID_OOM_ADJUST,
132 #ifdef CONFIG_SECCOMP
151 #ifdef CONFIG_SCHEDSTATS
154 #ifdef CONFIG_CPUSETS
157 #ifdef CONFIG_SECURITY
159 PROC_TID_ATTR_CURRENT,
162 PROC_TID_ATTR_FSCREATE,
164 #ifdef CONFIG_AUDITSYSCALL
170 /* Add new entries before this */
171 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
181 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
183 static struct pid_entry tgid_base_stuff[] = {
184 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
185 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
186 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
187 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
188 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
189 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
190 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
191 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
192 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
194 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
196 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
197 #ifdef CONFIG_SECCOMP
198 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
200 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
201 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
202 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
203 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
205 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
207 #ifdef CONFIG_SECURITY
208 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
210 #ifdef CONFIG_KALLSYMS
211 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
213 #ifdef CONFIG_SCHEDSTATS
214 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
216 #ifdef CONFIG_CPUSETS
217 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
219 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
220 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
221 #ifdef CONFIG_AUDITSYSCALL
222 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
226 static struct pid_entry tid_base_stuff[] = {
227 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
228 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
229 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
230 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
231 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
232 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
233 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
234 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
236 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
238 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
239 #ifdef CONFIG_SECCOMP
240 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
242 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
243 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
244 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
245 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
247 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
249 #ifdef CONFIG_SECURITY
250 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
252 #ifdef CONFIG_KALLSYMS
253 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
255 #ifdef CONFIG_SCHEDSTATS
256 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
258 #ifdef CONFIG_CPUSETS
259 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
261 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
262 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
263 #ifdef CONFIG_AUDITSYSCALL
264 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
269 #ifdef CONFIG_SECURITY
270 static struct pid_entry tgid_attr_stuff[] = {
271 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
272 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
273 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
274 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
277 static struct pid_entry tid_attr_stuff[] = {
278 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
279 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
280 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
281 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
288 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
290 struct task_struct *task = proc_task(inode);
291 struct files_struct *files;
293 int fd = proc_type(inode) - PROC_TID_FD_DIR;
295 files = get_files_struct(task);
298 file = fcheck_files(files, fd);
300 *mnt = mntget(file->f_vfsmnt);
301 *dentry = dget(file->f_dentry);
303 put_files_struct(files);
307 put_files_struct(files);
312 static struct fs_struct *get_fs_struct(struct task_struct *task)
314 struct fs_struct *fs;
318 atomic_inc(&fs->count);
323 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
325 struct fs_struct *fs = get_fs_struct(proc_task(inode));
326 int result = -ENOENT;
328 read_lock(&fs->lock);
329 *mnt = mntget(fs->pwdmnt);
330 *dentry = dget(fs->pwd);
331 read_unlock(&fs->lock);
338 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
340 struct fs_struct *fs = get_fs_struct(proc_task(inode));
341 int result = -ENOENT;
343 read_lock(&fs->lock);
344 *mnt = mntget(fs->rootmnt);
345 *dentry = dget(fs->root);
346 read_unlock(&fs->lock);
354 /* Same as proc_root_link, but this addionally tries to get fs from other
355 * threads in the group */
356 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
357 struct vfsmount **mnt)
359 struct fs_struct *fs;
360 int result = -ENOENT;
361 struct task_struct *leader = proc_task(inode);
366 atomic_inc(&fs->count);
369 /* Try to get fs from other threads */
371 read_lock(&tasklist_lock);
372 if (pid_alive(leader)) {
373 struct task_struct *task = leader;
375 while ((task = next_thread(task)) != leader) {
379 atomic_inc(&fs->count);
386 read_unlock(&tasklist_lock);
390 read_lock(&fs->lock);
391 *mnt = mntget(fs->rootmnt);
392 *dentry = dget(fs->root);
393 read_unlock(&fs->lock);
401 #define MAY_PTRACE(task) \
402 (task == current || \
403 (task->parent == current && \
404 (task->ptrace & PT_PTRACED) && \
405 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
406 security_ptrace(current,task) == 0))
408 static int proc_pid_environ(struct task_struct *task, char * buffer)
411 struct mm_struct *mm = get_task_mm(task);
413 unsigned int len = mm->env_end - mm->env_start;
416 res = access_process_vm(task, mm->env_start, buffer, len, 0);
417 if (!ptrace_may_attach(task))
424 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
428 struct mm_struct *mm = get_task_mm(task);
432 goto out_mm; /* Shh! No looking before we're done */
434 len = mm->arg_end - mm->arg_start;
439 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
441 // If the nul at the end of args has been overwritten, then
442 // assume application is using setproctitle(3).
443 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
444 len = strnlen(buffer, res);
448 len = mm->env_end - mm->env_start;
449 if (len > PAGE_SIZE - res)
450 len = PAGE_SIZE - res;
451 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
452 res = strnlen(buffer, res);
461 static int proc_pid_auxv(struct task_struct *task, char *buffer)
464 struct mm_struct *mm = get_task_mm(task);
466 unsigned int nwords = 0;
469 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
470 res = nwords * sizeof(mm->saved_auxv[0]);
473 memcpy(buffer, mm->saved_auxv, res);
480 #ifdef CONFIG_KALLSYMS
482 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
483 * Returns the resolved symbol. If that fails, simply return the address.
485 static int proc_pid_wchan(struct task_struct *task, char *buffer)
488 const char *sym_name;
489 unsigned long wchan, size, offset;
490 char namebuf[KSYM_NAME_LEN+1];
492 wchan = get_wchan(task);
494 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
496 return sprintf(buffer, "%s", sym_name);
497 return sprintf(buffer, "%lu", wchan);
499 #endif /* CONFIG_KALLSYMS */
501 #ifdef CONFIG_SCHEDSTATS
503 * Provides /proc/PID/schedstat
505 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
507 return sprintf(buffer, "%lu %lu %lu\n",
508 task->sched_info.cpu_time,
509 task->sched_info.run_delay,
510 task->sched_info.pcnt);
514 /* The badness from the OOM killer */
515 unsigned long badness(struct task_struct *p, unsigned long uptime);
516 static int proc_oom_score(struct task_struct *task, char *buffer)
518 unsigned long points;
519 struct timespec uptime;
521 do_posix_clock_monotonic_gettime(&uptime);
522 points = badness(task, uptime.tv_sec);
523 return sprintf(buffer, "%lu\n", points);
526 /************************************************************************/
527 /* Here the fs part begins */
528 /************************************************************************/
530 /* permission checks */
532 /* If the process being read is separated by chroot from the reading process,
533 * don't let the reader access the threads.
535 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
537 struct dentry *de, *base;
538 struct vfsmount *our_vfsmnt, *mnt;
540 read_lock(¤t->fs->lock);
541 our_vfsmnt = mntget(current->fs->rootmnt);
542 base = dget(current->fs->root);
543 read_unlock(¤t->fs->lock);
545 spin_lock(&vfsmount_lock);
549 while (vfsmnt != our_vfsmnt) {
550 if (vfsmnt == vfsmnt->mnt_parent)
552 de = vfsmnt->mnt_mountpoint;
553 vfsmnt = vfsmnt->mnt_parent;
556 if (!is_subdir(de, base))
558 spin_unlock(&vfsmount_lock);
567 spin_unlock(&vfsmount_lock);
572 static int proc_check_root(struct inode *inode)
575 struct vfsmount *vfsmnt;
577 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
579 return proc_check_chroot(root, vfsmnt);
582 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
584 if (generic_permission(inode, mask, NULL) != 0)
586 return proc_check_root(inode);
589 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
592 struct vfsmount *vfsmnt;
594 if (generic_permission(inode, mask, NULL) != 0)
597 if (proc_task_root_link(inode, &root, &vfsmnt))
600 return proc_check_chroot(root, vfsmnt);
603 extern struct seq_operations proc_pid_maps_op;
604 static int maps_open(struct inode *inode, struct file *file)
606 struct task_struct *task = proc_task(inode);
607 int ret = seq_open(file, &proc_pid_maps_op);
609 struct seq_file *m = file->private_data;
615 static struct file_operations proc_maps_operations = {
619 .release = seq_release,
623 extern struct seq_operations proc_pid_numa_maps_op;
624 static int numa_maps_open(struct inode *inode, struct file *file)
626 struct task_struct *task = proc_task(inode);
627 int ret = seq_open(file, &proc_pid_numa_maps_op);
629 struct seq_file *m = file->private_data;
635 static struct file_operations proc_numa_maps_operations = {
636 .open = numa_maps_open,
639 .release = seq_release,
644 extern struct seq_operations proc_pid_smaps_op;
645 static int smaps_open(struct inode *inode, struct file *file)
647 struct task_struct *task = proc_task(inode);
648 int ret = seq_open(file, &proc_pid_smaps_op);
650 struct seq_file *m = file->private_data;
656 static struct file_operations proc_smaps_operations = {
660 .release = seq_release,
664 extern struct seq_operations mounts_op;
670 static int mounts_open(struct inode *inode, struct file *file)
672 struct task_struct *task = proc_task(inode);
673 struct namespace *namespace;
674 struct proc_mounts *p;
678 namespace = task->namespace;
680 get_namespace(namespace);
685 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
687 file->private_data = &p->m;
688 ret = seq_open(file, &mounts_op);
690 p->m.private = namespace;
691 p->event = namespace->event;
696 put_namespace(namespace);
701 static int mounts_release(struct inode *inode, struct file *file)
703 struct seq_file *m = file->private_data;
704 struct namespace *namespace = m->private;
705 put_namespace(namespace);
706 return seq_release(inode, file);
709 static unsigned mounts_poll(struct file *file, poll_table *wait)
711 struct proc_mounts *p = file->private_data;
712 struct namespace *ns = p->m.private;
715 poll_wait(file, &ns->poll, wait);
717 spin_lock(&vfsmount_lock);
718 if (p->event != ns->event) {
719 p->event = ns->event;
722 spin_unlock(&vfsmount_lock);
727 static struct file_operations proc_mounts_operations = {
731 .release = mounts_release,
735 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
737 static ssize_t proc_info_read(struct file * file, char __user * buf,
738 size_t count, loff_t *ppos)
740 struct inode * inode = file->f_dentry->d_inode;
743 struct task_struct *task = proc_task(inode);
745 if (count > PROC_BLOCK_SIZE)
746 count = PROC_BLOCK_SIZE;
747 if (!(page = __get_free_page(GFP_KERNEL)))
750 length = PROC_I(inode)->op.proc_read(task, (char*)page);
753 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
758 static struct file_operations proc_info_file_operations = {
759 .read = proc_info_read,
762 static int mem_open(struct inode* inode, struct file* file)
764 file->private_data = (void*)((long)current->self_exec_id);
768 static ssize_t mem_read(struct file * file, char __user * buf,
769 size_t count, loff_t *ppos)
771 struct task_struct *task = proc_task(file->f_dentry->d_inode);
773 unsigned long src = *ppos;
775 struct mm_struct *mm;
777 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
781 page = (char *)__get_free_page(GFP_USER);
787 mm = get_task_mm(task);
793 if (file->private_data != (void*)((long)current->self_exec_id))
799 int this_len, retval;
801 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
802 retval = access_process_vm(task, src, page, this_len, 0);
803 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
809 if (copy_to_user(buf, page, retval)) {
824 free_page((unsigned long) page);
829 #define mem_write NULL
832 /* This is a security hazard */
833 static ssize_t mem_write(struct file * file, const char * buf,
834 size_t count, loff_t *ppos)
838 struct task_struct *task = proc_task(file->f_dentry->d_inode);
839 unsigned long dst = *ppos;
841 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
844 page = (char *)__get_free_page(GFP_USER);
849 int this_len, retval;
851 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
852 if (copy_from_user(page, buf, this_len)) {
856 retval = access_process_vm(task, dst, page, this_len, 1);
868 free_page((unsigned long) page);
873 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
877 file->f_pos = offset;
880 file->f_pos += offset;
885 force_successful_syscall_return();
889 static struct file_operations proc_mem_operations = {
896 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
897 size_t count, loff_t *ppos)
899 struct task_struct *task = proc_task(file->f_dentry->d_inode);
902 int oom_adjust = task->oomkilladj;
903 loff_t __ppos = *ppos;
905 len = sprintf(buffer, "%i\n", oom_adjust);
908 if (count > len-__ppos)
910 if (copy_to_user(buf, buffer + __ppos, count))
912 *ppos = __ppos + count;
916 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
917 size_t count, loff_t *ppos)
919 struct task_struct *task = proc_task(file->f_dentry->d_inode);
920 char buffer[8], *end;
923 if (!capable(CAP_SYS_RESOURCE))
925 memset(buffer, 0, 8);
928 if (copy_from_user(buffer, buf, count))
930 oom_adjust = simple_strtol(buffer, &end, 0);
931 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
935 task->oomkilladj = oom_adjust;
936 if (end - buffer == 0)
941 static struct file_operations proc_oom_adjust_operations = {
942 .read = oom_adjust_read,
943 .write = oom_adjust_write,
946 static struct inode_operations proc_mem_inode_operations = {
947 .permission = proc_permission,
950 #ifdef CONFIG_AUDITSYSCALL
952 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
953 size_t count, loff_t *ppos)
955 struct inode * inode = file->f_dentry->d_inode;
956 struct task_struct *task = proc_task(inode);
958 char tmpbuf[TMPBUFLEN];
960 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
961 audit_get_loginuid(task->audit_context));
962 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
965 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
966 size_t count, loff_t *ppos)
968 struct inode * inode = file->f_dentry->d_inode;
971 struct task_struct *task = proc_task(inode);
974 if (!capable(CAP_AUDIT_CONTROL))
980 if (count > PAGE_SIZE)
984 /* No partial writes. */
987 page = (char*)__get_free_page(GFP_USER);
991 if (copy_from_user(page, buf, count))
994 loginuid = simple_strtoul(page, &tmp, 10);
1000 length = audit_set_loginuid(task, loginuid);
1001 if (likely(length == 0))
1005 free_page((unsigned long) page);
1009 static struct file_operations proc_loginuid_operations = {
1010 .read = proc_loginuid_read,
1011 .write = proc_loginuid_write,
1015 #ifdef CONFIG_SECCOMP
1016 static ssize_t seccomp_read(struct file *file, char __user *buf,
1017 size_t count, loff_t *ppos)
1019 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1021 loff_t __ppos = *ppos;
1024 /* no need to print the trailing zero, so use only len */
1025 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1028 if (count > len - __ppos)
1029 count = len - __ppos;
1030 if (copy_to_user(buf, __buf + __ppos, count))
1032 *ppos = __ppos + count;
1036 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1037 size_t count, loff_t *ppos)
1039 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1040 char __buf[20], *end;
1041 unsigned int seccomp_mode;
1043 /* can set it only once to be even more secure */
1044 if (unlikely(tsk->seccomp.mode))
1047 memset(__buf, 0, sizeof(__buf));
1048 count = min(count, sizeof(__buf) - 1);
1049 if (copy_from_user(__buf, buf, count))
1051 seccomp_mode = simple_strtoul(__buf, &end, 0);
1054 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1055 tsk->seccomp.mode = seccomp_mode;
1056 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1059 if (unlikely(!(end - __buf)))
1064 static struct file_operations proc_seccomp_operations = {
1065 .read = seccomp_read,
1066 .write = seccomp_write,
1068 #endif /* CONFIG_SECCOMP */
1070 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1072 struct inode *inode = dentry->d_inode;
1073 int error = -EACCES;
1075 /* We don't need a base pointer in the /proc filesystem */
1078 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1080 error = proc_check_root(inode);
1084 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1085 nd->last_type = LAST_BIND;
1087 return ERR_PTR(error);
1090 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1091 char __user *buffer, int buflen)
1093 struct inode * inode;
1094 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1100 inode = dentry->d_inode;
1101 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1102 len = PTR_ERR(path);
1105 len = tmp + PAGE_SIZE - 1 - path;
1109 if (copy_to_user(buffer, path, len))
1112 free_page((unsigned long)tmp);
1116 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1118 int error = -EACCES;
1119 struct inode *inode = dentry->d_inode;
1121 struct vfsmount *mnt = NULL;
1125 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1127 error = proc_check_root(inode);
1131 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1135 error = do_proc_readlink(de, mnt, buffer, buflen);
1143 static struct inode_operations proc_pid_link_inode_operations = {
1144 .readlink = proc_pid_readlink,
1145 .follow_link = proc_pid_follow_link
1150 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1152 struct inode *inode = filp->f_dentry->d_inode;
1153 struct task_struct *p = proc_task(inode);
1154 unsigned int fd, tid, ino;
1157 struct files_struct * files;
1158 struct fdtable *fdt;
1169 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1173 ino = fake_ino(tid, PROC_TID_INO);
1174 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1178 files = get_files_struct(p);
1182 fdt = files_fdtable(files);
1183 for (fd = filp->f_pos-2;
1185 fd++, filp->f_pos++) {
1188 if (!fcheck_files(files, fd))
1196 buf[j] = '0' + (i % 10);
1200 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1201 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1208 put_files_struct(files);
1214 static int proc_pident_readdir(struct file *filp,
1215 void *dirent, filldir_t filldir,
1216 struct pid_entry *ents, unsigned int nents)
1220 struct dentry *dentry = filp->f_dentry;
1221 struct inode *inode = dentry->d_inode;
1222 struct pid_entry *p;
1227 if (!pid_alive(proc_task(inode)))
1231 pid = proc_task(inode)->pid;
1236 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1242 ino = parent_ino(dentry);
1243 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1256 if (filldir(dirent, p->name, p->len, filp->f_pos,
1257 fake_ino(pid, p->type), p->mode >> 12) < 0)
1269 static int proc_tgid_base_readdir(struct file * filp,
1270 void * dirent, filldir_t filldir)
1272 return proc_pident_readdir(filp,dirent,filldir,
1273 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1276 static int proc_tid_base_readdir(struct file * filp,
1277 void * dirent, filldir_t filldir)
1279 return proc_pident_readdir(filp,dirent,filldir,
1280 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1283 /* building an inode */
1285 static int task_dumpable(struct task_struct *task)
1288 struct mm_struct *mm;
1293 dumpable = mm->dumpable;
1301 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1303 struct inode * inode;
1304 struct proc_inode *ei;
1306 /* We need a new inode */
1308 inode = new_inode(sb);
1315 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1316 inode->i_ino = fake_ino(task->pid, ino);
1318 if (!pid_alive(task))
1322 * grab the reference to task.
1324 get_task_struct(task);
1329 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1330 inode->i_uid = task->euid;
1331 inode->i_gid = task->egid;
1333 security_task_to_inode(task, inode);
1347 * Exceptional case: normally we are not allowed to unhash a busy
1348 * directory. In this case, however, we can do it - no aliasing problems
1349 * due to the way we treat inodes.
1351 * Rewrite the inode's ownerships here because the owning task may have
1352 * performed a setuid(), etc.
1354 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1356 struct inode *inode = dentry->d_inode;
1357 struct task_struct *task = proc_task(inode);
1358 if (pid_alive(task)) {
1359 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1360 inode->i_uid = task->euid;
1361 inode->i_gid = task->egid;
1366 security_task_to_inode(task, inode);
1373 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1375 struct inode *inode = dentry->d_inode;
1376 struct task_struct *task = proc_task(inode);
1377 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1378 struct files_struct *files;
1380 files = get_files_struct(task);
1383 if (fcheck_files(files, fd)) {
1385 put_files_struct(files);
1386 if (task_dumpable(task)) {
1387 inode->i_uid = task->euid;
1388 inode->i_gid = task->egid;
1393 security_task_to_inode(task, inode);
1397 put_files_struct(files);
1403 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1405 struct task_struct *task = proc_task(inode);
1406 spin_lock(&task->proc_lock);
1407 if (task->proc_dentry == dentry)
1408 task->proc_dentry = NULL;
1409 spin_unlock(&task->proc_lock);
1413 static int pid_delete_dentry(struct dentry * dentry)
1415 /* Is the task we represent dead?
1416 * If so, then don't put the dentry on the lru list,
1417 * kill it immediately.
1419 return !pid_alive(proc_task(dentry->d_inode));
1422 static struct dentry_operations tid_fd_dentry_operations =
1424 .d_revalidate = tid_fd_revalidate,
1425 .d_delete = pid_delete_dentry,
1428 static struct dentry_operations pid_dentry_operations =
1430 .d_revalidate = pid_revalidate,
1431 .d_delete = pid_delete_dentry,
1434 static struct dentry_operations pid_base_dentry_operations =
1436 .d_revalidate = pid_revalidate,
1437 .d_iput = pid_base_iput,
1438 .d_delete = pid_delete_dentry,
1443 static unsigned name_to_int(struct dentry *dentry)
1445 const char *name = dentry->d_name.name;
1446 int len = dentry->d_name.len;
1449 if (len > 1 && *name == '0')
1452 unsigned c = *name++ - '0';
1455 if (n >= (~0U-9)/10)
1466 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1468 struct task_struct *task = proc_task(dir);
1469 unsigned fd = name_to_int(dentry);
1471 struct files_struct * files;
1472 struct inode *inode;
1473 struct proc_inode *ei;
1477 if (!pid_alive(task))
1480 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1484 files = get_files_struct(task);
1487 inode->i_mode = S_IFLNK;
1489 file = fcheck_files(files, fd);
1492 if (file->f_mode & 1)
1493 inode->i_mode |= S_IRUSR | S_IXUSR;
1494 if (file->f_mode & 2)
1495 inode->i_mode |= S_IWUSR | S_IXUSR;
1497 put_files_struct(files);
1498 inode->i_op = &proc_pid_link_inode_operations;
1500 ei->op.proc_get_link = proc_fd_link;
1501 dentry->d_op = &tid_fd_dentry_operations;
1502 d_add(dentry, inode);
1507 put_files_struct(files);
1511 return ERR_PTR(-ENOENT);
1514 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1515 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1517 static struct file_operations proc_fd_operations = {
1518 .read = generic_read_dir,
1519 .readdir = proc_readfd,
1522 static struct file_operations proc_task_operations = {
1523 .read = generic_read_dir,
1524 .readdir = proc_task_readdir,
1528 * proc directories can do almost nothing..
1530 static struct inode_operations proc_fd_inode_operations = {
1531 .lookup = proc_lookupfd,
1532 .permission = proc_permission,
1535 static struct inode_operations proc_task_inode_operations = {
1536 .lookup = proc_task_lookup,
1537 .permission = proc_task_permission,
1540 #ifdef CONFIG_SECURITY
1541 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1542 size_t count, loff_t *ppos)
1544 struct inode * inode = file->f_dentry->d_inode;
1547 struct task_struct *task = proc_task(inode);
1549 if (count > PAGE_SIZE)
1551 if (!(page = __get_free_page(GFP_KERNEL)))
1554 length = security_getprocattr(task,
1555 (char*)file->f_dentry->d_name.name,
1556 (void*)page, count);
1558 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1563 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1564 size_t count, loff_t *ppos)
1566 struct inode * inode = file->f_dentry->d_inode;
1569 struct task_struct *task = proc_task(inode);
1571 if (count > PAGE_SIZE)
1574 /* No partial writes. */
1577 page = (char*)__get_free_page(GFP_USER);
1581 if (copy_from_user(page, buf, count))
1584 length = security_setprocattr(task,
1585 (char*)file->f_dentry->d_name.name,
1586 (void*)page, count);
1588 free_page((unsigned long) page);
1592 static struct file_operations proc_pid_attr_operations = {
1593 .read = proc_pid_attr_read,
1594 .write = proc_pid_attr_write,
1597 static struct file_operations proc_tid_attr_operations;
1598 static struct inode_operations proc_tid_attr_inode_operations;
1599 static struct file_operations proc_tgid_attr_operations;
1600 static struct inode_operations proc_tgid_attr_inode_operations;
1603 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1606 static struct dentry *proc_pident_lookup(struct inode *dir,
1607 struct dentry *dentry,
1608 struct pid_entry *ents)
1610 struct inode *inode;
1612 struct task_struct *task = proc_task(dir);
1613 struct pid_entry *p;
1614 struct proc_inode *ei;
1619 if (!pid_alive(task))
1622 for (p = ents; p->name; p++) {
1623 if (p->len != dentry->d_name.len)
1625 if (!memcmp(dentry->d_name.name, p->name, p->len))
1632 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1637 inode->i_mode = p->mode;
1639 * Yes, it does not scale. And it should not. Don't add
1640 * new entries into /proc/<tgid>/ without very good reasons.
1643 case PROC_TGID_TASK:
1644 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1645 inode->i_op = &proc_task_inode_operations;
1646 inode->i_fop = &proc_task_operations;
1651 inode->i_op = &proc_fd_inode_operations;
1652 inode->i_fop = &proc_fd_operations;
1656 inode->i_op = &proc_pid_link_inode_operations;
1657 ei->op.proc_get_link = proc_exe_link;
1661 inode->i_op = &proc_pid_link_inode_operations;
1662 ei->op.proc_get_link = proc_cwd_link;
1665 case PROC_TGID_ROOT:
1666 inode->i_op = &proc_pid_link_inode_operations;
1667 ei->op.proc_get_link = proc_root_link;
1669 case PROC_TID_ENVIRON:
1670 case PROC_TGID_ENVIRON:
1671 inode->i_fop = &proc_info_file_operations;
1672 ei->op.proc_read = proc_pid_environ;
1675 case PROC_TGID_AUXV:
1676 inode->i_fop = &proc_info_file_operations;
1677 ei->op.proc_read = proc_pid_auxv;
1679 case PROC_TID_STATUS:
1680 case PROC_TGID_STATUS:
1681 inode->i_fop = &proc_info_file_operations;
1682 ei->op.proc_read = proc_pid_status;
1685 inode->i_fop = &proc_info_file_operations;
1686 ei->op.proc_read = proc_tid_stat;
1688 case PROC_TGID_STAT:
1689 inode->i_fop = &proc_info_file_operations;
1690 ei->op.proc_read = proc_tgid_stat;
1692 case PROC_TID_CMDLINE:
1693 case PROC_TGID_CMDLINE:
1694 inode->i_fop = &proc_info_file_operations;
1695 ei->op.proc_read = proc_pid_cmdline;
1697 case PROC_TID_STATM:
1698 case PROC_TGID_STATM:
1699 inode->i_fop = &proc_info_file_operations;
1700 ei->op.proc_read = proc_pid_statm;
1703 case PROC_TGID_MAPS:
1704 inode->i_fop = &proc_maps_operations;
1707 case PROC_TID_NUMA_MAPS:
1708 case PROC_TGID_NUMA_MAPS:
1709 inode->i_fop = &proc_numa_maps_operations;
1714 inode->i_op = &proc_mem_inode_operations;
1715 inode->i_fop = &proc_mem_operations;
1717 #ifdef CONFIG_SECCOMP
1718 case PROC_TID_SECCOMP:
1719 case PROC_TGID_SECCOMP:
1720 inode->i_fop = &proc_seccomp_operations;
1722 #endif /* CONFIG_SECCOMP */
1723 case PROC_TID_MOUNTS:
1724 case PROC_TGID_MOUNTS:
1725 inode->i_fop = &proc_mounts_operations;
1728 case PROC_TID_SMAPS:
1729 case PROC_TGID_SMAPS:
1730 inode->i_fop = &proc_smaps_operations;
1733 #ifdef CONFIG_SECURITY
1736 inode->i_op = &proc_tid_attr_inode_operations;
1737 inode->i_fop = &proc_tid_attr_operations;
1739 case PROC_TGID_ATTR:
1741 inode->i_op = &proc_tgid_attr_inode_operations;
1742 inode->i_fop = &proc_tgid_attr_operations;
1744 case PROC_TID_ATTR_CURRENT:
1745 case PROC_TGID_ATTR_CURRENT:
1746 case PROC_TID_ATTR_PREV:
1747 case PROC_TGID_ATTR_PREV:
1748 case PROC_TID_ATTR_EXEC:
1749 case PROC_TGID_ATTR_EXEC:
1750 case PROC_TID_ATTR_FSCREATE:
1751 case PROC_TGID_ATTR_FSCREATE:
1752 inode->i_fop = &proc_pid_attr_operations;
1755 #ifdef CONFIG_KALLSYMS
1756 case PROC_TID_WCHAN:
1757 case PROC_TGID_WCHAN:
1758 inode->i_fop = &proc_info_file_operations;
1759 ei->op.proc_read = proc_pid_wchan;
1762 #ifdef CONFIG_SCHEDSTATS
1763 case PROC_TID_SCHEDSTAT:
1764 case PROC_TGID_SCHEDSTAT:
1765 inode->i_fop = &proc_info_file_operations;
1766 ei->op.proc_read = proc_pid_schedstat;
1769 #ifdef CONFIG_CPUSETS
1770 case PROC_TID_CPUSET:
1771 case PROC_TGID_CPUSET:
1772 inode->i_fop = &proc_cpuset_operations;
1775 case PROC_TID_OOM_SCORE:
1776 case PROC_TGID_OOM_SCORE:
1777 inode->i_fop = &proc_info_file_operations;
1778 ei->op.proc_read = proc_oom_score;
1780 case PROC_TID_OOM_ADJUST:
1781 case PROC_TGID_OOM_ADJUST:
1782 inode->i_fop = &proc_oom_adjust_operations;
1784 #ifdef CONFIG_AUDITSYSCALL
1785 case PROC_TID_LOGINUID:
1786 case PROC_TGID_LOGINUID:
1787 inode->i_fop = &proc_loginuid_operations;
1791 printk("procfs: impossible type (%d)",p->type);
1793 return ERR_PTR(-EINVAL);
1795 dentry->d_op = &pid_dentry_operations;
1796 d_add(dentry, inode);
1800 return ERR_PTR(error);
1803 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1804 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1807 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1808 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1811 static struct file_operations proc_tgid_base_operations = {
1812 .read = generic_read_dir,
1813 .readdir = proc_tgid_base_readdir,
1816 static struct file_operations proc_tid_base_operations = {
1817 .read = generic_read_dir,
1818 .readdir = proc_tid_base_readdir,
1821 static struct inode_operations proc_tgid_base_inode_operations = {
1822 .lookup = proc_tgid_base_lookup,
1825 static struct inode_operations proc_tid_base_inode_operations = {
1826 .lookup = proc_tid_base_lookup,
1829 #ifdef CONFIG_SECURITY
1830 static int proc_tgid_attr_readdir(struct file * filp,
1831 void * dirent, filldir_t filldir)
1833 return proc_pident_readdir(filp,dirent,filldir,
1834 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1837 static int proc_tid_attr_readdir(struct file * filp,
1838 void * dirent, filldir_t filldir)
1840 return proc_pident_readdir(filp,dirent,filldir,
1841 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1844 static struct file_operations proc_tgid_attr_operations = {
1845 .read = generic_read_dir,
1846 .readdir = proc_tgid_attr_readdir,
1849 static struct file_operations proc_tid_attr_operations = {
1850 .read = generic_read_dir,
1851 .readdir = proc_tid_attr_readdir,
1854 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1855 struct dentry *dentry, struct nameidata *nd)
1857 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1860 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1861 struct dentry *dentry, struct nameidata *nd)
1863 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1866 static struct inode_operations proc_tgid_attr_inode_operations = {
1867 .lookup = proc_tgid_attr_lookup,
1870 static struct inode_operations proc_tid_attr_inode_operations = {
1871 .lookup = proc_tid_attr_lookup,
1878 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1882 sprintf(tmp, "%d", current->tgid);
1883 return vfs_readlink(dentry,buffer,buflen,tmp);
1886 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1889 sprintf(tmp, "%d", current->tgid);
1890 return ERR_PTR(vfs_follow_link(nd,tmp));
1893 static struct inode_operations proc_self_inode_operations = {
1894 .readlink = proc_self_readlink,
1895 .follow_link = proc_self_follow_link,
1899 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1900 * @p: task that should be flushed.
1902 * Drops the /proc/@pid dcache entry from the hash chains.
1904 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1905 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1906 * if the pid value is immediately reused. This is enforced by
1907 * - caller must acquire spin_lock(p->proc_lock)
1908 * - must be called before detach_pid()
1909 * - proc_pid_lookup acquires proc_lock, and checks that
1910 * the target is not dead by looking at the attach count
1914 struct dentry *proc_pid_unhash(struct task_struct *p)
1916 struct dentry *proc_dentry;
1918 proc_dentry = p->proc_dentry;
1919 if (proc_dentry != NULL) {
1921 spin_lock(&dcache_lock);
1922 spin_lock(&proc_dentry->d_lock);
1923 if (!d_unhashed(proc_dentry)) {
1924 dget_locked(proc_dentry);
1925 __d_drop(proc_dentry);
1926 spin_unlock(&proc_dentry->d_lock);
1928 spin_unlock(&proc_dentry->d_lock);
1931 spin_unlock(&dcache_lock);
1937 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1938 * @proc_dentry: directoy to prune.
1940 * Shrink the /proc directory that was used by the just killed thread.
1943 void proc_pid_flush(struct dentry *proc_dentry)
1946 if(proc_dentry != NULL) {
1947 shrink_dcache_parent(proc_dentry);
1953 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1955 struct task_struct *task;
1956 struct inode *inode;
1957 struct proc_inode *ei;
1961 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1962 inode = new_inode(dir->i_sb);
1964 return ERR_PTR(-ENOMEM);
1966 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1967 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1969 inode->i_mode = S_IFLNK|S_IRWXUGO;
1970 inode->i_uid = inode->i_gid = 0;
1972 inode->i_op = &proc_self_inode_operations;
1973 d_add(dentry, inode);
1976 tgid = name_to_int(dentry);
1980 read_lock(&tasklist_lock);
1981 task = find_task_by_pid(tgid);
1983 get_task_struct(task);
1984 read_unlock(&tasklist_lock);
1988 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1992 put_task_struct(task);
1995 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1996 inode->i_op = &proc_tgid_base_inode_operations;
1997 inode->i_fop = &proc_tgid_base_operations;
1998 inode->i_flags|=S_IMMUTABLE;
1999 #ifdef CONFIG_SECURITY
2005 dentry->d_op = &pid_base_dentry_operations;
2008 d_add(dentry, inode);
2009 spin_lock(&task->proc_lock);
2010 task->proc_dentry = dentry;
2011 if (!pid_alive(task)) {
2012 dentry = proc_pid_unhash(task);
2015 spin_unlock(&task->proc_lock);
2017 put_task_struct(task);
2019 proc_pid_flush(dentry);
2024 return ERR_PTR(-ENOENT);
2028 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2030 struct task_struct *task;
2031 struct task_struct *leader = proc_task(dir);
2032 struct inode *inode;
2035 tid = name_to_int(dentry);
2039 read_lock(&tasklist_lock);
2040 task = find_task_by_pid(tid);
2042 get_task_struct(task);
2043 read_unlock(&tasklist_lock);
2046 if (leader->tgid != task->tgid)
2049 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2054 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2055 inode->i_op = &proc_tid_base_inode_operations;
2056 inode->i_fop = &proc_tid_base_operations;
2057 inode->i_flags|=S_IMMUTABLE;
2058 #ifdef CONFIG_SECURITY
2064 dentry->d_op = &pid_base_dentry_operations;
2066 d_add(dentry, inode);
2068 put_task_struct(task);
2071 put_task_struct(task);
2073 return ERR_PTR(-ENOENT);
2076 #define PROC_NUMBUF 10
2077 #define PROC_MAXPIDS 20
2080 * Get a few tgid's to return for filldir - we need to hold the
2081 * tasklist lock while doing this, and we must release it before
2082 * we actually do the filldir itself, so we use a temp buffer..
2084 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2086 struct task_struct *p;
2090 read_lock(&tasklist_lock);
2093 p = find_task_by_pid(version);
2094 if (p && !thread_group_leader(p))
2101 p = next_task(&init_task);
2103 for ( ; p != &init_task; p = next_task(p)) {
2109 tgids[nr_tgids] = tgid;
2111 if (nr_tgids >= PROC_MAXPIDS)
2114 read_unlock(&tasklist_lock);
2119 * Get a few tid's to return for filldir - we need to hold the
2120 * tasklist lock while doing this, and we must release it before
2121 * we actually do the filldir itself, so we use a temp buffer..
2123 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2125 struct task_struct *leader_task = proc_task(dir);
2126 struct task_struct *task = leader_task;
2130 read_lock(&tasklist_lock);
2132 * The starting point task (leader_task) might be an already
2133 * unlinked task, which cannot be used to access the task-list
2134 * via next_thread().
2136 if (pid_alive(task)) do {
2137 int tid = task->pid;
2142 tids[nr_tids] = tid;
2144 if (nr_tids >= PROC_MAXPIDS)
2146 } while ((task = next_thread(task)) != leader_task);
2147 read_unlock(&tasklist_lock);
2151 /* for the /proc/ directory itself, after non-process stuff has been done */
2152 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2154 unsigned int tgid_array[PROC_MAXPIDS];
2155 char buf[PROC_NUMBUF];
2156 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2157 unsigned int nr_tgids, i;
2161 ino_t ino = fake_ino(0,PROC_TGID_INO);
2162 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2168 /* f_version caches the tgid value that the last readdir call couldn't
2169 * return. lseek aka telldir automagically resets f_version to 0.
2171 next_tgid = filp->f_version;
2172 filp->f_version = 0;
2174 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2176 /* no more entries ! */
2181 /* do not use the last found pid, reserve it for next_tgid */
2182 if (nr_tgids == PROC_MAXPIDS) {
2184 next_tgid = tgid_array[nr_tgids];
2187 for (i=0;i<nr_tgids;i++) {
2188 int tgid = tgid_array[i];
2189 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2190 unsigned long j = PROC_NUMBUF;
2193 buf[--j] = '0' + (tgid % 10);
2194 while ((tgid /= 10) != 0);
2196 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2197 /* returning this tgid failed, save it as the first
2198 * pid for the next readir call */
2199 filp->f_version = tgid_array[i];
2210 /* for the /proc/TGID/task/ directories */
2211 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2213 unsigned int tid_array[PROC_MAXPIDS];
2214 char buf[PROC_NUMBUF];
2215 unsigned int nr_tids, i;
2216 struct dentry *dentry = filp->f_dentry;
2217 struct inode *inode = dentry->d_inode;
2218 int retval = -ENOENT;
2220 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2222 if (!pid_alive(proc_task(inode)))
2229 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2234 ino = parent_ino(dentry);
2235 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2241 nr_tids = get_tid_list(pos, tid_array, inode);
2242 inode->i_nlink = pos + nr_tids;
2244 for (i = 0; i < nr_tids; i++) {
2245 unsigned long j = PROC_NUMBUF;
2246 int tid = tid_array[i];
2248 ino = fake_ino(tid,PROC_TID_INO);
2251 buf[--j] = '0' + (tid % 10);
2252 while ((tid /= 10) != 0);
2254 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)