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 may_ptrace_attach(struct task_struct
*task
)
357 if (((current
->uid
!= task
->euid
) ||
358 (current
->uid
!= task
->suid
) ||
359 (current
->uid
!= task
->uid
) ||
360 (current
->gid
!= task
->egid
) ||
361 (current
->gid
!= task
->sgid
) ||
362 (current
->gid
!= task
->gid
)) && !capable(CAP_SYS_PTRACE
))
365 if (task
->mm
->dumpable
!= 1 && !capable(CAP_SYS_PTRACE
))
367 if (security_ptrace(current
, task
))
376 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
379 struct mm_struct
*mm
= get_task_mm(task
);
381 unsigned int len
= mm
->env_end
- mm
->env_start
;
384 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
385 if (!may_ptrace_attach(task
))
392 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
396 struct mm_struct
*mm
= get_task_mm(task
);
400 goto out_mm
; /* Shh! No looking before we're done */
402 len
= mm
->arg_end
- mm
->arg_start
;
407 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
409 // If the nul at the end of args has been overwritten, then
410 // assume application is using setproctitle(3).
411 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
412 len
= strnlen(buffer
, res
);
416 len
= mm
->env_end
- mm
->env_start
;
417 if (len
> PAGE_SIZE
- res
)
418 len
= PAGE_SIZE
- res
;
419 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
420 res
= strnlen(buffer
, res
);
429 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
432 struct mm_struct
*mm
= get_task_mm(task
);
434 unsigned int nwords
= 0;
437 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
438 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
441 memcpy(buffer
, mm
->saved_auxv
, res
);
448 #ifdef CONFIG_KALLSYMS
450 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
451 * Returns the resolved symbol. If that fails, simply return the address.
453 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
456 const char *sym_name
;
457 unsigned long wchan
, size
, offset
;
458 char namebuf
[KSYM_NAME_LEN
+1];
460 wchan
= get_wchan(task
);
462 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
464 return sprintf(buffer
, "%s", sym_name
);
465 return sprintf(buffer
, "%lu", wchan
);
467 #endif /* CONFIG_KALLSYMS */
469 #ifdef CONFIG_SCHEDSTATS
471 * Provides /proc/PID/schedstat
473 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
475 return sprintf(buffer
, "%lu %lu %lu\n",
476 task
->sched_info
.cpu_time
,
477 task
->sched_info
.run_delay
,
478 task
->sched_info
.pcnt
);
482 /* The badness from the OOM killer */
483 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
484 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
486 unsigned long points
;
487 struct timespec uptime
;
489 do_posix_clock_monotonic_gettime(&uptime
);
490 points
= badness(task
, uptime
.tv_sec
);
491 return sprintf(buffer
, "%lu\n", points
);
494 /************************************************************************/
495 /* Here the fs part begins */
496 /************************************************************************/
498 /* permission checks */
500 static int proc_check_root(struct inode
*inode
)
502 struct dentry
*de
, *base
, *root
;
503 struct vfsmount
*our_vfsmnt
, *vfsmnt
, *mnt
;
506 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
508 read_lock(¤t
->fs
->lock
);
509 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
510 base
= dget(current
->fs
->root
);
511 read_unlock(¤t
->fs
->lock
);
513 spin_lock(&vfsmount_lock
);
517 while (vfsmnt
!= our_vfsmnt
) {
518 if (vfsmnt
== vfsmnt
->mnt_parent
)
520 de
= vfsmnt
->mnt_mountpoint
;
521 vfsmnt
= vfsmnt
->mnt_parent
;
524 if (!is_subdir(de
, base
))
526 spin_unlock(&vfsmount_lock
);
535 spin_unlock(&vfsmount_lock
);
540 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
542 if (generic_permission(inode
, mask
, NULL
) != 0)
544 return proc_check_root(inode
);
547 extern struct seq_operations proc_pid_maps_op
;
548 static int maps_open(struct inode
*inode
, struct file
*file
)
550 struct task_struct
*task
= proc_task(inode
);
551 int ret
= seq_open(file
, &proc_pid_maps_op
);
553 struct seq_file
*m
= file
->private_data
;
559 static struct file_operations proc_maps_operations
= {
563 .release
= seq_release
,
567 extern struct seq_operations proc_pid_numa_maps_op
;
568 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
570 struct task_struct
*task
= proc_task(inode
);
571 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
573 struct seq_file
*m
= file
->private_data
;
579 static struct file_operations proc_numa_maps_operations
= {
580 .open
= numa_maps_open
,
583 .release
= seq_release
,
587 extern struct seq_operations proc_pid_smaps_op
;
588 static int smaps_open(struct inode
*inode
, struct file
*file
)
590 struct task_struct
*task
= proc_task(inode
);
591 int ret
= seq_open(file
, &proc_pid_smaps_op
);
593 struct seq_file
*m
= file
->private_data
;
599 static struct file_operations proc_smaps_operations
= {
603 .release
= seq_release
,
606 extern struct seq_operations mounts_op
;
607 static int mounts_open(struct inode
*inode
, struct file
*file
)
609 struct task_struct
*task
= proc_task(inode
);
610 int ret
= seq_open(file
, &mounts_op
);
613 struct seq_file
*m
= file
->private_data
;
614 struct namespace *namespace;
616 namespace = task
->namespace;
618 get_namespace(namespace);
622 m
->private = namespace;
624 seq_release(inode
, file
);
631 static int mounts_release(struct inode
*inode
, struct file
*file
)
633 struct seq_file
*m
= file
->private_data
;
634 struct namespace *namespace = m
->private;
635 put_namespace(namespace);
636 return seq_release(inode
, file
);
639 static struct file_operations proc_mounts_operations
= {
643 .release
= mounts_release
,
646 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
648 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
649 size_t count
, loff_t
*ppos
)
651 struct inode
* inode
= file
->f_dentry
->d_inode
;
654 struct task_struct
*task
= proc_task(inode
);
656 if (count
> PROC_BLOCK_SIZE
)
657 count
= PROC_BLOCK_SIZE
;
658 if (!(page
= __get_free_page(GFP_KERNEL
)))
661 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
664 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
669 static struct file_operations proc_info_file_operations
= {
670 .read
= proc_info_read
,
673 static int mem_open(struct inode
* inode
, struct file
* file
)
675 file
->private_data
= (void*)((long)current
->self_exec_id
);
679 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
680 size_t count
, loff_t
*ppos
)
682 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
684 unsigned long src
= *ppos
;
686 struct mm_struct
*mm
;
688 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
692 page
= (char *)__get_free_page(GFP_USER
);
698 mm
= get_task_mm(task
);
704 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
710 int this_len
, retval
;
712 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
713 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
714 if (!retval
|| !MAY_PTRACE(task
) || !may_ptrace_attach(task
)) {
720 if (copy_to_user(buf
, page
, retval
)) {
735 free_page((unsigned long) page
);
740 #define mem_write NULL
743 /* This is a security hazard */
744 static ssize_t
mem_write(struct file
* file
, const char * buf
,
745 size_t count
, loff_t
*ppos
)
749 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
750 unsigned long dst
= *ppos
;
752 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
755 page
= (char *)__get_free_page(GFP_USER
);
760 int this_len
, retval
;
762 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
763 if (copy_from_user(page
, buf
, this_len
)) {
767 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
779 free_page((unsigned long) page
);
784 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
788 file
->f_pos
= offset
;
791 file
->f_pos
+= offset
;
796 force_successful_syscall_return();
800 static struct file_operations proc_mem_operations
= {
807 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
808 size_t count
, loff_t
*ppos
)
810 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
813 int oom_adjust
= task
->oomkilladj
;
814 loff_t __ppos
= *ppos
;
816 len
= sprintf(buffer
, "%i\n", oom_adjust
);
819 if (count
> len
-__ppos
)
821 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
823 *ppos
= __ppos
+ count
;
827 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
828 size_t count
, loff_t
*ppos
)
830 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
831 char buffer
[8], *end
;
834 if (!capable(CAP_SYS_RESOURCE
))
836 memset(buffer
, 0, 8);
839 if (copy_from_user(buffer
, buf
, count
))
841 oom_adjust
= simple_strtol(buffer
, &end
, 0);
842 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
846 task
->oomkilladj
= oom_adjust
;
847 if (end
- buffer
== 0)
852 static struct file_operations proc_oom_adjust_operations
= {
853 .read
= oom_adjust_read
,
854 .write
= oom_adjust_write
,
857 static struct inode_operations proc_mem_inode_operations
= {
858 .permission
= proc_permission
,
861 #ifdef CONFIG_AUDITSYSCALL
863 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
864 size_t count
, loff_t
*ppos
)
866 struct inode
* inode
= file
->f_dentry
->d_inode
;
867 struct task_struct
*task
= proc_task(inode
);
869 char tmpbuf
[TMPBUFLEN
];
871 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
872 audit_get_loginuid(task
->audit_context
));
873 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
876 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
877 size_t count
, loff_t
*ppos
)
879 struct inode
* inode
= file
->f_dentry
->d_inode
;
882 struct task_struct
*task
= proc_task(inode
);
885 if (!capable(CAP_AUDIT_CONTROL
))
891 if (count
> PAGE_SIZE
)
895 /* No partial writes. */
898 page
= (char*)__get_free_page(GFP_USER
);
902 if (copy_from_user(page
, buf
, count
))
905 loginuid
= simple_strtoul(page
, &tmp
, 10);
911 length
= audit_set_loginuid(task
, loginuid
);
912 if (likely(length
== 0))
916 free_page((unsigned long) page
);
920 static struct file_operations proc_loginuid_operations
= {
921 .read
= proc_loginuid_read
,
922 .write
= proc_loginuid_write
,
926 #ifdef CONFIG_SECCOMP
927 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
928 size_t count
, loff_t
*ppos
)
930 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
932 loff_t __ppos
= *ppos
;
935 /* no need to print the trailing zero, so use only len */
936 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
939 if (count
> len
- __ppos
)
940 count
= len
- __ppos
;
941 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
943 *ppos
= __ppos
+ count
;
947 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
948 size_t count
, loff_t
*ppos
)
950 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
951 char __buf
[20], *end
;
952 unsigned int seccomp_mode
;
954 /* can set it only once to be even more secure */
955 if (unlikely(tsk
->seccomp
.mode
))
958 memset(__buf
, 0, sizeof(__buf
));
959 count
= min(count
, sizeof(__buf
) - 1);
960 if (copy_from_user(__buf
, buf
, count
))
962 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
965 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
966 tsk
->seccomp
.mode
= seccomp_mode
;
967 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
970 if (unlikely(!(end
- __buf
)))
975 static struct file_operations proc_seccomp_operations
= {
976 .read
= seccomp_read
,
977 .write
= seccomp_write
,
979 #endif /* CONFIG_SECCOMP */
981 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
983 struct inode
*inode
= dentry
->d_inode
;
986 /* We don't need a base pointer in the /proc filesystem */
989 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
991 error
= proc_check_root(inode
);
995 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
996 nd
->last_type
= LAST_BIND
;
998 return ERR_PTR(error
);
1001 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1002 char __user
*buffer
, int buflen
)
1004 struct inode
* inode
;
1005 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1011 inode
= dentry
->d_inode
;
1012 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1013 len
= PTR_ERR(path
);
1016 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1020 if (copy_to_user(buffer
, path
, len
))
1023 free_page((unsigned long)tmp
);
1027 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1029 int error
= -EACCES
;
1030 struct inode
*inode
= dentry
->d_inode
;
1032 struct vfsmount
*mnt
= NULL
;
1036 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1038 error
= proc_check_root(inode
);
1042 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1046 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1054 static struct inode_operations proc_pid_link_inode_operations
= {
1055 .readlink
= proc_pid_readlink
,
1056 .follow_link
= proc_pid_follow_link
1061 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1063 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1064 struct task_struct
*p
= proc_task(inode
);
1065 unsigned int fd
, tid
, ino
;
1068 struct files_struct
* files
;
1079 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1083 ino
= fake_ino(tid
, PROC_TID_INO
);
1084 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1088 files
= get_files_struct(p
);
1091 spin_lock(&files
->file_lock
);
1092 for (fd
= filp
->f_pos
-2;
1093 fd
< files
->max_fds
;
1094 fd
++, filp
->f_pos
++) {
1097 if (!fcheck_files(files
, fd
))
1099 spin_unlock(&files
->file_lock
);
1105 buf
[j
] = '0' + (i
% 10);
1109 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1110 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1111 spin_lock(&files
->file_lock
);
1114 spin_lock(&files
->file_lock
);
1116 spin_unlock(&files
->file_lock
);
1117 put_files_struct(files
);
1123 static int proc_pident_readdir(struct file
*filp
,
1124 void *dirent
, filldir_t filldir
,
1125 struct pid_entry
*ents
, unsigned int nents
)
1129 struct dentry
*dentry
= filp
->f_dentry
;
1130 struct inode
*inode
= dentry
->d_inode
;
1131 struct pid_entry
*p
;
1136 if (!pid_alive(proc_task(inode
)))
1140 pid
= proc_task(inode
)->pid
;
1145 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1151 ino
= parent_ino(dentry
);
1152 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1165 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1166 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1178 static int proc_tgid_base_readdir(struct file
* filp
,
1179 void * dirent
, filldir_t filldir
)
1181 return proc_pident_readdir(filp
,dirent
,filldir
,
1182 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1185 static int proc_tid_base_readdir(struct file
* filp
,
1186 void * dirent
, filldir_t filldir
)
1188 return proc_pident_readdir(filp
,dirent
,filldir
,
1189 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1192 /* building an inode */
1194 static int task_dumpable(struct task_struct
*task
)
1197 struct mm_struct
*mm
;
1202 dumpable
= mm
->dumpable
;
1210 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1212 struct inode
* inode
;
1213 struct proc_inode
*ei
;
1215 /* We need a new inode */
1217 inode
= new_inode(sb
);
1224 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1225 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1227 if (!pid_alive(task
))
1231 * grab the reference to task.
1233 get_task_struct(task
);
1238 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1239 inode
->i_uid
= task
->euid
;
1240 inode
->i_gid
= task
->egid
;
1242 security_task_to_inode(task
, inode
);
1256 * Exceptional case: normally we are not allowed to unhash a busy
1257 * directory. In this case, however, we can do it - no aliasing problems
1258 * due to the way we treat inodes.
1260 * Rewrite the inode's ownerships here because the owning task may have
1261 * performed a setuid(), etc.
1263 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1265 struct inode
*inode
= dentry
->d_inode
;
1266 struct task_struct
*task
= proc_task(inode
);
1267 if (pid_alive(task
)) {
1268 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1269 inode
->i_uid
= task
->euid
;
1270 inode
->i_gid
= task
->egid
;
1275 security_task_to_inode(task
, inode
);
1282 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1284 struct inode
*inode
= dentry
->d_inode
;
1285 struct task_struct
*task
= proc_task(inode
);
1286 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1287 struct files_struct
*files
;
1289 files
= get_files_struct(task
);
1291 spin_lock(&files
->file_lock
);
1292 if (fcheck_files(files
, fd
)) {
1293 spin_unlock(&files
->file_lock
);
1294 put_files_struct(files
);
1295 if (task_dumpable(task
)) {
1296 inode
->i_uid
= task
->euid
;
1297 inode
->i_gid
= task
->egid
;
1302 security_task_to_inode(task
, inode
);
1305 spin_unlock(&files
->file_lock
);
1306 put_files_struct(files
);
1312 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1314 struct task_struct
*task
= proc_task(inode
);
1315 spin_lock(&task
->proc_lock
);
1316 if (task
->proc_dentry
== dentry
)
1317 task
->proc_dentry
= NULL
;
1318 spin_unlock(&task
->proc_lock
);
1322 static int pid_delete_dentry(struct dentry
* dentry
)
1324 /* Is the task we represent dead?
1325 * If so, then don't put the dentry on the lru list,
1326 * kill it immediately.
1328 return !pid_alive(proc_task(dentry
->d_inode
));
1331 static struct dentry_operations tid_fd_dentry_operations
=
1333 .d_revalidate
= tid_fd_revalidate
,
1334 .d_delete
= pid_delete_dentry
,
1337 static struct dentry_operations pid_dentry_operations
=
1339 .d_revalidate
= pid_revalidate
,
1340 .d_delete
= pid_delete_dentry
,
1343 static struct dentry_operations pid_base_dentry_operations
=
1345 .d_revalidate
= pid_revalidate
,
1346 .d_iput
= pid_base_iput
,
1347 .d_delete
= pid_delete_dentry
,
1352 static unsigned name_to_int(struct dentry
*dentry
)
1354 const char *name
= dentry
->d_name
.name
;
1355 int len
= dentry
->d_name
.len
;
1358 if (len
> 1 && *name
== '0')
1361 unsigned c
= *name
++ - '0';
1364 if (n
>= (~0U-9)/10)
1375 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1377 struct task_struct
*task
= proc_task(dir
);
1378 unsigned fd
= name_to_int(dentry
);
1380 struct files_struct
* files
;
1381 struct inode
*inode
;
1382 struct proc_inode
*ei
;
1386 if (!pid_alive(task
))
1389 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1393 files
= get_files_struct(task
);
1396 inode
->i_mode
= S_IFLNK
;
1397 spin_lock(&files
->file_lock
);
1398 file
= fcheck_files(files
, fd
);
1401 if (file
->f_mode
& 1)
1402 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1403 if (file
->f_mode
& 2)
1404 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1405 spin_unlock(&files
->file_lock
);
1406 put_files_struct(files
);
1407 inode
->i_op
= &proc_pid_link_inode_operations
;
1409 ei
->op
.proc_get_link
= proc_fd_link
;
1410 dentry
->d_op
= &tid_fd_dentry_operations
;
1411 d_add(dentry
, inode
);
1415 spin_unlock(&files
->file_lock
);
1416 put_files_struct(files
);
1420 return ERR_PTR(-ENOENT
);
1423 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1424 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1426 static struct file_operations proc_fd_operations
= {
1427 .read
= generic_read_dir
,
1428 .readdir
= proc_readfd
,
1431 static struct file_operations proc_task_operations
= {
1432 .read
= generic_read_dir
,
1433 .readdir
= proc_task_readdir
,
1437 * proc directories can do almost nothing..
1439 static struct inode_operations proc_fd_inode_operations
= {
1440 .lookup
= proc_lookupfd
,
1441 .permission
= proc_permission
,
1444 static struct inode_operations proc_task_inode_operations
= {
1445 .lookup
= proc_task_lookup
,
1446 .permission
= proc_permission
,
1449 #ifdef CONFIG_SECURITY
1450 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1451 size_t count
, loff_t
*ppos
)
1453 struct inode
* inode
= file
->f_dentry
->d_inode
;
1456 struct task_struct
*task
= proc_task(inode
);
1458 if (count
> PAGE_SIZE
)
1460 if (!(page
= __get_free_page(GFP_KERNEL
)))
1463 length
= security_getprocattr(task
,
1464 (char*)file
->f_dentry
->d_name
.name
,
1465 (void*)page
, count
);
1467 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1472 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1473 size_t count
, loff_t
*ppos
)
1475 struct inode
* inode
= file
->f_dentry
->d_inode
;
1478 struct task_struct
*task
= proc_task(inode
);
1480 if (count
> PAGE_SIZE
)
1483 /* No partial writes. */
1486 page
= (char*)__get_free_page(GFP_USER
);
1490 if (copy_from_user(page
, buf
, count
))
1493 length
= security_setprocattr(task
,
1494 (char*)file
->f_dentry
->d_name
.name
,
1495 (void*)page
, count
);
1497 free_page((unsigned long) page
);
1501 static struct file_operations proc_pid_attr_operations
= {
1502 .read
= proc_pid_attr_read
,
1503 .write
= proc_pid_attr_write
,
1506 static struct file_operations proc_tid_attr_operations
;
1507 static struct inode_operations proc_tid_attr_inode_operations
;
1508 static struct file_operations proc_tgid_attr_operations
;
1509 static struct inode_operations proc_tgid_attr_inode_operations
;
1512 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1515 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1516 struct dentry
*dentry
,
1517 struct pid_entry
*ents
)
1519 struct inode
*inode
;
1521 struct task_struct
*task
= proc_task(dir
);
1522 struct pid_entry
*p
;
1523 struct proc_inode
*ei
;
1528 if (!pid_alive(task
))
1531 for (p
= ents
; p
->name
; p
++) {
1532 if (p
->len
!= dentry
->d_name
.len
)
1534 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1541 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1546 inode
->i_mode
= p
->mode
;
1548 * Yes, it does not scale. And it should not. Don't add
1549 * new entries into /proc/<tgid>/ without very good reasons.
1552 case PROC_TGID_TASK
:
1553 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1554 inode
->i_op
= &proc_task_inode_operations
;
1555 inode
->i_fop
= &proc_task_operations
;
1560 inode
->i_op
= &proc_fd_inode_operations
;
1561 inode
->i_fop
= &proc_fd_operations
;
1565 inode
->i_op
= &proc_pid_link_inode_operations
;
1566 ei
->op
.proc_get_link
= proc_exe_link
;
1570 inode
->i_op
= &proc_pid_link_inode_operations
;
1571 ei
->op
.proc_get_link
= proc_cwd_link
;
1574 case PROC_TGID_ROOT
:
1575 inode
->i_op
= &proc_pid_link_inode_operations
;
1576 ei
->op
.proc_get_link
= proc_root_link
;
1578 case PROC_TID_ENVIRON
:
1579 case PROC_TGID_ENVIRON
:
1580 inode
->i_fop
= &proc_info_file_operations
;
1581 ei
->op
.proc_read
= proc_pid_environ
;
1584 case PROC_TGID_AUXV
:
1585 inode
->i_fop
= &proc_info_file_operations
;
1586 ei
->op
.proc_read
= proc_pid_auxv
;
1588 case PROC_TID_STATUS
:
1589 case PROC_TGID_STATUS
:
1590 inode
->i_fop
= &proc_info_file_operations
;
1591 ei
->op
.proc_read
= proc_pid_status
;
1594 inode
->i_fop
= &proc_info_file_operations
;
1595 ei
->op
.proc_read
= proc_tid_stat
;
1597 case PROC_TGID_STAT
:
1598 inode
->i_fop
= &proc_info_file_operations
;
1599 ei
->op
.proc_read
= proc_tgid_stat
;
1601 case PROC_TID_CMDLINE
:
1602 case PROC_TGID_CMDLINE
:
1603 inode
->i_fop
= &proc_info_file_operations
;
1604 ei
->op
.proc_read
= proc_pid_cmdline
;
1606 case PROC_TID_STATM
:
1607 case PROC_TGID_STATM
:
1608 inode
->i_fop
= &proc_info_file_operations
;
1609 ei
->op
.proc_read
= proc_pid_statm
;
1612 case PROC_TGID_MAPS
:
1613 inode
->i_fop
= &proc_maps_operations
;
1616 case PROC_TID_NUMA_MAPS
:
1617 case PROC_TGID_NUMA_MAPS
:
1618 inode
->i_fop
= &proc_numa_maps_operations
;
1623 inode
->i_op
= &proc_mem_inode_operations
;
1624 inode
->i_fop
= &proc_mem_operations
;
1626 #ifdef CONFIG_SECCOMP
1627 case PROC_TID_SECCOMP
:
1628 case PROC_TGID_SECCOMP
:
1629 inode
->i_fop
= &proc_seccomp_operations
;
1631 #endif /* CONFIG_SECCOMP */
1632 case PROC_TID_MOUNTS
:
1633 case PROC_TGID_MOUNTS
:
1634 inode
->i_fop
= &proc_mounts_operations
;
1636 case PROC_TID_SMAPS
:
1637 case PROC_TGID_SMAPS
:
1638 inode
->i_fop
= &proc_smaps_operations
;
1640 #ifdef CONFIG_SECURITY
1643 inode
->i_op
= &proc_tid_attr_inode_operations
;
1644 inode
->i_fop
= &proc_tid_attr_operations
;
1646 case PROC_TGID_ATTR
:
1648 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1649 inode
->i_fop
= &proc_tgid_attr_operations
;
1651 case PROC_TID_ATTR_CURRENT
:
1652 case PROC_TGID_ATTR_CURRENT
:
1653 case PROC_TID_ATTR_PREV
:
1654 case PROC_TGID_ATTR_PREV
:
1655 case PROC_TID_ATTR_EXEC
:
1656 case PROC_TGID_ATTR_EXEC
:
1657 case PROC_TID_ATTR_FSCREATE
:
1658 case PROC_TGID_ATTR_FSCREATE
:
1659 inode
->i_fop
= &proc_pid_attr_operations
;
1662 #ifdef CONFIG_KALLSYMS
1663 case PROC_TID_WCHAN
:
1664 case PROC_TGID_WCHAN
:
1665 inode
->i_fop
= &proc_info_file_operations
;
1666 ei
->op
.proc_read
= proc_pid_wchan
;
1669 #ifdef CONFIG_SCHEDSTATS
1670 case PROC_TID_SCHEDSTAT
:
1671 case PROC_TGID_SCHEDSTAT
:
1672 inode
->i_fop
= &proc_info_file_operations
;
1673 ei
->op
.proc_read
= proc_pid_schedstat
;
1676 #ifdef CONFIG_CPUSETS
1677 case PROC_TID_CPUSET
:
1678 case PROC_TGID_CPUSET
:
1679 inode
->i_fop
= &proc_cpuset_operations
;
1682 case PROC_TID_OOM_SCORE
:
1683 case PROC_TGID_OOM_SCORE
:
1684 inode
->i_fop
= &proc_info_file_operations
;
1685 ei
->op
.proc_read
= proc_oom_score
;
1687 case PROC_TID_OOM_ADJUST
:
1688 case PROC_TGID_OOM_ADJUST
:
1689 inode
->i_fop
= &proc_oom_adjust_operations
;
1691 #ifdef CONFIG_AUDITSYSCALL
1692 case PROC_TID_LOGINUID
:
1693 case PROC_TGID_LOGINUID
:
1694 inode
->i_fop
= &proc_loginuid_operations
;
1698 printk("procfs: impossible type (%d)",p
->type
);
1700 return ERR_PTR(-EINVAL
);
1702 dentry
->d_op
= &pid_dentry_operations
;
1703 d_add(dentry
, inode
);
1707 return ERR_PTR(error
);
1710 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1711 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1714 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1715 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1718 static struct file_operations proc_tgid_base_operations
= {
1719 .read
= generic_read_dir
,
1720 .readdir
= proc_tgid_base_readdir
,
1723 static struct file_operations proc_tid_base_operations
= {
1724 .read
= generic_read_dir
,
1725 .readdir
= proc_tid_base_readdir
,
1728 static struct inode_operations proc_tgid_base_inode_operations
= {
1729 .lookup
= proc_tgid_base_lookup
,
1732 static struct inode_operations proc_tid_base_inode_operations
= {
1733 .lookup
= proc_tid_base_lookup
,
1736 #ifdef CONFIG_SECURITY
1737 static int proc_tgid_attr_readdir(struct file
* filp
,
1738 void * dirent
, filldir_t filldir
)
1740 return proc_pident_readdir(filp
,dirent
,filldir
,
1741 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1744 static int proc_tid_attr_readdir(struct file
* filp
,
1745 void * dirent
, filldir_t filldir
)
1747 return proc_pident_readdir(filp
,dirent
,filldir
,
1748 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1751 static struct file_operations proc_tgid_attr_operations
= {
1752 .read
= generic_read_dir
,
1753 .readdir
= proc_tgid_attr_readdir
,
1756 static struct file_operations proc_tid_attr_operations
= {
1757 .read
= generic_read_dir
,
1758 .readdir
= proc_tid_attr_readdir
,
1761 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1762 struct dentry
*dentry
, struct nameidata
*nd
)
1764 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1767 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1768 struct dentry
*dentry
, struct nameidata
*nd
)
1770 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1773 static struct inode_operations proc_tgid_attr_inode_operations
= {
1774 .lookup
= proc_tgid_attr_lookup
,
1777 static struct inode_operations proc_tid_attr_inode_operations
= {
1778 .lookup
= proc_tid_attr_lookup
,
1785 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1789 sprintf(tmp
, "%d", current
->tgid
);
1790 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1793 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1796 sprintf(tmp
, "%d", current
->tgid
);
1797 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1800 static struct inode_operations proc_self_inode_operations
= {
1801 .readlink
= proc_self_readlink
,
1802 .follow_link
= proc_self_follow_link
,
1806 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1807 * @p: task that should be flushed.
1809 * Drops the /proc/@pid dcache entry from the hash chains.
1811 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1812 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1813 * if the pid value is immediately reused. This is enforced by
1814 * - caller must acquire spin_lock(p->proc_lock)
1815 * - must be called before detach_pid()
1816 * - proc_pid_lookup acquires proc_lock, and checks that
1817 * the target is not dead by looking at the attach count
1821 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1823 struct dentry
*proc_dentry
;
1825 proc_dentry
= p
->proc_dentry
;
1826 if (proc_dentry
!= NULL
) {
1828 spin_lock(&dcache_lock
);
1829 spin_lock(&proc_dentry
->d_lock
);
1830 if (!d_unhashed(proc_dentry
)) {
1831 dget_locked(proc_dentry
);
1832 __d_drop(proc_dentry
);
1833 spin_unlock(&proc_dentry
->d_lock
);
1835 spin_unlock(&proc_dentry
->d_lock
);
1838 spin_unlock(&dcache_lock
);
1844 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1845 * @proc_dentry: directoy to prune.
1847 * Shrink the /proc directory that was used by the just killed thread.
1850 void proc_pid_flush(struct dentry
*proc_dentry
)
1853 if(proc_dentry
!= NULL
) {
1854 shrink_dcache_parent(proc_dentry
);
1860 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1862 struct task_struct
*task
;
1863 struct inode
*inode
;
1864 struct proc_inode
*ei
;
1868 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1869 inode
= new_inode(dir
->i_sb
);
1871 return ERR_PTR(-ENOMEM
);
1873 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1874 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1876 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1877 inode
->i_uid
= inode
->i_gid
= 0;
1879 inode
->i_op
= &proc_self_inode_operations
;
1880 d_add(dentry
, inode
);
1883 tgid
= name_to_int(dentry
);
1887 read_lock(&tasklist_lock
);
1888 task
= find_task_by_pid(tgid
);
1890 get_task_struct(task
);
1891 read_unlock(&tasklist_lock
);
1895 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1899 put_task_struct(task
);
1902 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1903 inode
->i_op
= &proc_tgid_base_inode_operations
;
1904 inode
->i_fop
= &proc_tgid_base_operations
;
1905 inode
->i_flags
|=S_IMMUTABLE
;
1906 #ifdef CONFIG_SECURITY
1912 dentry
->d_op
= &pid_base_dentry_operations
;
1915 d_add(dentry
, inode
);
1916 spin_lock(&task
->proc_lock
);
1917 task
->proc_dentry
= dentry
;
1918 if (!pid_alive(task
)) {
1919 dentry
= proc_pid_unhash(task
);
1922 spin_unlock(&task
->proc_lock
);
1924 put_task_struct(task
);
1926 proc_pid_flush(dentry
);
1931 return ERR_PTR(-ENOENT
);
1935 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1937 struct task_struct
*task
;
1938 struct task_struct
*leader
= proc_task(dir
);
1939 struct inode
*inode
;
1942 tid
= name_to_int(dentry
);
1946 read_lock(&tasklist_lock
);
1947 task
= find_task_by_pid(tid
);
1949 get_task_struct(task
);
1950 read_unlock(&tasklist_lock
);
1953 if (leader
->tgid
!= task
->tgid
)
1956 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
1961 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1962 inode
->i_op
= &proc_tid_base_inode_operations
;
1963 inode
->i_fop
= &proc_tid_base_operations
;
1964 inode
->i_flags
|=S_IMMUTABLE
;
1965 #ifdef CONFIG_SECURITY
1971 dentry
->d_op
= &pid_base_dentry_operations
;
1973 d_add(dentry
, inode
);
1975 put_task_struct(task
);
1978 put_task_struct(task
);
1980 return ERR_PTR(-ENOENT
);
1983 #define PROC_NUMBUF 10
1984 #define PROC_MAXPIDS 20
1987 * Get a few tgid's to return for filldir - we need to hold the
1988 * tasklist lock while doing this, and we must release it before
1989 * we actually do the filldir itself, so we use a temp buffer..
1991 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
1993 struct task_struct
*p
;
1997 read_lock(&tasklist_lock
);
2000 p
= find_task_by_pid(version
);
2001 if (p
&& !thread_group_leader(p
))
2008 p
= next_task(&init_task
);
2010 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2016 tgids
[nr_tgids
] = tgid
;
2018 if (nr_tgids
>= PROC_MAXPIDS
)
2021 read_unlock(&tasklist_lock
);
2026 * Get a few tid's to return for filldir - we need to hold the
2027 * tasklist lock while doing this, and we must release it before
2028 * we actually do the filldir itself, so we use a temp buffer..
2030 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2032 struct task_struct
*leader_task
= proc_task(dir
);
2033 struct task_struct
*task
= leader_task
;
2037 read_lock(&tasklist_lock
);
2039 * The starting point task (leader_task) might be an already
2040 * unlinked task, which cannot be used to access the task-list
2041 * via next_thread().
2043 if (pid_alive(task
)) do {
2044 int tid
= task
->pid
;
2049 tids
[nr_tids
] = tid
;
2051 if (nr_tids
>= PROC_MAXPIDS
)
2053 } while ((task
= next_thread(task
)) != leader_task
);
2054 read_unlock(&tasklist_lock
);
2058 /* for the /proc/ directory itself, after non-process stuff has been done */
2059 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2061 unsigned int tgid_array
[PROC_MAXPIDS
];
2062 char buf
[PROC_NUMBUF
];
2063 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2064 unsigned int nr_tgids
, i
;
2068 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2069 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2075 /* f_version caches the tgid value that the last readdir call couldn't
2076 * return. lseek aka telldir automagically resets f_version to 0.
2078 next_tgid
= filp
->f_version
;
2079 filp
->f_version
= 0;
2081 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2083 /* no more entries ! */
2088 /* do not use the last found pid, reserve it for next_tgid */
2089 if (nr_tgids
== PROC_MAXPIDS
) {
2091 next_tgid
= tgid_array
[nr_tgids
];
2094 for (i
=0;i
<nr_tgids
;i
++) {
2095 int tgid
= tgid_array
[i
];
2096 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2097 unsigned long j
= PROC_NUMBUF
;
2100 buf
[--j
] = '0' + (tgid
% 10);
2101 while ((tgid
/= 10) != 0);
2103 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2104 /* returning this tgid failed, save it as the first
2105 * pid for the next readir call */
2106 filp
->f_version
= tgid_array
[i
];
2117 /* for the /proc/TGID/task/ directories */
2118 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2120 unsigned int tid_array
[PROC_MAXPIDS
];
2121 char buf
[PROC_NUMBUF
];
2122 unsigned int nr_tids
, i
;
2123 struct dentry
*dentry
= filp
->f_dentry
;
2124 struct inode
*inode
= dentry
->d_inode
;
2125 int retval
= -ENOENT
;
2127 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2129 if (!pid_alive(proc_task(inode
)))
2136 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2141 ino
= parent_ino(dentry
);
2142 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2148 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2149 inode
->i_nlink
= pos
+ nr_tids
;
2151 for (i
= 0; i
< nr_tids
; i
++) {
2152 unsigned long j
= PROC_NUMBUF
;
2153 int tid
= tid_array
[i
];
2155 ino
= fake_ino(tid
,PROC_TID_INO
);
2158 buf
[--j
] = '0' + (tid
% 10);
2159 while ((tid
/= 10) != 0);
2161 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)