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/stacktrace.h>
69 #include <linux/resource.h>
70 #include <linux/module.h>
71 #include <linux/mount.h>
72 #include <linux/security.h>
73 #include <linux/ptrace.h>
74 #include <linux/tracehook.h>
75 #include <linux/cgroup.h>
76 #include <linux/cpuset.h>
77 #include <linux/audit.h>
78 #include <linux/poll.h>
79 #include <linux/nsproxy.h>
80 #include <linux/oom.h>
81 #include <linux/elf.h>
82 #include <linux/pid_namespace.h>
83 #include <linux/fs_struct.h>
87 * Implementing inode permission operations in /proc is almost
88 * certainly an error. Permission checks need to happen during
89 * each system call not at open time. The reason is that most of
90 * what we wish to check for permissions in /proc varies at runtime.
92 * The classic example of a problem is opening file descriptors
93 * in /proc for a task before it execs a suid executable.
100 const struct inode_operations
*iop
;
101 const struct file_operations
*fop
;
105 #define NOD(NAME, MODE, IOP, FOP, OP) { \
107 .len = sizeof(NAME) - 1, \
114 #define DIR(NAME, MODE, iops, fops) \
115 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
116 #define LNK(NAME, get_link) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = get_link } )
120 #define REG(NAME, MODE, fops) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
122 #define INF(NAME, MODE, read) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = read } )
126 #define ONE(NAME, MODE, show) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = show } )
132 * Count the number of hardlinks for the pid_entry table, excluding the .
135 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
142 for (i
= 0; i
< n
; ++i
) {
143 if (S_ISDIR(entries
[i
].mode
))
150 static int get_fs_path(struct task_struct
*task
, struct path
*path
, bool root
)
152 struct fs_struct
*fs
;
153 int result
= -ENOENT
;
158 read_lock(&fs
->lock
);
159 *path
= root
? fs
->root
: fs
->pwd
;
161 read_unlock(&fs
->lock
);
168 static int get_nr_threads(struct task_struct
*tsk
)
173 if (lock_task_sighand(tsk
, &flags
)) {
174 count
= atomic_read(&tsk
->signal
->count
);
175 unlock_task_sighand(tsk
, &flags
);
180 static int proc_cwd_link(struct inode
*inode
, struct path
*path
)
182 struct task_struct
*task
= get_proc_task(inode
);
183 int result
= -ENOENT
;
186 result
= get_fs_path(task
, path
, 0);
187 put_task_struct(task
);
192 static int proc_root_link(struct inode
*inode
, struct path
*path
)
194 struct task_struct
*task
= get_proc_task(inode
);
195 int result
= -ENOENT
;
198 result
= get_fs_path(task
, path
, 1);
199 put_task_struct(task
);
205 * Return zero if current may access user memory in @task, -error if not.
207 static int check_mem_permission(struct task_struct
*task
)
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
220 if (task_is_stopped_or_traced(task
)) {
223 match
= (tracehook_tracer_task(task
) == current
);
225 if (match
&& ptrace_may_access(task
, PTRACE_MODE_ATTACH
))
230 * Noone else is allowed.
235 struct mm_struct
*mm_for_maps(struct task_struct
*task
)
237 struct mm_struct
*mm
;
239 if (mutex_lock_killable(&task
->cred_guard_mutex
))
242 mm
= get_task_mm(task
);
243 if (mm
&& mm
!= current
->mm
&&
244 !ptrace_may_access(task
, PTRACE_MODE_READ
)) {
248 mutex_unlock(&task
->cred_guard_mutex
);
253 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
257 struct mm_struct
*mm
= get_task_mm(task
);
261 goto out_mm
; /* Shh! No looking before we're done */
263 len
= mm
->arg_end
- mm
->arg_start
;
268 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
270 // If the nul at the end of args has been overwritten, then
271 // assume application is using setproctitle(3).
272 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
273 len
= strnlen(buffer
, res
);
277 len
= mm
->env_end
- mm
->env_start
;
278 if (len
> PAGE_SIZE
- res
)
279 len
= PAGE_SIZE
- res
;
280 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
281 res
= strnlen(buffer
, res
);
290 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
293 struct mm_struct
*mm
= get_task_mm(task
);
295 unsigned int nwords
= 0;
298 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
299 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
302 memcpy(buffer
, mm
->saved_auxv
, res
);
309 #ifdef CONFIG_KALLSYMS
311 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
312 * Returns the resolved symbol. If that fails, simply return the address.
314 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
317 char symname
[KSYM_NAME_LEN
];
319 wchan
= get_wchan(task
);
321 if (lookup_symbol_name(wchan
, symname
) < 0)
322 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
325 return sprintf(buffer
, "%lu", wchan
);
327 return sprintf(buffer
, "%s", symname
);
329 #endif /* CONFIG_KALLSYMS */
331 static int lock_trace(struct task_struct
*task
)
333 int err
= mutex_lock_killable(&task
->cred_guard_mutex
);
336 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
337 mutex_unlock(&task
->cred_guard_mutex
);
343 static void unlock_trace(struct task_struct
*task
)
345 mutex_unlock(&task
->cred_guard_mutex
);
348 #ifdef CONFIG_STACKTRACE
350 #define MAX_STACK_TRACE_DEPTH 64
352 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
353 struct pid
*pid
, struct task_struct
*task
)
355 struct stack_trace trace
;
356 unsigned long *entries
;
360 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
364 trace
.nr_entries
= 0;
365 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
366 trace
.entries
= entries
;
369 err
= lock_trace(task
);
371 save_stack_trace_tsk(task
, &trace
);
373 for (i
= 0; i
< trace
.nr_entries
; i
++) {
374 seq_printf(m
, "[<%p>] %pS\n",
375 (void *)entries
[i
], (void *)entries
[i
]);
385 #ifdef CONFIG_SCHEDSTATS
387 * Provides /proc/PID/schedstat
389 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
391 return sprintf(buffer
, "%llu %llu %lu\n",
392 (unsigned long long)task
->se
.sum_exec_runtime
,
393 (unsigned long long)task
->sched_info
.run_delay
,
394 task
->sched_info
.pcount
);
398 #ifdef CONFIG_LATENCYTOP
399 static int lstats_show_proc(struct seq_file
*m
, void *v
)
402 struct inode
*inode
= m
->private;
403 struct task_struct
*task
= get_proc_task(inode
);
407 seq_puts(m
, "Latency Top version : v0.1\n");
408 for (i
= 0; i
< 32; i
++) {
409 if (task
->latency_record
[i
].backtrace
[0]) {
411 seq_printf(m
, "%i %li %li ",
412 task
->latency_record
[i
].count
,
413 task
->latency_record
[i
].time
,
414 task
->latency_record
[i
].max
);
415 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
416 char sym
[KSYM_SYMBOL_LEN
];
418 if (!task
->latency_record
[i
].backtrace
[q
])
420 if (task
->latency_record
[i
].backtrace
[q
] == ULONG_MAX
)
422 sprint_symbol(sym
, task
->latency_record
[i
].backtrace
[q
]);
423 c
= strchr(sym
, '+');
426 seq_printf(m
, "%s ", sym
);
432 put_task_struct(task
);
436 static int lstats_open(struct inode
*inode
, struct file
*file
)
438 return single_open(file
, lstats_show_proc
, inode
);
441 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
442 size_t count
, loff_t
*offs
)
444 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
448 clear_all_latency_tracing(task
);
449 put_task_struct(task
);
454 static const struct file_operations proc_lstats_operations
= {
457 .write
= lstats_write
,
459 .release
= single_release
,
464 /* The badness from the OOM killer */
465 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
466 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
468 unsigned long points
= 0;
469 struct timespec uptime
;
471 do_posix_clock_monotonic_gettime(&uptime
);
472 read_lock(&tasklist_lock
);
474 points
= badness(task
, uptime
.tv_sec
);
475 read_unlock(&tasklist_lock
);
476 return sprintf(buffer
, "%lu\n", points
);
484 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
485 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
486 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
487 [RLIMIT_DATA
] = {"Max data size", "bytes"},
488 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
489 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
490 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
491 [RLIMIT_NPROC
] = {"Max processes", "processes"},
492 [RLIMIT_NOFILE
] = {"Max open files", "files"},
493 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
494 [RLIMIT_AS
] = {"Max address space", "bytes"},
495 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
496 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
497 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
498 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
499 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
500 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
503 /* Display limits for a process */
504 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
509 char *bufptr
= buffer
;
511 struct rlimit rlim
[RLIM_NLIMITS
];
513 if (!lock_task_sighand(task
, &flags
))
515 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
516 unlock_task_sighand(task
, &flags
);
519 * print the file header
521 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
522 "Limit", "Soft Limit", "Hard Limit", "Units");
524 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
525 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
526 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
527 lnames
[i
].name
, "unlimited");
529 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
530 lnames
[i
].name
, rlim
[i
].rlim_cur
);
532 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
533 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
535 count
+= sprintf(&bufptr
[count
], "%-20lu ",
539 count
+= sprintf(&bufptr
[count
], "%-10s\n",
542 count
+= sprintf(&bufptr
[count
], "\n");
548 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
549 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
552 unsigned long args
[6], sp
, pc
;
553 int res
= lock_trace(task
);
557 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
558 res
= sprintf(buffer
, "running\n");
560 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
562 res
= sprintf(buffer
,
563 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
565 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
570 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
572 /************************************************************************/
573 /* Here the fs part begins */
574 /************************************************************************/
576 /* permission checks */
577 static int proc_fd_access_allowed(struct inode
*inode
)
579 struct task_struct
*task
;
581 /* Allow access to a task's file descriptors if it is us or we
582 * may use ptrace attach to the process and find out that
585 task
= get_proc_task(inode
);
587 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
588 put_task_struct(task
);
593 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
596 struct inode
*inode
= dentry
->d_inode
;
598 if (attr
->ia_valid
& ATTR_MODE
)
601 error
= inode_change_ok(inode
, attr
);
603 error
= inode_setattr(inode
, attr
);
607 static const struct inode_operations proc_def_inode_operations
= {
608 .setattr
= proc_setattr
,
611 static int mounts_open_common(struct inode
*inode
, struct file
*file
,
612 const struct seq_operations
*op
)
614 struct task_struct
*task
= get_proc_task(inode
);
616 struct mnt_namespace
*ns
= NULL
;
618 struct proc_mounts
*p
;
623 nsp
= task_nsproxy(task
);
630 if (ns
&& get_fs_path(task
, &root
, 1) == 0)
632 put_task_struct(task
);
641 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
645 file
->private_data
= &p
->m
;
646 ret
= seq_open(file
, op
);
653 p
->event
= ns
->event
;
667 static int mounts_release(struct inode
*inode
, struct file
*file
)
669 struct proc_mounts
*p
= file
->private_data
;
672 return seq_release(inode
, file
);
675 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
677 struct proc_mounts
*p
= file
->private_data
;
678 struct mnt_namespace
*ns
= p
->ns
;
679 unsigned res
= POLLIN
| POLLRDNORM
;
681 poll_wait(file
, &ns
->poll
, wait
);
683 spin_lock(&vfsmount_lock
);
684 if (p
->event
!= ns
->event
) {
685 p
->event
= ns
->event
;
686 res
|= POLLERR
| POLLPRI
;
688 spin_unlock(&vfsmount_lock
);
693 static int mounts_open(struct inode
*inode
, struct file
*file
)
695 return mounts_open_common(inode
, file
, &mounts_op
);
698 static const struct file_operations proc_mounts_operations
= {
702 .release
= mounts_release
,
706 static int mountinfo_open(struct inode
*inode
, struct file
*file
)
708 return mounts_open_common(inode
, file
, &mountinfo_op
);
711 static const struct file_operations proc_mountinfo_operations
= {
712 .open
= mountinfo_open
,
715 .release
= mounts_release
,
719 static int mountstats_open(struct inode
*inode
, struct file
*file
)
721 return mounts_open_common(inode
, file
, &mountstats_op
);
724 static const struct file_operations proc_mountstats_operations
= {
725 .open
= mountstats_open
,
728 .release
= mounts_release
,
731 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
733 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
734 size_t count
, loff_t
*ppos
)
736 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
739 struct task_struct
*task
= get_proc_task(inode
);
745 if (count
> PROC_BLOCK_SIZE
)
746 count
= PROC_BLOCK_SIZE
;
749 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
752 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
755 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
758 put_task_struct(task
);
763 static const struct file_operations proc_info_file_operations
= {
764 .read
= proc_info_read
,
767 static int proc_single_show(struct seq_file
*m
, void *v
)
769 struct inode
*inode
= m
->private;
770 struct pid_namespace
*ns
;
772 struct task_struct
*task
;
775 ns
= inode
->i_sb
->s_fs_info
;
776 pid
= proc_pid(inode
);
777 task
= get_pid_task(pid
, PIDTYPE_PID
);
781 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
783 put_task_struct(task
);
787 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
790 ret
= single_open(filp
, proc_single_show
, NULL
);
792 struct seq_file
*m
= filp
->private_data
;
799 static const struct file_operations proc_single_file_operations
= {
800 .open
= proc_single_open
,
803 .release
= single_release
,
806 static int mem_open(struct inode
* inode
, struct file
* file
)
808 file
->private_data
= (void*)((long)current
->self_exec_id
);
812 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
813 size_t count
, loff_t
*ppos
)
815 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
817 unsigned long src
= *ppos
;
819 struct mm_struct
*mm
;
824 if (check_mem_permission(task
))
828 page
= (char *)__get_free_page(GFP_TEMPORARY
);
834 mm
= get_task_mm(task
);
840 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
846 int this_len
, retval
;
848 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
849 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
850 if (!retval
|| check_mem_permission(task
)) {
856 if (copy_to_user(buf
, page
, retval
)) {
871 free_page((unsigned long) page
);
873 put_task_struct(task
);
878 #define mem_write NULL
881 /* This is a security hazard */
882 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
883 size_t count
, loff_t
*ppos
)
887 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
888 unsigned long dst
= *ppos
;
894 if (check_mem_permission(task
))
898 page
= (char *)__get_free_page(GFP_TEMPORARY
);
904 int this_len
, retval
;
906 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
907 if (copy_from_user(page
, buf
, this_len
)) {
911 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
923 free_page((unsigned long) page
);
925 put_task_struct(task
);
931 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
935 file
->f_pos
= offset
;
938 file
->f_pos
+= offset
;
943 force_successful_syscall_return();
947 static const struct file_operations proc_mem_operations
= {
954 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
955 size_t count
, loff_t
*ppos
)
957 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
959 unsigned long src
= *ppos
;
961 struct mm_struct
*mm
;
966 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
970 page
= (char *)__get_free_page(GFP_TEMPORARY
);
976 mm
= get_task_mm(task
);
981 int this_len
, retval
, max_len
;
983 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
988 max_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
989 this_len
= (this_len
> max_len
) ? max_len
: this_len
;
991 retval
= access_process_vm(task
, (mm
->env_start
+ src
),
999 if (copy_to_user(buf
, page
, retval
)) {
1013 free_page((unsigned long) page
);
1015 put_task_struct(task
);
1020 static const struct file_operations proc_environ_operations
= {
1021 .read
= environ_read
,
1024 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
1025 size_t count
, loff_t
*ppos
)
1027 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1028 char buffer
[PROC_NUMBUF
];
1030 int oom_adjust
= OOM_DISABLE
;
1031 unsigned long flags
;
1036 if (lock_task_sighand(task
, &flags
)) {
1037 oom_adjust
= task
->signal
->oom_adj
;
1038 unlock_task_sighand(task
, &flags
);
1041 put_task_struct(task
);
1043 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
1045 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1048 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
1049 size_t count
, loff_t
*ppos
)
1051 struct task_struct
*task
;
1052 char buffer
[PROC_NUMBUF
];
1054 unsigned long flags
;
1057 memset(buffer
, 0, sizeof(buffer
));
1058 if (count
> sizeof(buffer
) - 1)
1059 count
= sizeof(buffer
) - 1;
1060 if (copy_from_user(buffer
, buf
, count
))
1063 err
= strict_strtol(strstrip(buffer
), 0, &oom_adjust
);
1066 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
1067 oom_adjust
!= OOM_DISABLE
)
1070 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1073 if (!lock_task_sighand(task
, &flags
)) {
1074 put_task_struct(task
);
1078 if (oom_adjust
< task
->signal
->oom_adj
&& !capable(CAP_SYS_RESOURCE
)) {
1079 unlock_task_sighand(task
, &flags
);
1080 put_task_struct(task
);
1084 task
->signal
->oom_adj
= oom_adjust
;
1086 unlock_task_sighand(task
, &flags
);
1087 put_task_struct(task
);
1092 static const struct file_operations proc_oom_adjust_operations
= {
1093 .read
= oom_adjust_read
,
1094 .write
= oom_adjust_write
,
1097 #ifdef CONFIG_AUDITSYSCALL
1098 #define TMPBUFLEN 21
1099 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1100 size_t count
, loff_t
*ppos
)
1102 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1103 struct task_struct
*task
= get_proc_task(inode
);
1105 char tmpbuf
[TMPBUFLEN
];
1109 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1110 audit_get_loginuid(task
));
1111 put_task_struct(task
);
1112 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1115 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1116 size_t count
, loff_t
*ppos
)
1118 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1123 if (!capable(CAP_AUDIT_CONTROL
))
1126 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
1129 if (count
>= PAGE_SIZE
)
1130 count
= PAGE_SIZE
- 1;
1133 /* No partial writes. */
1136 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1140 if (copy_from_user(page
, buf
, count
))
1144 loginuid
= simple_strtoul(page
, &tmp
, 10);
1150 length
= audit_set_loginuid(current
, loginuid
);
1151 if (likely(length
== 0))
1155 free_page((unsigned long) page
);
1159 static const struct file_operations proc_loginuid_operations
= {
1160 .read
= proc_loginuid_read
,
1161 .write
= proc_loginuid_write
,
1164 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1165 size_t count
, loff_t
*ppos
)
1167 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1168 struct task_struct
*task
= get_proc_task(inode
);
1170 char tmpbuf
[TMPBUFLEN
];
1174 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1175 audit_get_sessionid(task
));
1176 put_task_struct(task
);
1177 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1180 static const struct file_operations proc_sessionid_operations
= {
1181 .read
= proc_sessionid_read
,
1185 #ifdef CONFIG_FAULT_INJECTION
1186 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1187 size_t count
, loff_t
*ppos
)
1189 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1190 char buffer
[PROC_NUMBUF
];
1196 make_it_fail
= task
->make_it_fail
;
1197 put_task_struct(task
);
1199 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1201 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1204 static ssize_t
proc_fault_inject_write(struct file
* file
,
1205 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1207 struct task_struct
*task
;
1208 char buffer
[PROC_NUMBUF
], *end
;
1211 if (!capable(CAP_SYS_RESOURCE
))
1213 memset(buffer
, 0, sizeof(buffer
));
1214 if (count
> sizeof(buffer
) - 1)
1215 count
= sizeof(buffer
) - 1;
1216 if (copy_from_user(buffer
, buf
, count
))
1218 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1221 task
= get_proc_task(file
->f_dentry
->d_inode
);
1224 task
->make_it_fail
= make_it_fail
;
1225 put_task_struct(task
);
1230 static const struct file_operations proc_fault_inject_operations
= {
1231 .read
= proc_fault_inject_read
,
1232 .write
= proc_fault_inject_write
,
1237 #ifdef CONFIG_SCHED_DEBUG
1239 * Print out various scheduling related per-task fields:
1241 static int sched_show(struct seq_file
*m
, void *v
)
1243 struct inode
*inode
= m
->private;
1244 struct task_struct
*p
;
1246 p
= get_proc_task(inode
);
1249 proc_sched_show_task(p
, m
);
1257 sched_write(struct file
*file
, const char __user
*buf
,
1258 size_t count
, loff_t
*offset
)
1260 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1261 struct task_struct
*p
;
1263 p
= get_proc_task(inode
);
1266 proc_sched_set_task(p
);
1273 static int sched_open(struct inode
*inode
, struct file
*filp
)
1277 ret
= single_open(filp
, sched_show
, NULL
);
1279 struct seq_file
*m
= filp
->private_data
;
1286 static const struct file_operations proc_pid_sched_operations
= {
1289 .write
= sched_write
,
1290 .llseek
= seq_lseek
,
1291 .release
= single_release
,
1297 * We added or removed a vma mapping the executable. The vmas are only mapped
1298 * during exec and are not mapped with the mmap system call.
1299 * Callers must hold down_write() on the mm's mmap_sem for these
1301 void added_exe_file_vma(struct mm_struct
*mm
)
1303 mm
->num_exe_file_vmas
++;
1306 void removed_exe_file_vma(struct mm_struct
*mm
)
1308 mm
->num_exe_file_vmas
--;
1309 if ((mm
->num_exe_file_vmas
== 0) && mm
->exe_file
){
1311 mm
->exe_file
= NULL
;
1316 void set_mm_exe_file(struct mm_struct
*mm
, struct file
*new_exe_file
)
1319 get_file(new_exe_file
);
1322 mm
->exe_file
= new_exe_file
;
1323 mm
->num_exe_file_vmas
= 0;
1326 struct file
*get_mm_exe_file(struct mm_struct
*mm
)
1328 struct file
*exe_file
;
1330 /* We need mmap_sem to protect against races with removal of
1331 * VM_EXECUTABLE vmas */
1332 down_read(&mm
->mmap_sem
);
1333 exe_file
= mm
->exe_file
;
1336 up_read(&mm
->mmap_sem
);
1340 void dup_mm_exe_file(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
1342 /* It's safe to write the exe_file pointer without exe_file_lock because
1343 * this is called during fork when the task is not yet in /proc */
1344 newmm
->exe_file
= get_mm_exe_file(oldmm
);
1347 static int proc_exe_link(struct inode
*inode
, struct path
*exe_path
)
1349 struct task_struct
*task
;
1350 struct mm_struct
*mm
;
1351 struct file
*exe_file
;
1353 task
= get_proc_task(inode
);
1356 mm
= get_task_mm(task
);
1357 put_task_struct(task
);
1360 exe_file
= get_mm_exe_file(mm
);
1363 *exe_path
= exe_file
->f_path
;
1364 path_get(&exe_file
->f_path
);
1371 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1373 struct inode
*inode
= dentry
->d_inode
;
1374 int error
= -EACCES
;
1376 /* We don't need a base pointer in the /proc filesystem */
1377 path_put(&nd
->path
);
1379 /* Are we allowed to snoop on the tasks file descriptors? */
1380 if (!proc_fd_access_allowed(inode
))
1383 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->path
);
1384 nd
->last_type
= LAST_BIND
;
1386 return ERR_PTR(error
);
1389 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1391 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1398 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1399 len
= PTR_ERR(pathname
);
1400 if (IS_ERR(pathname
))
1402 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1406 if (copy_to_user(buffer
, pathname
, len
))
1409 free_page((unsigned long)tmp
);
1413 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1415 int error
= -EACCES
;
1416 struct inode
*inode
= dentry
->d_inode
;
1419 /* Are we allowed to snoop on the tasks file descriptors? */
1420 if (!proc_fd_access_allowed(inode
))
1423 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &path
);
1427 error
= do_proc_readlink(&path
, buffer
, buflen
);
1433 static const struct inode_operations proc_pid_link_inode_operations
= {
1434 .readlink
= proc_pid_readlink
,
1435 .follow_link
= proc_pid_follow_link
,
1436 .setattr
= proc_setattr
,
1440 /* building an inode */
1442 static int task_dumpable(struct task_struct
*task
)
1445 struct mm_struct
*mm
;
1450 dumpable
= get_dumpable(mm
);
1458 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1460 struct inode
* inode
;
1461 struct proc_inode
*ei
;
1462 const struct cred
*cred
;
1464 /* We need a new inode */
1466 inode
= new_inode(sb
);
1472 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1473 inode
->i_op
= &proc_def_inode_operations
;
1476 * grab the reference to task.
1478 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1482 if (task_dumpable(task
)) {
1484 cred
= __task_cred(task
);
1485 inode
->i_uid
= cred
->euid
;
1486 inode
->i_gid
= cred
->egid
;
1489 security_task_to_inode(task
, inode
);
1499 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1501 struct inode
*inode
= dentry
->d_inode
;
1502 struct task_struct
*task
;
1503 const struct cred
*cred
;
1505 generic_fillattr(inode
, stat
);
1510 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1512 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1513 task_dumpable(task
)) {
1514 cred
= __task_cred(task
);
1515 stat
->uid
= cred
->euid
;
1516 stat
->gid
= cred
->egid
;
1526 * Exceptional case: normally we are not allowed to unhash a busy
1527 * directory. In this case, however, we can do it - no aliasing problems
1528 * due to the way we treat inodes.
1530 * Rewrite the inode's ownerships here because the owning task may have
1531 * performed a setuid(), etc.
1533 * Before the /proc/pid/status file was created the only way to read
1534 * the effective uid of a /process was to stat /proc/pid. Reading
1535 * /proc/pid/status is slow enough that procps and other packages
1536 * kept stating /proc/pid. To keep the rules in /proc simple I have
1537 * made this apply to all per process world readable and executable
1540 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1542 struct inode
*inode
= dentry
->d_inode
;
1543 struct task_struct
*task
= get_proc_task(inode
);
1544 const struct cred
*cred
;
1547 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1548 task_dumpable(task
)) {
1550 cred
= __task_cred(task
);
1551 inode
->i_uid
= cred
->euid
;
1552 inode
->i_gid
= cred
->egid
;
1558 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1559 security_task_to_inode(task
, inode
);
1560 put_task_struct(task
);
1567 static int pid_delete_dentry(struct dentry
* dentry
)
1569 /* Is the task we represent dead?
1570 * If so, then don't put the dentry on the lru list,
1571 * kill it immediately.
1573 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1576 static const struct dentry_operations pid_dentry_operations
=
1578 .d_revalidate
= pid_revalidate
,
1579 .d_delete
= pid_delete_dentry
,
1584 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1585 struct task_struct
*, const void *);
1588 * Fill a directory entry.
1590 * If possible create the dcache entry and derive our inode number and
1591 * file type from dcache entry.
1593 * Since all of the proc inode numbers are dynamically generated, the inode
1594 * numbers do not exist until the inode is cache. This means creating the
1595 * the dcache entry in readdir is necessary to keep the inode numbers
1596 * reported by readdir in sync with the inode numbers reported
1599 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1600 char *name
, int len
,
1601 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1603 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1604 struct inode
*inode
;
1607 unsigned type
= DT_UNKNOWN
;
1611 qname
.hash
= full_name_hash(name
, len
);
1613 child
= d_lookup(dir
, &qname
);
1616 new = d_alloc(dir
, &qname
);
1618 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1625 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1626 goto end_instantiate
;
1627 inode
= child
->d_inode
;
1630 type
= inode
->i_mode
>> 12;
1635 ino
= find_inode_number(dir
, &qname
);
1638 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1641 static unsigned name_to_int(struct dentry
*dentry
)
1643 const char *name
= dentry
->d_name
.name
;
1644 int len
= dentry
->d_name
.len
;
1647 if (len
> 1 && *name
== '0')
1650 unsigned c
= *name
++ - '0';
1653 if (n
>= (~0U-9)/10)
1663 #define PROC_FDINFO_MAX 64
1665 static int proc_fd_info(struct inode
*inode
, struct path
*path
, char *info
)
1667 struct task_struct
*task
= get_proc_task(inode
);
1668 struct files_struct
*files
= NULL
;
1670 int fd
= proc_fd(inode
);
1673 files
= get_files_struct(task
);
1674 put_task_struct(task
);
1678 * We are not taking a ref to the file structure, so we must
1681 spin_lock(&files
->file_lock
);
1682 file
= fcheck_files(files
, fd
);
1685 *path
= file
->f_path
;
1686 path_get(&file
->f_path
);
1689 snprintf(info
, PROC_FDINFO_MAX
,
1692 (long long) file
->f_pos
,
1694 spin_unlock(&files
->file_lock
);
1695 put_files_struct(files
);
1698 spin_unlock(&files
->file_lock
);
1699 put_files_struct(files
);
1704 static int proc_fd_link(struct inode
*inode
, struct path
*path
)
1706 return proc_fd_info(inode
, path
, NULL
);
1709 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1711 struct inode
*inode
= dentry
->d_inode
;
1712 struct task_struct
*task
= get_proc_task(inode
);
1713 int fd
= proc_fd(inode
);
1714 struct files_struct
*files
;
1715 const struct cred
*cred
;
1718 files
= get_files_struct(task
);
1721 if (fcheck_files(files
, fd
)) {
1723 put_files_struct(files
);
1724 if (task_dumpable(task
)) {
1726 cred
= __task_cred(task
);
1727 inode
->i_uid
= cred
->euid
;
1728 inode
->i_gid
= cred
->egid
;
1734 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1735 security_task_to_inode(task
, inode
);
1736 put_task_struct(task
);
1740 put_files_struct(files
);
1742 put_task_struct(task
);
1748 static const struct dentry_operations tid_fd_dentry_operations
=
1750 .d_revalidate
= tid_fd_revalidate
,
1751 .d_delete
= pid_delete_dentry
,
1754 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1755 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1757 unsigned fd
= *(const unsigned *)ptr
;
1759 struct files_struct
*files
;
1760 struct inode
*inode
;
1761 struct proc_inode
*ei
;
1762 struct dentry
*error
= ERR_PTR(-ENOENT
);
1764 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1769 files
= get_files_struct(task
);
1772 inode
->i_mode
= S_IFLNK
;
1775 * We are not taking a ref to the file structure, so we must
1778 spin_lock(&files
->file_lock
);
1779 file
= fcheck_files(files
, fd
);
1782 if (file
->f_mode
& FMODE_READ
)
1783 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1784 if (file
->f_mode
& FMODE_WRITE
)
1785 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1786 spin_unlock(&files
->file_lock
);
1787 put_files_struct(files
);
1789 inode
->i_op
= &proc_pid_link_inode_operations
;
1791 ei
->op
.proc_get_link
= proc_fd_link
;
1792 dentry
->d_op
= &tid_fd_dentry_operations
;
1793 d_add(dentry
, inode
);
1794 /* Close the race of the process dying before we return the dentry */
1795 if (tid_fd_revalidate(dentry
, NULL
))
1801 spin_unlock(&files
->file_lock
);
1802 put_files_struct(files
);
1808 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1809 struct dentry
*dentry
,
1810 instantiate_t instantiate
)
1812 struct task_struct
*task
= get_proc_task(dir
);
1813 unsigned fd
= name_to_int(dentry
);
1814 struct dentry
*result
= ERR_PTR(-ENOENT
);
1821 result
= instantiate(dir
, dentry
, task
, &fd
);
1823 put_task_struct(task
);
1828 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1829 filldir_t filldir
, instantiate_t instantiate
)
1831 struct dentry
*dentry
= filp
->f_path
.dentry
;
1832 struct inode
*inode
= dentry
->d_inode
;
1833 struct task_struct
*p
= get_proc_task(inode
);
1834 unsigned int fd
, ino
;
1836 struct files_struct
* files
;
1846 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1850 ino
= parent_ino(dentry
);
1851 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1855 files
= get_files_struct(p
);
1859 for (fd
= filp
->f_pos
-2;
1860 fd
< files_fdtable(files
)->max_fds
;
1861 fd
++, filp
->f_pos
++) {
1862 char name
[PROC_NUMBUF
];
1865 if (!fcheck_files(files
, fd
))
1869 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1870 if (proc_fill_cache(filp
, dirent
, filldir
,
1871 name
, len
, instantiate
,
1879 put_files_struct(files
);
1887 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1888 struct nameidata
*nd
)
1890 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1893 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1895 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1898 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1899 size_t len
, loff_t
*ppos
)
1901 char tmp
[PROC_FDINFO_MAX
];
1902 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, tmp
);
1904 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1908 static const struct file_operations proc_fdinfo_file_operations
= {
1909 .open
= nonseekable_open
,
1910 .read
= proc_fdinfo_read
,
1913 static const struct file_operations proc_fd_operations
= {
1914 .read
= generic_read_dir
,
1915 .readdir
= proc_readfd
,
1919 * /proc/pid/fd needs a special permission handler so that a process can still
1920 * access /proc/self/fd after it has executed a setuid().
1922 static int proc_fd_permission(struct inode
*inode
, int mask
)
1926 rv
= generic_permission(inode
, mask
, NULL
);
1929 if (task_pid(current
) == proc_pid(inode
))
1935 * proc directories can do almost nothing..
1937 static const struct inode_operations proc_fd_inode_operations
= {
1938 .lookup
= proc_lookupfd
,
1939 .permission
= proc_fd_permission
,
1940 .setattr
= proc_setattr
,
1943 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1944 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1946 unsigned fd
= *(unsigned *)ptr
;
1947 struct inode
*inode
;
1948 struct proc_inode
*ei
;
1949 struct dentry
*error
= ERR_PTR(-ENOENT
);
1951 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1956 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1957 inode
->i_fop
= &proc_fdinfo_file_operations
;
1958 dentry
->d_op
= &tid_fd_dentry_operations
;
1959 d_add(dentry
, inode
);
1960 /* Close the race of the process dying before we return the dentry */
1961 if (tid_fd_revalidate(dentry
, NULL
))
1968 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1969 struct dentry
*dentry
,
1970 struct nameidata
*nd
)
1972 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1975 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1977 return proc_readfd_common(filp
, dirent
, filldir
,
1978 proc_fdinfo_instantiate
);
1981 static const struct file_operations proc_fdinfo_operations
= {
1982 .read
= generic_read_dir
,
1983 .readdir
= proc_readfdinfo
,
1987 * proc directories can do almost nothing..
1989 static const struct inode_operations proc_fdinfo_inode_operations
= {
1990 .lookup
= proc_lookupfdinfo
,
1991 .setattr
= proc_setattr
,
1995 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1996 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1998 const struct pid_entry
*p
= ptr
;
1999 struct inode
*inode
;
2000 struct proc_inode
*ei
;
2001 struct dentry
*error
= ERR_PTR(-ENOENT
);
2003 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2008 inode
->i_mode
= p
->mode
;
2009 if (S_ISDIR(inode
->i_mode
))
2010 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
2012 inode
->i_op
= p
->iop
;
2014 inode
->i_fop
= p
->fop
;
2016 dentry
->d_op
= &pid_dentry_operations
;
2017 d_add(dentry
, inode
);
2018 /* Close the race of the process dying before we return the dentry */
2019 if (pid_revalidate(dentry
, NULL
))
2025 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2026 struct dentry
*dentry
,
2027 const struct pid_entry
*ents
,
2030 struct dentry
*error
;
2031 struct task_struct
*task
= get_proc_task(dir
);
2032 const struct pid_entry
*p
, *last
;
2034 error
= ERR_PTR(-ENOENT
);
2040 * Yes, it does not scale. And it should not. Don't add
2041 * new entries into /proc/<tgid>/ without very good reasons.
2043 last
= &ents
[nents
- 1];
2044 for (p
= ents
; p
<= last
; p
++) {
2045 if (p
->len
!= dentry
->d_name
.len
)
2047 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2053 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2055 put_task_struct(task
);
2060 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2061 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2063 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2064 proc_pident_instantiate
, task
, p
);
2067 static int proc_pident_readdir(struct file
*filp
,
2068 void *dirent
, filldir_t filldir
,
2069 const struct pid_entry
*ents
, unsigned int nents
)
2072 struct dentry
*dentry
= filp
->f_path
.dentry
;
2073 struct inode
*inode
= dentry
->d_inode
;
2074 struct task_struct
*task
= get_proc_task(inode
);
2075 const struct pid_entry
*p
, *last
;
2088 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2094 ino
= parent_ino(dentry
);
2095 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2107 last
= &ents
[nents
- 1];
2109 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2118 put_task_struct(task
);
2123 #ifdef CONFIG_SECURITY
2124 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2125 size_t count
, loff_t
*ppos
)
2127 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2130 struct task_struct
*task
= get_proc_task(inode
);
2135 length
= security_getprocattr(task
,
2136 (char*)file
->f_path
.dentry
->d_name
.name
,
2138 put_task_struct(task
);
2140 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2145 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2146 size_t count
, loff_t
*ppos
)
2148 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2151 struct task_struct
*task
= get_proc_task(inode
);
2156 if (count
> PAGE_SIZE
)
2159 /* No partial writes. */
2165 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2170 if (copy_from_user(page
, buf
, count
))
2173 /* Guard against adverse ptrace interaction */
2174 length
= mutex_lock_interruptible(&task
->cred_guard_mutex
);
2178 length
= security_setprocattr(task
,
2179 (char*)file
->f_path
.dentry
->d_name
.name
,
2180 (void*)page
, count
);
2181 mutex_unlock(&task
->cred_guard_mutex
);
2183 free_page((unsigned long) page
);
2185 put_task_struct(task
);
2190 static const struct file_operations proc_pid_attr_operations
= {
2191 .read
= proc_pid_attr_read
,
2192 .write
= proc_pid_attr_write
,
2195 static const struct pid_entry attr_dir_stuff
[] = {
2196 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2197 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2198 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2199 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2200 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2201 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2204 static int proc_attr_dir_readdir(struct file
* filp
,
2205 void * dirent
, filldir_t filldir
)
2207 return proc_pident_readdir(filp
,dirent
,filldir
,
2208 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2211 static const struct file_operations proc_attr_dir_operations
= {
2212 .read
= generic_read_dir
,
2213 .readdir
= proc_attr_dir_readdir
,
2216 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2217 struct dentry
*dentry
, struct nameidata
*nd
)
2219 return proc_pident_lookup(dir
, dentry
,
2220 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2223 static const struct inode_operations proc_attr_dir_inode_operations
= {
2224 .lookup
= proc_attr_dir_lookup
,
2225 .getattr
= pid_getattr
,
2226 .setattr
= proc_setattr
,
2231 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2232 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2233 size_t count
, loff_t
*ppos
)
2235 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2236 struct mm_struct
*mm
;
2237 char buffer
[PROC_NUMBUF
];
2245 mm
= get_task_mm(task
);
2247 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2248 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2249 MMF_DUMP_FILTER_SHIFT
));
2251 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2254 put_task_struct(task
);
2259 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2260 const char __user
*buf
,
2264 struct task_struct
*task
;
2265 struct mm_struct
*mm
;
2266 char buffer
[PROC_NUMBUF
], *end
;
2273 memset(buffer
, 0, sizeof(buffer
));
2274 if (count
> sizeof(buffer
) - 1)
2275 count
= sizeof(buffer
) - 1;
2276 if (copy_from_user(buffer
, buf
, count
))
2280 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2283 if (end
- buffer
== 0)
2287 task
= get_proc_task(file
->f_dentry
->d_inode
);
2292 mm
= get_task_mm(task
);
2296 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2298 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2300 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2305 put_task_struct(task
);
2310 static const struct file_operations proc_coredump_filter_operations
= {
2311 .read
= proc_coredump_filter_read
,
2312 .write
= proc_coredump_filter_write
,
2319 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2322 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2323 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2324 char tmp
[PROC_NUMBUF
];
2327 sprintf(tmp
, "%d", tgid
);
2328 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2331 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2333 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2334 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2335 char *name
= ERR_PTR(-ENOENT
);
2339 name
= ERR_PTR(-ENOMEM
);
2341 sprintf(name
, "%d", tgid
);
2343 nd_set_link(nd
, name
);
2347 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2350 char *s
= nd_get_link(nd
);
2355 static const struct inode_operations proc_self_inode_operations
= {
2356 .readlink
= proc_self_readlink
,
2357 .follow_link
= proc_self_follow_link
,
2358 .put_link
= proc_self_put_link
,
2364 * These are the directory entries in the root directory of /proc
2365 * that properly belong to the /proc filesystem, as they describe
2366 * describe something that is process related.
2368 static const struct pid_entry proc_base_stuff
[] = {
2369 NOD("self", S_IFLNK
|S_IRWXUGO
,
2370 &proc_self_inode_operations
, NULL
, {}),
2374 * Exceptional case: normally we are not allowed to unhash a busy
2375 * directory. In this case, however, we can do it - no aliasing problems
2376 * due to the way we treat inodes.
2378 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
2380 struct inode
*inode
= dentry
->d_inode
;
2381 struct task_struct
*task
= get_proc_task(inode
);
2383 put_task_struct(task
);
2390 static const struct dentry_operations proc_base_dentry_operations
=
2392 .d_revalidate
= proc_base_revalidate
,
2393 .d_delete
= pid_delete_dentry
,
2396 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2397 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2399 const struct pid_entry
*p
= ptr
;
2400 struct inode
*inode
;
2401 struct proc_inode
*ei
;
2402 struct dentry
*error
= ERR_PTR(-EINVAL
);
2404 /* Allocate the inode */
2405 error
= ERR_PTR(-ENOMEM
);
2406 inode
= new_inode(dir
->i_sb
);
2410 /* Initialize the inode */
2412 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2415 * grab the reference to the task.
2417 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2421 inode
->i_mode
= p
->mode
;
2422 if (S_ISDIR(inode
->i_mode
))
2424 if (S_ISLNK(inode
->i_mode
))
2427 inode
->i_op
= p
->iop
;
2429 inode
->i_fop
= p
->fop
;
2431 dentry
->d_op
= &proc_base_dentry_operations
;
2432 d_add(dentry
, inode
);
2441 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2443 struct dentry
*error
;
2444 struct task_struct
*task
= get_proc_task(dir
);
2445 const struct pid_entry
*p
, *last
;
2447 error
= ERR_PTR(-ENOENT
);
2452 /* Lookup the directory entry */
2453 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2454 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2455 if (p
->len
!= dentry
->d_name
.len
)
2457 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2463 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2466 put_task_struct(task
);
2471 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2472 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2474 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2475 proc_base_instantiate
, task
, p
);
2478 #ifdef CONFIG_TASK_IO_ACCOUNTING
2479 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2481 struct task_io_accounting acct
= task
->ioac
;
2482 unsigned long flags
;
2484 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
2487 if (whole
&& lock_task_sighand(task
, &flags
)) {
2488 struct task_struct
*t
= task
;
2490 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2491 while_each_thread(task
, t
)
2492 task_io_accounting_add(&acct
, &t
->ioac
);
2494 unlock_task_sighand(task
, &flags
);
2496 return sprintf(buffer
,
2501 "read_bytes: %llu\n"
2502 "write_bytes: %llu\n"
2503 "cancelled_write_bytes: %llu\n",
2504 (unsigned long long)acct
.rchar
,
2505 (unsigned long long)acct
.wchar
,
2506 (unsigned long long)acct
.syscr
,
2507 (unsigned long long)acct
.syscw
,
2508 (unsigned long long)acct
.read_bytes
,
2509 (unsigned long long)acct
.write_bytes
,
2510 (unsigned long long)acct
.cancelled_write_bytes
);
2513 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2515 return do_io_accounting(task
, buffer
, 0);
2518 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2520 return do_io_accounting(task
, buffer
, 1);
2522 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2524 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2525 struct pid
*pid
, struct task_struct
*task
)
2527 int err
= lock_trace(task
);
2529 seq_printf(m
, "%08x\n", task
->personality
);
2538 static const struct file_operations proc_task_operations
;
2539 static const struct inode_operations proc_task_inode_operations
;
2541 static const struct pid_entry tgid_base_stuff
[] = {
2542 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2543 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2544 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2546 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2548 REG("environ", S_IRUSR
, proc_environ_operations
),
2549 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2550 ONE("status", S_IRUGO
, proc_pid_status
),
2551 ONE("personality", S_IRUGO
, proc_pid_personality
),
2552 INF("limits", S_IRUSR
, proc_pid_limits
),
2553 #ifdef CONFIG_SCHED_DEBUG
2554 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2556 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2557 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2559 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2560 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2561 ONE("statm", S_IRUGO
, proc_pid_statm
),
2562 REG("maps", S_IRUGO
, proc_maps_operations
),
2564 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2566 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2567 LNK("cwd", proc_cwd_link
),
2568 LNK("root", proc_root_link
),
2569 LNK("exe", proc_exe_link
),
2570 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2571 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2572 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2573 #ifdef CONFIG_PROC_PAGE_MONITOR
2574 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2575 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2576 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2578 #ifdef CONFIG_SECURITY
2579 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2581 #ifdef CONFIG_KALLSYMS
2582 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2584 #ifdef CONFIG_STACKTRACE
2585 ONE("stack", S_IRUGO
, proc_pid_stack
),
2587 #ifdef CONFIG_SCHEDSTATS
2588 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2590 #ifdef CONFIG_LATENCYTOP
2591 REG("latency", S_IRUGO
, proc_lstats_operations
),
2593 #ifdef CONFIG_PROC_PID_CPUSET
2594 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2596 #ifdef CONFIG_CGROUPS
2597 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2599 INF("oom_score", S_IRUGO
, proc_oom_score
),
2600 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2601 #ifdef CONFIG_AUDITSYSCALL
2602 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2603 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2605 #ifdef CONFIG_FAULT_INJECTION
2606 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2608 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2609 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2611 #ifdef CONFIG_TASK_IO_ACCOUNTING
2612 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2616 static int proc_tgid_base_readdir(struct file
* filp
,
2617 void * dirent
, filldir_t filldir
)
2619 return proc_pident_readdir(filp
,dirent
,filldir
,
2620 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2623 static const struct file_operations proc_tgid_base_operations
= {
2624 .read
= generic_read_dir
,
2625 .readdir
= proc_tgid_base_readdir
,
2628 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2629 return proc_pident_lookup(dir
, dentry
,
2630 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2633 static const struct inode_operations proc_tgid_base_inode_operations
= {
2634 .lookup
= proc_tgid_base_lookup
,
2635 .getattr
= pid_getattr
,
2636 .setattr
= proc_setattr
,
2639 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2641 struct dentry
*dentry
, *leader
, *dir
;
2642 char buf
[PROC_NUMBUF
];
2646 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2647 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2649 shrink_dcache_parent(dentry
);
2655 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2656 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2661 name
.len
= strlen(name
.name
);
2662 dir
= d_hash_and_lookup(leader
, &name
);
2664 goto out_put_leader
;
2667 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2668 dentry
= d_hash_and_lookup(dir
, &name
);
2670 shrink_dcache_parent(dentry
);
2683 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2684 * @task: task that should be flushed.
2686 * When flushing dentries from proc, one needs to flush them from global
2687 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2688 * in. This call is supposed to do all of this job.
2690 * Looks in the dcache for
2692 * /proc/@tgid/task/@pid
2693 * if either directory is present flushes it and all of it'ts children
2696 * It is safe and reasonable to cache /proc entries for a task until
2697 * that task exits. After that they just clog up the dcache with
2698 * useless entries, possibly causing useful dcache entries to be
2699 * flushed instead. This routine is proved to flush those useless
2700 * dcache entries at process exit time.
2702 * NOTE: This routine is just an optimization so it does not guarantee
2703 * that no dcache entries will exist at process exit time it
2704 * just makes it very unlikely that any will persist.
2707 void proc_flush_task(struct task_struct
*task
)
2710 struct pid
*pid
, *tgid
;
2713 pid
= task_pid(task
);
2714 tgid
= task_tgid(task
);
2716 for (i
= 0; i
<= pid
->level
; i
++) {
2717 upid
= &pid
->numbers
[i
];
2718 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2719 tgid
->numbers
[i
].nr
);
2722 upid
= &pid
->numbers
[pid
->level
];
2724 pid_ns_release_proc(upid
->ns
);
2727 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2728 struct dentry
* dentry
,
2729 struct task_struct
*task
, const void *ptr
)
2731 struct dentry
*error
= ERR_PTR(-ENOENT
);
2732 struct inode
*inode
;
2734 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2738 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2739 inode
->i_op
= &proc_tgid_base_inode_operations
;
2740 inode
->i_fop
= &proc_tgid_base_operations
;
2741 inode
->i_flags
|=S_IMMUTABLE
;
2743 inode
->i_nlink
= 2 + pid_entry_count_dirs(tgid_base_stuff
,
2744 ARRAY_SIZE(tgid_base_stuff
));
2746 dentry
->d_op
= &pid_dentry_operations
;
2748 d_add(dentry
, inode
);
2749 /* Close the race of the process dying before we return the dentry */
2750 if (pid_revalidate(dentry
, NULL
))
2756 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2758 struct dentry
*result
= ERR_PTR(-ENOENT
);
2759 struct task_struct
*task
;
2761 struct pid_namespace
*ns
;
2763 result
= proc_base_lookup(dir
, dentry
);
2764 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2767 tgid
= name_to_int(dentry
);
2771 ns
= dentry
->d_sb
->s_fs_info
;
2773 task
= find_task_by_pid_ns(tgid
, ns
);
2775 get_task_struct(task
);
2780 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2781 put_task_struct(task
);
2787 * Find the first task with tgid >= tgid
2792 struct task_struct
*task
;
2794 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2799 put_task_struct(iter
.task
);
2803 pid
= find_ge_pid(iter
.tgid
, ns
);
2805 iter
.tgid
= pid_nr_ns(pid
, ns
);
2806 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2807 /* What we to know is if the pid we have find is the
2808 * pid of a thread_group_leader. Testing for task
2809 * being a thread_group_leader is the obvious thing
2810 * todo but there is a window when it fails, due to
2811 * the pid transfer logic in de_thread.
2813 * So we perform the straight forward test of seeing
2814 * if the pid we have found is the pid of a thread
2815 * group leader, and don't worry if the task we have
2816 * found doesn't happen to be a thread group leader.
2817 * As we don't care in the case of readdir.
2819 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2823 get_task_struct(iter
.task
);
2829 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2831 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2832 struct tgid_iter iter
)
2834 char name
[PROC_NUMBUF
];
2835 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2836 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2837 proc_pid_instantiate
, iter
.task
, NULL
);
2840 /* for the /proc/ directory itself, after non-process stuff has been done */
2841 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2844 struct task_struct
*reaper
;
2845 struct tgid_iter iter
;
2846 struct pid_namespace
*ns
;
2848 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2850 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2852 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2856 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2857 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2858 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2862 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2864 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2865 for (iter
= next_tgid(ns
, iter
);
2867 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2868 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2869 if (proc_pid_fill_cache(filp
, dirent
, filldir
, iter
) < 0) {
2870 put_task_struct(iter
.task
);
2874 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2876 put_task_struct(reaper
);
2884 static const struct pid_entry tid_base_stuff
[] = {
2885 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2886 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2887 REG("environ", S_IRUSR
, proc_environ_operations
),
2888 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2889 ONE("status", S_IRUGO
, proc_pid_status
),
2890 ONE("personality", S_IRUGO
, proc_pid_personality
),
2891 INF("limits", S_IRUSR
, proc_pid_limits
),
2892 #ifdef CONFIG_SCHED_DEBUG
2893 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2895 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2896 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2898 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2899 ONE("stat", S_IRUGO
, proc_tid_stat
),
2900 ONE("statm", S_IRUGO
, proc_pid_statm
),
2901 REG("maps", S_IRUGO
, proc_maps_operations
),
2903 REG("numa_maps", S_IRUGO
, proc_numa_maps_operations
),
2905 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2906 LNK("cwd", proc_cwd_link
),
2907 LNK("root", proc_root_link
),
2908 LNK("exe", proc_exe_link
),
2909 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2910 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2911 #ifdef CONFIG_PROC_PAGE_MONITOR
2912 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2913 REG("smaps", S_IRUGO
, proc_smaps_operations
),
2914 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2916 #ifdef CONFIG_SECURITY
2917 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2919 #ifdef CONFIG_KALLSYMS
2920 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2922 #ifdef CONFIG_STACKTRACE
2923 ONE("stack", S_IRUGO
, proc_pid_stack
),
2925 #ifdef CONFIG_SCHEDSTATS
2926 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2928 #ifdef CONFIG_LATENCYTOP
2929 REG("latency", S_IRUGO
, proc_lstats_operations
),
2931 #ifdef CONFIG_PROC_PID_CPUSET
2932 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2934 #ifdef CONFIG_CGROUPS
2935 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2937 INF("oom_score", S_IRUGO
, proc_oom_score
),
2938 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adjust_operations
),
2939 #ifdef CONFIG_AUDITSYSCALL
2940 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2941 REG("sessionid", S_IRUSR
, proc_sessionid_operations
),
2943 #ifdef CONFIG_FAULT_INJECTION
2944 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2946 #ifdef CONFIG_TASK_IO_ACCOUNTING
2947 INF("io", S_IRUSR
, proc_tid_io_accounting
),
2951 static int proc_tid_base_readdir(struct file
* filp
,
2952 void * dirent
, filldir_t filldir
)
2954 return proc_pident_readdir(filp
,dirent
,filldir
,
2955 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2958 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2959 return proc_pident_lookup(dir
, dentry
,
2960 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2963 static const struct file_operations proc_tid_base_operations
= {
2964 .read
= generic_read_dir
,
2965 .readdir
= proc_tid_base_readdir
,
2968 static const struct inode_operations proc_tid_base_inode_operations
= {
2969 .lookup
= proc_tid_base_lookup
,
2970 .getattr
= pid_getattr
,
2971 .setattr
= proc_setattr
,
2974 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2975 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2977 struct dentry
*error
= ERR_PTR(-ENOENT
);
2978 struct inode
*inode
;
2979 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2983 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2984 inode
->i_op
= &proc_tid_base_inode_operations
;
2985 inode
->i_fop
= &proc_tid_base_operations
;
2986 inode
->i_flags
|=S_IMMUTABLE
;
2988 inode
->i_nlink
= 2 + pid_entry_count_dirs(tid_base_stuff
,
2989 ARRAY_SIZE(tid_base_stuff
));
2991 dentry
->d_op
= &pid_dentry_operations
;
2993 d_add(dentry
, inode
);
2994 /* Close the race of the process dying before we return the dentry */
2995 if (pid_revalidate(dentry
, NULL
))
3001 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
3003 struct dentry
*result
= ERR_PTR(-ENOENT
);
3004 struct task_struct
*task
;
3005 struct task_struct
*leader
= get_proc_task(dir
);
3007 struct pid_namespace
*ns
;
3012 tid
= name_to_int(dentry
);
3016 ns
= dentry
->d_sb
->s_fs_info
;
3018 task
= find_task_by_pid_ns(tid
, ns
);
3020 get_task_struct(task
);
3024 if (!same_thread_group(leader
, task
))
3027 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3029 put_task_struct(task
);
3031 put_task_struct(leader
);
3037 * Find the first tid of a thread group to return to user space.
3039 * Usually this is just the thread group leader, but if the users
3040 * buffer was too small or there was a seek into the middle of the
3041 * directory we have more work todo.
3043 * In the case of a short read we start with find_task_by_pid.
3045 * In the case of a seek we start with the leader and walk nr
3048 static struct task_struct
*first_tid(struct task_struct
*leader
,
3049 int tid
, int nr
, struct pid_namespace
*ns
)
3051 struct task_struct
*pos
;
3054 /* Attempt to start with the pid of a thread */
3055 if (tid
&& (nr
> 0)) {
3056 pos
= find_task_by_pid_ns(tid
, ns
);
3057 if (pos
&& (pos
->group_leader
== leader
))
3061 /* If nr exceeds the number of threads there is nothing todo */
3063 if (nr
&& nr
>= get_nr_threads(leader
))
3066 /* If we haven't found our starting place yet start
3067 * with the leader and walk nr threads forward.
3069 for (pos
= leader
; nr
> 0; --nr
) {
3070 pos
= next_thread(pos
);
3071 if (pos
== leader
) {
3077 get_task_struct(pos
);
3084 * Find the next thread in the thread list.
3085 * Return NULL if there is an error or no next thread.
3087 * The reference to the input task_struct is released.
3089 static struct task_struct
*next_tid(struct task_struct
*start
)
3091 struct task_struct
*pos
= NULL
;
3093 if (pid_alive(start
)) {
3094 pos
= next_thread(start
);
3095 if (thread_group_leader(pos
))
3098 get_task_struct(pos
);
3101 put_task_struct(start
);
3105 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3106 struct task_struct
*task
, int tid
)
3108 char name
[PROC_NUMBUF
];
3109 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3110 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3111 proc_task_instantiate
, task
, NULL
);
3114 /* for the /proc/TGID/task/ directories */
3115 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3117 struct dentry
*dentry
= filp
->f_path
.dentry
;
3118 struct inode
*inode
= dentry
->d_inode
;
3119 struct task_struct
*leader
= NULL
;
3120 struct task_struct
*task
;
3121 int retval
= -ENOENT
;
3124 struct pid_namespace
*ns
;
3126 task
= get_proc_task(inode
);
3130 if (pid_alive(task
)) {
3131 leader
= task
->group_leader
;
3132 get_task_struct(leader
);
3135 put_task_struct(task
);
3140 switch ((unsigned long)filp
->f_pos
) {
3143 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3148 ino
= parent_ino(dentry
);
3149 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3155 /* f_version caches the tgid value that the last readdir call couldn't
3156 * return. lseek aka telldir automagically resets f_version to 0.
3158 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3159 tid
= (int)filp
->f_version
;
3160 filp
->f_version
= 0;
3161 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3163 task
= next_tid(task
), filp
->f_pos
++) {
3164 tid
= task_pid_nr_ns(task
, ns
);
3165 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3166 /* returning this tgid failed, save it as the first
3167 * pid for the next readir call */
3168 filp
->f_version
= (u64
)tid
;
3169 put_task_struct(task
);
3174 put_task_struct(leader
);
3179 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3181 struct inode
*inode
= dentry
->d_inode
;
3182 struct task_struct
*p
= get_proc_task(inode
);
3183 generic_fillattr(inode
, stat
);
3186 stat
->nlink
+= get_nr_threads(p
);
3193 static const struct inode_operations proc_task_inode_operations
= {
3194 .lookup
= proc_task_lookup
,
3195 .getattr
= proc_task_getattr
,
3196 .setattr
= proc_setattr
,
3199 static const struct file_operations proc_task_operations
= {
3200 .read
= generic_read_dir
,
3201 .readdir
= proc_task_readdir
,