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 {
107 PROC_TGID_MOUNTSTATS,
112 #ifdef CONFIG_SCHEDSTATS
115 #ifdef CONFIG_CPUSETS
118 #ifdef CONFIG_SECURITY
120 PROC_TGID_ATTR_CURRENT,
123 PROC_TGID_ATTR_FSCREATE,
125 #ifdef CONFIG_AUDITSYSCALL
129 PROC_TGID_OOM_ADJUST,
133 #ifdef CONFIG_SECCOMP
153 #ifdef CONFIG_SCHEDSTATS
156 #ifdef CONFIG_CPUSETS
159 #ifdef CONFIG_SECURITY
161 PROC_TID_ATTR_CURRENT,
164 PROC_TID_ATTR_FSCREATE,
166 #ifdef CONFIG_AUDITSYSCALL
172 /* Add new entries before this */
173 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
183 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
185 static struct pid_entry tgid_base_stuff[] = {
186 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
187 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
188 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
189 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
190 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
191 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
192 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
193 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
194 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
196 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
198 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
199 #ifdef CONFIG_SECCOMP
200 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
202 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
203 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
204 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
205 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
206 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
208 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
210 #ifdef CONFIG_SECURITY
211 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
213 #ifdef CONFIG_KALLSYMS
214 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
216 #ifdef CONFIG_SCHEDSTATS
217 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
219 #ifdef CONFIG_CPUSETS
220 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
222 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
223 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
224 #ifdef CONFIG_AUDITSYSCALL
225 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
229 static struct pid_entry tid_base_stuff[] = {
230 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
231 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
232 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
233 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
234 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
235 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
236 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
237 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
239 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
241 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
242 #ifdef CONFIG_SECCOMP
243 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
245 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
246 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
247 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
248 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
250 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
252 #ifdef CONFIG_SECURITY
253 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
255 #ifdef CONFIG_KALLSYMS
256 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
258 #ifdef CONFIG_SCHEDSTATS
259 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
261 #ifdef CONFIG_CPUSETS
262 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
264 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
265 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
266 #ifdef CONFIG_AUDITSYSCALL
267 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
272 #ifdef CONFIG_SECURITY
273 static struct pid_entry tgid_attr_stuff[] = {
274 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
275 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
276 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
277 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
280 static struct pid_entry tid_attr_stuff[] = {
281 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
282 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
283 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
284 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
291 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
293 struct task_struct *task = proc_task(inode);
294 struct files_struct *files;
296 int fd = proc_type(inode) - PROC_TID_FD_DIR;
298 files = get_files_struct(task);
301 file = fcheck_files(files, fd);
303 *mnt = mntget(file->f_vfsmnt);
304 *dentry = dget(file->f_dentry);
306 put_files_struct(files);
310 put_files_struct(files);
315 static struct fs_struct *get_fs_struct(struct task_struct *task)
317 struct fs_struct *fs;
321 atomic_inc(&fs->count);
326 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
328 struct fs_struct *fs = get_fs_struct(proc_task(inode));
329 int result = -ENOENT;
331 read_lock(&fs->lock);
332 *mnt = mntget(fs->pwdmnt);
333 *dentry = dget(fs->pwd);
334 read_unlock(&fs->lock);
341 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
343 struct fs_struct *fs = get_fs_struct(proc_task(inode));
344 int result = -ENOENT;
346 read_lock(&fs->lock);
347 *mnt = mntget(fs->rootmnt);
348 *dentry = dget(fs->root);
349 read_unlock(&fs->lock);
357 /* Same as proc_root_link, but this addionally tries to get fs from other
358 * threads in the group */
359 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
360 struct vfsmount **mnt)
362 struct fs_struct *fs;
363 int result = -ENOENT;
364 struct task_struct *leader = proc_task(inode);
369 atomic_inc(&fs->count);
372 /* Try to get fs from other threads */
374 read_lock(&tasklist_lock);
375 if (pid_alive(leader)) {
376 struct task_struct *task = leader;
378 while ((task = next_thread(task)) != leader) {
382 atomic_inc(&fs->count);
389 read_unlock(&tasklist_lock);
393 read_lock(&fs->lock);
394 *mnt = mntget(fs->rootmnt);
395 *dentry = dget(fs->root);
396 read_unlock(&fs->lock);
404 #define MAY_PTRACE(task) \
405 (task == current || \
406 (task->parent == current && \
407 (task->ptrace & PT_PTRACED) && \
408 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
409 security_ptrace(current,task) == 0))
411 static int proc_pid_environ(struct task_struct *task, char * buffer)
414 struct mm_struct *mm = get_task_mm(task);
416 unsigned int len = mm->env_end - mm->env_start;
419 res = access_process_vm(task, mm->env_start, buffer, len, 0);
420 if (!ptrace_may_attach(task))
427 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
431 struct mm_struct *mm = get_task_mm(task);
435 goto out_mm; /* Shh! No looking before we're done */
437 len = mm->arg_end - mm->arg_start;
442 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
444 // If the nul at the end of args has been overwritten, then
445 // assume application is using setproctitle(3).
446 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
447 len = strnlen(buffer, res);
451 len = mm->env_end - mm->env_start;
452 if (len > PAGE_SIZE - res)
453 len = PAGE_SIZE - res;
454 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
455 res = strnlen(buffer, res);
464 static int proc_pid_auxv(struct task_struct *task, char *buffer)
467 struct mm_struct *mm = get_task_mm(task);
469 unsigned int nwords = 0;
472 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
473 res = nwords * sizeof(mm->saved_auxv[0]);
476 memcpy(buffer, mm->saved_auxv, res);
483 #ifdef CONFIG_KALLSYMS
485 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
486 * Returns the resolved symbol. If that fails, simply return the address.
488 static int proc_pid_wchan(struct task_struct *task, char *buffer)
491 const char *sym_name;
492 unsigned long wchan, size, offset;
493 char namebuf[KSYM_NAME_LEN+1];
495 wchan = get_wchan(task);
497 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
499 return sprintf(buffer, "%s", sym_name);
500 return sprintf(buffer, "%lu", wchan);
502 #endif /* CONFIG_KALLSYMS */
504 #ifdef CONFIG_SCHEDSTATS
506 * Provides /proc/PID/schedstat
508 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
510 return sprintf(buffer, "%lu %lu %lu\n",
511 task->sched_info.cpu_time,
512 task->sched_info.run_delay,
513 task->sched_info.pcnt);
517 /* The badness from the OOM killer */
518 unsigned long badness(struct task_struct *p, unsigned long uptime);
519 static int proc_oom_score(struct task_struct *task, char *buffer)
521 unsigned long points;
522 struct timespec uptime;
524 do_posix_clock_monotonic_gettime(&uptime);
525 points = badness(task, uptime.tv_sec);
526 return sprintf(buffer, "%lu\n", points);
529 /************************************************************************/
530 /* Here the fs part begins */
531 /************************************************************************/
533 /* permission checks */
535 /* If the process being read is separated by chroot from the reading process,
536 * don't let the reader access the threads.
538 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
540 struct dentry *de, *base;
541 struct vfsmount *our_vfsmnt, *mnt;
543 read_lock(¤t->fs->lock);
544 our_vfsmnt = mntget(current->fs->rootmnt);
545 base = dget(current->fs->root);
546 read_unlock(¤t->fs->lock);
548 spin_lock(&vfsmount_lock);
552 while (vfsmnt != our_vfsmnt) {
553 if (vfsmnt == vfsmnt->mnt_parent)
555 de = vfsmnt->mnt_mountpoint;
556 vfsmnt = vfsmnt->mnt_parent;
559 if (!is_subdir(de, base))
561 spin_unlock(&vfsmount_lock);
570 spin_unlock(&vfsmount_lock);
575 static int proc_check_root(struct inode *inode)
578 struct vfsmount *vfsmnt;
580 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
582 return proc_check_chroot(root, vfsmnt);
585 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
587 if (generic_permission(inode, mask, NULL) != 0)
589 return proc_check_root(inode);
592 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
595 struct vfsmount *vfsmnt;
597 if (generic_permission(inode, mask, NULL) != 0)
600 if (proc_task_root_link(inode, &root, &vfsmnt))
603 return proc_check_chroot(root, vfsmnt);
606 extern struct seq_operations proc_pid_maps_op;
607 static int maps_open(struct inode *inode, struct file *file)
609 struct task_struct *task = proc_task(inode);
610 int ret = seq_open(file, &proc_pid_maps_op);
612 struct seq_file *m = file->private_data;
618 static struct file_operations proc_maps_operations = {
622 .release = seq_release,
626 extern struct seq_operations proc_pid_numa_maps_op;
627 static int numa_maps_open(struct inode *inode, struct file *file)
629 struct task_struct *task = proc_task(inode);
630 int ret = seq_open(file, &proc_pid_numa_maps_op);
632 struct seq_file *m = file->private_data;
638 static struct file_operations proc_numa_maps_operations = {
639 .open = numa_maps_open,
642 .release = seq_release,
647 extern struct seq_operations proc_pid_smaps_op;
648 static int smaps_open(struct inode *inode, struct file *file)
650 struct task_struct *task = proc_task(inode);
651 int ret = seq_open(file, &proc_pid_smaps_op);
653 struct seq_file *m = file->private_data;
659 static struct file_operations proc_smaps_operations = {
663 .release = seq_release,
667 extern struct seq_operations mounts_op;
673 static int mounts_open(struct inode *inode, struct file *file)
675 struct task_struct *task = proc_task(inode);
676 struct namespace *namespace;
677 struct proc_mounts *p;
681 namespace = task->namespace;
683 get_namespace(namespace);
688 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
690 file->private_data = &p->m;
691 ret = seq_open(file, &mounts_op);
693 p->m.private = namespace;
694 p->event = namespace->event;
699 put_namespace(namespace);
704 static int mounts_release(struct inode *inode, struct file *file)
706 struct seq_file *m = file->private_data;
707 struct namespace *namespace = m->private;
708 put_namespace(namespace);
709 return seq_release(inode, file);
712 static unsigned mounts_poll(struct file *file, poll_table *wait)
714 struct proc_mounts *p = file->private_data;
715 struct namespace *ns = p->m.private;
718 poll_wait(file, &ns->poll, wait);
720 spin_lock(&vfsmount_lock);
721 if (p->event != ns->event) {
722 p->event = ns->event;
725 spin_unlock(&vfsmount_lock);
730 static struct file_operations proc_mounts_operations = {
734 .release = mounts_release,
738 extern struct seq_operations mountstats_op;
739 static int mountstats_open(struct inode *inode, struct file *file)
741 struct task_struct *task = proc_task(inode);
742 int ret = seq_open(file, &mountstats_op);
745 struct seq_file *m = file->private_data;
746 struct namespace *namespace;
748 namespace = task->namespace;
750 get_namespace(namespace);
754 m->private = namespace;
756 seq_release(inode, file);
763 static struct file_operations proc_mountstats_operations = {
764 .open = mountstats_open,
767 .release = mounts_release,
770 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
772 static ssize_t proc_info_read(struct file * file, char __user * buf,
773 size_t count, loff_t *ppos)
775 struct inode * inode = file->f_dentry->d_inode;
778 struct task_struct *task = proc_task(inode);
780 if (count > PROC_BLOCK_SIZE)
781 count = PROC_BLOCK_SIZE;
782 if (!(page = __get_free_page(GFP_KERNEL)))
785 length = PROC_I(inode)->op.proc_read(task, (char*)page);
788 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
793 static struct file_operations proc_info_file_operations = {
794 .read = proc_info_read,
797 static int mem_open(struct inode* inode, struct file* file)
799 file->private_data = (void*)((long)current->self_exec_id);
803 static ssize_t mem_read(struct file * file, char __user * buf,
804 size_t count, loff_t *ppos)
806 struct task_struct *task = proc_task(file->f_dentry->d_inode);
808 unsigned long src = *ppos;
810 struct mm_struct *mm;
812 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
816 page = (char *)__get_free_page(GFP_USER);
822 mm = get_task_mm(task);
828 if (file->private_data != (void*)((long)current->self_exec_id))
834 int this_len, retval;
836 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
837 retval = access_process_vm(task, src, page, this_len, 0);
838 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
844 if (copy_to_user(buf, page, retval)) {
859 free_page((unsigned long) page);
864 #define mem_write NULL
867 /* This is a security hazard */
868 static ssize_t mem_write(struct file * file, const char * buf,
869 size_t count, loff_t *ppos)
873 struct task_struct *task = proc_task(file->f_dentry->d_inode);
874 unsigned long dst = *ppos;
876 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
879 page = (char *)__get_free_page(GFP_USER);
884 int this_len, retval;
886 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
887 if (copy_from_user(page, buf, this_len)) {
891 retval = access_process_vm(task, dst, page, this_len, 1);
903 free_page((unsigned long) page);
908 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
912 file->f_pos = offset;
915 file->f_pos += offset;
920 force_successful_syscall_return();
924 static struct file_operations proc_mem_operations = {
931 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
932 size_t count, loff_t *ppos)
934 struct task_struct *task = proc_task(file->f_dentry->d_inode);
937 int oom_adjust = task->oomkilladj;
938 loff_t __ppos = *ppos;
940 len = sprintf(buffer, "%i\n", oom_adjust);
943 if (count > len-__ppos)
945 if (copy_to_user(buf, buffer + __ppos, count))
947 *ppos = __ppos + count;
951 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
952 size_t count, loff_t *ppos)
954 struct task_struct *task = proc_task(file->f_dentry->d_inode);
955 char buffer[8], *end;
958 if (!capable(CAP_SYS_RESOURCE))
960 memset(buffer, 0, 8);
963 if (copy_from_user(buffer, buf, count))
965 oom_adjust = simple_strtol(buffer, &end, 0);
966 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
970 task->oomkilladj = oom_adjust;
971 if (end - buffer == 0)
976 static struct file_operations proc_oom_adjust_operations = {
977 .read = oom_adjust_read,
978 .write = oom_adjust_write,
981 static struct inode_operations proc_mem_inode_operations = {
982 .permission = proc_permission,
985 #ifdef CONFIG_AUDITSYSCALL
987 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
988 size_t count, loff_t *ppos)
990 struct inode * inode = file->f_dentry->d_inode;
991 struct task_struct *task = proc_task(inode);
993 char tmpbuf[TMPBUFLEN];
995 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
996 audit_get_loginuid(task->audit_context));
997 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1000 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1001 size_t count, loff_t *ppos)
1003 struct inode * inode = file->f_dentry->d_inode;
1006 struct task_struct *task = proc_task(inode);
1009 if (!capable(CAP_AUDIT_CONTROL))
1012 if (current != task)
1015 if (count > PAGE_SIZE)
1019 /* No partial writes. */
1022 page = (char*)__get_free_page(GFP_USER);
1026 if (copy_from_user(page, buf, count))
1029 loginuid = simple_strtoul(page, &tmp, 10);
1035 length = audit_set_loginuid(task, loginuid);
1036 if (likely(length == 0))
1040 free_page((unsigned long) page);
1044 static struct file_operations proc_loginuid_operations = {
1045 .read = proc_loginuid_read,
1046 .write = proc_loginuid_write,
1050 #ifdef CONFIG_SECCOMP
1051 static ssize_t seccomp_read(struct file *file, char __user *buf,
1052 size_t count, loff_t *ppos)
1054 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1056 loff_t __ppos = *ppos;
1059 /* no need to print the trailing zero, so use only len */
1060 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1063 if (count > len - __ppos)
1064 count = len - __ppos;
1065 if (copy_to_user(buf, __buf + __ppos, count))
1067 *ppos = __ppos + count;
1071 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1072 size_t count, loff_t *ppos)
1074 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1075 char __buf[20], *end;
1076 unsigned int seccomp_mode;
1078 /* can set it only once to be even more secure */
1079 if (unlikely(tsk->seccomp.mode))
1082 memset(__buf, 0, sizeof(__buf));
1083 count = min(count, sizeof(__buf) - 1);
1084 if (copy_from_user(__buf, buf, count))
1086 seccomp_mode = simple_strtoul(__buf, &end, 0);
1089 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1090 tsk->seccomp.mode = seccomp_mode;
1091 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1094 if (unlikely(!(end - __buf)))
1099 static struct file_operations proc_seccomp_operations = {
1100 .read = seccomp_read,
1101 .write = seccomp_write,
1103 #endif /* CONFIG_SECCOMP */
1105 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1107 struct inode *inode = dentry->d_inode;
1108 int error = -EACCES;
1110 /* We don't need a base pointer in the /proc filesystem */
1113 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1115 error = proc_check_root(inode);
1119 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1120 nd->last_type = LAST_BIND;
1122 return ERR_PTR(error);
1125 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1126 char __user *buffer, int buflen)
1128 struct inode * inode;
1129 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1135 inode = dentry->d_inode;
1136 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1137 len = PTR_ERR(path);
1140 len = tmp + PAGE_SIZE - 1 - path;
1144 if (copy_to_user(buffer, path, len))
1147 free_page((unsigned long)tmp);
1151 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1153 int error = -EACCES;
1154 struct inode *inode = dentry->d_inode;
1156 struct vfsmount *mnt = NULL;
1160 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1162 error = proc_check_root(inode);
1166 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1170 error = do_proc_readlink(de, mnt, buffer, buflen);
1178 static struct inode_operations proc_pid_link_inode_operations = {
1179 .readlink = proc_pid_readlink,
1180 .follow_link = proc_pid_follow_link
1185 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1187 struct inode *inode = filp->f_dentry->d_inode;
1188 struct task_struct *p = proc_task(inode);
1189 unsigned int fd, tid, ino;
1192 struct files_struct * files;
1193 struct fdtable *fdt;
1204 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1208 ino = fake_ino(tid, PROC_TID_INO);
1209 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1213 files = get_files_struct(p);
1217 fdt = files_fdtable(files);
1218 for (fd = filp->f_pos-2;
1220 fd++, filp->f_pos++) {
1223 if (!fcheck_files(files, fd))
1231 buf[j] = '0' + (i % 10);
1235 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1236 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1243 put_files_struct(files);
1249 static int proc_pident_readdir(struct file *filp,
1250 void *dirent, filldir_t filldir,
1251 struct pid_entry *ents, unsigned int nents)
1255 struct dentry *dentry = filp->f_dentry;
1256 struct inode *inode = dentry->d_inode;
1257 struct pid_entry *p;
1262 if (!pid_alive(proc_task(inode)))
1266 pid = proc_task(inode)->pid;
1271 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1277 ino = parent_ino(dentry);
1278 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1291 if (filldir(dirent, p->name, p->len, filp->f_pos,
1292 fake_ino(pid, p->type), p->mode >> 12) < 0)
1304 static int proc_tgid_base_readdir(struct file * filp,
1305 void * dirent, filldir_t filldir)
1307 return proc_pident_readdir(filp,dirent,filldir,
1308 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1311 static int proc_tid_base_readdir(struct file * filp,
1312 void * dirent, filldir_t filldir)
1314 return proc_pident_readdir(filp,dirent,filldir,
1315 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1318 /* building an inode */
1320 static int task_dumpable(struct task_struct *task)
1323 struct mm_struct *mm;
1328 dumpable = mm->dumpable;
1336 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1338 struct inode * inode;
1339 struct proc_inode *ei;
1341 /* We need a new inode */
1343 inode = new_inode(sb);
1350 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1351 inode->i_ino = fake_ino(task->pid, ino);
1353 if (!pid_alive(task))
1357 * grab the reference to task.
1359 get_task_struct(task);
1364 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1365 inode->i_uid = task->euid;
1366 inode->i_gid = task->egid;
1368 security_task_to_inode(task, inode);
1382 * Exceptional case: normally we are not allowed to unhash a busy
1383 * directory. In this case, however, we can do it - no aliasing problems
1384 * due to the way we treat inodes.
1386 * Rewrite the inode's ownerships here because the owning task may have
1387 * performed a setuid(), etc.
1389 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1391 struct inode *inode = dentry->d_inode;
1392 struct task_struct *task = proc_task(inode);
1393 if (pid_alive(task)) {
1394 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1395 inode->i_uid = task->euid;
1396 inode->i_gid = task->egid;
1401 security_task_to_inode(task, inode);
1408 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1410 struct inode *inode = dentry->d_inode;
1411 struct task_struct *task = proc_task(inode);
1412 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1413 struct files_struct *files;
1415 files = get_files_struct(task);
1418 if (fcheck_files(files, fd)) {
1420 put_files_struct(files);
1421 if (task_dumpable(task)) {
1422 inode->i_uid = task->euid;
1423 inode->i_gid = task->egid;
1428 security_task_to_inode(task, inode);
1432 put_files_struct(files);
1438 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1440 struct task_struct *task = proc_task(inode);
1441 spin_lock(&task->proc_lock);
1442 if (task->proc_dentry == dentry)
1443 task->proc_dentry = NULL;
1444 spin_unlock(&task->proc_lock);
1448 static int pid_delete_dentry(struct dentry * dentry)
1450 /* Is the task we represent dead?
1451 * If so, then don't put the dentry on the lru list,
1452 * kill it immediately.
1454 return !pid_alive(proc_task(dentry->d_inode));
1457 static struct dentry_operations tid_fd_dentry_operations =
1459 .d_revalidate = tid_fd_revalidate,
1460 .d_delete = pid_delete_dentry,
1463 static struct dentry_operations pid_dentry_operations =
1465 .d_revalidate = pid_revalidate,
1466 .d_delete = pid_delete_dentry,
1469 static struct dentry_operations pid_base_dentry_operations =
1471 .d_revalidate = pid_revalidate,
1472 .d_iput = pid_base_iput,
1473 .d_delete = pid_delete_dentry,
1478 static unsigned name_to_int(struct dentry *dentry)
1480 const char *name = dentry->d_name.name;
1481 int len = dentry->d_name.len;
1484 if (len > 1 && *name == '0')
1487 unsigned c = *name++ - '0';
1490 if (n >= (~0U-9)/10)
1501 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1503 struct task_struct *task = proc_task(dir);
1504 unsigned fd = name_to_int(dentry);
1506 struct files_struct * files;
1507 struct inode *inode;
1508 struct proc_inode *ei;
1512 if (!pid_alive(task))
1515 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1519 files = get_files_struct(task);
1522 inode->i_mode = S_IFLNK;
1524 file = fcheck_files(files, fd);
1527 if (file->f_mode & 1)
1528 inode->i_mode |= S_IRUSR | S_IXUSR;
1529 if (file->f_mode & 2)
1530 inode->i_mode |= S_IWUSR | S_IXUSR;
1532 put_files_struct(files);
1533 inode->i_op = &proc_pid_link_inode_operations;
1535 ei->op.proc_get_link = proc_fd_link;
1536 dentry->d_op = &tid_fd_dentry_operations;
1537 d_add(dentry, inode);
1542 put_files_struct(files);
1546 return ERR_PTR(-ENOENT);
1549 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1550 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1552 static struct file_operations proc_fd_operations = {
1553 .read = generic_read_dir,
1554 .readdir = proc_readfd,
1557 static struct file_operations proc_task_operations = {
1558 .read = generic_read_dir,
1559 .readdir = proc_task_readdir,
1563 * proc directories can do almost nothing..
1565 static struct inode_operations proc_fd_inode_operations = {
1566 .lookup = proc_lookupfd,
1567 .permission = proc_permission,
1570 static struct inode_operations proc_task_inode_operations = {
1571 .lookup = proc_task_lookup,
1572 .permission = proc_task_permission,
1575 #ifdef CONFIG_SECURITY
1576 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1577 size_t count, loff_t *ppos)
1579 struct inode * inode = file->f_dentry->d_inode;
1582 struct task_struct *task = proc_task(inode);
1584 if (count > PAGE_SIZE)
1586 if (!(page = __get_free_page(GFP_KERNEL)))
1589 length = security_getprocattr(task,
1590 (char*)file->f_dentry->d_name.name,
1591 (void*)page, count);
1593 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1598 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1599 size_t count, loff_t *ppos)
1601 struct inode * inode = file->f_dentry->d_inode;
1604 struct task_struct *task = proc_task(inode);
1606 if (count > PAGE_SIZE)
1609 /* No partial writes. */
1612 page = (char*)__get_free_page(GFP_USER);
1616 if (copy_from_user(page, buf, count))
1619 length = security_setprocattr(task,
1620 (char*)file->f_dentry->d_name.name,
1621 (void*)page, count);
1623 free_page((unsigned long) page);
1627 static struct file_operations proc_pid_attr_operations = {
1628 .read = proc_pid_attr_read,
1629 .write = proc_pid_attr_write,
1632 static struct file_operations proc_tid_attr_operations;
1633 static struct inode_operations proc_tid_attr_inode_operations;
1634 static struct file_operations proc_tgid_attr_operations;
1635 static struct inode_operations proc_tgid_attr_inode_operations;
1638 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1641 static struct dentry *proc_pident_lookup(struct inode *dir,
1642 struct dentry *dentry,
1643 struct pid_entry *ents)
1645 struct inode *inode;
1647 struct task_struct *task = proc_task(dir);
1648 struct pid_entry *p;
1649 struct proc_inode *ei;
1654 if (!pid_alive(task))
1657 for (p = ents; p->name; p++) {
1658 if (p->len != dentry->d_name.len)
1660 if (!memcmp(dentry->d_name.name, p->name, p->len))
1667 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1672 inode->i_mode = p->mode;
1674 * Yes, it does not scale. And it should not. Don't add
1675 * new entries into /proc/<tgid>/ without very good reasons.
1678 case PROC_TGID_TASK:
1679 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1680 inode->i_op = &proc_task_inode_operations;
1681 inode->i_fop = &proc_task_operations;
1686 inode->i_op = &proc_fd_inode_operations;
1687 inode->i_fop = &proc_fd_operations;
1691 inode->i_op = &proc_pid_link_inode_operations;
1692 ei->op.proc_get_link = proc_exe_link;
1696 inode->i_op = &proc_pid_link_inode_operations;
1697 ei->op.proc_get_link = proc_cwd_link;
1700 case PROC_TGID_ROOT:
1701 inode->i_op = &proc_pid_link_inode_operations;
1702 ei->op.proc_get_link = proc_root_link;
1704 case PROC_TID_ENVIRON:
1705 case PROC_TGID_ENVIRON:
1706 inode->i_fop = &proc_info_file_operations;
1707 ei->op.proc_read = proc_pid_environ;
1710 case PROC_TGID_AUXV:
1711 inode->i_fop = &proc_info_file_operations;
1712 ei->op.proc_read = proc_pid_auxv;
1714 case PROC_TID_STATUS:
1715 case PROC_TGID_STATUS:
1716 inode->i_fop = &proc_info_file_operations;
1717 ei->op.proc_read = proc_pid_status;
1720 inode->i_fop = &proc_info_file_operations;
1721 ei->op.proc_read = proc_tid_stat;
1723 case PROC_TGID_STAT:
1724 inode->i_fop = &proc_info_file_operations;
1725 ei->op.proc_read = proc_tgid_stat;
1727 case PROC_TID_CMDLINE:
1728 case PROC_TGID_CMDLINE:
1729 inode->i_fop = &proc_info_file_operations;
1730 ei->op.proc_read = proc_pid_cmdline;
1732 case PROC_TID_STATM:
1733 case PROC_TGID_STATM:
1734 inode->i_fop = &proc_info_file_operations;
1735 ei->op.proc_read = proc_pid_statm;
1738 case PROC_TGID_MAPS:
1739 inode->i_fop = &proc_maps_operations;
1742 case PROC_TID_NUMA_MAPS:
1743 case PROC_TGID_NUMA_MAPS:
1744 inode->i_fop = &proc_numa_maps_operations;
1749 inode->i_op = &proc_mem_inode_operations;
1750 inode->i_fop = &proc_mem_operations;
1752 #ifdef CONFIG_SECCOMP
1753 case PROC_TID_SECCOMP:
1754 case PROC_TGID_SECCOMP:
1755 inode->i_fop = &proc_seccomp_operations;
1757 #endif /* CONFIG_SECCOMP */
1758 case PROC_TID_MOUNTS:
1759 case PROC_TGID_MOUNTS:
1760 inode->i_fop = &proc_mounts_operations;
1763 case PROC_TID_SMAPS:
1764 case PROC_TGID_SMAPS:
1765 inode->i_fop = &proc_smaps_operations;
1768 case PROC_TID_MOUNTSTATS:
1769 case PROC_TGID_MOUNTSTATS:
1770 inode->i_fop = &proc_mountstats_operations;
1772 #ifdef CONFIG_SECURITY
1775 inode->i_op = &proc_tid_attr_inode_operations;
1776 inode->i_fop = &proc_tid_attr_operations;
1778 case PROC_TGID_ATTR:
1780 inode->i_op = &proc_tgid_attr_inode_operations;
1781 inode->i_fop = &proc_tgid_attr_operations;
1783 case PROC_TID_ATTR_CURRENT:
1784 case PROC_TGID_ATTR_CURRENT:
1785 case PROC_TID_ATTR_PREV:
1786 case PROC_TGID_ATTR_PREV:
1787 case PROC_TID_ATTR_EXEC:
1788 case PROC_TGID_ATTR_EXEC:
1789 case PROC_TID_ATTR_FSCREATE:
1790 case PROC_TGID_ATTR_FSCREATE:
1791 inode->i_fop = &proc_pid_attr_operations;
1794 #ifdef CONFIG_KALLSYMS
1795 case PROC_TID_WCHAN:
1796 case PROC_TGID_WCHAN:
1797 inode->i_fop = &proc_info_file_operations;
1798 ei->op.proc_read = proc_pid_wchan;
1801 #ifdef CONFIG_SCHEDSTATS
1802 case PROC_TID_SCHEDSTAT:
1803 case PROC_TGID_SCHEDSTAT:
1804 inode->i_fop = &proc_info_file_operations;
1805 ei->op.proc_read = proc_pid_schedstat;
1808 #ifdef CONFIG_CPUSETS
1809 case PROC_TID_CPUSET:
1810 case PROC_TGID_CPUSET:
1811 inode->i_fop = &proc_cpuset_operations;
1814 case PROC_TID_OOM_SCORE:
1815 case PROC_TGID_OOM_SCORE:
1816 inode->i_fop = &proc_info_file_operations;
1817 ei->op.proc_read = proc_oom_score;
1819 case PROC_TID_OOM_ADJUST:
1820 case PROC_TGID_OOM_ADJUST:
1821 inode->i_fop = &proc_oom_adjust_operations;
1823 #ifdef CONFIG_AUDITSYSCALL
1824 case PROC_TID_LOGINUID:
1825 case PROC_TGID_LOGINUID:
1826 inode->i_fop = &proc_loginuid_operations;
1830 printk("procfs: impossible type (%d)",p->type);
1832 return ERR_PTR(-EINVAL);
1834 dentry->d_op = &pid_dentry_operations;
1835 d_add(dentry, inode);
1839 return ERR_PTR(error);
1842 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1843 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1846 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1847 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1850 static struct file_operations proc_tgid_base_operations = {
1851 .read = generic_read_dir,
1852 .readdir = proc_tgid_base_readdir,
1855 static struct file_operations proc_tid_base_operations = {
1856 .read = generic_read_dir,
1857 .readdir = proc_tid_base_readdir,
1860 static struct inode_operations proc_tgid_base_inode_operations = {
1861 .lookup = proc_tgid_base_lookup,
1864 static struct inode_operations proc_tid_base_inode_operations = {
1865 .lookup = proc_tid_base_lookup,
1868 #ifdef CONFIG_SECURITY
1869 static int proc_tgid_attr_readdir(struct file * filp,
1870 void * dirent, filldir_t filldir)
1872 return proc_pident_readdir(filp,dirent,filldir,
1873 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1876 static int proc_tid_attr_readdir(struct file * filp,
1877 void * dirent, filldir_t filldir)
1879 return proc_pident_readdir(filp,dirent,filldir,
1880 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1883 static struct file_operations proc_tgid_attr_operations = {
1884 .read = generic_read_dir,
1885 .readdir = proc_tgid_attr_readdir,
1888 static struct file_operations proc_tid_attr_operations = {
1889 .read = generic_read_dir,
1890 .readdir = proc_tid_attr_readdir,
1893 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1894 struct dentry *dentry, struct nameidata *nd)
1896 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1899 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1900 struct dentry *dentry, struct nameidata *nd)
1902 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1905 static struct inode_operations proc_tgid_attr_inode_operations = {
1906 .lookup = proc_tgid_attr_lookup,
1909 static struct inode_operations proc_tid_attr_inode_operations = {
1910 .lookup = proc_tid_attr_lookup,
1917 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1921 sprintf(tmp, "%d", current->tgid);
1922 return vfs_readlink(dentry,buffer,buflen,tmp);
1925 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1928 sprintf(tmp, "%d", current->tgid);
1929 return ERR_PTR(vfs_follow_link(nd,tmp));
1932 static struct inode_operations proc_self_inode_operations = {
1933 .readlink = proc_self_readlink,
1934 .follow_link = proc_self_follow_link,
1938 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1939 * @p: task that should be flushed.
1941 * Drops the /proc/@pid dcache entry from the hash chains.
1943 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1944 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1945 * if the pid value is immediately reused. This is enforced by
1946 * - caller must acquire spin_lock(p->proc_lock)
1947 * - must be called before detach_pid()
1948 * - proc_pid_lookup acquires proc_lock, and checks that
1949 * the target is not dead by looking at the attach count
1953 struct dentry *proc_pid_unhash(struct task_struct *p)
1955 struct dentry *proc_dentry;
1957 proc_dentry = p->proc_dentry;
1958 if (proc_dentry != NULL) {
1960 spin_lock(&dcache_lock);
1961 spin_lock(&proc_dentry->d_lock);
1962 if (!d_unhashed(proc_dentry)) {
1963 dget_locked(proc_dentry);
1964 __d_drop(proc_dentry);
1965 spin_unlock(&proc_dentry->d_lock);
1967 spin_unlock(&proc_dentry->d_lock);
1970 spin_unlock(&dcache_lock);
1976 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1977 * @proc_dentry: directoy to prune.
1979 * Shrink the /proc directory that was used by the just killed thread.
1982 void proc_pid_flush(struct dentry *proc_dentry)
1985 if(proc_dentry != NULL) {
1986 shrink_dcache_parent(proc_dentry);
1992 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1994 struct task_struct *task;
1995 struct inode *inode;
1996 struct proc_inode *ei;
2000 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2001 inode = new_inode(dir->i_sb);
2003 return ERR_PTR(-ENOMEM);
2005 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2006 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2008 inode->i_mode = S_IFLNK|S_IRWXUGO;
2009 inode->i_uid = inode->i_gid = 0;
2011 inode->i_op = &proc_self_inode_operations;
2012 d_add(dentry, inode);
2015 tgid = name_to_int(dentry);
2019 read_lock(&tasklist_lock);
2020 task = find_task_by_pid(tgid);
2022 get_task_struct(task);
2023 read_unlock(&tasklist_lock);
2027 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2031 put_task_struct(task);
2034 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2035 inode->i_op = &proc_tgid_base_inode_operations;
2036 inode->i_fop = &proc_tgid_base_operations;
2037 inode->i_flags|=S_IMMUTABLE;
2038 #ifdef CONFIG_SECURITY
2044 dentry->d_op = &pid_base_dentry_operations;
2047 d_add(dentry, inode);
2048 spin_lock(&task->proc_lock);
2049 task->proc_dentry = dentry;
2050 if (!pid_alive(task)) {
2051 dentry = proc_pid_unhash(task);
2054 spin_unlock(&task->proc_lock);
2056 put_task_struct(task);
2058 proc_pid_flush(dentry);
2063 return ERR_PTR(-ENOENT);
2067 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2069 struct task_struct *task;
2070 struct task_struct *leader = proc_task(dir);
2071 struct inode *inode;
2074 tid = name_to_int(dentry);
2078 read_lock(&tasklist_lock);
2079 task = find_task_by_pid(tid);
2081 get_task_struct(task);
2082 read_unlock(&tasklist_lock);
2085 if (leader->tgid != task->tgid)
2088 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2093 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2094 inode->i_op = &proc_tid_base_inode_operations;
2095 inode->i_fop = &proc_tid_base_operations;
2096 inode->i_flags|=S_IMMUTABLE;
2097 #ifdef CONFIG_SECURITY
2103 dentry->d_op = &pid_base_dentry_operations;
2105 d_add(dentry, inode);
2107 put_task_struct(task);
2110 put_task_struct(task);
2112 return ERR_PTR(-ENOENT);
2115 #define PROC_NUMBUF 10
2116 #define PROC_MAXPIDS 20
2119 * Get a few tgid'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_tgid_list(int index, unsigned long version, unsigned int *tgids)
2125 struct task_struct *p;
2129 read_lock(&tasklist_lock);
2132 p = find_task_by_pid(version);
2133 if (p && !thread_group_leader(p))
2140 p = next_task(&init_task);
2142 for ( ; p != &init_task; p = next_task(p)) {
2148 tgids[nr_tgids] = tgid;
2150 if (nr_tgids >= PROC_MAXPIDS)
2153 read_unlock(&tasklist_lock);
2158 * Get a few tid's to return for filldir - we need to hold the
2159 * tasklist lock while doing this, and we must release it before
2160 * we actually do the filldir itself, so we use a temp buffer..
2162 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2164 struct task_struct *leader_task = proc_task(dir);
2165 struct task_struct *task = leader_task;
2169 read_lock(&tasklist_lock);
2171 * The starting point task (leader_task) might be an already
2172 * unlinked task, which cannot be used to access the task-list
2173 * via next_thread().
2175 if (pid_alive(task)) do {
2176 int tid = task->pid;
2181 tids[nr_tids] = tid;
2183 if (nr_tids >= PROC_MAXPIDS)
2185 } while ((task = next_thread(task)) != leader_task);
2186 read_unlock(&tasklist_lock);
2190 /* for the /proc/ directory itself, after non-process stuff has been done */
2191 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2193 unsigned int tgid_array[PROC_MAXPIDS];
2194 char buf[PROC_NUMBUF];
2195 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2196 unsigned int nr_tgids, i;
2200 ino_t ino = fake_ino(0,PROC_TGID_INO);
2201 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2207 /* f_version caches the tgid value that the last readdir call couldn't
2208 * return. lseek aka telldir automagically resets f_version to 0.
2210 next_tgid = filp->f_version;
2211 filp->f_version = 0;
2213 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2215 /* no more entries ! */
2220 /* do not use the last found pid, reserve it for next_tgid */
2221 if (nr_tgids == PROC_MAXPIDS) {
2223 next_tgid = tgid_array[nr_tgids];
2226 for (i=0;i<nr_tgids;i++) {
2227 int tgid = tgid_array[i];
2228 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2229 unsigned long j = PROC_NUMBUF;
2232 buf[--j] = '0' + (tgid % 10);
2233 while ((tgid /= 10) != 0);
2235 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2236 /* returning this tgid failed, save it as the first
2237 * pid for the next readir call */
2238 filp->f_version = tgid_array[i];
2249 /* for the /proc/TGID/task/ directories */
2250 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2252 unsigned int tid_array[PROC_MAXPIDS];
2253 char buf[PROC_NUMBUF];
2254 unsigned int nr_tids, i;
2255 struct dentry *dentry = filp->f_dentry;
2256 struct inode *inode = dentry->d_inode;
2257 int retval = -ENOENT;
2259 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2261 if (!pid_alive(proc_task(inode)))
2268 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2273 ino = parent_ino(dentry);
2274 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2280 nr_tids = get_tid_list(pos, tid_array, inode);
2281 inode->i_nlink = pos + nr_tids;
2283 for (i = 0; i < nr_tids; i++) {
2284 unsigned long j = PROC_NUMBUF;
2285 int tid = tid_array[i];
2287 ino = fake_ino(tid,PROC_TID_INO);
2290 buf[--j] = '0' + (tid % 10);
2291 while ((tid /= 10) != 0);
2293 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)