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/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/kallsyms.h>
66 #include <linux/mount.h>
67 #include <linux/security.h>
68 #include <linux/ptrace.h>
69 #include <linux/seccomp.h>
70 #include <linux/cpuset.h>
71 #include <linux/audit.h>
75 * For hysterical raisins we keep the same inumbers as in the old procfs.
76 * Feel free to change the macro below - just keep the range distinct from
77 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
78 * As soon as we'll get a separate superblock we will be able to forget
79 * about magical ranges too.
82 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
84 enum pid_directory_inos
{
106 #ifdef CONFIG_SCHEDSTATS
109 #ifdef CONFIG_CPUSETS
112 #ifdef CONFIG_SECURITY
114 PROC_TGID_ATTR_CURRENT
,
117 PROC_TGID_ATTR_FSCREATE
,
119 #ifdef CONFIG_AUDITSYSCALL
123 PROC_TGID_OOM_ADJUST
,
127 #ifdef CONFIG_SECCOMP
144 #ifdef CONFIG_SCHEDSTATS
147 #ifdef CONFIG_CPUSETS
150 #ifdef CONFIG_SECURITY
152 PROC_TID_ATTR_CURRENT
,
155 PROC_TID_ATTR_FSCREATE
,
157 #ifdef CONFIG_AUDITSYSCALL
163 /* Add new entries before this */
164 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
174 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
176 static struct pid_entry tgid_base_stuff
[] = {
177 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
178 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
179 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
180 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
181 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
182 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
183 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
184 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
185 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
187 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
189 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
190 #ifdef CONFIG_SECCOMP
191 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
193 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
194 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
195 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
196 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
197 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
198 #ifdef CONFIG_SECURITY
199 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
201 #ifdef CONFIG_KALLSYMS
202 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
204 #ifdef CONFIG_SCHEDSTATS
205 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
207 #ifdef CONFIG_CPUSETS
208 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
210 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
211 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
212 #ifdef CONFIG_AUDITSYSCALL
213 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
217 static struct pid_entry tid_base_stuff
[] = {
218 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
219 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
220 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
221 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
222 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
223 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
224 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
225 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
227 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
229 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
230 #ifdef CONFIG_SECCOMP
231 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
233 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
234 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
235 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
236 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
237 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
238 #ifdef CONFIG_SECURITY
239 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
241 #ifdef CONFIG_KALLSYMS
242 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
244 #ifdef CONFIG_SCHEDSTATS
245 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
247 #ifdef CONFIG_CPUSETS
248 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
250 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
251 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
252 #ifdef CONFIG_AUDITSYSCALL
253 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
258 #ifdef CONFIG_SECURITY
259 static struct pid_entry tgid_attr_stuff
[] = {
260 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
261 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
262 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
263 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
266 static struct pid_entry tid_attr_stuff
[] = {
267 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
268 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
269 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
270 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
279 struct task_struct
*task
= proc_task(inode
);
280 struct files_struct
*files
;
282 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
284 files
= get_files_struct(task
);
286 spin_lock(&files
->file_lock
);
287 file
= fcheck_files(files
, fd
);
289 *mnt
= mntget(file
->f_vfsmnt
);
290 *dentry
= dget(file
->f_dentry
);
291 spin_unlock(&files
->file_lock
);
292 put_files_struct(files
);
295 spin_unlock(&files
->file_lock
);
296 put_files_struct(files
);
301 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
303 struct fs_struct
*fs
;
307 atomic_inc(&fs
->count
);
312 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
314 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
315 int result
= -ENOENT
;
317 read_lock(&fs
->lock
);
318 *mnt
= mntget(fs
->pwdmnt
);
319 *dentry
= dget(fs
->pwd
);
320 read_unlock(&fs
->lock
);
327 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
329 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
330 int result
= -ENOENT
;
332 read_lock(&fs
->lock
);
333 *mnt
= mntget(fs
->rootmnt
);
334 *dentry
= dget(fs
->root
);
335 read_unlock(&fs
->lock
);
342 #define MAY_PTRACE(task) \
343 (task == current || \
344 (task->parent == current && \
345 (task->ptrace & PT_PTRACED) && \
346 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
347 security_ptrace(current,task) == 0))
349 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
352 struct mm_struct
*mm
= get_task_mm(task
);
354 unsigned int len
= mm
->env_end
- mm
->env_start
;
357 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
358 if (!ptrace_may_attach(task
))
365 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
369 struct mm_struct
*mm
= get_task_mm(task
);
373 goto out_mm
; /* Shh! No looking before we're done */
375 len
= mm
->arg_end
- mm
->arg_start
;
380 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
382 // If the nul at the end of args has been overwritten, then
383 // assume application is using setproctitle(3).
384 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
385 len
= strnlen(buffer
, res
);
389 len
= mm
->env_end
- mm
->env_start
;
390 if (len
> PAGE_SIZE
- res
)
391 len
= PAGE_SIZE
- res
;
392 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
393 res
= strnlen(buffer
, res
);
402 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
405 struct mm_struct
*mm
= get_task_mm(task
);
407 unsigned int nwords
= 0;
410 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
411 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
414 memcpy(buffer
, mm
->saved_auxv
, res
);
421 #ifdef CONFIG_KALLSYMS
423 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
424 * Returns the resolved symbol. If that fails, simply return the address.
426 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
429 const char *sym_name
;
430 unsigned long wchan
, size
, offset
;
431 char namebuf
[KSYM_NAME_LEN
+1];
433 wchan
= get_wchan(task
);
435 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
437 return sprintf(buffer
, "%s", sym_name
);
438 return sprintf(buffer
, "%lu", wchan
);
440 #endif /* CONFIG_KALLSYMS */
442 #ifdef CONFIG_SCHEDSTATS
444 * Provides /proc/PID/schedstat
446 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
448 return sprintf(buffer
, "%lu %lu %lu\n",
449 task
->sched_info
.cpu_time
,
450 task
->sched_info
.run_delay
,
451 task
->sched_info
.pcnt
);
455 /* The badness from the OOM killer */
456 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
457 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
459 unsigned long points
;
460 struct timespec uptime
;
462 do_posix_clock_monotonic_gettime(&uptime
);
463 points
= badness(task
, uptime
.tv_sec
);
464 return sprintf(buffer
, "%lu\n", points
);
467 /************************************************************************/
468 /* Here the fs part begins */
469 /************************************************************************/
471 /* permission checks */
473 static int proc_check_root(struct inode
*inode
)
475 struct dentry
*de
, *base
, *root
;
476 struct vfsmount
*our_vfsmnt
, *vfsmnt
, *mnt
;
479 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
481 read_lock(¤t
->fs
->lock
);
482 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
483 base
= dget(current
->fs
->root
);
484 read_unlock(¤t
->fs
->lock
);
486 spin_lock(&vfsmount_lock
);
490 while (vfsmnt
!= our_vfsmnt
) {
491 if (vfsmnt
== vfsmnt
->mnt_parent
)
493 de
= vfsmnt
->mnt_mountpoint
;
494 vfsmnt
= vfsmnt
->mnt_parent
;
497 if (!is_subdir(de
, base
))
499 spin_unlock(&vfsmount_lock
);
508 spin_unlock(&vfsmount_lock
);
513 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
515 if (generic_permission(inode
, mask
, NULL
) != 0)
517 return proc_check_root(inode
);
520 extern struct seq_operations proc_pid_maps_op
;
521 static int maps_open(struct inode
*inode
, struct file
*file
)
523 struct task_struct
*task
= proc_task(inode
);
524 int ret
= seq_open(file
, &proc_pid_maps_op
);
526 struct seq_file
*m
= file
->private_data
;
532 static struct file_operations proc_maps_operations
= {
536 .release
= seq_release
,
540 extern struct seq_operations proc_pid_numa_maps_op
;
541 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
543 struct task_struct
*task
= proc_task(inode
);
544 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
546 struct seq_file
*m
= file
->private_data
;
552 static struct file_operations proc_numa_maps_operations
= {
553 .open
= numa_maps_open
,
556 .release
= seq_release
,
560 extern struct seq_operations proc_pid_smaps_op
;
561 static int smaps_open(struct inode
*inode
, struct file
*file
)
563 struct task_struct
*task
= proc_task(inode
);
564 int ret
= seq_open(file
, &proc_pid_smaps_op
);
566 struct seq_file
*m
= file
->private_data
;
572 static struct file_operations proc_smaps_operations
= {
576 .release
= seq_release
,
579 extern struct seq_operations mounts_op
;
580 static int mounts_open(struct inode
*inode
, struct file
*file
)
582 struct task_struct
*task
= proc_task(inode
);
583 int ret
= seq_open(file
, &mounts_op
);
586 struct seq_file
*m
= file
->private_data
;
587 struct namespace *namespace;
589 namespace = task
->namespace;
591 get_namespace(namespace);
595 m
->private = namespace;
597 seq_release(inode
, file
);
604 static int mounts_release(struct inode
*inode
, struct file
*file
)
606 struct seq_file
*m
= file
->private_data
;
607 struct namespace *namespace = m
->private;
608 put_namespace(namespace);
609 return seq_release(inode
, file
);
612 static struct file_operations proc_mounts_operations
= {
616 .release
= mounts_release
,
619 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
621 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
622 size_t count
, loff_t
*ppos
)
624 struct inode
* inode
= file
->f_dentry
->d_inode
;
627 struct task_struct
*task
= proc_task(inode
);
629 if (count
> PROC_BLOCK_SIZE
)
630 count
= PROC_BLOCK_SIZE
;
631 if (!(page
= __get_free_page(GFP_KERNEL
)))
634 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
637 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
642 static struct file_operations proc_info_file_operations
= {
643 .read
= proc_info_read
,
646 static int mem_open(struct inode
* inode
, struct file
* file
)
648 file
->private_data
= (void*)((long)current
->self_exec_id
);
652 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
653 size_t count
, loff_t
*ppos
)
655 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
657 unsigned long src
= *ppos
;
659 struct mm_struct
*mm
;
661 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
665 page
= (char *)__get_free_page(GFP_USER
);
671 mm
= get_task_mm(task
);
677 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
683 int this_len
, retval
;
685 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
686 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
687 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
693 if (copy_to_user(buf
, page
, retval
)) {
708 free_page((unsigned long) page
);
713 #define mem_write NULL
716 /* This is a security hazard */
717 static ssize_t
mem_write(struct file
* file
, const char * buf
,
718 size_t count
, loff_t
*ppos
)
722 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
723 unsigned long dst
= *ppos
;
725 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
728 page
= (char *)__get_free_page(GFP_USER
);
733 int this_len
, retval
;
735 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
736 if (copy_from_user(page
, buf
, this_len
)) {
740 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
752 free_page((unsigned long) page
);
757 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
761 file
->f_pos
= offset
;
764 file
->f_pos
+= offset
;
769 force_successful_syscall_return();
773 static struct file_operations proc_mem_operations
= {
780 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
781 size_t count
, loff_t
*ppos
)
783 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
786 int oom_adjust
= task
->oomkilladj
;
787 loff_t __ppos
= *ppos
;
789 len
= sprintf(buffer
, "%i\n", oom_adjust
);
792 if (count
> len
-__ppos
)
794 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
796 *ppos
= __ppos
+ count
;
800 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
801 size_t count
, loff_t
*ppos
)
803 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
804 char buffer
[8], *end
;
807 if (!capable(CAP_SYS_RESOURCE
))
809 memset(buffer
, 0, 8);
812 if (copy_from_user(buffer
, buf
, count
))
814 oom_adjust
= simple_strtol(buffer
, &end
, 0);
815 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
819 task
->oomkilladj
= oom_adjust
;
820 if (end
- buffer
== 0)
825 static struct file_operations proc_oom_adjust_operations
= {
826 .read
= oom_adjust_read
,
827 .write
= oom_adjust_write
,
830 static struct inode_operations proc_mem_inode_operations
= {
831 .permission
= proc_permission
,
834 #ifdef CONFIG_AUDITSYSCALL
836 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
837 size_t count
, loff_t
*ppos
)
839 struct inode
* inode
= file
->f_dentry
->d_inode
;
840 struct task_struct
*task
= proc_task(inode
);
842 char tmpbuf
[TMPBUFLEN
];
844 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
845 audit_get_loginuid(task
->audit_context
));
846 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
849 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
850 size_t count
, loff_t
*ppos
)
852 struct inode
* inode
= file
->f_dentry
->d_inode
;
855 struct task_struct
*task
= proc_task(inode
);
858 if (!capable(CAP_AUDIT_CONTROL
))
864 if (count
> PAGE_SIZE
)
868 /* No partial writes. */
871 page
= (char*)__get_free_page(GFP_USER
);
875 if (copy_from_user(page
, buf
, count
))
878 loginuid
= simple_strtoul(page
, &tmp
, 10);
884 length
= audit_set_loginuid(task
, loginuid
);
885 if (likely(length
== 0))
889 free_page((unsigned long) page
);
893 static struct file_operations proc_loginuid_operations
= {
894 .read
= proc_loginuid_read
,
895 .write
= proc_loginuid_write
,
899 #ifdef CONFIG_SECCOMP
900 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
901 size_t count
, loff_t
*ppos
)
903 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
905 loff_t __ppos
= *ppos
;
908 /* no need to print the trailing zero, so use only len */
909 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
912 if (count
> len
- __ppos
)
913 count
= len
- __ppos
;
914 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
916 *ppos
= __ppos
+ count
;
920 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
921 size_t count
, loff_t
*ppos
)
923 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
924 char __buf
[20], *end
;
925 unsigned int seccomp_mode
;
927 /* can set it only once to be even more secure */
928 if (unlikely(tsk
->seccomp
.mode
))
931 memset(__buf
, 0, sizeof(__buf
));
932 count
= min(count
, sizeof(__buf
) - 1);
933 if (copy_from_user(__buf
, buf
, count
))
935 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
938 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
939 tsk
->seccomp
.mode
= seccomp_mode
;
940 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
943 if (unlikely(!(end
- __buf
)))
948 static struct file_operations proc_seccomp_operations
= {
949 .read
= seccomp_read
,
950 .write
= seccomp_write
,
952 #endif /* CONFIG_SECCOMP */
954 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
956 struct inode
*inode
= dentry
->d_inode
;
959 /* We don't need a base pointer in the /proc filesystem */
962 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
964 error
= proc_check_root(inode
);
968 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
969 nd
->last_type
= LAST_BIND
;
971 return ERR_PTR(error
);
974 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
975 char __user
*buffer
, int buflen
)
977 struct inode
* inode
;
978 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
984 inode
= dentry
->d_inode
;
985 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
989 len
= tmp
+ PAGE_SIZE
- 1 - path
;
993 if (copy_to_user(buffer
, path
, len
))
996 free_page((unsigned long)tmp
);
1000 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1002 int error
= -EACCES
;
1003 struct inode
*inode
= dentry
->d_inode
;
1005 struct vfsmount
*mnt
= NULL
;
1009 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1011 error
= proc_check_root(inode
);
1015 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1019 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1027 static struct inode_operations proc_pid_link_inode_operations
= {
1028 .readlink
= proc_pid_readlink
,
1029 .follow_link
= proc_pid_follow_link
1034 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1036 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1037 struct task_struct
*p
= proc_task(inode
);
1038 unsigned int fd
, tid
, ino
;
1041 struct files_struct
* files
;
1052 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1056 ino
= fake_ino(tid
, PROC_TID_INO
);
1057 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1061 files
= get_files_struct(p
);
1064 spin_lock(&files
->file_lock
);
1065 for (fd
= filp
->f_pos
-2;
1066 fd
< files
->max_fds
;
1067 fd
++, filp
->f_pos
++) {
1070 if (!fcheck_files(files
, fd
))
1072 spin_unlock(&files
->file_lock
);
1078 buf
[j
] = '0' + (i
% 10);
1082 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1083 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1084 spin_lock(&files
->file_lock
);
1087 spin_lock(&files
->file_lock
);
1089 spin_unlock(&files
->file_lock
);
1090 put_files_struct(files
);
1096 static int proc_pident_readdir(struct file
*filp
,
1097 void *dirent
, filldir_t filldir
,
1098 struct pid_entry
*ents
, unsigned int nents
)
1102 struct dentry
*dentry
= filp
->f_dentry
;
1103 struct inode
*inode
= dentry
->d_inode
;
1104 struct pid_entry
*p
;
1109 if (!pid_alive(proc_task(inode
)))
1113 pid
= proc_task(inode
)->pid
;
1118 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1124 ino
= parent_ino(dentry
);
1125 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1138 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1139 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1151 static int proc_tgid_base_readdir(struct file
* filp
,
1152 void * dirent
, filldir_t filldir
)
1154 return proc_pident_readdir(filp
,dirent
,filldir
,
1155 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1158 static int proc_tid_base_readdir(struct file
* filp
,
1159 void * dirent
, filldir_t filldir
)
1161 return proc_pident_readdir(filp
,dirent
,filldir
,
1162 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1165 /* building an inode */
1167 static int task_dumpable(struct task_struct
*task
)
1170 struct mm_struct
*mm
;
1175 dumpable
= mm
->dumpable
;
1183 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1185 struct inode
* inode
;
1186 struct proc_inode
*ei
;
1188 /* We need a new inode */
1190 inode
= new_inode(sb
);
1197 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1198 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1200 if (!pid_alive(task
))
1204 * grab the reference to task.
1206 get_task_struct(task
);
1211 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1212 inode
->i_uid
= task
->euid
;
1213 inode
->i_gid
= task
->egid
;
1215 security_task_to_inode(task
, inode
);
1229 * Exceptional case: normally we are not allowed to unhash a busy
1230 * directory. In this case, however, we can do it - no aliasing problems
1231 * due to the way we treat inodes.
1233 * Rewrite the inode's ownerships here because the owning task may have
1234 * performed a setuid(), etc.
1236 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1238 struct inode
*inode
= dentry
->d_inode
;
1239 struct task_struct
*task
= proc_task(inode
);
1240 if (pid_alive(task
)) {
1241 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1242 inode
->i_uid
= task
->euid
;
1243 inode
->i_gid
= task
->egid
;
1248 security_task_to_inode(task
, inode
);
1255 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1257 struct inode
*inode
= dentry
->d_inode
;
1258 struct task_struct
*task
= proc_task(inode
);
1259 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1260 struct files_struct
*files
;
1262 files
= get_files_struct(task
);
1264 spin_lock(&files
->file_lock
);
1265 if (fcheck_files(files
, fd
)) {
1266 spin_unlock(&files
->file_lock
);
1267 put_files_struct(files
);
1268 if (task_dumpable(task
)) {
1269 inode
->i_uid
= task
->euid
;
1270 inode
->i_gid
= task
->egid
;
1275 security_task_to_inode(task
, inode
);
1278 spin_unlock(&files
->file_lock
);
1279 put_files_struct(files
);
1285 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1287 struct task_struct
*task
= proc_task(inode
);
1288 spin_lock(&task
->proc_lock
);
1289 if (task
->proc_dentry
== dentry
)
1290 task
->proc_dentry
= NULL
;
1291 spin_unlock(&task
->proc_lock
);
1295 static int pid_delete_dentry(struct dentry
* dentry
)
1297 /* Is the task we represent dead?
1298 * If so, then don't put the dentry on the lru list,
1299 * kill it immediately.
1301 return !pid_alive(proc_task(dentry
->d_inode
));
1304 static struct dentry_operations tid_fd_dentry_operations
=
1306 .d_revalidate
= tid_fd_revalidate
,
1307 .d_delete
= pid_delete_dentry
,
1310 static struct dentry_operations pid_dentry_operations
=
1312 .d_revalidate
= pid_revalidate
,
1313 .d_delete
= pid_delete_dentry
,
1316 static struct dentry_operations pid_base_dentry_operations
=
1318 .d_revalidate
= pid_revalidate
,
1319 .d_iput
= pid_base_iput
,
1320 .d_delete
= pid_delete_dentry
,
1325 static unsigned name_to_int(struct dentry
*dentry
)
1327 const char *name
= dentry
->d_name
.name
;
1328 int len
= dentry
->d_name
.len
;
1331 if (len
> 1 && *name
== '0')
1334 unsigned c
= *name
++ - '0';
1337 if (n
>= (~0U-9)/10)
1348 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1350 struct task_struct
*task
= proc_task(dir
);
1351 unsigned fd
= name_to_int(dentry
);
1353 struct files_struct
* files
;
1354 struct inode
*inode
;
1355 struct proc_inode
*ei
;
1359 if (!pid_alive(task
))
1362 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1366 files
= get_files_struct(task
);
1369 inode
->i_mode
= S_IFLNK
;
1370 spin_lock(&files
->file_lock
);
1371 file
= fcheck_files(files
, fd
);
1374 if (file
->f_mode
& 1)
1375 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1376 if (file
->f_mode
& 2)
1377 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1378 spin_unlock(&files
->file_lock
);
1379 put_files_struct(files
);
1380 inode
->i_op
= &proc_pid_link_inode_operations
;
1382 ei
->op
.proc_get_link
= proc_fd_link
;
1383 dentry
->d_op
= &tid_fd_dentry_operations
;
1384 d_add(dentry
, inode
);
1388 spin_unlock(&files
->file_lock
);
1389 put_files_struct(files
);
1393 return ERR_PTR(-ENOENT
);
1396 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1397 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1399 static struct file_operations proc_fd_operations
= {
1400 .read
= generic_read_dir
,
1401 .readdir
= proc_readfd
,
1404 static struct file_operations proc_task_operations
= {
1405 .read
= generic_read_dir
,
1406 .readdir
= proc_task_readdir
,
1410 * proc directories can do almost nothing..
1412 static struct inode_operations proc_fd_inode_operations
= {
1413 .lookup
= proc_lookupfd
,
1414 .permission
= proc_permission
,
1417 static struct inode_operations proc_task_inode_operations
= {
1418 .lookup
= proc_task_lookup
,
1419 .permission
= proc_permission
,
1422 #ifdef CONFIG_SECURITY
1423 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1424 size_t count
, loff_t
*ppos
)
1426 struct inode
* inode
= file
->f_dentry
->d_inode
;
1429 struct task_struct
*task
= proc_task(inode
);
1431 if (count
> PAGE_SIZE
)
1433 if (!(page
= __get_free_page(GFP_KERNEL
)))
1436 length
= security_getprocattr(task
,
1437 (char*)file
->f_dentry
->d_name
.name
,
1438 (void*)page
, count
);
1440 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1445 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1446 size_t count
, loff_t
*ppos
)
1448 struct inode
* inode
= file
->f_dentry
->d_inode
;
1451 struct task_struct
*task
= proc_task(inode
);
1453 if (count
> PAGE_SIZE
)
1456 /* No partial writes. */
1459 page
= (char*)__get_free_page(GFP_USER
);
1463 if (copy_from_user(page
, buf
, count
))
1466 length
= security_setprocattr(task
,
1467 (char*)file
->f_dentry
->d_name
.name
,
1468 (void*)page
, count
);
1470 free_page((unsigned long) page
);
1474 static struct file_operations proc_pid_attr_operations
= {
1475 .read
= proc_pid_attr_read
,
1476 .write
= proc_pid_attr_write
,
1479 static struct file_operations proc_tid_attr_operations
;
1480 static struct inode_operations proc_tid_attr_inode_operations
;
1481 static struct file_operations proc_tgid_attr_operations
;
1482 static struct inode_operations proc_tgid_attr_inode_operations
;
1485 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1488 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1489 struct dentry
*dentry
,
1490 struct pid_entry
*ents
)
1492 struct inode
*inode
;
1494 struct task_struct
*task
= proc_task(dir
);
1495 struct pid_entry
*p
;
1496 struct proc_inode
*ei
;
1501 if (!pid_alive(task
))
1504 for (p
= ents
; p
->name
; p
++) {
1505 if (p
->len
!= dentry
->d_name
.len
)
1507 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1514 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1519 inode
->i_mode
= p
->mode
;
1521 * Yes, it does not scale. And it should not. Don't add
1522 * new entries into /proc/<tgid>/ without very good reasons.
1525 case PROC_TGID_TASK
:
1526 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1527 inode
->i_op
= &proc_task_inode_operations
;
1528 inode
->i_fop
= &proc_task_operations
;
1533 inode
->i_op
= &proc_fd_inode_operations
;
1534 inode
->i_fop
= &proc_fd_operations
;
1538 inode
->i_op
= &proc_pid_link_inode_operations
;
1539 ei
->op
.proc_get_link
= proc_exe_link
;
1543 inode
->i_op
= &proc_pid_link_inode_operations
;
1544 ei
->op
.proc_get_link
= proc_cwd_link
;
1547 case PROC_TGID_ROOT
:
1548 inode
->i_op
= &proc_pid_link_inode_operations
;
1549 ei
->op
.proc_get_link
= proc_root_link
;
1551 case PROC_TID_ENVIRON
:
1552 case PROC_TGID_ENVIRON
:
1553 inode
->i_fop
= &proc_info_file_operations
;
1554 ei
->op
.proc_read
= proc_pid_environ
;
1557 case PROC_TGID_AUXV
:
1558 inode
->i_fop
= &proc_info_file_operations
;
1559 ei
->op
.proc_read
= proc_pid_auxv
;
1561 case PROC_TID_STATUS
:
1562 case PROC_TGID_STATUS
:
1563 inode
->i_fop
= &proc_info_file_operations
;
1564 ei
->op
.proc_read
= proc_pid_status
;
1567 inode
->i_fop
= &proc_info_file_operations
;
1568 ei
->op
.proc_read
= proc_tid_stat
;
1570 case PROC_TGID_STAT
:
1571 inode
->i_fop
= &proc_info_file_operations
;
1572 ei
->op
.proc_read
= proc_tgid_stat
;
1574 case PROC_TID_CMDLINE
:
1575 case PROC_TGID_CMDLINE
:
1576 inode
->i_fop
= &proc_info_file_operations
;
1577 ei
->op
.proc_read
= proc_pid_cmdline
;
1579 case PROC_TID_STATM
:
1580 case PROC_TGID_STATM
:
1581 inode
->i_fop
= &proc_info_file_operations
;
1582 ei
->op
.proc_read
= proc_pid_statm
;
1585 case PROC_TGID_MAPS
:
1586 inode
->i_fop
= &proc_maps_operations
;
1589 case PROC_TID_NUMA_MAPS
:
1590 case PROC_TGID_NUMA_MAPS
:
1591 inode
->i_fop
= &proc_numa_maps_operations
;
1596 inode
->i_op
= &proc_mem_inode_operations
;
1597 inode
->i_fop
= &proc_mem_operations
;
1599 #ifdef CONFIG_SECCOMP
1600 case PROC_TID_SECCOMP
:
1601 case PROC_TGID_SECCOMP
:
1602 inode
->i_fop
= &proc_seccomp_operations
;
1604 #endif /* CONFIG_SECCOMP */
1605 case PROC_TID_MOUNTS
:
1606 case PROC_TGID_MOUNTS
:
1607 inode
->i_fop
= &proc_mounts_operations
;
1609 case PROC_TID_SMAPS
:
1610 case PROC_TGID_SMAPS
:
1611 inode
->i_fop
= &proc_smaps_operations
;
1613 #ifdef CONFIG_SECURITY
1616 inode
->i_op
= &proc_tid_attr_inode_operations
;
1617 inode
->i_fop
= &proc_tid_attr_operations
;
1619 case PROC_TGID_ATTR
:
1621 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1622 inode
->i_fop
= &proc_tgid_attr_operations
;
1624 case PROC_TID_ATTR_CURRENT
:
1625 case PROC_TGID_ATTR_CURRENT
:
1626 case PROC_TID_ATTR_PREV
:
1627 case PROC_TGID_ATTR_PREV
:
1628 case PROC_TID_ATTR_EXEC
:
1629 case PROC_TGID_ATTR_EXEC
:
1630 case PROC_TID_ATTR_FSCREATE
:
1631 case PROC_TGID_ATTR_FSCREATE
:
1632 inode
->i_fop
= &proc_pid_attr_operations
;
1635 #ifdef CONFIG_KALLSYMS
1636 case PROC_TID_WCHAN
:
1637 case PROC_TGID_WCHAN
:
1638 inode
->i_fop
= &proc_info_file_operations
;
1639 ei
->op
.proc_read
= proc_pid_wchan
;
1642 #ifdef CONFIG_SCHEDSTATS
1643 case PROC_TID_SCHEDSTAT
:
1644 case PROC_TGID_SCHEDSTAT
:
1645 inode
->i_fop
= &proc_info_file_operations
;
1646 ei
->op
.proc_read
= proc_pid_schedstat
;
1649 #ifdef CONFIG_CPUSETS
1650 case PROC_TID_CPUSET
:
1651 case PROC_TGID_CPUSET
:
1652 inode
->i_fop
= &proc_cpuset_operations
;
1655 case PROC_TID_OOM_SCORE
:
1656 case PROC_TGID_OOM_SCORE
:
1657 inode
->i_fop
= &proc_info_file_operations
;
1658 ei
->op
.proc_read
= proc_oom_score
;
1660 case PROC_TID_OOM_ADJUST
:
1661 case PROC_TGID_OOM_ADJUST
:
1662 inode
->i_fop
= &proc_oom_adjust_operations
;
1664 #ifdef CONFIG_AUDITSYSCALL
1665 case PROC_TID_LOGINUID
:
1666 case PROC_TGID_LOGINUID
:
1667 inode
->i_fop
= &proc_loginuid_operations
;
1671 printk("procfs: impossible type (%d)",p
->type
);
1673 return ERR_PTR(-EINVAL
);
1675 dentry
->d_op
= &pid_dentry_operations
;
1676 d_add(dentry
, inode
);
1680 return ERR_PTR(error
);
1683 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1684 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1687 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1688 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1691 static struct file_operations proc_tgid_base_operations
= {
1692 .read
= generic_read_dir
,
1693 .readdir
= proc_tgid_base_readdir
,
1696 static struct file_operations proc_tid_base_operations
= {
1697 .read
= generic_read_dir
,
1698 .readdir
= proc_tid_base_readdir
,
1701 static struct inode_operations proc_tgid_base_inode_operations
= {
1702 .lookup
= proc_tgid_base_lookup
,
1705 static struct inode_operations proc_tid_base_inode_operations
= {
1706 .lookup
= proc_tid_base_lookup
,
1709 #ifdef CONFIG_SECURITY
1710 static int proc_tgid_attr_readdir(struct file
* filp
,
1711 void * dirent
, filldir_t filldir
)
1713 return proc_pident_readdir(filp
,dirent
,filldir
,
1714 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1717 static int proc_tid_attr_readdir(struct file
* filp
,
1718 void * dirent
, filldir_t filldir
)
1720 return proc_pident_readdir(filp
,dirent
,filldir
,
1721 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1724 static struct file_operations proc_tgid_attr_operations
= {
1725 .read
= generic_read_dir
,
1726 .readdir
= proc_tgid_attr_readdir
,
1729 static struct file_operations proc_tid_attr_operations
= {
1730 .read
= generic_read_dir
,
1731 .readdir
= proc_tid_attr_readdir
,
1734 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1735 struct dentry
*dentry
, struct nameidata
*nd
)
1737 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1740 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1741 struct dentry
*dentry
, struct nameidata
*nd
)
1743 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1746 static struct inode_operations proc_tgid_attr_inode_operations
= {
1747 .lookup
= proc_tgid_attr_lookup
,
1750 static struct inode_operations proc_tid_attr_inode_operations
= {
1751 .lookup
= proc_tid_attr_lookup
,
1758 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1762 sprintf(tmp
, "%d", current
->tgid
);
1763 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1766 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1769 sprintf(tmp
, "%d", current
->tgid
);
1770 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1773 static struct inode_operations proc_self_inode_operations
= {
1774 .readlink
= proc_self_readlink
,
1775 .follow_link
= proc_self_follow_link
,
1779 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1780 * @p: task that should be flushed.
1782 * Drops the /proc/@pid dcache entry from the hash chains.
1784 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1785 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1786 * if the pid value is immediately reused. This is enforced by
1787 * - caller must acquire spin_lock(p->proc_lock)
1788 * - must be called before detach_pid()
1789 * - proc_pid_lookup acquires proc_lock, and checks that
1790 * the target is not dead by looking at the attach count
1794 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1796 struct dentry
*proc_dentry
;
1798 proc_dentry
= p
->proc_dentry
;
1799 if (proc_dentry
!= NULL
) {
1801 spin_lock(&dcache_lock
);
1802 spin_lock(&proc_dentry
->d_lock
);
1803 if (!d_unhashed(proc_dentry
)) {
1804 dget_locked(proc_dentry
);
1805 __d_drop(proc_dentry
);
1806 spin_unlock(&proc_dentry
->d_lock
);
1808 spin_unlock(&proc_dentry
->d_lock
);
1811 spin_unlock(&dcache_lock
);
1817 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1818 * @proc_dentry: directoy to prune.
1820 * Shrink the /proc directory that was used by the just killed thread.
1823 void proc_pid_flush(struct dentry
*proc_dentry
)
1826 if(proc_dentry
!= NULL
) {
1827 shrink_dcache_parent(proc_dentry
);
1833 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1835 struct task_struct
*task
;
1836 struct inode
*inode
;
1837 struct proc_inode
*ei
;
1841 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1842 inode
= new_inode(dir
->i_sb
);
1844 return ERR_PTR(-ENOMEM
);
1846 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1847 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1849 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1850 inode
->i_uid
= inode
->i_gid
= 0;
1852 inode
->i_op
= &proc_self_inode_operations
;
1853 d_add(dentry
, inode
);
1856 tgid
= name_to_int(dentry
);
1860 read_lock(&tasklist_lock
);
1861 task
= find_task_by_pid(tgid
);
1863 get_task_struct(task
);
1864 read_unlock(&tasklist_lock
);
1868 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1872 put_task_struct(task
);
1875 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1876 inode
->i_op
= &proc_tgid_base_inode_operations
;
1877 inode
->i_fop
= &proc_tgid_base_operations
;
1878 inode
->i_flags
|=S_IMMUTABLE
;
1879 #ifdef CONFIG_SECURITY
1885 dentry
->d_op
= &pid_base_dentry_operations
;
1888 d_add(dentry
, inode
);
1889 spin_lock(&task
->proc_lock
);
1890 task
->proc_dentry
= dentry
;
1891 if (!pid_alive(task
)) {
1892 dentry
= proc_pid_unhash(task
);
1895 spin_unlock(&task
->proc_lock
);
1897 put_task_struct(task
);
1899 proc_pid_flush(dentry
);
1904 return ERR_PTR(-ENOENT
);
1908 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1910 struct task_struct
*task
;
1911 struct task_struct
*leader
= proc_task(dir
);
1912 struct inode
*inode
;
1915 tid
= name_to_int(dentry
);
1919 read_lock(&tasklist_lock
);
1920 task
= find_task_by_pid(tid
);
1922 get_task_struct(task
);
1923 read_unlock(&tasklist_lock
);
1926 if (leader
->tgid
!= task
->tgid
)
1929 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
1934 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1935 inode
->i_op
= &proc_tid_base_inode_operations
;
1936 inode
->i_fop
= &proc_tid_base_operations
;
1937 inode
->i_flags
|=S_IMMUTABLE
;
1938 #ifdef CONFIG_SECURITY
1944 dentry
->d_op
= &pid_base_dentry_operations
;
1946 d_add(dentry
, inode
);
1948 put_task_struct(task
);
1951 put_task_struct(task
);
1953 return ERR_PTR(-ENOENT
);
1956 #define PROC_NUMBUF 10
1957 #define PROC_MAXPIDS 20
1960 * Get a few tgid's to return for filldir - we need to hold the
1961 * tasklist lock while doing this, and we must release it before
1962 * we actually do the filldir itself, so we use a temp buffer..
1964 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
1966 struct task_struct
*p
;
1970 read_lock(&tasklist_lock
);
1973 p
= find_task_by_pid(version
);
1974 if (p
&& !thread_group_leader(p
))
1981 p
= next_task(&init_task
);
1983 for ( ; p
!= &init_task
; p
= next_task(p
)) {
1989 tgids
[nr_tgids
] = tgid
;
1991 if (nr_tgids
>= PROC_MAXPIDS
)
1994 read_unlock(&tasklist_lock
);
1999 * Get a few tid's to return for filldir - we need to hold the
2000 * tasklist lock while doing this, and we must release it before
2001 * we actually do the filldir itself, so we use a temp buffer..
2003 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2005 struct task_struct
*leader_task
= proc_task(dir
);
2006 struct task_struct
*task
= leader_task
;
2010 read_lock(&tasklist_lock
);
2012 * The starting point task (leader_task) might be an already
2013 * unlinked task, which cannot be used to access the task-list
2014 * via next_thread().
2016 if (pid_alive(task
)) do {
2017 int tid
= task
->pid
;
2022 tids
[nr_tids
] = tid
;
2024 if (nr_tids
>= PROC_MAXPIDS
)
2026 } while ((task
= next_thread(task
)) != leader_task
);
2027 read_unlock(&tasklist_lock
);
2031 /* for the /proc/ directory itself, after non-process stuff has been done */
2032 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2034 unsigned int tgid_array
[PROC_MAXPIDS
];
2035 char buf
[PROC_NUMBUF
];
2036 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2037 unsigned int nr_tgids
, i
;
2041 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2042 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2048 /* f_version caches the tgid value that the last readdir call couldn't
2049 * return. lseek aka telldir automagically resets f_version to 0.
2051 next_tgid
= filp
->f_version
;
2052 filp
->f_version
= 0;
2054 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2056 /* no more entries ! */
2061 /* do not use the last found pid, reserve it for next_tgid */
2062 if (nr_tgids
== PROC_MAXPIDS
) {
2064 next_tgid
= tgid_array
[nr_tgids
];
2067 for (i
=0;i
<nr_tgids
;i
++) {
2068 int tgid
= tgid_array
[i
];
2069 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2070 unsigned long j
= PROC_NUMBUF
;
2073 buf
[--j
] = '0' + (tgid
% 10);
2074 while ((tgid
/= 10) != 0);
2076 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2077 /* returning this tgid failed, save it as the first
2078 * pid for the next readir call */
2079 filp
->f_version
= tgid_array
[i
];
2090 /* for the /proc/TGID/task/ directories */
2091 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2093 unsigned int tid_array
[PROC_MAXPIDS
];
2094 char buf
[PROC_NUMBUF
];
2095 unsigned int nr_tids
, i
;
2096 struct dentry
*dentry
= filp
->f_dentry
;
2097 struct inode
*inode
= dentry
->d_inode
;
2098 int retval
= -ENOENT
;
2100 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2102 if (!pid_alive(proc_task(inode
)))
2109 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2114 ino
= parent_ino(dentry
);
2115 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2121 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2122 inode
->i_nlink
= pos
+ nr_tids
;
2124 for (i
= 0; i
< nr_tids
; i
++) {
2125 unsigned long j
= PROC_NUMBUF
;
2126 int tid
= tid_array
[i
];
2128 ino
= fake_ino(tid
,PROC_TID_INO
);
2131 buf
[--j
] = '0' + (tid
% 10);
2132 while ((tid
/= 10) != 0);
2134 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)