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.
16 #include <asm/uaccess.h>
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/namei.h>
28 #include <linux/namespace.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kallsyms.h>
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/ptrace.h>
35 #include <linux/seccomp.h>
36 #include <linux/cpuset.h>
37 #include <linux/audit.h>
41 * For hysterical raisins we keep the same inumbers as in the old procfs.
42 * Feel free to change the macro below - just keep the range distinct from
43 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
44 * As soon as we'll get a separate superblock we will be able to forget
45 * about magical ranges too.
48 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
50 enum pid_directory_inos
{
70 #ifdef CONFIG_SCHEDSTATS
76 #ifdef CONFIG_SECURITY
78 PROC_TGID_ATTR_CURRENT
,
81 PROC_TGID_ATTR_FSCREATE
,
83 #ifdef CONFIG_AUDITSYSCALL
107 #ifdef CONFIG_SCHEDSTATS
110 #ifdef CONFIG_CPUSETS
113 #ifdef CONFIG_SECURITY
115 PROC_TID_ATTR_CURRENT
,
118 PROC_TID_ATTR_FSCREATE
,
120 #ifdef CONFIG_AUDITSYSCALL
123 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
135 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
137 static struct pid_entry tgid_base_stuff
[] = {
138 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
139 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
140 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
141 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
142 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
143 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
144 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
145 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
146 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
147 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
148 #ifdef CONFIG_SECCOMP
149 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
151 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
152 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
153 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
154 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
155 #ifdef CONFIG_SECURITY
156 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
158 #ifdef CONFIG_KALLSYMS
159 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
161 #ifdef CONFIG_SCHEDSTATS
162 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
164 #ifdef CONFIG_CPUSETS
165 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
167 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
168 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
169 #ifdef CONFIG_AUDITSYSCALL
170 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
174 static struct pid_entry tid_base_stuff
[] = {
175 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
176 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
177 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
178 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
179 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
180 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
181 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
182 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
183 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
184 #ifdef CONFIG_SECCOMP
185 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
187 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
188 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
189 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
190 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
191 #ifdef CONFIG_SECURITY
192 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
194 #ifdef CONFIG_KALLSYMS
195 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
197 #ifdef CONFIG_SCHEDSTATS
198 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
200 #ifdef CONFIG_CPUSETS
201 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
203 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
204 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
205 #ifdef CONFIG_AUDITSYSCALL
206 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
211 #ifdef CONFIG_SECURITY
212 static struct pid_entry tgid_attr_stuff
[] = {
213 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
214 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
215 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
216 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
219 static struct pid_entry tid_attr_stuff
[] = {
220 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
221 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
222 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
223 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
230 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
232 struct task_struct
*task
= proc_task(inode
);
233 struct files_struct
*files
;
235 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
237 files
= get_files_struct(task
);
239 spin_lock(&files
->file_lock
);
240 file
= fcheck_files(files
, fd
);
242 *mnt
= mntget(file
->f_vfsmnt
);
243 *dentry
= dget(file
->f_dentry
);
244 spin_unlock(&files
->file_lock
);
245 put_files_struct(files
);
248 spin_unlock(&files
->file_lock
);
249 put_files_struct(files
);
254 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
256 struct fs_struct
*fs
;
257 int result
= -ENOENT
;
258 task_lock(proc_task(inode
));
259 fs
= proc_task(inode
)->fs
;
261 atomic_inc(&fs
->count
);
262 task_unlock(proc_task(inode
));
264 read_lock(&fs
->lock
);
265 *mnt
= mntget(fs
->pwdmnt
);
266 *dentry
= dget(fs
->pwd
);
267 read_unlock(&fs
->lock
);
274 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
276 struct fs_struct
*fs
;
277 int result
= -ENOENT
;
278 task_lock(proc_task(inode
));
279 fs
= proc_task(inode
)->fs
;
281 atomic_inc(&fs
->count
);
282 task_unlock(proc_task(inode
));
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 may_ptrace_attach(struct task_struct
*task
)
309 if (((current
->uid
!= task
->euid
) ||
310 (current
->uid
!= task
->suid
) ||
311 (current
->uid
!= task
->uid
) ||
312 (current
->gid
!= task
->egid
) ||
313 (current
->gid
!= task
->sgid
) ||
314 (current
->gid
!= task
->gid
)) && !capable(CAP_SYS_PTRACE
))
317 if (!task
->mm
->dumpable
&& !capable(CAP_SYS_PTRACE
))
319 if (security_ptrace(current
, task
))
328 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
331 struct mm_struct
*mm
= get_task_mm(task
);
333 unsigned int len
= mm
->env_end
- mm
->env_start
;
336 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
337 if (!may_ptrace_attach(task
))
344 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
348 struct mm_struct
*mm
= get_task_mm(task
);
352 goto out_mm
; /* Shh! No looking before we're done */
354 len
= mm
->arg_end
- mm
->arg_start
;
359 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
361 // If the nul at the end of args has been overwritten, then
362 // assume application is using setproctitle(3).
363 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
364 len
= strnlen(buffer
, res
);
368 len
= mm
->env_end
- mm
->env_start
;
369 if (len
> PAGE_SIZE
- res
)
370 len
= PAGE_SIZE
- res
;
371 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
372 res
= strnlen(buffer
, res
);
381 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
384 struct mm_struct
*mm
= get_task_mm(task
);
386 unsigned int nwords
= 0;
389 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
390 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
393 memcpy(buffer
, mm
->saved_auxv
, res
);
400 #ifdef CONFIG_KALLSYMS
402 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
403 * Returns the resolved symbol. If that fails, simply return the address.
405 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
408 const char *sym_name
;
409 unsigned long wchan
, size
, offset
;
410 char namebuf
[KSYM_NAME_LEN
+1];
412 wchan
= get_wchan(task
);
414 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
416 return sprintf(buffer
, "%s", sym_name
);
417 return sprintf(buffer
, "%lu", wchan
);
419 #endif /* CONFIG_KALLSYMS */
421 #ifdef CONFIG_SCHEDSTATS
423 * Provides /proc/PID/schedstat
425 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
427 return sprintf(buffer
, "%lu %lu %lu\n",
428 task
->sched_info
.cpu_time
,
429 task
->sched_info
.run_delay
,
430 task
->sched_info
.pcnt
);
434 /* The badness from the OOM killer */
435 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
436 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
438 unsigned long points
;
439 struct timespec uptime
;
441 do_posix_clock_monotonic_gettime(&uptime
);
442 points
= badness(task
, uptime
.tv_sec
);
443 return sprintf(buffer
, "%lu\n", points
);
446 /************************************************************************/
447 /* Here the fs part begins */
448 /************************************************************************/
450 /* permission checks */
452 static int proc_check_root(struct inode
*inode
)
454 struct dentry
*de
, *base
, *root
;
455 struct vfsmount
*our_vfsmnt
, *vfsmnt
, *mnt
;
458 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
460 read_lock(¤t
->fs
->lock
);
461 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
462 base
= dget(current
->fs
->root
);
463 read_unlock(¤t
->fs
->lock
);
465 spin_lock(&vfsmount_lock
);
469 while (vfsmnt
!= our_vfsmnt
) {
470 if (vfsmnt
== vfsmnt
->mnt_parent
)
472 de
= vfsmnt
->mnt_mountpoint
;
473 vfsmnt
= vfsmnt
->mnt_parent
;
476 if (!is_subdir(de
, base
))
478 spin_unlock(&vfsmount_lock
);
487 spin_unlock(&vfsmount_lock
);
492 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
494 if (generic_permission(inode
, mask
, NULL
) != 0)
496 return proc_check_root(inode
);
499 extern struct seq_operations proc_pid_maps_op
;
500 static int maps_open(struct inode
*inode
, struct file
*file
)
502 struct task_struct
*task
= proc_task(inode
);
503 int ret
= seq_open(file
, &proc_pid_maps_op
);
505 struct seq_file
*m
= file
->private_data
;
511 static struct file_operations proc_maps_operations
= {
515 .release
= seq_release
,
518 extern struct seq_operations mounts_op
;
519 static int mounts_open(struct inode
*inode
, struct file
*file
)
521 struct task_struct
*task
= proc_task(inode
);
522 int ret
= seq_open(file
, &mounts_op
);
525 struct seq_file
*m
= file
->private_data
;
526 struct namespace *namespace;
528 namespace = task
->namespace;
530 get_namespace(namespace);
534 m
->private = namespace;
536 seq_release(inode
, file
);
543 static int mounts_release(struct inode
*inode
, struct file
*file
)
545 struct seq_file
*m
= file
->private_data
;
546 struct namespace *namespace = m
->private;
547 put_namespace(namespace);
548 return seq_release(inode
, file
);
551 static struct file_operations proc_mounts_operations
= {
555 .release
= mounts_release
,
558 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
560 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
561 size_t count
, loff_t
*ppos
)
563 struct inode
* inode
= file
->f_dentry
->d_inode
;
566 struct task_struct
*task
= proc_task(inode
);
568 if (count
> PROC_BLOCK_SIZE
)
569 count
= PROC_BLOCK_SIZE
;
570 if (!(page
= __get_free_page(GFP_KERNEL
)))
573 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
576 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
581 static struct file_operations proc_info_file_operations
= {
582 .read
= proc_info_read
,
585 static int mem_open(struct inode
* inode
, struct file
* file
)
587 file
->private_data
= (void*)((long)current
->self_exec_id
);
591 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
592 size_t count
, loff_t
*ppos
)
594 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
596 unsigned long src
= *ppos
;
598 struct mm_struct
*mm
;
600 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
604 page
= (char *)__get_free_page(GFP_USER
);
610 mm
= get_task_mm(task
);
616 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
622 int this_len
, retval
;
624 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
625 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
626 if (!retval
|| !MAY_PTRACE(task
) || !may_ptrace_attach(task
)) {
632 if (copy_to_user(buf
, page
, retval
)) {
647 free_page((unsigned long) page
);
652 #define mem_write NULL
655 /* This is a security hazard */
656 static ssize_t
mem_write(struct file
* file
, const char * buf
,
657 size_t count
, loff_t
*ppos
)
661 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
662 unsigned long dst
= *ppos
;
664 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
667 page
= (char *)__get_free_page(GFP_USER
);
672 int this_len
, retval
;
674 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
675 if (copy_from_user(page
, buf
, this_len
)) {
679 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
691 free_page((unsigned long) page
);
696 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
700 file
->f_pos
= offset
;
703 file
->f_pos
+= offset
;
708 force_successful_syscall_return();
712 static struct file_operations proc_mem_operations
= {
719 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
720 size_t count
, loff_t
*ppos
)
722 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
725 int oom_adjust
= task
->oomkilladj
;
726 loff_t __ppos
= *ppos
;
728 len
= sprintf(buffer
, "%i\n", oom_adjust
);
731 if (count
> len
-__ppos
)
733 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
735 *ppos
= __ppos
+ count
;
739 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
740 size_t count
, loff_t
*ppos
)
742 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
743 char buffer
[8], *end
;
746 if (!capable(CAP_SYS_RESOURCE
))
748 memset(buffer
, 0, 8);
751 if (copy_from_user(buffer
, buf
, count
))
753 oom_adjust
= simple_strtol(buffer
, &end
, 0);
754 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
758 task
->oomkilladj
= oom_adjust
;
759 if (end
- buffer
== 0)
764 static struct file_operations proc_oom_adjust_operations
= {
765 .read
= oom_adjust_read
,
766 .write
= oom_adjust_write
,
769 static struct inode_operations proc_mem_inode_operations
= {
770 .permission
= proc_permission
,
773 #ifdef CONFIG_AUDITSYSCALL
775 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
776 size_t count
, loff_t
*ppos
)
778 struct inode
* inode
= file
->f_dentry
->d_inode
;
779 struct task_struct
*task
= proc_task(inode
);
781 char tmpbuf
[TMPBUFLEN
];
783 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
784 audit_get_loginuid(task
->audit_context
));
785 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
788 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
789 size_t count
, loff_t
*ppos
)
791 struct inode
* inode
= file
->f_dentry
->d_inode
;
794 struct task_struct
*task
= proc_task(inode
);
797 if (!capable(CAP_AUDIT_CONTROL
))
803 if (count
> PAGE_SIZE
)
807 /* No partial writes. */
810 page
= (char*)__get_free_page(GFP_USER
);
814 if (copy_from_user(page
, buf
, count
))
817 loginuid
= simple_strtoul(page
, &tmp
, 10);
823 length
= audit_set_loginuid(task
, loginuid
);
824 if (likely(length
== 0))
828 free_page((unsigned long) page
);
832 static struct file_operations proc_loginuid_operations
= {
833 .read
= proc_loginuid_read
,
834 .write
= proc_loginuid_write
,
838 #ifdef CONFIG_SECCOMP
839 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
840 size_t count
, loff_t
*ppos
)
842 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
844 loff_t __ppos
= *ppos
;
847 /* no need to print the trailing zero, so use only len */
848 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
851 if (count
> len
- __ppos
)
852 count
= len
- __ppos
;
853 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
855 *ppos
= __ppos
+ count
;
859 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
860 size_t count
, loff_t
*ppos
)
862 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
863 char __buf
[20], *end
;
864 unsigned int seccomp_mode
;
866 /* can set it only once to be even more secure */
867 if (unlikely(tsk
->seccomp
.mode
))
870 memset(__buf
, 0, sizeof(__buf
));
871 count
= min(count
, sizeof(__buf
) - 1);
872 if (copy_from_user(__buf
, buf
, count
))
874 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
877 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
878 tsk
->seccomp
.mode
= seccomp_mode
;
879 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
882 if (unlikely(!(end
- __buf
)))
887 static struct file_operations proc_seccomp_operations
= {
888 .read
= seccomp_read
,
889 .write
= seccomp_write
,
891 #endif /* CONFIG_SECCOMP */
893 static int proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
895 struct inode
*inode
= dentry
->d_inode
;
898 /* We don't need a base pointer in the /proc filesystem */
901 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
903 error
= proc_check_root(inode
);
907 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
908 nd
->last_type
= LAST_BIND
;
913 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
914 char __user
*buffer
, int buflen
)
916 struct inode
* inode
;
917 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
923 inode
= dentry
->d_inode
;
924 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
928 len
= tmp
+ PAGE_SIZE
- 1 - path
;
932 if (copy_to_user(buffer
, path
, len
))
935 free_page((unsigned long)tmp
);
939 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
942 struct inode
*inode
= dentry
->d_inode
;
944 struct vfsmount
*mnt
= NULL
;
948 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
950 error
= proc_check_root(inode
);
954 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
958 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
966 static struct inode_operations proc_pid_link_inode_operations
= {
967 .readlink
= proc_pid_readlink
,
968 .follow_link
= proc_pid_follow_link
973 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
975 struct inode
*inode
= filp
->f_dentry
->d_inode
;
976 struct task_struct
*p
= proc_task(inode
);
977 unsigned int fd
, tid
, ino
;
980 struct files_struct
* files
;
991 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
995 ino
= fake_ino(tid
, PROC_TID_INO
);
996 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1000 files
= get_files_struct(p
);
1003 spin_lock(&files
->file_lock
);
1004 for (fd
= filp
->f_pos
-2;
1005 fd
< files
->max_fds
;
1006 fd
++, filp
->f_pos
++) {
1009 if (!fcheck_files(files
, fd
))
1011 spin_unlock(&files
->file_lock
);
1017 buf
[j
] = '0' + (i
% 10);
1021 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1022 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1023 spin_lock(&files
->file_lock
);
1026 spin_lock(&files
->file_lock
);
1028 spin_unlock(&files
->file_lock
);
1029 put_files_struct(files
);
1035 static int proc_pident_readdir(struct file
*filp
,
1036 void *dirent
, filldir_t filldir
,
1037 struct pid_entry
*ents
, unsigned int nents
)
1041 struct dentry
*dentry
= filp
->f_dentry
;
1042 struct inode
*inode
= dentry
->d_inode
;
1043 struct pid_entry
*p
;
1048 if (!pid_alive(proc_task(inode
)))
1052 pid
= proc_task(inode
)->pid
;
1057 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1063 ino
= parent_ino(dentry
);
1064 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1077 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1078 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1090 static int proc_tgid_base_readdir(struct file
* filp
,
1091 void * dirent
, filldir_t filldir
)
1093 return proc_pident_readdir(filp
,dirent
,filldir
,
1094 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1097 static int proc_tid_base_readdir(struct file
* filp
,
1098 void * dirent
, filldir_t filldir
)
1100 return proc_pident_readdir(filp
,dirent
,filldir
,
1101 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1104 /* building an inode */
1106 static int task_dumpable(struct task_struct
*task
)
1109 struct mm_struct
*mm
;
1114 dumpable
= mm
->dumpable
;
1120 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1122 struct inode
* inode
;
1123 struct proc_inode
*ei
;
1125 /* We need a new inode */
1127 inode
= new_inode(sb
);
1134 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1135 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1137 if (!pid_alive(task
))
1141 * grab the reference to task.
1143 get_task_struct(task
);
1148 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1149 inode
->i_uid
= task
->euid
;
1150 inode
->i_gid
= task
->egid
;
1152 security_task_to_inode(task
, inode
);
1166 * Exceptional case: normally we are not allowed to unhash a busy
1167 * directory. In this case, however, we can do it - no aliasing problems
1168 * due to the way we treat inodes.
1170 * Rewrite the inode's ownerships here because the owning task may have
1171 * performed a setuid(), etc.
1173 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1175 struct inode
*inode
= dentry
->d_inode
;
1176 struct task_struct
*task
= proc_task(inode
);
1177 if (pid_alive(task
)) {
1178 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1179 inode
->i_uid
= task
->euid
;
1180 inode
->i_gid
= task
->egid
;
1185 security_task_to_inode(task
, inode
);
1192 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1194 struct inode
*inode
= dentry
->d_inode
;
1195 struct task_struct
*task
= proc_task(inode
);
1196 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1197 struct files_struct
*files
;
1199 files
= get_files_struct(task
);
1201 spin_lock(&files
->file_lock
);
1202 if (fcheck_files(files
, fd
)) {
1203 spin_unlock(&files
->file_lock
);
1204 put_files_struct(files
);
1205 if (task_dumpable(task
)) {
1206 inode
->i_uid
= task
->euid
;
1207 inode
->i_gid
= task
->egid
;
1212 security_task_to_inode(task
, inode
);
1215 spin_unlock(&files
->file_lock
);
1216 put_files_struct(files
);
1222 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1224 struct task_struct
*task
= proc_task(inode
);
1225 spin_lock(&task
->proc_lock
);
1226 if (task
->proc_dentry
== dentry
)
1227 task
->proc_dentry
= NULL
;
1228 spin_unlock(&task
->proc_lock
);
1232 static int pid_delete_dentry(struct dentry
* dentry
)
1234 /* Is the task we represent dead?
1235 * If so, then don't put the dentry on the lru list,
1236 * kill it immediately.
1238 return !pid_alive(proc_task(dentry
->d_inode
));
1241 static struct dentry_operations tid_fd_dentry_operations
=
1243 .d_revalidate
= tid_fd_revalidate
,
1244 .d_delete
= pid_delete_dentry
,
1247 static struct dentry_operations pid_dentry_operations
=
1249 .d_revalidate
= pid_revalidate
,
1250 .d_delete
= pid_delete_dentry
,
1253 static struct dentry_operations pid_base_dentry_operations
=
1255 .d_revalidate
= pid_revalidate
,
1256 .d_iput
= pid_base_iput
,
1257 .d_delete
= pid_delete_dentry
,
1262 static unsigned name_to_int(struct dentry
*dentry
)
1264 const char *name
= dentry
->d_name
.name
;
1265 int len
= dentry
->d_name
.len
;
1268 if (len
> 1 && *name
== '0')
1271 unsigned c
= *name
++ - '0';
1274 if (n
>= (~0U-9)/10)
1285 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1287 struct task_struct
*task
= proc_task(dir
);
1288 unsigned fd
= name_to_int(dentry
);
1290 struct files_struct
* files
;
1291 struct inode
*inode
;
1292 struct proc_inode
*ei
;
1296 if (!pid_alive(task
))
1299 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1303 files
= get_files_struct(task
);
1306 inode
->i_mode
= S_IFLNK
;
1307 spin_lock(&files
->file_lock
);
1308 file
= fcheck_files(files
, fd
);
1311 if (file
->f_mode
& 1)
1312 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1313 if (file
->f_mode
& 2)
1314 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1315 spin_unlock(&files
->file_lock
);
1316 put_files_struct(files
);
1317 inode
->i_op
= &proc_pid_link_inode_operations
;
1319 ei
->op
.proc_get_link
= proc_fd_link
;
1320 dentry
->d_op
= &tid_fd_dentry_operations
;
1321 d_add(dentry
, inode
);
1325 spin_unlock(&files
->file_lock
);
1326 put_files_struct(files
);
1330 return ERR_PTR(-ENOENT
);
1333 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1334 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1336 static struct file_operations proc_fd_operations
= {
1337 .read
= generic_read_dir
,
1338 .readdir
= proc_readfd
,
1341 static struct file_operations proc_task_operations
= {
1342 .read
= generic_read_dir
,
1343 .readdir
= proc_task_readdir
,
1347 * proc directories can do almost nothing..
1349 static struct inode_operations proc_fd_inode_operations
= {
1350 .lookup
= proc_lookupfd
,
1351 .permission
= proc_permission
,
1354 static struct inode_operations proc_task_inode_operations
= {
1355 .lookup
= proc_task_lookup
,
1356 .permission
= proc_permission
,
1359 #ifdef CONFIG_SECURITY
1360 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1361 size_t count
, loff_t
*ppos
)
1363 struct inode
* inode
= file
->f_dentry
->d_inode
;
1366 struct task_struct
*task
= proc_task(inode
);
1368 if (count
> PAGE_SIZE
)
1370 if (!(page
= __get_free_page(GFP_KERNEL
)))
1373 length
= security_getprocattr(task
,
1374 (char*)file
->f_dentry
->d_name
.name
,
1375 (void*)page
, count
);
1377 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1382 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1383 size_t count
, loff_t
*ppos
)
1385 struct inode
* inode
= file
->f_dentry
->d_inode
;
1388 struct task_struct
*task
= proc_task(inode
);
1390 if (count
> PAGE_SIZE
)
1393 /* No partial writes. */
1396 page
= (char*)__get_free_page(GFP_USER
);
1400 if (copy_from_user(page
, buf
, count
))
1403 length
= security_setprocattr(task
,
1404 (char*)file
->f_dentry
->d_name
.name
,
1405 (void*)page
, count
);
1407 free_page((unsigned long) page
);
1411 static struct file_operations proc_pid_attr_operations
= {
1412 .read
= proc_pid_attr_read
,
1413 .write
= proc_pid_attr_write
,
1416 static struct file_operations proc_tid_attr_operations
;
1417 static struct inode_operations proc_tid_attr_inode_operations
;
1418 static struct file_operations proc_tgid_attr_operations
;
1419 static struct inode_operations proc_tgid_attr_inode_operations
;
1422 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1425 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1426 struct dentry
*dentry
,
1427 struct pid_entry
*ents
)
1429 struct inode
*inode
;
1431 struct task_struct
*task
= proc_task(dir
);
1432 struct pid_entry
*p
;
1433 struct proc_inode
*ei
;
1438 if (!pid_alive(task
))
1441 for (p
= ents
; p
->name
; p
++) {
1442 if (p
->len
!= dentry
->d_name
.len
)
1444 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1451 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1456 inode
->i_mode
= p
->mode
;
1458 * Yes, it does not scale. And it should not. Don't add
1459 * new entries into /proc/<tgid>/ without very good reasons.
1462 case PROC_TGID_TASK
:
1463 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1464 inode
->i_op
= &proc_task_inode_operations
;
1465 inode
->i_fop
= &proc_task_operations
;
1470 inode
->i_op
= &proc_fd_inode_operations
;
1471 inode
->i_fop
= &proc_fd_operations
;
1475 inode
->i_op
= &proc_pid_link_inode_operations
;
1476 ei
->op
.proc_get_link
= proc_exe_link
;
1480 inode
->i_op
= &proc_pid_link_inode_operations
;
1481 ei
->op
.proc_get_link
= proc_cwd_link
;
1484 case PROC_TGID_ROOT
:
1485 inode
->i_op
= &proc_pid_link_inode_operations
;
1486 ei
->op
.proc_get_link
= proc_root_link
;
1488 case PROC_TID_ENVIRON
:
1489 case PROC_TGID_ENVIRON
:
1490 inode
->i_fop
= &proc_info_file_operations
;
1491 ei
->op
.proc_read
= proc_pid_environ
;
1494 case PROC_TGID_AUXV
:
1495 inode
->i_fop
= &proc_info_file_operations
;
1496 ei
->op
.proc_read
= proc_pid_auxv
;
1498 case PROC_TID_STATUS
:
1499 case PROC_TGID_STATUS
:
1500 inode
->i_fop
= &proc_info_file_operations
;
1501 ei
->op
.proc_read
= proc_pid_status
;
1504 inode
->i_fop
= &proc_info_file_operations
;
1505 ei
->op
.proc_read
= proc_tid_stat
;
1507 case PROC_TGID_STAT
:
1508 inode
->i_fop
= &proc_info_file_operations
;
1509 ei
->op
.proc_read
= proc_tgid_stat
;
1511 case PROC_TID_CMDLINE
:
1512 case PROC_TGID_CMDLINE
:
1513 inode
->i_fop
= &proc_info_file_operations
;
1514 ei
->op
.proc_read
= proc_pid_cmdline
;
1516 case PROC_TID_STATM
:
1517 case PROC_TGID_STATM
:
1518 inode
->i_fop
= &proc_info_file_operations
;
1519 ei
->op
.proc_read
= proc_pid_statm
;
1522 case PROC_TGID_MAPS
:
1523 inode
->i_fop
= &proc_maps_operations
;
1527 inode
->i_op
= &proc_mem_inode_operations
;
1528 inode
->i_fop
= &proc_mem_operations
;
1530 #ifdef CONFIG_SECCOMP
1531 case PROC_TID_SECCOMP
:
1532 case PROC_TGID_SECCOMP
:
1533 inode
->i_fop
= &proc_seccomp_operations
;
1535 #endif /* CONFIG_SECCOMP */
1536 case PROC_TID_MOUNTS
:
1537 case PROC_TGID_MOUNTS
:
1538 inode
->i_fop
= &proc_mounts_operations
;
1540 #ifdef CONFIG_SECURITY
1543 inode
->i_op
= &proc_tid_attr_inode_operations
;
1544 inode
->i_fop
= &proc_tid_attr_operations
;
1546 case PROC_TGID_ATTR
:
1548 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1549 inode
->i_fop
= &proc_tgid_attr_operations
;
1551 case PROC_TID_ATTR_CURRENT
:
1552 case PROC_TGID_ATTR_CURRENT
:
1553 case PROC_TID_ATTR_PREV
:
1554 case PROC_TGID_ATTR_PREV
:
1555 case PROC_TID_ATTR_EXEC
:
1556 case PROC_TGID_ATTR_EXEC
:
1557 case PROC_TID_ATTR_FSCREATE
:
1558 case PROC_TGID_ATTR_FSCREATE
:
1559 inode
->i_fop
= &proc_pid_attr_operations
;
1562 #ifdef CONFIG_KALLSYMS
1563 case PROC_TID_WCHAN
:
1564 case PROC_TGID_WCHAN
:
1565 inode
->i_fop
= &proc_info_file_operations
;
1566 ei
->op
.proc_read
= proc_pid_wchan
;
1569 #ifdef CONFIG_SCHEDSTATS
1570 case PROC_TID_SCHEDSTAT
:
1571 case PROC_TGID_SCHEDSTAT
:
1572 inode
->i_fop
= &proc_info_file_operations
;
1573 ei
->op
.proc_read
= proc_pid_schedstat
;
1576 #ifdef CONFIG_CPUSETS
1577 case PROC_TID_CPUSET
:
1578 case PROC_TGID_CPUSET
:
1579 inode
->i_fop
= &proc_cpuset_operations
;
1582 case PROC_TID_OOM_SCORE
:
1583 case PROC_TGID_OOM_SCORE
:
1584 inode
->i_fop
= &proc_info_file_operations
;
1585 ei
->op
.proc_read
= proc_oom_score
;
1587 case PROC_TID_OOM_ADJUST
:
1588 case PROC_TGID_OOM_ADJUST
:
1589 inode
->i_fop
= &proc_oom_adjust_operations
;
1591 #ifdef CONFIG_AUDITSYSCALL
1592 case PROC_TID_LOGINUID
:
1593 case PROC_TGID_LOGINUID
:
1594 inode
->i_fop
= &proc_loginuid_operations
;
1598 printk("procfs: impossible type (%d)",p
->type
);
1600 return ERR_PTR(-EINVAL
);
1602 dentry
->d_op
= &pid_dentry_operations
;
1603 d_add(dentry
, inode
);
1607 return ERR_PTR(error
);
1610 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1611 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1614 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1615 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1618 static struct file_operations proc_tgid_base_operations
= {
1619 .read
= generic_read_dir
,
1620 .readdir
= proc_tgid_base_readdir
,
1623 static struct file_operations proc_tid_base_operations
= {
1624 .read
= generic_read_dir
,
1625 .readdir
= proc_tid_base_readdir
,
1628 static struct inode_operations proc_tgid_base_inode_operations
= {
1629 .lookup
= proc_tgid_base_lookup
,
1632 static struct inode_operations proc_tid_base_inode_operations
= {
1633 .lookup
= proc_tid_base_lookup
,
1636 #ifdef CONFIG_SECURITY
1637 static int proc_tgid_attr_readdir(struct file
* filp
,
1638 void * dirent
, filldir_t filldir
)
1640 return proc_pident_readdir(filp
,dirent
,filldir
,
1641 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1644 static int proc_tid_attr_readdir(struct file
* filp
,
1645 void * dirent
, filldir_t filldir
)
1647 return proc_pident_readdir(filp
,dirent
,filldir
,
1648 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1651 static struct file_operations proc_tgid_attr_operations
= {
1652 .read
= generic_read_dir
,
1653 .readdir
= proc_tgid_attr_readdir
,
1656 static struct file_operations proc_tid_attr_operations
= {
1657 .read
= generic_read_dir
,
1658 .readdir
= proc_tid_attr_readdir
,
1661 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1662 struct dentry
*dentry
, struct nameidata
*nd
)
1664 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1667 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1668 struct dentry
*dentry
, struct nameidata
*nd
)
1670 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1673 static struct inode_operations proc_tgid_attr_inode_operations
= {
1674 .lookup
= proc_tgid_attr_lookup
,
1677 static struct inode_operations proc_tid_attr_inode_operations
= {
1678 .lookup
= proc_tid_attr_lookup
,
1685 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1689 sprintf(tmp
, "%d", current
->tgid
);
1690 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1693 static int proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1696 sprintf(tmp
, "%d", current
->tgid
);
1697 return vfs_follow_link(nd
,tmp
);
1700 static struct inode_operations proc_self_inode_operations
= {
1701 .readlink
= proc_self_readlink
,
1702 .follow_link
= proc_self_follow_link
,
1706 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1707 * @p: task that should be flushed.
1709 * Drops the /proc/@pid dcache entry from the hash chains.
1711 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1712 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1713 * if the pid value is immediately reused. This is enforced by
1714 * - caller must acquire spin_lock(p->proc_lock)
1715 * - must be called before detach_pid()
1716 * - proc_pid_lookup acquires proc_lock, and checks that
1717 * the target is not dead by looking at the attach count
1721 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1723 struct dentry
*proc_dentry
;
1725 proc_dentry
= p
->proc_dentry
;
1726 if (proc_dentry
!= NULL
) {
1728 spin_lock(&dcache_lock
);
1729 spin_lock(&proc_dentry
->d_lock
);
1730 if (!d_unhashed(proc_dentry
)) {
1731 dget_locked(proc_dentry
);
1732 __d_drop(proc_dentry
);
1733 spin_unlock(&proc_dentry
->d_lock
);
1735 spin_unlock(&proc_dentry
->d_lock
);
1738 spin_unlock(&dcache_lock
);
1744 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1745 * @proc_dentry: directoy to prune.
1747 * Shrink the /proc directory that was used by the just killed thread.
1750 void proc_pid_flush(struct dentry
*proc_dentry
)
1753 if(proc_dentry
!= NULL
) {
1754 shrink_dcache_parent(proc_dentry
);
1760 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1762 struct task_struct
*task
;
1763 struct inode
*inode
;
1764 struct proc_inode
*ei
;
1768 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1769 inode
= new_inode(dir
->i_sb
);
1771 return ERR_PTR(-ENOMEM
);
1773 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1774 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1776 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1777 inode
->i_uid
= inode
->i_gid
= 0;
1779 inode
->i_op
= &proc_self_inode_operations
;
1780 d_add(dentry
, inode
);
1783 tgid
= name_to_int(dentry
);
1787 read_lock(&tasklist_lock
);
1788 task
= find_task_by_pid(tgid
);
1790 get_task_struct(task
);
1791 read_unlock(&tasklist_lock
);
1795 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1799 put_task_struct(task
);
1802 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1803 inode
->i_op
= &proc_tgid_base_inode_operations
;
1804 inode
->i_fop
= &proc_tgid_base_operations
;
1805 inode
->i_flags
|=S_IMMUTABLE
;
1806 #ifdef CONFIG_SECURITY
1812 dentry
->d_op
= &pid_base_dentry_operations
;
1815 d_add(dentry
, inode
);
1816 spin_lock(&task
->proc_lock
);
1817 task
->proc_dentry
= dentry
;
1818 if (!pid_alive(task
)) {
1819 dentry
= proc_pid_unhash(task
);
1822 spin_unlock(&task
->proc_lock
);
1824 put_task_struct(task
);
1826 proc_pid_flush(dentry
);
1831 return ERR_PTR(-ENOENT
);
1835 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1837 struct task_struct
*task
;
1838 struct task_struct
*leader
= proc_task(dir
);
1839 struct inode
*inode
;
1842 tid
= name_to_int(dentry
);
1846 read_lock(&tasklist_lock
);
1847 task
= find_task_by_pid(tid
);
1849 get_task_struct(task
);
1850 read_unlock(&tasklist_lock
);
1853 if (leader
->tgid
!= task
->tgid
)
1856 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
1861 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1862 inode
->i_op
= &proc_tid_base_inode_operations
;
1863 inode
->i_fop
= &proc_tid_base_operations
;
1864 inode
->i_flags
|=S_IMMUTABLE
;
1865 #ifdef CONFIG_SECURITY
1871 dentry
->d_op
= &pid_base_dentry_operations
;
1873 d_add(dentry
, inode
);
1875 put_task_struct(task
);
1878 put_task_struct(task
);
1880 return ERR_PTR(-ENOENT
);
1883 #define PROC_NUMBUF 10
1884 #define PROC_MAXPIDS 20
1887 * Get a few tgid's to return for filldir - we need to hold the
1888 * tasklist lock while doing this, and we must release it before
1889 * we actually do the filldir itself, so we use a temp buffer..
1891 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
1893 struct task_struct
*p
;
1897 read_lock(&tasklist_lock
);
1900 p
= find_task_by_pid(version
);
1901 if (p
&& !thread_group_leader(p
))
1908 p
= next_task(&init_task
);
1910 for ( ; p
!= &init_task
; p
= next_task(p
)) {
1916 tgids
[nr_tgids
] = tgid
;
1918 if (nr_tgids
>= PROC_MAXPIDS
)
1921 read_unlock(&tasklist_lock
);
1926 * Get a few tid's to return for filldir - we need to hold the
1927 * tasklist lock while doing this, and we must release it before
1928 * we actually do the filldir itself, so we use a temp buffer..
1930 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
1932 struct task_struct
*leader_task
= proc_task(dir
);
1933 struct task_struct
*task
= leader_task
;
1937 read_lock(&tasklist_lock
);
1939 * The starting point task (leader_task) might be an already
1940 * unlinked task, which cannot be used to access the task-list
1941 * via next_thread().
1943 if (pid_alive(task
)) do {
1944 int tid
= task
->pid
;
1949 tids
[nr_tids
] = tid
;
1951 if (nr_tids
>= PROC_MAXPIDS
)
1953 } while ((task
= next_thread(task
)) != leader_task
);
1954 read_unlock(&tasklist_lock
);
1958 /* for the /proc/ directory itself, after non-process stuff has been done */
1959 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1961 unsigned int tgid_array
[PROC_MAXPIDS
];
1962 char buf
[PROC_NUMBUF
];
1963 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
1964 unsigned int nr_tgids
, i
;
1968 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
1969 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
1975 /* f_version caches the tgid value that the last readdir call couldn't
1976 * return. lseek aka telldir automagically resets f_version to 0.
1978 next_tgid
= filp
->f_version
;
1979 filp
->f_version
= 0;
1981 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
1983 /* no more entries ! */
1988 /* do not use the last found pid, reserve it for next_tgid */
1989 if (nr_tgids
== PROC_MAXPIDS
) {
1991 next_tgid
= tgid_array
[nr_tgids
];
1994 for (i
=0;i
<nr_tgids
;i
++) {
1995 int tgid
= tgid_array
[i
];
1996 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
1997 unsigned long j
= PROC_NUMBUF
;
2000 buf
[--j
] = '0' + (tgid
% 10);
2001 while ((tgid
/= 10) != 0);
2003 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2004 /* returning this tgid failed, save it as the first
2005 * pid for the next readir call */
2006 filp
->f_version
= tgid_array
[i
];
2017 /* for the /proc/TGID/task/ directories */
2018 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2020 unsigned int tid_array
[PROC_MAXPIDS
];
2021 char buf
[PROC_NUMBUF
];
2022 unsigned int nr_tids
, i
;
2023 struct dentry
*dentry
= filp
->f_dentry
;
2024 struct inode
*inode
= dentry
->d_inode
;
2025 int retval
= -ENOENT
;
2027 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2029 if (!pid_alive(proc_task(inode
)))
2036 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2041 ino
= parent_ino(dentry
);
2042 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2048 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2049 inode
->i_nlink
= pos
+ nr_tids
;
2051 for (i
= 0; i
< nr_tids
; i
++) {
2052 unsigned long j
= PROC_NUMBUF
;
2053 int tid
= tid_array
[i
];
2055 ino
= fake_ino(tid
,PROC_TID_INO
);
2058 buf
[--j
] = '0' + (tid
% 10);
2059 while ((tid
/= 10) != 0);
2061 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)