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
124 PROC_TGID_OOM_ADJUST
,
128 #ifdef CONFIG_SECCOMP
145 #ifdef CONFIG_SCHEDSTATS
148 #ifdef CONFIG_CPUSETS
151 #ifdef CONFIG_SECURITY
153 PROC_TID_ATTR_CURRENT
,
156 PROC_TID_ATTR_FSCREATE
,
158 #ifdef CONFIG_AUDITSYSCALL
161 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
173 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
175 static struct pid_entry tgid_base_stuff
[] = {
176 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
177 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
178 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
179 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
180 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
181 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
182 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
183 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
184 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
186 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
188 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
189 #ifdef CONFIG_SECCOMP
190 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
192 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
193 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
194 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
195 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
197 #ifdef CONFIG_SECURITY
198 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
200 #ifdef CONFIG_KALLSYMS
201 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
203 #ifdef CONFIG_SCHEDSTATS
204 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
206 #ifdef CONFIG_CPUSETS
207 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
209 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
210 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
211 #ifdef CONFIG_AUDITSYSCALL
212 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
216 static struct pid_entry tid_base_stuff
[] = {
217 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
218 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
219 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
220 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
221 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
222 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
223 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
224 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
226 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
228 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
229 #ifdef CONFIG_SECCOMP
230 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
232 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
233 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
234 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
235 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
237 #ifdef CONFIG_SECURITY
238 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
240 #ifdef CONFIG_KALLSYMS
241 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
243 #ifdef CONFIG_SCHEDSTATS
244 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
246 #ifdef CONFIG_CPUSETS
247 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
249 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
250 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
251 #ifdef CONFIG_AUDITSYSCALL
252 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
257 #ifdef CONFIG_SECURITY
258 static struct pid_entry tgid_attr_stuff
[] = {
259 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
260 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
261 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
262 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
265 static struct pid_entry tid_attr_stuff
[] = {
266 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
267 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
268 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
269 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
276 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
278 struct task_struct
*task
= proc_task(inode
);
279 struct files_struct
*files
;
281 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
283 files
= get_files_struct(task
);
285 spin_lock(&files
->file_lock
);
286 file
= fcheck_files(files
, fd
);
288 *mnt
= mntget(file
->f_vfsmnt
);
289 *dentry
= dget(file
->f_dentry
);
290 spin_unlock(&files
->file_lock
);
291 put_files_struct(files
);
294 spin_unlock(&files
->file_lock
);
295 put_files_struct(files
);
300 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
302 struct fs_struct
*fs
;
306 atomic_inc(&fs
->count
);
311 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
313 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
314 int result
= -ENOENT
;
316 read_lock(&fs
->lock
);
317 *mnt
= mntget(fs
->pwdmnt
);
318 *dentry
= dget(fs
->pwd
);
319 read_unlock(&fs
->lock
);
326 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
328 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
329 int result
= -ENOENT
;
331 read_lock(&fs
->lock
);
332 *mnt
= mntget(fs
->rootmnt
);
333 *dentry
= dget(fs
->root
);
334 read_unlock(&fs
->lock
);
341 #define MAY_PTRACE(task) \
342 (task == current || \
343 (task->parent == current && \
344 (task->ptrace & PT_PTRACED) && \
345 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
346 security_ptrace(current,task) == 0))
348 static int may_ptrace_attach(struct task_struct
*task
)
356 if (((current
->uid
!= task
->euid
) ||
357 (current
->uid
!= task
->suid
) ||
358 (current
->uid
!= task
->uid
) ||
359 (current
->gid
!= task
->egid
) ||
360 (current
->gid
!= task
->sgid
) ||
361 (current
->gid
!= task
->gid
)) && !capable(CAP_SYS_PTRACE
))
364 if (task
->mm
->dumpable
!= 1 && !capable(CAP_SYS_PTRACE
))
366 if (security_ptrace(current
, task
))
375 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
378 struct mm_struct
*mm
= get_task_mm(task
);
380 unsigned int len
= mm
->env_end
- mm
->env_start
;
383 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
384 if (!may_ptrace_attach(task
))
391 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
395 struct mm_struct
*mm
= get_task_mm(task
);
399 goto out_mm
; /* Shh! No looking before we're done */
401 len
= mm
->arg_end
- mm
->arg_start
;
406 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
408 // If the nul at the end of args has been overwritten, then
409 // assume application is using setproctitle(3).
410 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
411 len
= strnlen(buffer
, res
);
415 len
= mm
->env_end
- mm
->env_start
;
416 if (len
> PAGE_SIZE
- res
)
417 len
= PAGE_SIZE
- res
;
418 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
419 res
= strnlen(buffer
, res
);
428 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
431 struct mm_struct
*mm
= get_task_mm(task
);
433 unsigned int nwords
= 0;
436 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
437 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
440 memcpy(buffer
, mm
->saved_auxv
, res
);
447 #ifdef CONFIG_KALLSYMS
449 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
450 * Returns the resolved symbol. If that fails, simply return the address.
452 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
455 const char *sym_name
;
456 unsigned long wchan
, size
, offset
;
457 char namebuf
[KSYM_NAME_LEN
+1];
459 wchan
= get_wchan(task
);
461 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
463 return sprintf(buffer
, "%s", sym_name
);
464 return sprintf(buffer
, "%lu", wchan
);
466 #endif /* CONFIG_KALLSYMS */
468 #ifdef CONFIG_SCHEDSTATS
470 * Provides /proc/PID/schedstat
472 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
474 return sprintf(buffer
, "%lu %lu %lu\n",
475 task
->sched_info
.cpu_time
,
476 task
->sched_info
.run_delay
,
477 task
->sched_info
.pcnt
);
481 /* The badness from the OOM killer */
482 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
483 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
485 unsigned long points
;
486 struct timespec uptime
;
488 do_posix_clock_monotonic_gettime(&uptime
);
489 points
= badness(task
, uptime
.tv_sec
);
490 return sprintf(buffer
, "%lu\n", points
);
493 /************************************************************************/
494 /* Here the fs part begins */
495 /************************************************************************/
497 /* permission checks */
499 static int proc_check_root(struct inode
*inode
)
501 struct dentry
*de
, *base
, *root
;
502 struct vfsmount
*our_vfsmnt
, *vfsmnt
, *mnt
;
505 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
507 read_lock(¤t
->fs
->lock
);
508 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
509 base
= dget(current
->fs
->root
);
510 read_unlock(¤t
->fs
->lock
);
512 spin_lock(&vfsmount_lock
);
516 while (vfsmnt
!= our_vfsmnt
) {
517 if (vfsmnt
== vfsmnt
->mnt_parent
)
519 de
= vfsmnt
->mnt_mountpoint
;
520 vfsmnt
= vfsmnt
->mnt_parent
;
523 if (!is_subdir(de
, base
))
525 spin_unlock(&vfsmount_lock
);
534 spin_unlock(&vfsmount_lock
);
539 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
541 if (generic_permission(inode
, mask
, NULL
) != 0)
543 return proc_check_root(inode
);
546 extern struct seq_operations proc_pid_maps_op
;
547 static int maps_open(struct inode
*inode
, struct file
*file
)
549 struct task_struct
*task
= proc_task(inode
);
550 int ret
= seq_open(file
, &proc_pid_maps_op
);
552 struct seq_file
*m
= file
->private_data
;
558 static struct file_operations proc_maps_operations
= {
562 .release
= seq_release
,
566 extern struct seq_operations proc_pid_numa_maps_op
;
567 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
569 struct task_struct
*task
= proc_task(inode
);
570 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
572 struct seq_file
*m
= file
->private_data
;
578 static struct file_operations proc_numa_maps_operations
= {
579 .open
= numa_maps_open
,
582 .release
= seq_release
,
586 extern struct seq_operations proc_pid_smaps_op
;
587 static int smaps_open(struct inode
*inode
, struct file
*file
)
589 struct task_struct
*task
= proc_task(inode
);
590 int ret
= seq_open(file
, &proc_pid_smaps_op
);
592 struct seq_file
*m
= file
->private_data
;
598 static struct file_operations proc_smaps_operations
= {
602 .release
= seq_release
,
605 extern struct seq_operations mounts_op
;
606 static int mounts_open(struct inode
*inode
, struct file
*file
)
608 struct task_struct
*task
= proc_task(inode
);
609 int ret
= seq_open(file
, &mounts_op
);
612 struct seq_file
*m
= file
->private_data
;
613 struct namespace *namespace;
615 namespace = task
->namespace;
617 get_namespace(namespace);
621 m
->private = namespace;
623 seq_release(inode
, file
);
630 static int mounts_release(struct inode
*inode
, struct file
*file
)
632 struct seq_file
*m
= file
->private_data
;
633 struct namespace *namespace = m
->private;
634 put_namespace(namespace);
635 return seq_release(inode
, file
);
638 static struct file_operations proc_mounts_operations
= {
642 .release
= mounts_release
,
645 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
647 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
648 size_t count
, loff_t
*ppos
)
650 struct inode
* inode
= file
->f_dentry
->d_inode
;
653 struct task_struct
*task
= proc_task(inode
);
655 if (count
> PROC_BLOCK_SIZE
)
656 count
= PROC_BLOCK_SIZE
;
657 if (!(page
= __get_free_page(GFP_KERNEL
)))
660 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
663 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
668 static struct file_operations proc_info_file_operations
= {
669 .read
= proc_info_read
,
672 static int mem_open(struct inode
* inode
, struct file
* file
)
674 file
->private_data
= (void*)((long)current
->self_exec_id
);
678 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
679 size_t count
, loff_t
*ppos
)
681 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
683 unsigned long src
= *ppos
;
685 struct mm_struct
*mm
;
687 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
691 page
= (char *)__get_free_page(GFP_USER
);
697 mm
= get_task_mm(task
);
703 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
709 int this_len
, retval
;
711 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
712 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
713 if (!retval
|| !MAY_PTRACE(task
) || !may_ptrace_attach(task
)) {
719 if (copy_to_user(buf
, page
, retval
)) {
734 free_page((unsigned long) page
);
739 #define mem_write NULL
742 /* This is a security hazard */
743 static ssize_t
mem_write(struct file
* file
, const char * buf
,
744 size_t count
, loff_t
*ppos
)
748 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
749 unsigned long dst
= *ppos
;
751 if (!MAY_PTRACE(task
) || !may_ptrace_attach(task
))
754 page
= (char *)__get_free_page(GFP_USER
);
759 int this_len
, retval
;
761 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
762 if (copy_from_user(page
, buf
, this_len
)) {
766 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
778 free_page((unsigned long) page
);
783 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
787 file
->f_pos
= offset
;
790 file
->f_pos
+= offset
;
795 force_successful_syscall_return();
799 static struct file_operations proc_mem_operations
= {
806 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
807 size_t count
, loff_t
*ppos
)
809 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
812 int oom_adjust
= task
->oomkilladj
;
813 loff_t __ppos
= *ppos
;
815 len
= sprintf(buffer
, "%i\n", oom_adjust
);
818 if (count
> len
-__ppos
)
820 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
822 *ppos
= __ppos
+ count
;
826 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
827 size_t count
, loff_t
*ppos
)
829 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
830 char buffer
[8], *end
;
833 if (!capable(CAP_SYS_RESOURCE
))
835 memset(buffer
, 0, 8);
838 if (copy_from_user(buffer
, buf
, count
))
840 oom_adjust
= simple_strtol(buffer
, &end
, 0);
841 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
845 task
->oomkilladj
= oom_adjust
;
846 if (end
- buffer
== 0)
851 static struct file_operations proc_oom_adjust_operations
= {
852 .read
= oom_adjust_read
,
853 .write
= oom_adjust_write
,
856 static struct inode_operations proc_mem_inode_operations
= {
857 .permission
= proc_permission
,
860 #ifdef CONFIG_AUDITSYSCALL
862 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
863 size_t count
, loff_t
*ppos
)
865 struct inode
* inode
= file
->f_dentry
->d_inode
;
866 struct task_struct
*task
= proc_task(inode
);
868 char tmpbuf
[TMPBUFLEN
];
870 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
871 audit_get_loginuid(task
->audit_context
));
872 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
875 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
876 size_t count
, loff_t
*ppos
)
878 struct inode
* inode
= file
->f_dentry
->d_inode
;
881 struct task_struct
*task
= proc_task(inode
);
884 if (!capable(CAP_AUDIT_CONTROL
))
890 if (count
> PAGE_SIZE
)
894 /* No partial writes. */
897 page
= (char*)__get_free_page(GFP_USER
);
901 if (copy_from_user(page
, buf
, count
))
904 loginuid
= simple_strtoul(page
, &tmp
, 10);
910 length
= audit_set_loginuid(task
, loginuid
);
911 if (likely(length
== 0))
915 free_page((unsigned long) page
);
919 static struct file_operations proc_loginuid_operations
= {
920 .read
= proc_loginuid_read
,
921 .write
= proc_loginuid_write
,
925 #ifdef CONFIG_SECCOMP
926 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
927 size_t count
, loff_t
*ppos
)
929 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
931 loff_t __ppos
= *ppos
;
934 /* no need to print the trailing zero, so use only len */
935 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
938 if (count
> len
- __ppos
)
939 count
= len
- __ppos
;
940 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
942 *ppos
= __ppos
+ count
;
946 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
947 size_t count
, loff_t
*ppos
)
949 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
950 char __buf
[20], *end
;
951 unsigned int seccomp_mode
;
953 /* can set it only once to be even more secure */
954 if (unlikely(tsk
->seccomp
.mode
))
957 memset(__buf
, 0, sizeof(__buf
));
958 count
= min(count
, sizeof(__buf
) - 1);
959 if (copy_from_user(__buf
, buf
, count
))
961 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
964 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
965 tsk
->seccomp
.mode
= seccomp_mode
;
966 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
969 if (unlikely(!(end
- __buf
)))
974 static struct file_operations proc_seccomp_operations
= {
975 .read
= seccomp_read
,
976 .write
= seccomp_write
,
978 #endif /* CONFIG_SECCOMP */
980 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
982 struct inode
*inode
= dentry
->d_inode
;
985 /* We don't need a base pointer in the /proc filesystem */
988 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
990 error
= proc_check_root(inode
);
994 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
995 nd
->last_type
= LAST_BIND
;
997 return ERR_PTR(error
);
1000 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1001 char __user
*buffer
, int buflen
)
1003 struct inode
* inode
;
1004 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1010 inode
= dentry
->d_inode
;
1011 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1012 len
= PTR_ERR(path
);
1015 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1019 if (copy_to_user(buffer
, path
, len
))
1022 free_page((unsigned long)tmp
);
1026 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1028 int error
= -EACCES
;
1029 struct inode
*inode
= dentry
->d_inode
;
1031 struct vfsmount
*mnt
= NULL
;
1035 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1037 error
= proc_check_root(inode
);
1041 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1045 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1053 static struct inode_operations proc_pid_link_inode_operations
= {
1054 .readlink
= proc_pid_readlink
,
1055 .follow_link
= proc_pid_follow_link
1060 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1062 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1063 struct task_struct
*p
= proc_task(inode
);
1064 unsigned int fd
, tid
, ino
;
1067 struct files_struct
* files
;
1078 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1082 ino
= fake_ino(tid
, PROC_TID_INO
);
1083 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1087 files
= get_files_struct(p
);
1090 spin_lock(&files
->file_lock
);
1091 for (fd
= filp
->f_pos
-2;
1092 fd
< files
->max_fds
;
1093 fd
++, filp
->f_pos
++) {
1096 if (!fcheck_files(files
, fd
))
1098 spin_unlock(&files
->file_lock
);
1104 buf
[j
] = '0' + (i
% 10);
1108 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1109 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1110 spin_lock(&files
->file_lock
);
1113 spin_lock(&files
->file_lock
);
1115 spin_unlock(&files
->file_lock
);
1116 put_files_struct(files
);
1122 static int proc_pident_readdir(struct file
*filp
,
1123 void *dirent
, filldir_t filldir
,
1124 struct pid_entry
*ents
, unsigned int nents
)
1128 struct dentry
*dentry
= filp
->f_dentry
;
1129 struct inode
*inode
= dentry
->d_inode
;
1130 struct pid_entry
*p
;
1135 if (!pid_alive(proc_task(inode
)))
1139 pid
= proc_task(inode
)->pid
;
1144 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1150 ino
= parent_ino(dentry
);
1151 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1164 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1165 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1177 static int proc_tgid_base_readdir(struct file
* filp
,
1178 void * dirent
, filldir_t filldir
)
1180 return proc_pident_readdir(filp
,dirent
,filldir
,
1181 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1184 static int proc_tid_base_readdir(struct file
* filp
,
1185 void * dirent
, filldir_t filldir
)
1187 return proc_pident_readdir(filp
,dirent
,filldir
,
1188 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1191 /* building an inode */
1193 static int task_dumpable(struct task_struct
*task
)
1196 struct mm_struct
*mm
;
1201 dumpable
= mm
->dumpable
;
1209 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1211 struct inode
* inode
;
1212 struct proc_inode
*ei
;
1214 /* We need a new inode */
1216 inode
= new_inode(sb
);
1223 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1224 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1226 if (!pid_alive(task
))
1230 * grab the reference to task.
1232 get_task_struct(task
);
1237 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1238 inode
->i_uid
= task
->euid
;
1239 inode
->i_gid
= task
->egid
;
1241 security_task_to_inode(task
, inode
);
1255 * Exceptional case: normally we are not allowed to unhash a busy
1256 * directory. In this case, however, we can do it - no aliasing problems
1257 * due to the way we treat inodes.
1259 * Rewrite the inode's ownerships here because the owning task may have
1260 * performed a setuid(), etc.
1262 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1264 struct inode
*inode
= dentry
->d_inode
;
1265 struct task_struct
*task
= proc_task(inode
);
1266 if (pid_alive(task
)) {
1267 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1268 inode
->i_uid
= task
->euid
;
1269 inode
->i_gid
= task
->egid
;
1274 security_task_to_inode(task
, inode
);
1281 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1283 struct inode
*inode
= dentry
->d_inode
;
1284 struct task_struct
*task
= proc_task(inode
);
1285 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1286 struct files_struct
*files
;
1288 files
= get_files_struct(task
);
1290 spin_lock(&files
->file_lock
);
1291 if (fcheck_files(files
, fd
)) {
1292 spin_unlock(&files
->file_lock
);
1293 put_files_struct(files
);
1294 if (task_dumpable(task
)) {
1295 inode
->i_uid
= task
->euid
;
1296 inode
->i_gid
= task
->egid
;
1301 security_task_to_inode(task
, inode
);
1304 spin_unlock(&files
->file_lock
);
1305 put_files_struct(files
);
1311 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1313 struct task_struct
*task
= proc_task(inode
);
1314 spin_lock(&task
->proc_lock
);
1315 if (task
->proc_dentry
== dentry
)
1316 task
->proc_dentry
= NULL
;
1317 spin_unlock(&task
->proc_lock
);
1321 static int pid_delete_dentry(struct dentry
* dentry
)
1323 /* Is the task we represent dead?
1324 * If so, then don't put the dentry on the lru list,
1325 * kill it immediately.
1327 return !pid_alive(proc_task(dentry
->d_inode
));
1330 static struct dentry_operations tid_fd_dentry_operations
=
1332 .d_revalidate
= tid_fd_revalidate
,
1333 .d_delete
= pid_delete_dentry
,
1336 static struct dentry_operations pid_dentry_operations
=
1338 .d_revalidate
= pid_revalidate
,
1339 .d_delete
= pid_delete_dentry
,
1342 static struct dentry_operations pid_base_dentry_operations
=
1344 .d_revalidate
= pid_revalidate
,
1345 .d_iput
= pid_base_iput
,
1346 .d_delete
= pid_delete_dentry
,
1351 static unsigned name_to_int(struct dentry
*dentry
)
1353 const char *name
= dentry
->d_name
.name
;
1354 int len
= dentry
->d_name
.len
;
1357 if (len
> 1 && *name
== '0')
1360 unsigned c
= *name
++ - '0';
1363 if (n
>= (~0U-9)/10)
1374 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1376 struct task_struct
*task
= proc_task(dir
);
1377 unsigned fd
= name_to_int(dentry
);
1379 struct files_struct
* files
;
1380 struct inode
*inode
;
1381 struct proc_inode
*ei
;
1385 if (!pid_alive(task
))
1388 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1392 files
= get_files_struct(task
);
1395 inode
->i_mode
= S_IFLNK
;
1396 spin_lock(&files
->file_lock
);
1397 file
= fcheck_files(files
, fd
);
1400 if (file
->f_mode
& 1)
1401 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1402 if (file
->f_mode
& 2)
1403 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1404 spin_unlock(&files
->file_lock
);
1405 put_files_struct(files
);
1406 inode
->i_op
= &proc_pid_link_inode_operations
;
1408 ei
->op
.proc_get_link
= proc_fd_link
;
1409 dentry
->d_op
= &tid_fd_dentry_operations
;
1410 d_add(dentry
, inode
);
1414 spin_unlock(&files
->file_lock
);
1415 put_files_struct(files
);
1419 return ERR_PTR(-ENOENT
);
1422 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1423 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1425 static struct file_operations proc_fd_operations
= {
1426 .read
= generic_read_dir
,
1427 .readdir
= proc_readfd
,
1430 static struct file_operations proc_task_operations
= {
1431 .read
= generic_read_dir
,
1432 .readdir
= proc_task_readdir
,
1436 * proc directories can do almost nothing..
1438 static struct inode_operations proc_fd_inode_operations
= {
1439 .lookup
= proc_lookupfd
,
1440 .permission
= proc_permission
,
1443 static struct inode_operations proc_task_inode_operations
= {
1444 .lookup
= proc_task_lookup
,
1445 .permission
= proc_permission
,
1448 #ifdef CONFIG_SECURITY
1449 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1450 size_t count
, loff_t
*ppos
)
1452 struct inode
* inode
= file
->f_dentry
->d_inode
;
1455 struct task_struct
*task
= proc_task(inode
);
1457 if (count
> PAGE_SIZE
)
1459 if (!(page
= __get_free_page(GFP_KERNEL
)))
1462 length
= security_getprocattr(task
,
1463 (char*)file
->f_dentry
->d_name
.name
,
1464 (void*)page
, count
);
1466 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1471 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1472 size_t count
, loff_t
*ppos
)
1474 struct inode
* inode
= file
->f_dentry
->d_inode
;
1477 struct task_struct
*task
= proc_task(inode
);
1479 if (count
> PAGE_SIZE
)
1482 /* No partial writes. */
1485 page
= (char*)__get_free_page(GFP_USER
);
1489 if (copy_from_user(page
, buf
, count
))
1492 length
= security_setprocattr(task
,
1493 (char*)file
->f_dentry
->d_name
.name
,
1494 (void*)page
, count
);
1496 free_page((unsigned long) page
);
1500 static struct file_operations proc_pid_attr_operations
= {
1501 .read
= proc_pid_attr_read
,
1502 .write
= proc_pid_attr_write
,
1505 static struct file_operations proc_tid_attr_operations
;
1506 static struct inode_operations proc_tid_attr_inode_operations
;
1507 static struct file_operations proc_tgid_attr_operations
;
1508 static struct inode_operations proc_tgid_attr_inode_operations
;
1511 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1514 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1515 struct dentry
*dentry
,
1516 struct pid_entry
*ents
)
1518 struct inode
*inode
;
1520 struct task_struct
*task
= proc_task(dir
);
1521 struct pid_entry
*p
;
1522 struct proc_inode
*ei
;
1527 if (!pid_alive(task
))
1530 for (p
= ents
; p
->name
; p
++) {
1531 if (p
->len
!= dentry
->d_name
.len
)
1533 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1540 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1545 inode
->i_mode
= p
->mode
;
1547 * Yes, it does not scale. And it should not. Don't add
1548 * new entries into /proc/<tgid>/ without very good reasons.
1551 case PROC_TGID_TASK
:
1552 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1553 inode
->i_op
= &proc_task_inode_operations
;
1554 inode
->i_fop
= &proc_task_operations
;
1559 inode
->i_op
= &proc_fd_inode_operations
;
1560 inode
->i_fop
= &proc_fd_operations
;
1564 inode
->i_op
= &proc_pid_link_inode_operations
;
1565 ei
->op
.proc_get_link
= proc_exe_link
;
1569 inode
->i_op
= &proc_pid_link_inode_operations
;
1570 ei
->op
.proc_get_link
= proc_cwd_link
;
1573 case PROC_TGID_ROOT
:
1574 inode
->i_op
= &proc_pid_link_inode_operations
;
1575 ei
->op
.proc_get_link
= proc_root_link
;
1577 case PROC_TID_ENVIRON
:
1578 case PROC_TGID_ENVIRON
:
1579 inode
->i_fop
= &proc_info_file_operations
;
1580 ei
->op
.proc_read
= proc_pid_environ
;
1583 case PROC_TGID_AUXV
:
1584 inode
->i_fop
= &proc_info_file_operations
;
1585 ei
->op
.proc_read
= proc_pid_auxv
;
1587 case PROC_TID_STATUS
:
1588 case PROC_TGID_STATUS
:
1589 inode
->i_fop
= &proc_info_file_operations
;
1590 ei
->op
.proc_read
= proc_pid_status
;
1593 inode
->i_fop
= &proc_info_file_operations
;
1594 ei
->op
.proc_read
= proc_tid_stat
;
1596 case PROC_TGID_STAT
:
1597 inode
->i_fop
= &proc_info_file_operations
;
1598 ei
->op
.proc_read
= proc_tgid_stat
;
1600 case PROC_TID_CMDLINE
:
1601 case PROC_TGID_CMDLINE
:
1602 inode
->i_fop
= &proc_info_file_operations
;
1603 ei
->op
.proc_read
= proc_pid_cmdline
;
1605 case PROC_TID_STATM
:
1606 case PROC_TGID_STATM
:
1607 inode
->i_fop
= &proc_info_file_operations
;
1608 ei
->op
.proc_read
= proc_pid_statm
;
1611 case PROC_TGID_MAPS
:
1612 inode
->i_fop
= &proc_maps_operations
;
1615 case PROC_TID_NUMA_MAPS
:
1616 case PROC_TGID_NUMA_MAPS
:
1617 inode
->i_fop
= &proc_numa_maps_operations
;
1622 inode
->i_op
= &proc_mem_inode_operations
;
1623 inode
->i_fop
= &proc_mem_operations
;
1625 #ifdef CONFIG_SECCOMP
1626 case PROC_TID_SECCOMP
:
1627 case PROC_TGID_SECCOMP
:
1628 inode
->i_fop
= &proc_seccomp_operations
;
1630 #endif /* CONFIG_SECCOMP */
1631 case PROC_TID_MOUNTS
:
1632 case PROC_TGID_MOUNTS
:
1633 inode
->i_fop
= &proc_mounts_operations
;
1635 case PROC_TID_SMAPS
:
1636 case PROC_TGID_SMAPS
:
1637 inode
->i_fop
= &proc_smaps_operations
;
1639 #ifdef CONFIG_SECURITY
1642 inode
->i_op
= &proc_tid_attr_inode_operations
;
1643 inode
->i_fop
= &proc_tid_attr_operations
;
1645 case PROC_TGID_ATTR
:
1647 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1648 inode
->i_fop
= &proc_tgid_attr_operations
;
1650 case PROC_TID_ATTR_CURRENT
:
1651 case PROC_TGID_ATTR_CURRENT
:
1652 case PROC_TID_ATTR_PREV
:
1653 case PROC_TGID_ATTR_PREV
:
1654 case PROC_TID_ATTR_EXEC
:
1655 case PROC_TGID_ATTR_EXEC
:
1656 case PROC_TID_ATTR_FSCREATE
:
1657 case PROC_TGID_ATTR_FSCREATE
:
1658 inode
->i_fop
= &proc_pid_attr_operations
;
1661 #ifdef CONFIG_KALLSYMS
1662 case PROC_TID_WCHAN
:
1663 case PROC_TGID_WCHAN
:
1664 inode
->i_fop
= &proc_info_file_operations
;
1665 ei
->op
.proc_read
= proc_pid_wchan
;
1668 #ifdef CONFIG_SCHEDSTATS
1669 case PROC_TID_SCHEDSTAT
:
1670 case PROC_TGID_SCHEDSTAT
:
1671 inode
->i_fop
= &proc_info_file_operations
;
1672 ei
->op
.proc_read
= proc_pid_schedstat
;
1675 #ifdef CONFIG_CPUSETS
1676 case PROC_TID_CPUSET
:
1677 case PROC_TGID_CPUSET
:
1678 inode
->i_fop
= &proc_cpuset_operations
;
1681 case PROC_TID_OOM_SCORE
:
1682 case PROC_TGID_OOM_SCORE
:
1683 inode
->i_fop
= &proc_info_file_operations
;
1684 ei
->op
.proc_read
= proc_oom_score
;
1686 case PROC_TID_OOM_ADJUST
:
1687 case PROC_TGID_OOM_ADJUST
:
1688 inode
->i_fop
= &proc_oom_adjust_operations
;
1690 #ifdef CONFIG_AUDITSYSCALL
1691 case PROC_TID_LOGINUID
:
1692 case PROC_TGID_LOGINUID
:
1693 inode
->i_fop
= &proc_loginuid_operations
;
1697 printk("procfs: impossible type (%d)",p
->type
);
1699 return ERR_PTR(-EINVAL
);
1701 dentry
->d_op
= &pid_dentry_operations
;
1702 d_add(dentry
, inode
);
1706 return ERR_PTR(error
);
1709 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1710 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1713 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1714 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1717 static struct file_operations proc_tgid_base_operations
= {
1718 .read
= generic_read_dir
,
1719 .readdir
= proc_tgid_base_readdir
,
1722 static struct file_operations proc_tid_base_operations
= {
1723 .read
= generic_read_dir
,
1724 .readdir
= proc_tid_base_readdir
,
1727 static struct inode_operations proc_tgid_base_inode_operations
= {
1728 .lookup
= proc_tgid_base_lookup
,
1731 static struct inode_operations proc_tid_base_inode_operations
= {
1732 .lookup
= proc_tid_base_lookup
,
1735 #ifdef CONFIG_SECURITY
1736 static int proc_tgid_attr_readdir(struct file
* filp
,
1737 void * dirent
, filldir_t filldir
)
1739 return proc_pident_readdir(filp
,dirent
,filldir
,
1740 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1743 static int proc_tid_attr_readdir(struct file
* filp
,
1744 void * dirent
, filldir_t filldir
)
1746 return proc_pident_readdir(filp
,dirent
,filldir
,
1747 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1750 static struct file_operations proc_tgid_attr_operations
= {
1751 .read
= generic_read_dir
,
1752 .readdir
= proc_tgid_attr_readdir
,
1755 static struct file_operations proc_tid_attr_operations
= {
1756 .read
= generic_read_dir
,
1757 .readdir
= proc_tid_attr_readdir
,
1760 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1761 struct dentry
*dentry
, struct nameidata
*nd
)
1763 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1766 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1767 struct dentry
*dentry
, struct nameidata
*nd
)
1769 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1772 static struct inode_operations proc_tgid_attr_inode_operations
= {
1773 .lookup
= proc_tgid_attr_lookup
,
1776 static struct inode_operations proc_tid_attr_inode_operations
= {
1777 .lookup
= proc_tid_attr_lookup
,
1784 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1788 sprintf(tmp
, "%d", current
->tgid
);
1789 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1792 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1795 sprintf(tmp
, "%d", current
->tgid
);
1796 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1799 static struct inode_operations proc_self_inode_operations
= {
1800 .readlink
= proc_self_readlink
,
1801 .follow_link
= proc_self_follow_link
,
1805 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1806 * @p: task that should be flushed.
1808 * Drops the /proc/@pid dcache entry from the hash chains.
1810 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1811 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1812 * if the pid value is immediately reused. This is enforced by
1813 * - caller must acquire spin_lock(p->proc_lock)
1814 * - must be called before detach_pid()
1815 * - proc_pid_lookup acquires proc_lock, and checks that
1816 * the target is not dead by looking at the attach count
1820 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1822 struct dentry
*proc_dentry
;
1824 proc_dentry
= p
->proc_dentry
;
1825 if (proc_dentry
!= NULL
) {
1827 spin_lock(&dcache_lock
);
1828 spin_lock(&proc_dentry
->d_lock
);
1829 if (!d_unhashed(proc_dentry
)) {
1830 dget_locked(proc_dentry
);
1831 __d_drop(proc_dentry
);
1832 spin_unlock(&proc_dentry
->d_lock
);
1834 spin_unlock(&proc_dentry
->d_lock
);
1837 spin_unlock(&dcache_lock
);
1843 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1844 * @proc_dentry: directoy to prune.
1846 * Shrink the /proc directory that was used by the just killed thread.
1849 void proc_pid_flush(struct dentry
*proc_dentry
)
1852 if(proc_dentry
!= NULL
) {
1853 shrink_dcache_parent(proc_dentry
);
1859 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1861 struct task_struct
*task
;
1862 struct inode
*inode
;
1863 struct proc_inode
*ei
;
1867 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1868 inode
= new_inode(dir
->i_sb
);
1870 return ERR_PTR(-ENOMEM
);
1872 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1873 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1875 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1876 inode
->i_uid
= inode
->i_gid
= 0;
1878 inode
->i_op
= &proc_self_inode_operations
;
1879 d_add(dentry
, inode
);
1882 tgid
= name_to_int(dentry
);
1886 read_lock(&tasklist_lock
);
1887 task
= find_task_by_pid(tgid
);
1889 get_task_struct(task
);
1890 read_unlock(&tasklist_lock
);
1894 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1898 put_task_struct(task
);
1901 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1902 inode
->i_op
= &proc_tgid_base_inode_operations
;
1903 inode
->i_fop
= &proc_tgid_base_operations
;
1904 inode
->i_flags
|=S_IMMUTABLE
;
1905 #ifdef CONFIG_SECURITY
1911 dentry
->d_op
= &pid_base_dentry_operations
;
1914 d_add(dentry
, inode
);
1915 spin_lock(&task
->proc_lock
);
1916 task
->proc_dentry
= dentry
;
1917 if (!pid_alive(task
)) {
1918 dentry
= proc_pid_unhash(task
);
1921 spin_unlock(&task
->proc_lock
);
1923 put_task_struct(task
);
1925 proc_pid_flush(dentry
);
1930 return ERR_PTR(-ENOENT
);
1934 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1936 struct task_struct
*task
;
1937 struct task_struct
*leader
= proc_task(dir
);
1938 struct inode
*inode
;
1941 tid
= name_to_int(dentry
);
1945 read_lock(&tasklist_lock
);
1946 task
= find_task_by_pid(tid
);
1948 get_task_struct(task
);
1949 read_unlock(&tasklist_lock
);
1952 if (leader
->tgid
!= task
->tgid
)
1955 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
1960 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1961 inode
->i_op
= &proc_tid_base_inode_operations
;
1962 inode
->i_fop
= &proc_tid_base_operations
;
1963 inode
->i_flags
|=S_IMMUTABLE
;
1964 #ifdef CONFIG_SECURITY
1970 dentry
->d_op
= &pid_base_dentry_operations
;
1972 d_add(dentry
, inode
);
1974 put_task_struct(task
);
1977 put_task_struct(task
);
1979 return ERR_PTR(-ENOENT
);
1982 #define PROC_NUMBUF 10
1983 #define PROC_MAXPIDS 20
1986 * Get a few tgid's to return for filldir - we need to hold the
1987 * tasklist lock while doing this, and we must release it before
1988 * we actually do the filldir itself, so we use a temp buffer..
1990 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
1992 struct task_struct
*p
;
1996 read_lock(&tasklist_lock
);
1999 p
= find_task_by_pid(version
);
2000 if (p
&& !thread_group_leader(p
))
2007 p
= next_task(&init_task
);
2009 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2015 tgids
[nr_tgids
] = tgid
;
2017 if (nr_tgids
>= PROC_MAXPIDS
)
2020 read_unlock(&tasklist_lock
);
2025 * Get a few tid's to return for filldir - we need to hold the
2026 * tasklist lock while doing this, and we must release it before
2027 * we actually do the filldir itself, so we use a temp buffer..
2029 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2031 struct task_struct
*leader_task
= proc_task(dir
);
2032 struct task_struct
*task
= leader_task
;
2036 read_lock(&tasklist_lock
);
2038 * The starting point task (leader_task) might be an already
2039 * unlinked task, which cannot be used to access the task-list
2040 * via next_thread().
2042 if (pid_alive(task
)) do {
2043 int tid
= task
->pid
;
2048 tids
[nr_tids
] = tid
;
2050 if (nr_tids
>= PROC_MAXPIDS
)
2052 } while ((task
= next_thread(task
)) != leader_task
);
2053 read_unlock(&tasklist_lock
);
2057 /* for the /proc/ directory itself, after non-process stuff has been done */
2058 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2060 unsigned int tgid_array
[PROC_MAXPIDS
];
2061 char buf
[PROC_NUMBUF
];
2062 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2063 unsigned int nr_tgids
, i
;
2067 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2068 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2074 /* f_version caches the tgid value that the last readdir call couldn't
2075 * return. lseek aka telldir automagically resets f_version to 0.
2077 next_tgid
= filp
->f_version
;
2078 filp
->f_version
= 0;
2080 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2082 /* no more entries ! */
2087 /* do not use the last found pid, reserve it for next_tgid */
2088 if (nr_tgids
== PROC_MAXPIDS
) {
2090 next_tgid
= tgid_array
[nr_tgids
];
2093 for (i
=0;i
<nr_tgids
;i
++) {
2094 int tgid
= tgid_array
[i
];
2095 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2096 unsigned long j
= PROC_NUMBUF
;
2099 buf
[--j
] = '0' + (tgid
% 10);
2100 while ((tgid
/= 10) != 0);
2102 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2103 /* returning this tgid failed, save it as the first
2104 * pid for the next readir call */
2105 filp
->f_version
= tgid_array
[i
];
2116 /* for the /proc/TGID/task/ directories */
2117 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2119 unsigned int tid_array
[PROC_MAXPIDS
];
2120 char buf
[PROC_NUMBUF
];
2121 unsigned int nr_tids
, i
;
2122 struct dentry
*dentry
= filp
->f_dentry
;
2123 struct inode
*inode
= dentry
->d_inode
;
2124 int retval
= -ENOENT
;
2126 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2128 if (!pid_alive(proc_task(inode
)))
2135 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2140 ino
= parent_ino(dentry
);
2141 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2147 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2148 inode
->i_nlink
= pos
+ nr_tids
;
2150 for (i
= 0; i
< nr_tids
; i
++) {
2151 unsigned long j
= PROC_NUMBUF
;
2152 int tid
= tid_array
[i
];
2154 ino
= fake_ino(tid
,PROC_TID_INO
);
2157 buf
[--j
] = '0' + (tid
% 10);
2158 while ((tid
/= 10) != 0);
2160 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)