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/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/resource.h>
69 #include <linux/module.h>
70 #include <linux/mount.h>
71 #include <linux/security.h>
72 #include <linux/ptrace.h>
73 #include <linux/tracehook.h>
74 #include <linux/cgroup.h>
75 #include <linux/cpuset.h>
76 #include <linux/audit.h>
77 #include <linux/poll.h>
78 #include <linux/nsproxy.h>
79 #include <linux/oom.h>
80 #include <linux/elf.h>
81 #include <linux/pid_namespace.h>
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
98 const struct inode_operations
*iop
;
99 const struct file_operations
*fop
;
103 #define NOD(NAME, MODE, IOP, FOP, OP) { \
105 .len = sizeof(NAME) - 1, \
112 #define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
116 #define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
120 #define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
122 &proc_##OTYPE##_operations, {})
123 #define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
127 #define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
133 * Count the number of hardlinks for the pid_entry table, excluding the .
136 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
143 for (i
= 0; i
< n
; ++i
) {
144 if (S_ISDIR(entries
[i
].mode
))
151 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
153 struct fs_struct
*fs
;
157 atomic_inc(&fs
->count
);
162 static int get_nr_threads(struct task_struct
*tsk
)
167 if (lock_task_sighand(tsk
, &flags
)) {
168 count
= atomic_read(&tsk
->signal
->count
);
169 unlock_task_sighand(tsk
, &flags
);
174 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
176 struct task_struct
*task
= get_proc_task(inode
);
177 struct fs_struct
*fs
= NULL
;
178 int result
= -ENOENT
;
181 fs
= get_fs_struct(task
);
182 put_task_struct(task
);
185 read_lock(&fs
->lock
);
188 read_unlock(&fs
->lock
);
195 static int proc_root_link(struct inode
*inode
, struct path
*path
)
197 struct task_struct
*task
= get_proc_task(inode
);
198 struct fs_struct
*fs
= NULL
;
199 int result
= -ENOENT
;
202 fs
= get_fs_struct(task
);
203 put_task_struct(task
);
206 read_lock(&fs
->lock
);
209 read_unlock(&fs
->lock
);
217 * Return zero if current may access user memory in @task, -error if not.
219 static int check_mem_permission(struct task_struct
*task
)
222 * A task can always look at itself, in case it chooses
223 * to use system calls instead of load instructions.
229 * If current is actively ptrace'ing, and would also be
230 * permitted to freshly attach with ptrace now, permit it.
232 if (task_is_stopped_or_traced(task
)) {
235 match
= (tracehook_tracer_task(task
) == current
);
237 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
242 * Noone else is allowed.
247 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
249 struct mm_struct
*mm
= get_task_mm(task
);
252 down_read(&mm
->mmap_sem
);
256 if (task
->mm
!= current
->mm
&&
257 __ptrace_may_access(task
, PTRACE_MODE_READ
) < 0)
263 up_read(&mm
->mmap_sem
);
268 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
272 struct mm_struct
*mm
= get_task_mm(task
);
276 goto out_mm
; /* Shh! No looking before we're done */
278 len
= mm
->arg_end
- mm
->arg_start
;
283 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
285 // If the nul at the end of args has been overwritten, then
286 // assume application is using setproctitle(3).
287 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
288 len
= strnlen(buffer
, res
);
292 len
= mm
->env_end
- mm
->env_start
;
293 if (len
> PAGE_SIZE
- res
)
294 len
= PAGE_SIZE
- res
;
295 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
296 res
= strnlen(buffer
, res
);
305 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
308 struct mm_struct
*mm
= get_task_mm(task
);
310 unsigned int nwords
= 0;
313 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
314 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
317 memcpy(buffer
, mm
->saved_auxv
, res
);
324 #ifdef CONFIG_KALLSYMS
326 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
327 * Returns the resolved symbol. If that fails, simply return the address.
329 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
332 char symname
[KSYM_NAME_LEN
];
334 wchan
= get_wchan(task
);
336 if (lookup_symbol_name(wchan
, symname
) < 0)
337 return sprintf(buffer
, "%lu", wchan
);
339 return sprintf(buffer
, "%s", symname
);
341 #endif /* CONFIG_KALLSYMS */
343 #ifdef CONFIG_SCHEDSTATS
345 * Provides /proc/PID/schedstat
347 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
349 return sprintf(buffer
, "%llu %llu %lu\n",
350 (unsigned long long)task
->se
.sum_exec_runtime
,
351 (unsigned long long)task
->sched_info
.run_delay
,
352 task
->sched_info
.pcount
);
356 #ifdef CONFIG_LATENCYTOP
357 static int lstats_show_proc(struct seq_file
*m
, void *v
)
360 struct inode
*inode
= m
->private;
361 struct task_struct
*task
= get_proc_task(inode
);
365 seq_puts(m
, "Latency Top version : v0.1\n");
366 for (i
= 0; i
< 32; i
++) {
367 if (task
->latency_record
[i
].backtrace
[0]) {
369 seq_printf(m
, "%i %li %li ",
370 task
->latency_record
[i
].count
,
371 task
->latency_record
[i
].time
,
372 task
->latency_record
[i
].max
);
373 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
374 char sym
[KSYM_SYMBOL_LEN
];
376 if (!task
->latency_record
[i
].backtrace
[q
])
378 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
380 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
381 c
= strchr(sym
, '+');
384 seq_printf(m
, "%s ", sym
);
390 put_task_struct(task
);
394 static int lstats_open(struct inode
*inode
, struct file
*file
)
396 return single_open(file
, lstats_show_proc
, inode
);
399 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
400 size_t count
, loff_t
*offs
)
402 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
406 clear_all_latency_tracing(task
);
407 put_task_struct(task
);
412 static const struct file_operations proc_lstats_operations
= {
415 .write
= lstats_write
,
417 .release
= single_release
,
422 /* The badness from the OOM killer */
423 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
424 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
426 unsigned long points
;
427 struct timespec uptime
;
429 do_posix_clock_monotonic_gettime(&uptime
);
430 read_lock(&tasklist_lock
);
431 points
= badness(task
, uptime
.tv_sec
);
432 read_unlock(&tasklist_lock
);
433 return sprintf(buffer
, "%lu\n", points
);
441 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
442 [RLIMIT_CPU
] = {"Max cpu time", "ms"},
443 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
444 [RLIMIT_DATA
] = {"Max data size", "bytes"},
445 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
446 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
447 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
448 [RLIMIT_NPROC
] = {"Max processes", "processes"},
449 [RLIMIT_NOFILE
] = {"Max open files", "files"},
450 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
451 [RLIMIT_AS
] = {"Max address space", "bytes"},
452 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
453 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
454 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
455 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
456 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
457 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
460 /* Display limits for a process */
461 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
466 char *bufptr
= buffer
;
468 struct rlimit rlim
[RLIM_NLIMITS
];
470 if (!lock_task_sighand(task
, &flags
))
472 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
473 unlock_task_sighand(task
, &flags
);
476 * print the file header
478 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
479 "Limit", "Soft Limit", "Hard Limit", "Units");
481 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
482 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
483 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
484 lnames
[i
].name
, "unlimited");
486 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
487 lnames
[i
].name
, rlim
[i
].rlim_cur
);
489 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
490 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
492 count
+= sprintf(&bufptr
[count
], "%-20lu ",
496 count
+= sprintf(&bufptr
[count
], "%-10s\n",
499 count
+= sprintf(&bufptr
[count
], "\n");
505 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
506 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
509 unsigned long args
[6], sp
, pc
;
511 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
512 return sprintf(buffer
, "running\n");
515 return sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
517 return sprintf(buffer
,
518 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
520 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
523 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
525 /************************************************************************/
526 /* Here the fs part begins */
527 /************************************************************************/
529 /* permission checks */
530 static int proc_fd_access_allowed(struct inode
*inode
)
532 struct task_struct
*task
;
534 /* Allow access to a task's file descriptors if it is us or we
535 * may use ptrace attach to the process and find out that
538 task
= get_proc_task(inode
);
540 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
541 put_task_struct(task
);
546 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
549 struct inode
*inode
= dentry
->d_inode
;
551 if (attr
->ia_valid
& ATTR_MODE
)
554 error
= inode_change_ok(inode
, attr
);
556 error
= inode_setattr(inode
, attr
);
560 static const struct inode_operations proc_def_inode_operations
= {
561 .setattr
= proc_setattr
,
564 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
565 const struct seq_operations
*op
)
567 struct task_struct
*task
= get_proc_task(inode
);
569 struct mnt_namespace
*ns
= NULL
;
570 struct fs_struct
*fs
= NULL
;
572 struct proc_mounts
*p
;
577 nsp
= task_nsproxy(task
);
585 fs
= get_fs_struct(task
);
586 put_task_struct(task
);
594 read_lock(&fs
->lock
);
597 read_unlock(&fs
->lock
);
601 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
605 file
->private_data
= &p
->m
;
606 ret
= seq_open(file
, op
);
613 p
->event
= ns
->event
;
627 static int mounts_release(struct inode
*inode
, struct file
*file
)
629 struct proc_mounts
*p
= file
->private_data
;
632 return seq_release(inode
, file
);
635 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
637 struct proc_mounts
*p
= file
->private_data
;
638 struct mnt_namespace
*ns
= p
->ns
;
641 poll_wait(file
, &ns
->poll
, wait
);
643 spin_lock(&vfsmount_lock
);
644 if (p
->event
!= ns
->event
) {
645 p
->event
= ns
->event
;
648 spin_unlock(&vfsmount_lock
);
653 static int mounts_open(struct inode
*inode
, struct file
*file
)
655 return mounts_open_common(inode
, file
, &mounts_op
);
658 static const struct file_operations proc_mounts_operations
= {
662 .release
= mounts_release
,
666 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
668 return mounts_open_common(inode
, file
, &mountinfo_op
);
671 static const struct file_operations proc_mountinfo_operations
= {
672 .open
= mountinfo_open
,
675 .release
= mounts_release
,
679 static int mountstats_open(struct inode
*inode
, struct file
*file
)
681 return mounts_open_common(inode
, file
, &mountstats_op
);
684 static const struct file_operations proc_mountstats_operations
= {
685 .open
= mountstats_open
,
688 .release
= mounts_release
,
691 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
693 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
694 size_t count
, loff_t
*ppos
)
696 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
699 struct task_struct
*task
= get_proc_task(inode
);
705 if (count
> PROC_BLOCK_SIZE
)
706 count
= PROC_BLOCK_SIZE
;
709 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
712 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
715 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
718 put_task_struct(task
);
723 static const struct file_operations proc_info_file_operations
= {
724 .read
= proc_info_read
,
727 static int proc_single_show(struct seq_file
*m
, void *v
)
729 struct inode
*inode
= m
->private;
730 struct pid_namespace
*ns
;
732 struct task_struct
*task
;
735 ns
= inode
->i_sb
->s_fs_info
;
736 pid
= proc_pid(inode
);
737 task
= get_pid_task(pid
, PIDTYPE_PID
);
741 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
743 put_task_struct(task
);
747 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
750 ret
= single_open(filp
, proc_single_show
, NULL
);
752 struct seq_file
*m
= filp
->private_data
;
759 static const struct file_operations proc_single_file_operations
= {
760 .open
= proc_single_open
,
763 .release
= single_release
,
766 static int mem_open(struct inode
* inode
, struct file
* file
)
768 file
->private_data
= (void*)((long)current
->self_exec_id
);
772 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
773 size_t count
, loff_t
*ppos
)
775 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
777 unsigned long src
= *ppos
;
779 struct mm_struct
*mm
;
784 if (check_mem_permission(task
))
788 page
= (char *)__get_free_page(GFP_TEMPORARY
);
794 mm
= get_task_mm(task
);
800 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
806 int this_len
, retval
;
808 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
809 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
810 if (!retval
|| check_mem_permission(task
)) {
816 if (copy_to_user(buf
, page
, retval
)) {
831 free_page((unsigned long) page
);
833 put_task_struct(task
);
838 #define mem_write NULL
841 /* This is a security hazard */
842 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
843 size_t count
, loff_t
*ppos
)
847 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
848 unsigned long dst
= *ppos
;
854 if (check_mem_permission(task
))
858 page
= (char *)__get_free_page(GFP_TEMPORARY
);
864 int this_len
, retval
;
866 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
867 if (copy_from_user(page
, buf
, this_len
)) {
871 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
883 free_page((unsigned long) page
);
885 put_task_struct(task
);
891 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
895 file
->f_pos
= offset
;
898 file
->f_pos
+= offset
;
903 force_successful_syscall_return();
907 static const struct file_operations proc_mem_operations
= {
914 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
915 size_t count
, loff_t
*ppos
)
917 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
919 unsigned long src
= *ppos
;
921 struct mm_struct
*mm
;
926 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
930 page
= (char *)__get_free_page(GFP_TEMPORARY
);
936 mm
= get_task_mm(task
);
941 int this_len
, retval
, max_len
;
943 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
948 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
949 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
951 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
959 if (copy_to_user(buf
, page
, retval
)) {
973 free_page((unsigned long) page
);
975 put_task_struct(task
);
980 static const struct file_operations proc_environ_operations
= {
981 .read
= environ_read
,
984 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
985 size_t count
, loff_t
*ppos
)
987 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
988 char buffer
[PROC_NUMBUF
];
994 oom_adjust
= task
->oomkilladj
;
995 put_task_struct(task
);
997 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
999 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1002 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1003 size_t count
, loff_t
*ppos
)
1005 struct task_struct
*task
;
1006 char buffer
[PROC_NUMBUF
], *end
;
1009 memset(buffer
, 0, sizeof(buffer
));
1010 if (count
> sizeof(buffer
) - 1)
1011 count
= sizeof(buffer
) - 1;
1012 if (copy_from_user(buffer
, buf
, count
))
1014 oom_adjust
= simple_strtol(buffer
, &end
, 0);
1015 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1016 oom_adjust
!= OOM_DISABLE
)
1020 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1023 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
1024 put_task_struct(task
);
1027 task
->oomkilladj
= oom_adjust
;
1028 put_task_struct(task
);
1029 if (end
- buffer
== 0)
1031 return end
- buffer
;
1034 static const struct file_operations proc_oom_adjust_operations
= {
1035 .read
= oom_adjust_read
,
1036 .write
= oom_adjust_write
,
1039 #ifdef CONFIG_AUDITSYSCALL
1040 #define TMPBUFLEN 21
1041 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1042 size_t count
, loff_t
*ppos
)
1044 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1045 struct task_struct
*task
= get_proc_task(inode
);
1047 char tmpbuf
[TMPBUFLEN
];
1051 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1052 audit_get_loginuid(task
));
1053 put_task_struct(task
);
1054 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1057 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1058 size_t count
, loff_t
*ppos
)
1060 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1065 if (!capable(CAP_AUDIT_CONTROL
))
1068 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1071 if (count
>= PAGE_SIZE
)
1072 count
= PAGE_SIZE
- 1;
1075 /* No partial writes. */
1078 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1082 if (copy_from_user(page
, buf
, count
))
1086 loginuid
= simple_strtoul(page
, &tmp
, 10);
1092 length
= audit_set_loginuid(current
, loginuid
);
1093 if (likely(length
== 0))
1097 free_page((unsigned long) page
);
1101 static const struct file_operations proc_loginuid_operations
= {
1102 .read
= proc_loginuid_read
,
1103 .write
= proc_loginuid_write
,
1106 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1107 size_t count
, loff_t
*ppos
)
1109 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1110 struct task_struct
*task
= get_proc_task(inode
);
1112 char tmpbuf
[TMPBUFLEN
];
1116 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1117 audit_get_sessionid(task
));
1118 put_task_struct(task
);
1119 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1122 static const struct file_operations proc_sessionid_operations
= {
1123 .read
= proc_sessionid_read
,
1127 #ifdef CONFIG_FAULT_INJECTION
1128 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1129 size_t count
, loff_t
*ppos
)
1131 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1132 char buffer
[PROC_NUMBUF
];
1138 make_it_fail
= task
->make_it_fail
;
1139 put_task_struct(task
);
1141 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1143 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1146 static ssize_t
proc_fault_inject_write(struct file
* file
,
1147 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1149 struct task_struct
*task
;
1150 char buffer
[PROC_NUMBUF
], *end
;
1153 if (!capable(CAP_SYS_RESOURCE
))
1155 memset(buffer
, 0, sizeof(buffer
));
1156 if (count
> sizeof(buffer
) - 1)
1157 count
= sizeof(buffer
) - 1;
1158 if (copy_from_user(buffer
, buf
, count
))
1160 make_it_fail
= simple_strtol(buffer
, &end
, 0);
1163 task
= get_proc_task(file
->f_dentry
->d_inode
);
1166 task
->make_it_fail
= make_it_fail
;
1167 put_task_struct(task
);
1168 if (end
- buffer
== 0)
1170 return end
- buffer
;
1173 static const struct file_operations proc_fault_inject_operations
= {
1174 .read
= proc_fault_inject_read
,
1175 .write
= proc_fault_inject_write
,
1180 #ifdef CONFIG_SCHED_DEBUG
1182 * Print out various scheduling related per-task fields:
1184 static int sched_show(struct seq_file
*m
, void *v
)
1186 struct inode
*inode
= m
->private;
1187 struct task_struct
*p
;
1191 p
= get_proc_task(inode
);
1194 proc_sched_show_task(p
, m
);
1202 sched_write(struct file
*file
, const char __user
*buf
,
1203 size_t count
, loff_t
*offset
)
1205 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1206 struct task_struct
*p
;
1210 p
= get_proc_task(inode
);
1213 proc_sched_set_task(p
);
1220 static int sched_open(struct inode
*inode
, struct file
*filp
)
1224 ret
= single_open(filp
, sched_show
, NULL
);
1226 struct seq_file
*m
= filp
->private_data
;
1233 static const struct file_operations proc_pid_sched_operations
= {
1236 .write
= sched_write
,
1237 .llseek
= seq_lseek
,
1238 .release
= single_release
,
1244 * We added or removed a vma mapping the executable. The vmas are only mapped
1245 * during exec and are not mapped with the mmap system call.
1246 * Callers must hold down_write() on the mm's mmap_sem for these
1248 void added_exe_file_vma(struct mm_struct
*mm
)
1250 mm
->num_exe_file_vmas
++;
1253 void removed_exe_file_vma(struct mm_struct
*mm
)
1255 mm
->num_exe_file_vmas
--;
1256 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1258 mm
->exe_file
= NULL
;
1263 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1266 get_file(new_exe_file
);
1269 mm
->exe_file
= new_exe_file
;
1270 mm
->num_exe_file_vmas
= 0;
1273 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1275 struct file
*exe_file
;
1277 /* We need mmap_sem to protect against races with removal of
1278 * VM_EXECUTABLE vmas */
1279 down_read(&mm
->mmap_sem
);
1280 exe_file
= mm
->exe_file
;
1283 up_read(&mm
->mmap_sem
);
1287 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1289 /* It's safe to write the exe_file pointer without exe_file_lock because
1290 * this is called during fork when the task is not yet in /proc */
1291 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1294 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1296 struct task_struct
*task
;
1297 struct mm_struct
*mm
;
1298 struct file
*exe_file
;
1300 task
= get_proc_task(inode
);
1303 mm
= get_task_mm(task
);
1304 put_task_struct(task
);
1307 exe_file
= get_mm_exe_file(mm
);
1310 *exe_path
= exe_file
->f_path
;
1311 path_get(&exe_file
->f_path
);
1318 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1320 struct inode
*inode
= dentry
->d_inode
;
1321 int error
= -EACCES
;
1323 /* We don't need a base pointer in the /proc filesystem */
1324 path_put(&nd
->path
);
1326 /* Are we allowed to snoop on the tasks file descriptors? */
1327 if (!proc_fd_access_allowed(inode
))
1330 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1331 nd
->last_type
= LAST_BIND
;
1333 return ERR_PTR(error
);
1336 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1338 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1345 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1346 len
= PTR_ERR(pathname
);
1347 if (IS_ERR(pathname
))
1349 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1353 if (copy_to_user(buffer
, pathname
, len
))
1356 free_page((unsigned long)tmp
);
1360 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1362 int error
= -EACCES
;
1363 struct inode
*inode
= dentry
->d_inode
;
1366 /* Are we allowed to snoop on the tasks file descriptors? */
1367 if (!proc_fd_access_allowed(inode
))
1370 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1374 error
= do_proc_readlink(&path
, buffer
, buflen
);
1380 static const struct inode_operations proc_pid_link_inode_operations
= {
1381 .readlink
= proc_pid_readlink
,
1382 .follow_link
= proc_pid_follow_link
,
1383 .setattr
= proc_setattr
,
1387 /* building an inode */
1389 static int task_dumpable(struct task_struct
*task
)
1392 struct mm_struct
*mm
;
1397 dumpable
= get_dumpable(mm
);
1405 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1407 struct inode
* inode
;
1408 struct proc_inode
*ei
;
1409 const struct cred
*cred
;
1411 /* We need a new inode */
1413 inode
= new_inode(sb
);
1419 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1420 inode
->i_op
= &proc_def_inode_operations
;
1423 * grab the reference to task.
1425 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1431 if (task_dumpable(task
)) {
1433 cred
= __task_cred(task
);
1434 inode
->i_uid
= cred
->euid
;
1435 inode
->i_gid
= cred
->egid
;
1438 security_task_to_inode(task
, inode
);
1448 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1450 struct inode
*inode
= dentry
->d_inode
;
1451 struct task_struct
*task
;
1452 const struct cred
*cred
;
1454 generic_fillattr(inode
, stat
);
1459 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1461 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1462 task_dumpable(task
)) {
1463 cred
= __task_cred(task
);
1464 stat
->uid
= cred
->euid
;
1465 stat
->gid
= cred
->egid
;
1475 * Exceptional case: normally we are not allowed to unhash a busy
1476 * directory. In this case, however, we can do it - no aliasing problems
1477 * due to the way we treat inodes.
1479 * Rewrite the inode's ownerships here because the owning task may have
1480 * performed a setuid(), etc.
1482 * Before the /proc/pid/status file was created the only way to read
1483 * the effective uid of a /process was to stat /proc/pid. Reading
1484 * /proc/pid/status is slow enough that procps and other packages
1485 * kept stating /proc/pid. To keep the rules in /proc simple I have
1486 * made this apply to all per process world readable and executable
1489 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1491 struct inode
*inode
= dentry
->d_inode
;
1492 struct task_struct
*task
= get_proc_task(inode
);
1493 const struct cred
*cred
;
1496 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1497 task_dumpable(task
)) {
1499 cred
= __task_cred(task
);
1500 inode
->i_uid
= cred
->euid
;
1501 inode
->i_gid
= cred
->egid
;
1507 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1508 security_task_to_inode(task
, inode
);
1509 put_task_struct(task
);
1516 static int pid_delete_dentry(struct dentry
* dentry
)
1518 /* Is the task we represent dead?
1519 * If so, then don't put the dentry on the lru list,
1520 * kill it immediately.
1522 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1525 static struct dentry_operations pid_dentry_operations
=
1527 .d_revalidate
= pid_revalidate
,
1528 .d_delete
= pid_delete_dentry
,
1533 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1534 struct task_struct
*, const void *);
1537 * Fill a directory entry.
1539 * If possible create the dcache entry and derive our inode number and
1540 * file type from dcache entry.
1542 * Since all of the proc inode numbers are dynamically generated, the inode
1543 * numbers do not exist until the inode is cache. This means creating the
1544 * the dcache entry in readdir is necessary to keep the inode numbers
1545 * reported by readdir in sync with the inode numbers reported
1548 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1549 char *name
, int len
,
1550 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1552 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1553 struct inode
*inode
;
1556 unsigned type
= DT_UNKNOWN
;
1560 qname
.hash
= full_name_hash(name
, len
);
1562 child
= d_lookup(dir
, &qname
);
1565 new = d_alloc(dir
, &qname
);
1567 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1574 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1575 goto end_instantiate
;
1576 inode
= child
->d_inode
;
1579 type
= inode
->i_mode
>> 12;
1584 ino
= find_inode_number(dir
, &qname
);
1587 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1590 static unsigned name_to_int(struct dentry
*dentry
)
1592 const char *name
= dentry
->d_name
.name
;
1593 int len
= dentry
->d_name
.len
;
1596 if (len
> 1 && *name
== '0')
1599 unsigned c
= *name
++ - '0';
1602 if (n
>= (~0U-9)/10)
1612 #define PROC_FDINFO_MAX 64
1614 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1616 struct task_struct
*task
= get_proc_task(inode
);
1617 struct files_struct
*files
= NULL
;
1619 int fd
= proc_fd(inode
);
1622 files
= get_files_struct(task
);
1623 put_task_struct(task
);
1627 * We are not taking a ref to the file structure, so we must
1630 spin_lock(&files
->file_lock
);
1631 file
= fcheck_files(files
, fd
);
1634 *path
= file
->f_path
;
1635 path_get(&file
->f_path
);
1638 snprintf(info
, PROC_FDINFO_MAX
,
1641 (long long) file
->f_pos
,
1643 spin_unlock(&files
->file_lock
);
1644 put_files_struct(files
);
1647 spin_unlock(&files
->file_lock
);
1648 put_files_struct(files
);
1653 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1655 return proc_fd_info(inode
, path
, NULL
);
1658 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1660 struct inode
*inode
= dentry
->d_inode
;
1661 struct task_struct
*task
= get_proc_task(inode
);
1662 int fd
= proc_fd(inode
);
1663 struct files_struct
*files
;
1664 const struct cred
*cred
;
1667 files
= get_files_struct(task
);
1670 if (fcheck_files(files
, fd
)) {
1672 put_files_struct(files
);
1673 if (task_dumpable(task
)) {
1675 cred
= __task_cred(task
);
1676 inode
->i_uid
= cred
->euid
;
1677 inode
->i_gid
= cred
->egid
;
1683 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1684 security_task_to_inode(task
, inode
);
1685 put_task_struct(task
);
1689 put_files_struct(files
);
1691 put_task_struct(task
);
1697 static struct dentry_operations tid_fd_dentry_operations
=
1699 .d_revalidate
= tid_fd_revalidate
,
1700 .d_delete
= pid_delete_dentry
,
1703 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1704 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1706 unsigned fd
= *(const unsigned *)ptr
;
1708 struct files_struct
*files
;
1709 struct inode
*inode
;
1710 struct proc_inode
*ei
;
1711 struct dentry
*error
= ERR_PTR(-ENOENT
);
1713 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1718 files
= get_files_struct(task
);
1721 inode
->i_mode
= S_IFLNK
;
1724 * We are not taking a ref to the file structure, so we must
1727 spin_lock(&files
->file_lock
);
1728 file
= fcheck_files(files
, fd
);
1731 if (file
->f_mode
& FMODE_READ
)
1732 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1733 if (file
->f_mode
& FMODE_WRITE
)
1734 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1735 spin_unlock(&files
->file_lock
);
1736 put_files_struct(files
);
1738 inode
->i_op
= &proc_pid_link_inode_operations
;
1740 ei
->op
.proc_get_link
= proc_fd_link
;
1741 dentry
->d_op
= &tid_fd_dentry_operations
;
1742 d_add(dentry
, inode
);
1743 /* Close the race of the process dying before we return the dentry */
1744 if (tid_fd_revalidate(dentry
, NULL
))
1750 spin_unlock(&files
->file_lock
);
1751 put_files_struct(files
);
1757 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1758 struct dentry
*dentry
,
1759 instantiate_t instantiate
)
1761 struct task_struct
*task
= get_proc_task(dir
);
1762 unsigned fd
= name_to_int(dentry
);
1763 struct dentry
*result
= ERR_PTR(-ENOENT
);
1770 result
= instantiate(dir
, dentry
, task
, &fd
);
1772 put_task_struct(task
);
1777 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1778 filldir_t filldir
, instantiate_t instantiate
)
1780 struct dentry
*dentry
= filp
->f_path
.dentry
;
1781 struct inode
*inode
= dentry
->d_inode
;
1782 struct task_struct
*p
= get_proc_task(inode
);
1783 unsigned int fd
, ino
;
1785 struct files_struct
* files
;
1795 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1799 ino
= parent_ino(dentry
);
1800 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1804 files
= get_files_struct(p
);
1808 for (fd
= filp
->f_pos
-2;
1809 fd
< files_fdtable(files
)->max_fds
;
1810 fd
++, filp
->f_pos
++) {
1811 char name
[PROC_NUMBUF
];
1814 if (!fcheck_files(files
, fd
))
1818 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1819 if (proc_fill_cache(filp
, dirent
, filldir
,
1820 name
, len
, instantiate
,
1828 put_files_struct(files
);
1836 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1837 struct nameidata
*nd
)
1839 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1842 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1844 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1847 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1848 size_t len
, loff_t
*ppos
)
1850 char tmp
[PROC_FDINFO_MAX
];
1851 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1853 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1857 static const struct file_operations proc_fdinfo_file_operations
= {
1858 .open
= nonseekable_open
,
1859 .read
= proc_fdinfo_read
,
1862 static const struct file_operations proc_fd_operations
= {
1863 .read
= generic_read_dir
,
1864 .readdir
= proc_readfd
,
1868 * /proc/pid/fd needs a special permission handler so that a process can still
1869 * access /proc/self/fd after it has executed a setuid().
1871 static int proc_fd_permission(struct inode
*inode
, int mask
)
1875 rv
= generic_permission(inode
, mask
, NULL
);
1878 if (task_pid(current
) == proc_pid(inode
))
1884 * proc directories can do almost nothing..
1886 static const struct inode_operations proc_fd_inode_operations
= {
1887 .lookup
= proc_lookupfd
,
1888 .permission
= proc_fd_permission
,
1889 .setattr
= proc_setattr
,
1892 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1893 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1895 unsigned fd
= *(unsigned *)ptr
;
1896 struct inode
*inode
;
1897 struct proc_inode
*ei
;
1898 struct dentry
*error
= ERR_PTR(-ENOENT
);
1900 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1905 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1906 inode
->i_fop
= &proc_fdinfo_file_operations
;
1907 dentry
->d_op
= &tid_fd_dentry_operations
;
1908 d_add(dentry
, inode
);
1909 /* Close the race of the process dying before we return the dentry */
1910 if (tid_fd_revalidate(dentry
, NULL
))
1917 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1918 struct dentry
*dentry
,
1919 struct nameidata
*nd
)
1921 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1924 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1926 return proc_readfd_common(filp
, dirent
, filldir
,
1927 proc_fdinfo_instantiate
);
1930 static const struct file_operations proc_fdinfo_operations
= {
1931 .read
= generic_read_dir
,
1932 .readdir
= proc_readfdinfo
,
1936 * proc directories can do almost nothing..
1938 static const struct inode_operations proc_fdinfo_inode_operations
= {
1939 .lookup
= proc_lookupfdinfo
,
1940 .setattr
= proc_setattr
,
1944 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1945 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1947 const struct pid_entry
*p
= ptr
;
1948 struct inode
*inode
;
1949 struct proc_inode
*ei
;
1950 struct dentry
*error
= ERR_PTR(-EINVAL
);
1952 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1957 inode
->i_mode
= p
->mode
;
1958 if (S_ISDIR(inode
->i_mode
))
1959 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1961 inode
->i_op
= p
->iop
;
1963 inode
->i_fop
= p
->fop
;
1965 dentry
->d_op
= &pid_dentry_operations
;
1966 d_add(dentry
, inode
);
1967 /* Close the race of the process dying before we return the dentry */
1968 if (pid_revalidate(dentry
, NULL
))
1974 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1975 struct dentry
*dentry
,
1976 const struct pid_entry
*ents
,
1979 struct inode
*inode
;
1980 struct dentry
*error
;
1981 struct task_struct
*task
= get_proc_task(dir
);
1982 const struct pid_entry
*p
, *last
;
1984 error
= ERR_PTR(-ENOENT
);
1991 * Yes, it does not scale. And it should not. Don't add
1992 * new entries into /proc/<tgid>/ without very good reasons.
1994 last
= &ents
[nents
- 1];
1995 for (p
= ents
; p
<= last
; p
++) {
1996 if (p
->len
!= dentry
->d_name
.len
)
1998 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2004 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2006 put_task_struct(task
);
2011 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2012 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2014 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2015 proc_pident_instantiate
, task
, p
);
2018 static int proc_pident_readdir(struct file
*filp
,
2019 void *dirent
, filldir_t filldir
,
2020 const struct pid_entry
*ents
, unsigned int nents
)
2023 struct dentry
*dentry
= filp
->f_path
.dentry
;
2024 struct inode
*inode
= dentry
->d_inode
;
2025 struct task_struct
*task
= get_proc_task(inode
);
2026 const struct pid_entry
*p
, *last
;
2039 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2045 ino
= parent_ino(dentry
);
2046 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2058 last
= &ents
[nents
- 1];
2060 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2069 put_task_struct(task
);
2074 #ifdef CONFIG_SECURITY
2075 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2076 size_t count
, loff_t
*ppos
)
2078 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2081 struct task_struct
*task
= get_proc_task(inode
);
2086 length
= security_getprocattr(task
,
2087 (char*)file
->f_path
.dentry
->d_name
.name
,
2089 put_task_struct(task
);
2091 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2096 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2097 size_t count
, loff_t
*ppos
)
2099 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2102 struct task_struct
*task
= get_proc_task(inode
);
2107 if (count
> PAGE_SIZE
)
2110 /* No partial writes. */
2116 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2121 if (copy_from_user(page
, buf
, count
))
2124 length
= security_setprocattr(task
,
2125 (char*)file
->f_path
.dentry
->d_name
.name
,
2126 (void*)page
, count
);
2128 free_page((unsigned long) page
);
2130 put_task_struct(task
);
2135 static const struct file_operations proc_pid_attr_operations
= {
2136 .read
= proc_pid_attr_read
,
2137 .write
= proc_pid_attr_write
,
2140 static const struct pid_entry attr_dir_stuff
[] = {
2141 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
2142 REG("prev", S_IRUGO
, pid_attr
),
2143 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
2144 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2145 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2146 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
2149 static int proc_attr_dir_readdir(struct file
* filp
,
2150 void * dirent
, filldir_t filldir
)
2152 return proc_pident_readdir(filp
,dirent
,filldir
,
2153 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2156 static const struct file_operations proc_attr_dir_operations
= {
2157 .read
= generic_read_dir
,
2158 .readdir
= proc_attr_dir_readdir
,
2161 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2162 struct dentry
*dentry
, struct nameidata
*nd
)
2164 return proc_pident_lookup(dir
, dentry
,
2165 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2168 static const struct inode_operations proc_attr_dir_inode_operations
= {
2169 .lookup
= proc_attr_dir_lookup
,
2170 .getattr
= pid_getattr
,
2171 .setattr
= proc_setattr
,
2176 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2177 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2178 size_t count
, loff_t
*ppos
)
2180 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2181 struct mm_struct
*mm
;
2182 char buffer
[PROC_NUMBUF
];
2190 mm
= get_task_mm(task
);
2192 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2193 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2194 MMF_DUMP_FILTER_SHIFT
));
2196 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2199 put_task_struct(task
);
2204 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2205 const char __user
*buf
,
2209 struct task_struct
*task
;
2210 struct mm_struct
*mm
;
2211 char buffer
[PROC_NUMBUF
], *end
;
2218 memset(buffer
, 0, sizeof(buffer
));
2219 if (count
> sizeof(buffer
) - 1)
2220 count
= sizeof(buffer
) - 1;
2221 if (copy_from_user(buffer
, buf
, count
))
2225 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2228 if (end
- buffer
== 0)
2232 task
= get_proc_task(file
->f_dentry
->d_inode
);
2237 mm
= get_task_mm(task
);
2241 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2243 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2245 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2250 put_task_struct(task
);
2255 static const struct file_operations proc_coredump_filter_operations
= {
2256 .read
= proc_coredump_filter_read
,
2257 .write
= proc_coredump_filter_write
,
2264 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2267 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2268 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2269 char tmp
[PROC_NUMBUF
];
2272 sprintf(tmp
, "%d", tgid
);
2273 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2276 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2278 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2279 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2280 char tmp
[PROC_NUMBUF
];
2282 return ERR_PTR(-ENOENT
);
2283 sprintf(tmp
, "%d", task_tgid_nr_ns(current
, ns
));
2284 return ERR_PTR(vfs_follow_link(nd
,tmp
));
2287 static const struct inode_operations proc_self_inode_operations
= {
2288 .readlink
= proc_self_readlink
,
2289 .follow_link
= proc_self_follow_link
,
2295 * These are the directory entries in the root directory of /proc
2296 * that properly belong to the /proc filesystem, as they describe
2297 * describe something that is process related.
2299 static const struct pid_entry proc_base_stuff
[] = {
2300 NOD("self", S_IFLNK
|S_IRWXUGO
,
2301 &proc_self_inode_operations
, NULL
, {}),
2305 * Exceptional case: normally we are not allowed to unhash a busy
2306 * directory. In this case, however, we can do it - no aliasing problems
2307 * due to the way we treat inodes.
2309 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2311 struct inode
*inode
= dentry
->d_inode
;
2312 struct task_struct
*task
= get_proc_task(inode
);
2314 put_task_struct(task
);
2321 static struct dentry_operations proc_base_dentry_operations
=
2323 .d_revalidate
= proc_base_revalidate
,
2324 .d_delete
= pid_delete_dentry
,
2327 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2328 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2330 const struct pid_entry
*p
= ptr
;
2331 struct inode
*inode
;
2332 struct proc_inode
*ei
;
2333 struct dentry
*error
= ERR_PTR(-EINVAL
);
2335 /* Allocate the inode */
2336 error
= ERR_PTR(-ENOMEM
);
2337 inode
= new_inode(dir
->i_sb
);
2341 /* Initialize the inode */
2343 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2346 * grab the reference to the task.
2348 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2354 inode
->i_mode
= p
->mode
;
2355 if (S_ISDIR(inode
->i_mode
))
2357 if (S_ISLNK(inode
->i_mode
))
2360 inode
->i_op
= p
->iop
;
2362 inode
->i_fop
= p
->fop
;
2364 dentry
->d_op
= &proc_base_dentry_operations
;
2365 d_add(dentry
, inode
);
2374 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2376 struct dentry
*error
;
2377 struct task_struct
*task
= get_proc_task(dir
);
2378 const struct pid_entry
*p
, *last
;
2380 error
= ERR_PTR(-ENOENT
);
2385 /* Lookup the directory entry */
2386 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2387 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2388 if (p
->len
!= dentry
->d_name
.len
)
2390 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2396 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2399 put_task_struct(task
);
2404 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2405 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2407 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2408 proc_base_instantiate
, task
, p
);
2411 #ifdef CONFIG_TASK_IO_ACCOUNTING
2412 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2414 struct task_io_accounting acct
= task
->ioac
;
2415 unsigned long flags
;
2417 if (whole
&& lock_task_sighand(task
, &flags
)) {
2418 struct task_struct
*t
= task
;
2420 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2421 while_each_thread(task
, t
)
2422 task_io_accounting_add(&acct
, &t
->ioac
);
2424 unlock_task_sighand(task
, &flags
);
2426 return sprintf(buffer
,
2431 "read_bytes: %llu\n"
2432 "write_bytes: %llu\n"
2433 "cancelled_write_bytes: %llu\n",
2434 (unsigned long long)acct
.rchar
,
2435 (unsigned long long)acct
.wchar
,
2436 (unsigned long long)acct
.syscr
,
2437 (unsigned long long)acct
.syscw
,
2438 (unsigned long long)acct
.read_bytes
,
2439 (unsigned long long)acct
.write_bytes
,
2440 (unsigned long long)acct
.cancelled_write_bytes
);
2443 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2445 return do_io_accounting(task
, buffer
, 0);
2448 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2450 return do_io_accounting(task
, buffer
, 1);
2452 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2454 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2455 struct pid
*pid
, struct task_struct
*task
)
2457 seq_printf(m
, "%08x\n", task
->personality
);
2464 static const struct file_operations proc_task_operations
;
2465 static const struct inode_operations proc_task_inode_operations
;
2467 static const struct pid_entry tgid_base_stuff
[] = {
2468 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2469 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2470 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2472 DIR("net", S_IRUGO
|S_IXUGO
, net
),
2474 REG("environ", S_IRUSR
, environ
),
2475 INF("auxv", S_IRUSR
, pid_auxv
),
2476 ONE("status", S_IRUGO
, pid_status
),
2477 ONE("personality", S_IRUSR
, pid_personality
),
2478 INF("limits", S_IRUSR
, pid_limits
),
2479 #ifdef CONFIG_SCHED_DEBUG
2480 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2482 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2483 INF("syscall", S_IRUSR
, pid_syscall
),
2485 INF("cmdline", S_IRUGO
, pid_cmdline
),
2486 ONE("stat", S_IRUGO
, tgid_stat
),
2487 ONE("statm", S_IRUGO
, pid_statm
),
2488 REG("maps", S_IRUGO
, maps
),
2490 REG("numa_maps", S_IRUGO
, numa_maps
),
2492 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2496 REG("mounts", S_IRUGO
, mounts
),
2497 REG("mountinfo", S_IRUGO
, mountinfo
),
2498 REG("mountstats", S_IRUSR
, mountstats
),
2499 #ifdef CONFIG_PROC_PAGE_MONITOR
2500 REG("clear_refs", S_IWUSR
, clear_refs
),
2501 REG("smaps", S_IRUGO
, smaps
),
2502 REG("pagemap", S_IRUSR
, pagemap
),
2504 #ifdef CONFIG_SECURITY
2505 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2507 #ifdef CONFIG_KALLSYMS
2508 INF("wchan", S_IRUGO
, pid_wchan
),
2510 #ifdef CONFIG_SCHEDSTATS
2511 INF("schedstat", S_IRUGO
, pid_schedstat
),
2513 #ifdef CONFIG_LATENCYTOP
2514 REG("latency", S_IRUGO
, lstats
),
2516 #ifdef CONFIG_PROC_PID_CPUSET
2517 REG("cpuset", S_IRUGO
, cpuset
),
2519 #ifdef CONFIG_CGROUPS
2520 REG("cgroup", S_IRUGO
, cgroup
),
2522 INF("oom_score", S_IRUGO
, oom_score
),
2523 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2524 #ifdef CONFIG_AUDITSYSCALL
2525 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2526 REG("sessionid", S_IRUGO
, sessionid
),
2528 #ifdef CONFIG_FAULT_INJECTION
2529 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2531 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2532 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2534 #ifdef CONFIG_TASK_IO_ACCOUNTING
2535 INF("io", S_IRUGO
, tgid_io_accounting
),
2539 static int proc_tgid_base_readdir(struct file
* filp
,
2540 void * dirent
, filldir_t filldir
)
2542 return proc_pident_readdir(filp
,dirent
,filldir
,
2543 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2546 static const struct file_operations proc_tgid_base_operations
= {
2547 .read
= generic_read_dir
,
2548 .readdir
= proc_tgid_base_readdir
,
2551 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2552 return proc_pident_lookup(dir
, dentry
,
2553 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2556 static const struct inode_operations proc_tgid_base_inode_operations
= {
2557 .lookup
= proc_tgid_base_lookup
,
2558 .getattr
= pid_getattr
,
2559 .setattr
= proc_setattr
,
2562 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2564 struct dentry
*dentry
, *leader
, *dir
;
2565 char buf
[PROC_NUMBUF
];
2569 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2570 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2572 if (!(current
->flags
& PF_EXITING
))
2573 shrink_dcache_parent(dentry
);
2582 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2583 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2588 name
.len
= strlen(name
.name
);
2589 dir
= d_hash_and_lookup(leader
, &name
);
2591 goto out_put_leader
;
2594 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2595 dentry
= d_hash_and_lookup(dir
, &name
);
2597 shrink_dcache_parent(dentry
);
2610 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2611 * @task: task that should be flushed.
2613 * When flushing dentries from proc, one needs to flush them from global
2614 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2615 * in. This call is supposed to do all of this job.
2617 * Looks in the dcache for
2619 * /proc/@tgid/task/@pid
2620 * if either directory is present flushes it and all of it'ts children
2623 * It is safe and reasonable to cache /proc entries for a task until
2624 * that task exits. After that they just clog up the dcache with
2625 * useless entries, possibly causing useful dcache entries to be
2626 * flushed instead. This routine is proved to flush those useless
2627 * dcache entries at process exit time.
2629 * NOTE: This routine is just an optimization so it does not guarantee
2630 * that no dcache entries will exist at process exit time it
2631 * just makes it very unlikely that any will persist.
2634 void proc_flush_task(struct task_struct
*task
)
2637 struct pid
*pid
, *tgid
= NULL
;
2640 pid
= task_pid(task
);
2641 if (thread_group_leader(task
))
2642 tgid
= task_tgid(task
);
2644 for (i
= 0; i
<= pid
->level
; i
++) {
2645 upid
= &pid
->numbers
[i
];
2646 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2647 tgid
? tgid
->numbers
[i
].nr
: 0);
2650 upid
= &pid
->numbers
[pid
->level
];
2652 pid_ns_release_proc(upid
->ns
);
2655 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2656 struct dentry
* dentry
,
2657 struct task_struct
*task
, const void *ptr
)
2659 struct dentry
*error
= ERR_PTR(-ENOENT
);
2660 struct inode
*inode
;
2662 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2666 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2667 inode
->i_op
= &proc_tgid_base_inode_operations
;
2668 inode
->i_fop
= &proc_tgid_base_operations
;
2669 inode
->i_flags
|=S_IMMUTABLE
;
2671 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2672 ARRAY_SIZE(tgid_base_stuff
));
2674 dentry
->d_op
= &pid_dentry_operations
;
2676 d_add(dentry
, inode
);
2677 /* Close the race of the process dying before we return the dentry */
2678 if (pid_revalidate(dentry
, NULL
))
2684 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2686 struct dentry
*result
= ERR_PTR(-ENOENT
);
2687 struct task_struct
*task
;
2689 struct pid_namespace
*ns
;
2691 result
= proc_base_lookup(dir
, dentry
);
2692 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2695 tgid
= name_to_int(dentry
);
2699 ns
= dentry
->d_sb
->s_fs_info
;
2701 task
= find_task_by_pid_ns(tgid
, ns
);
2703 get_task_struct(task
);
2708 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2709 put_task_struct(task
);
2715 * Find the first task with tgid >= tgid
2720 struct task_struct
*task
;
2722 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2727 put_task_struct(iter
.task
);
2731 pid
= find_ge_pid(iter
.tgid
, ns
);
2733 iter
.tgid
= pid_nr_ns(pid
, ns
);
2734 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2735 /* What we to know is if the pid we have find is the
2736 * pid of a thread_group_leader. Testing for task
2737 * being a thread_group_leader is the obvious thing
2738 * todo but there is a window when it fails, due to
2739 * the pid transfer logic in de_thread.
2741 * So we perform the straight forward test of seeing
2742 * if the pid we have found is the pid of a thread
2743 * group leader, and don't worry if the task we have
2744 * found doesn't happen to be a thread group leader.
2745 * As we don't care in the case of readdir.
2747 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2751 get_task_struct(iter
.task
);
2757 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2759 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2760 struct tgid_iter iter
)
2762 char name
[PROC_NUMBUF
];
2763 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2764 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2765 proc_pid_instantiate
, iter
.task
, NULL
);
2768 /* for the /proc/ directory itself, after non-process stuff has been done */
2769 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2771 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2772 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2773 struct tgid_iter iter
;
2774 struct pid_namespace
*ns
;
2779 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2780 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2781 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2785 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2787 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2788 for (iter
= next_tgid(ns
, iter
);
2790 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2791 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2792 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2793 put_task_struct(iter
.task
);
2797 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2799 put_task_struct(reaper
);
2807 static const struct pid_entry tid_base_stuff
[] = {
2808 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2809 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2810 REG("environ", S_IRUSR
, environ
),
2811 INF("auxv", S_IRUSR
, pid_auxv
),
2812 ONE("status", S_IRUGO
, pid_status
),
2813 ONE("personality", S_IRUSR
, pid_personality
),
2814 INF("limits", S_IRUSR
, pid_limits
),
2815 #ifdef CONFIG_SCHED_DEBUG
2816 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2818 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2819 INF("syscall", S_IRUSR
, pid_syscall
),
2821 INF("cmdline", S_IRUGO
, pid_cmdline
),
2822 ONE("stat", S_IRUGO
, tid_stat
),
2823 ONE("statm", S_IRUGO
, pid_statm
),
2824 REG("maps", S_IRUGO
, maps
),
2826 REG("numa_maps", S_IRUGO
, numa_maps
),
2828 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2832 REG("mounts", S_IRUGO
, mounts
),
2833 REG("mountinfo", S_IRUGO
, mountinfo
),
2834 #ifdef CONFIG_PROC_PAGE_MONITOR
2835 REG("clear_refs", S_IWUSR
, clear_refs
),
2836 REG("smaps", S_IRUGO
, smaps
),
2837 REG("pagemap", S_IRUSR
, pagemap
),
2839 #ifdef CONFIG_SECURITY
2840 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2842 #ifdef CONFIG_KALLSYMS
2843 INF("wchan", S_IRUGO
, pid_wchan
),
2845 #ifdef CONFIG_SCHEDSTATS
2846 INF("schedstat", S_IRUGO
, pid_schedstat
),
2848 #ifdef CONFIG_LATENCYTOP
2849 REG("latency", S_IRUGO
, lstats
),
2851 #ifdef CONFIG_PROC_PID_CPUSET
2852 REG("cpuset", S_IRUGO
, cpuset
),
2854 #ifdef CONFIG_CGROUPS
2855 REG("cgroup", S_IRUGO
, cgroup
),
2857 INF("oom_score", S_IRUGO
, oom_score
),
2858 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2859 #ifdef CONFIG_AUDITSYSCALL
2860 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2861 REG("sessionid", S_IRUSR
, sessionid
),
2863 #ifdef CONFIG_FAULT_INJECTION
2864 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2866 #ifdef CONFIG_TASK_IO_ACCOUNTING
2867 INF("io", S_IRUGO
, tid_io_accounting
),
2871 static int proc_tid_base_readdir(struct file
* filp
,
2872 void * dirent
, filldir_t filldir
)
2874 return proc_pident_readdir(filp
,dirent
,filldir
,
2875 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2878 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2879 return proc_pident_lookup(dir
, dentry
,
2880 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2883 static const struct file_operations proc_tid_base_operations
= {
2884 .read
= generic_read_dir
,
2885 .readdir
= proc_tid_base_readdir
,
2888 static const struct inode_operations proc_tid_base_inode_operations
= {
2889 .lookup
= proc_tid_base_lookup
,
2890 .getattr
= pid_getattr
,
2891 .setattr
= proc_setattr
,
2894 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2895 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2897 struct dentry
*error
= ERR_PTR(-ENOENT
);
2898 struct inode
*inode
;
2899 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2903 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2904 inode
->i_op
= &proc_tid_base_inode_operations
;
2905 inode
->i_fop
= &proc_tid_base_operations
;
2906 inode
->i_flags
|=S_IMMUTABLE
;
2908 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2909 ARRAY_SIZE(tid_base_stuff
));
2911 dentry
->d_op
= &pid_dentry_operations
;
2913 d_add(dentry
, inode
);
2914 /* Close the race of the process dying before we return the dentry */
2915 if (pid_revalidate(dentry
, NULL
))
2921 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2923 struct dentry
*result
= ERR_PTR(-ENOENT
);
2924 struct task_struct
*task
;
2925 struct task_struct
*leader
= get_proc_task(dir
);
2927 struct pid_namespace
*ns
;
2932 tid
= name_to_int(dentry
);
2936 ns
= dentry
->d_sb
->s_fs_info
;
2938 task
= find_task_by_pid_ns(tid
, ns
);
2940 get_task_struct(task
);
2944 if (!same_thread_group(leader
, task
))
2947 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2949 put_task_struct(task
);
2951 put_task_struct(leader
);
2957 * Find the first tid of a thread group to return to user space.
2959 * Usually this is just the thread group leader, but if the users
2960 * buffer was too small or there was a seek into the middle of the
2961 * directory we have more work todo.
2963 * In the case of a short read we start with find_task_by_pid.
2965 * In the case of a seek we start with the leader and walk nr
2968 static struct task_struct
*first_tid(struct task_struct
*leader
,
2969 int tid
, int nr
, struct pid_namespace
*ns
)
2971 struct task_struct
*pos
;
2974 /* Attempt to start with the pid of a thread */
2975 if (tid
&& (nr
> 0)) {
2976 pos
= find_task_by_pid_ns(tid
, ns
);
2977 if (pos
&& (pos
->group_leader
== leader
))
2981 /* If nr exceeds the number of threads there is nothing todo */
2983 if (nr
&& nr
>= get_nr_threads(leader
))
2986 /* If we haven't found our starting place yet start
2987 * with the leader and walk nr threads forward.
2989 for (pos
= leader
; nr
> 0; --nr
) {
2990 pos
= next_thread(pos
);
2991 if (pos
== leader
) {
2997 get_task_struct(pos
);
3004 * Find the next thread in the thread list.
3005 * Return NULL if there is an error or no next thread.
3007 * The reference to the input task_struct is released.
3009 static struct task_struct
*next_tid(struct task_struct
*start
)
3011 struct task_struct
*pos
= NULL
;
3013 if (pid_alive(start
)) {
3014 pos
= next_thread(start
);
3015 if (thread_group_leader(pos
))
3018 get_task_struct(pos
);
3021 put_task_struct(start
);
3025 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3026 struct task_struct
*task
, int tid
)
3028 char name
[PROC_NUMBUF
];
3029 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3030 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3031 proc_task_instantiate
, task
, NULL
);
3034 /* for the /proc/TGID/task/ directories */
3035 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3037 struct dentry
*dentry
= filp
->f_path
.dentry
;
3038 struct inode
*inode
= dentry
->d_inode
;
3039 struct task_struct
*leader
= NULL
;
3040 struct task_struct
*task
;
3041 int retval
= -ENOENT
;
3044 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
3045 struct pid_namespace
*ns
;
3047 task
= get_proc_task(inode
);
3051 if (pid_alive(task
)) {
3052 leader
= task
->group_leader
;
3053 get_task_struct(leader
);
3056 put_task_struct(task
);
3064 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
3069 ino
= parent_ino(dentry
);
3070 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
3076 /* f_version caches the tgid value that the last readdir call couldn't
3077 * return. lseek aka telldir automagically resets f_version to 0.
3079 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3080 tid
= (int)filp
->f_version
;
3081 filp
->f_version
= 0;
3082 for (task
= first_tid(leader
, tid
, pos
- 2, ns
);
3084 task
= next_tid(task
), pos
++) {
3085 tid
= task_pid_nr_ns(task
, ns
);
3086 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3087 /* returning this tgid failed, save it as the first
3088 * pid for the next readir call */
3089 filp
->f_version
= (u64
)tid
;
3090 put_task_struct(task
);
3096 put_task_struct(leader
);
3101 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3103 struct inode
*inode
= dentry
->d_inode
;
3104 struct task_struct
*p
= get_proc_task(inode
);
3105 generic_fillattr(inode
, stat
);
3108 stat
->nlink
+= get_nr_threads(p
);
3115 static const struct inode_operations proc_task_inode_operations
= {
3116 .lookup
= proc_task_lookup
,
3117 .getattr
= proc_task_getattr
,
3118 .setattr
= proc_setattr
,
3121 static const struct file_operations proc_task_operations
= {
3122 .read
= generic_read_dir
,
3123 .readdir
= proc_task_readdir
,