| @@ -1,713 +1,717 @@ | | | @@ -1,713 +1,717 @@ |
1 | /* $NetBSD: procfs_linux.c,v 1.61 2011/09/04 17:32:10 jmcneill Exp $ */ | | 1 | /* $NetBSD: procfs_linux.c,v 1.62 2011/12/15 20:55:02 christos Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2001 Wasabi Systems, Inc. | | 4 | * Copyright (c) 2001 Wasabi Systems, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Written by Frank van der Linden for Wasabi Systems, Inc. | | 7 | * Written by Frank van der Linden for Wasabi Systems, Inc. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the | | 15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. | | 16 | * documentation and/or other materials provided with the distribution. |
17 | * 3. All advertising materials mentioning features or use of this software | | 17 | * 3. All advertising materials mentioning features or use of this software |
18 | * must display the following acknowledgement: | | 18 | * must display the following acknowledgement: |
19 | * This product includes software developed for the NetBSD Project by | | 19 | * This product includes software developed for the NetBSD Project by |
20 | * Wasabi Systems, Inc. | | 20 | * Wasabi Systems, Inc. |
21 | * 4. The name of Wasabi Systems, Inc. may not be used to endorse | | 21 | * 4. The name of Wasabi Systems, Inc. may not be used to endorse |
22 | * or promote products derived from this software without specific prior | | 22 | * or promote products derived from this software without specific prior |
23 | * written permission. | | 23 | * written permission. |
24 | * | | 24 | * |
25 | * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND | | 25 | * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 27 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC | | 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC |
29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 29 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
35 | * POSSIBILITY OF SUCH DAMAGE. | | 35 | * POSSIBILITY OF SUCH DAMAGE. |
36 | */ | | 36 | */ |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.61 2011/09/04 17:32:10 jmcneill Exp $"); | | 39 | __KERNEL_RCSID(0, "$NetBSD: procfs_linux.c,v 1.62 2011/12/15 20:55:02 christos Exp $"); |
40 | | | 40 | |
41 | #include <sys/param.h> | | 41 | #include <sys/param.h> |
42 | #include <sys/systm.h> | | 42 | #include <sys/systm.h> |
43 | #include <sys/time.h> | | 43 | #include <sys/time.h> |
44 | #include <sys/kernel.h> | | 44 | #include <sys/kernel.h> |
45 | #include <sys/proc.h> | | 45 | #include <sys/proc.h> |
46 | #include <sys/vnode.h> | | 46 | #include <sys/vnode.h> |
47 | #include <sys/exec.h> | | 47 | #include <sys/exec.h> |
48 | #include <sys/resource.h> | | 48 | #include <sys/resource.h> |
49 | #include <sys/resourcevar.h> | | 49 | #include <sys/resourcevar.h> |
50 | #include <sys/signal.h> | | 50 | #include <sys/signal.h> |
51 | #include <sys/signalvar.h> | | 51 | #include <sys/signalvar.h> |
52 | #include <sys/tty.h> | | 52 | #include <sys/tty.h> |
53 | #include <sys/malloc.h> | | 53 | #include <sys/malloc.h> |
54 | #include <sys/mount.h> | | 54 | #include <sys/mount.h> |
55 | #include <sys/conf.h> | | 55 | #include <sys/conf.h> |
56 | #include <sys/sysctl.h> | | 56 | #include <sys/sysctl.h> |
| | | 57 | #include <sys/kauth.h> |
57 | | | 58 | |
58 | #include <miscfs/procfs/procfs.h> | | 59 | #include <miscfs/procfs/procfs.h> |
59 | | | 60 | |
60 | #include <compat/linux/common/linux_exec.h> | | 61 | #include <compat/linux/common/linux_exec.h> |
61 | #include <compat/linux32/common/linux32_sysctl.h> | | 62 | #include <compat/linux32/common/linux32_sysctl.h> |
62 | | | 63 | |
63 | #include <uvm/uvm_extern.h> | | 64 | #include <uvm/uvm_extern.h> |
64 | #include <uvm/uvm.h> | | 65 | #include <uvm/uvm.h> |
65 | | | 66 | |
66 | extern struct devsw_conv *devsw_conv; | | 67 | extern struct devsw_conv *devsw_conv; |
67 | extern int max_devsw_convs; | | 68 | extern int max_devsw_convs; |
68 | | | 69 | |
69 | #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT) | | 70 | #define PGTOB(p) ((unsigned long)(p) << PAGE_SHIFT) |
70 | #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10)) | | 71 | #define PGTOKB(p) ((unsigned long)(p) << (PAGE_SHIFT - 10)) |
71 | | | 72 | |
72 | #define LBFSZ (8 * 1024) | | 73 | #define LBFSZ (8 * 1024) |
73 | | | 74 | |
74 | static void | | 75 | static void |
75 | get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack) | | 76 | get_proc_size_info(struct lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack) |
76 | { | | 77 | { |
77 | struct proc *p = l->l_proc; | | 78 | struct proc *p = l->l_proc; |
78 | struct vmspace *vm; | | 79 | struct vmspace *vm; |
79 | struct vm_map *map; | | 80 | struct vm_map *map; |
80 | struct vm_map_entry *entry; | | 81 | struct vm_map_entry *entry; |
81 | | | 82 | |
82 | *stext = 0; | | 83 | *stext = 0; |
83 | *etext = 0; | | 84 | *etext = 0; |
84 | *sstack = 0; | | 85 | *sstack = 0; |
85 | | | 86 | |
86 | proc_vmspace_getref(p, &vm); | | 87 | proc_vmspace_getref(p, &vm); |
87 | map = &vm->vm_map; | | 88 | map = &vm->vm_map; |
88 | vm_map_lock_read(map); | | 89 | vm_map_lock_read(map); |
89 | | | 90 | |
90 | for (entry = map->header.next; entry != &map->header; | | 91 | for (entry = map->header.next; entry != &map->header; |
91 | entry = entry->next) { | | 92 | entry = entry->next) { |
92 | if (UVM_ET_ISSUBMAP(entry)) | | 93 | if (UVM_ET_ISSUBMAP(entry)) |
93 | continue; | | 94 | continue; |
94 | /* assume text is the first entry */ | | 95 | /* assume text is the first entry */ |
95 | if (*stext == *etext) { | | 96 | if (*stext == *etext) { |
96 | *stext = entry->start; | | 97 | *stext = entry->start; |
97 | *etext = entry->end; | | 98 | *etext = entry->end; |
98 | break; | | 99 | break; |
99 | } | | 100 | } |
100 | } | | 101 | } |
101 | #if defined(LINUX_USRSTACK32) && defined(USRSTACK32) | | 102 | #if defined(LINUX_USRSTACK32) && defined(USRSTACK32) |
102 | if (strcmp(p->p_emul->e_name, "linux32") == 0 && | | 103 | if (strcmp(p->p_emul->e_name, "linux32") == 0 && |
103 | LINUX_USRSTACK32 < USRSTACK32) | | 104 | LINUX_USRSTACK32 < USRSTACK32) |
104 | *sstack = (unsigned long)LINUX_USRSTACK32; | | 105 | *sstack = (unsigned long)LINUX_USRSTACK32; |
105 | else | | 106 | else |
106 | #endif | | 107 | #endif |
107 | #ifdef LINUX_USRSTACK | | 108 | #ifdef LINUX_USRSTACK |
108 | if (strcmp(p->p_emul->e_name, "linux") == 0 && | | 109 | if (strcmp(p->p_emul->e_name, "linux") == 0 && |
109 | LINUX_USRSTACK < USRSTACK) | | 110 | LINUX_USRSTACK < USRSTACK) |
110 | *sstack = (unsigned long)LINUX_USRSTACK; | | 111 | *sstack = (unsigned long)LINUX_USRSTACK; |
111 | else | | 112 | else |
112 | #endif | | 113 | #endif |
113 | #ifdef USRSTACK32 | | 114 | #ifdef USRSTACK32 |
114 | if (strstr(p->p_emul->e_name, "32") != NULL) | | 115 | if (strstr(p->p_emul->e_name, "32") != NULL) |
115 | *sstack = (unsigned long)USRSTACK32; | | 116 | *sstack = (unsigned long)USRSTACK32; |
116 | else | | 117 | else |
117 | #endif | | 118 | #endif |
118 | *sstack = (unsigned long)USRSTACK; | | 119 | *sstack = (unsigned long)USRSTACK; |
119 | | | 120 | |
120 | /* | | 121 | /* |
121 | * jdk 1.6 compares low <= addr && addr < high | | 122 | * jdk 1.6 compares low <= addr && addr < high |
122 | * if we put addr == high, then the test fails | | 123 | * if we put addr == high, then the test fails |
123 | * so eat one page. | | 124 | * so eat one page. |
124 | */ | | 125 | */ |
125 | *sstack -= PAGE_SIZE; | | 126 | *sstack -= PAGE_SIZE; |
126 | | | 127 | |
127 | vm_map_unlock_read(map); | | 128 | vm_map_unlock_read(map); |
128 | uvmspace_free(vm); | | 129 | uvmspace_free(vm); |
129 | } | | 130 | } |
130 | | | 131 | |
131 | /* | | 132 | /* |
132 | * Linux compatible /proc/meminfo. Only active when the -o linux | | 133 | * Linux compatible /proc/meminfo. Only active when the -o linux |
133 | * mountflag is used. | | 134 | * mountflag is used. |
134 | */ | | 135 | */ |
135 | int | | 136 | int |
136 | procfs_domeminfo(struct lwp *curl, struct proc *p, | | 137 | procfs_domeminfo(struct lwp *curl, struct proc *p, |
137 | struct pfsnode *pfs, struct uio *uio) | | 138 | struct pfsnode *pfs, struct uio *uio) |
138 | { | | 139 | { |
139 | char *bf; | | 140 | char *bf; |
140 | int len; | | 141 | int len; |
141 | int error = 0; | | 142 | int error = 0; |
142 | | | 143 | |
143 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 144 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
144 | | | 145 | |
145 | len = snprintf(bf, LBFSZ, | | 146 | len = snprintf(bf, LBFSZ, |
146 | " total: used: free: shared: buffers: cached:\n" | | 147 | " total: used: free: shared: buffers: cached:\n" |
147 | "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n" | | 148 | "Mem: %8lu %8lu %8lu %8lu %8lu %8lu\n" |
148 | "Swap: %8lu %8lu %8lu\n" | | 149 | "Swap: %8lu %8lu %8lu\n" |
149 | "MemTotal: %8lu kB\n" | | 150 | "MemTotal: %8lu kB\n" |
150 | "MemFree: %8lu kB\n" | | 151 | "MemFree: %8lu kB\n" |
151 | "MemShared: %8lu kB\n" | | 152 | "MemShared: %8lu kB\n" |
152 | "Buffers: %8lu kB\n" | | 153 | "Buffers: %8lu kB\n" |
153 | "Cached: %8lu kB\n" | | 154 | "Cached: %8lu kB\n" |
154 | "SwapTotal: %8lu kB\n" | | 155 | "SwapTotal: %8lu kB\n" |
155 | "SwapFree: %8lu kB\n", | | 156 | "SwapFree: %8lu kB\n", |
156 | PGTOB(uvmexp.npages), | | 157 | PGTOB(uvmexp.npages), |
157 | PGTOB(uvmexp.npages - uvmexp.free), | | 158 | PGTOB(uvmexp.npages - uvmexp.free), |
158 | PGTOB(uvmexp.free), | | 159 | PGTOB(uvmexp.free), |
159 | 0L, | | 160 | 0L, |
160 | PGTOB(uvmexp.filepages), | | 161 | PGTOB(uvmexp.filepages), |
161 | PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), | | 162 | PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), |
162 | PGTOB(uvmexp.swpages), | | 163 | PGTOB(uvmexp.swpages), |
163 | PGTOB(uvmexp.swpginuse), | | 164 | PGTOB(uvmexp.swpginuse), |
164 | PGTOB(uvmexp.swpages - uvmexp.swpginuse), | | 165 | PGTOB(uvmexp.swpages - uvmexp.swpginuse), |
165 | PGTOKB(uvmexp.npages), | | 166 | PGTOKB(uvmexp.npages), |
166 | PGTOKB(uvmexp.free), | | 167 | PGTOKB(uvmexp.free), |
167 | 0L, | | 168 | 0L, |
168 | PGTOKB(uvmexp.filepages), | | 169 | PGTOKB(uvmexp.filepages), |
169 | PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), | | 170 | PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages), |
170 | PGTOKB(uvmexp.swpages), | | 171 | PGTOKB(uvmexp.swpages), |
171 | PGTOKB(uvmexp.swpages - uvmexp.swpginuse)); | | 172 | PGTOKB(uvmexp.swpages - uvmexp.swpginuse)); |
172 | | | 173 | |
173 | if (len == 0) | | 174 | if (len == 0) |
174 | goto out; | | 175 | goto out; |
175 | | | 176 | |
176 | error = uiomove_frombuf(bf, len, uio); | | 177 | error = uiomove_frombuf(bf, len, uio); |
177 | out: | | 178 | out: |
178 | free(bf, M_TEMP); | | 179 | free(bf, M_TEMP); |
179 | return error; | | 180 | return error; |
180 | } | | 181 | } |
181 | | | 182 | |
182 | /* | | 183 | /* |
183 | * Linux compatible /proc/devices. Only active when the -o linux | | 184 | * Linux compatible /proc/devices. Only active when the -o linux |
184 | * mountflag is used. | | 185 | * mountflag is used. |
185 | */ | | 186 | */ |
186 | int | | 187 | int |
187 | procfs_dodevices(struct lwp *curl, struct proc *p, | | 188 | procfs_dodevices(struct lwp *curl, struct proc *p, |
188 | struct pfsnode *pfs, struct uio *uio) | | 189 | struct pfsnode *pfs, struct uio *uio) |
189 | { | | 190 | { |
190 | char *bf; | | 191 | char *bf; |
191 | int offset = 0; | | 192 | int offset = 0; |
192 | int i, error = ENAMETOOLONG; | | 193 | int i, error = ENAMETOOLONG; |
193 | | | 194 | |
194 | /* XXX elad - may need filtering. */ | | 195 | /* XXX elad - may need filtering. */ |
195 | | | 196 | |
196 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 197 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
197 | | | 198 | |
198 | offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n"); | | 199 | offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n"); |
199 | if (offset >= LBFSZ) | | 200 | if (offset >= LBFSZ) |
200 | goto out; | | 201 | goto out; |
201 | | | 202 | |
202 | mutex_enter(&device_lock); | | 203 | mutex_enter(&device_lock); |
203 | for (i = 0; i < max_devsw_convs; i++) { | | 204 | for (i = 0; i < max_devsw_convs; i++) { |
204 | if ((devsw_conv[i].d_name == NULL) || | | 205 | if ((devsw_conv[i].d_name == NULL) || |
205 | (devsw_conv[i].d_cmajor == -1)) | | 206 | (devsw_conv[i].d_cmajor == -1)) |
206 | continue; | | 207 | continue; |
207 | | | 208 | |
208 | offset += snprintf(&bf[offset], LBFSZ - offset, | | 209 | offset += snprintf(&bf[offset], LBFSZ - offset, |
209 | "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name); | | 210 | "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name); |
210 | if (offset >= LBFSZ) { | | 211 | if (offset >= LBFSZ) { |
211 | mutex_exit(&device_lock); | | 212 | mutex_exit(&device_lock); |
212 | goto out; | | 213 | goto out; |
213 | } | | 214 | } |
214 | } | | 215 | } |
215 | | | 216 | |
216 | offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n"); | | 217 | offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n"); |
217 | if (offset >= LBFSZ) { | | 218 | if (offset >= LBFSZ) { |
218 | mutex_exit(&device_lock); | | 219 | mutex_exit(&device_lock); |
219 | goto out; | | 220 | goto out; |
220 | } | | 221 | } |
221 | | | 222 | |
222 | for (i = 0; i < max_devsw_convs; i++) { | | 223 | for (i = 0; i < max_devsw_convs; i++) { |
223 | if ((devsw_conv[i].d_name == NULL) || | | 224 | if ((devsw_conv[i].d_name == NULL) || |
224 | (devsw_conv[i].d_bmajor == -1)) | | 225 | (devsw_conv[i].d_bmajor == -1)) |
225 | continue; | | 226 | continue; |
226 | | | 227 | |
227 | offset += snprintf(&bf[offset], LBFSZ - offset, | | 228 | offset += snprintf(&bf[offset], LBFSZ - offset, |
228 | "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name); | | 229 | "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name); |
229 | if (offset >= LBFSZ) { | | 230 | if (offset >= LBFSZ) { |
230 | mutex_exit(&device_lock); | | 231 | mutex_exit(&device_lock); |
231 | goto out; | | 232 | goto out; |
232 | } | | 233 | } |
233 | } | | 234 | } |
234 | mutex_exit(&device_lock); | | 235 | mutex_exit(&device_lock); |
235 | | | 236 | |
236 | error = uiomove_frombuf(bf, offset, uio); | | 237 | error = uiomove_frombuf(bf, offset, uio); |
237 | out: | | 238 | out: |
238 | free(bf, M_TEMP); | | 239 | free(bf, M_TEMP); |
239 | return error; | | 240 | return error; |
240 | } | | 241 | } |
241 | | | 242 | |
242 | /* | | 243 | /* |
243 | * Linux compatible /proc/stat. Only active when the -o linux | | 244 | * Linux compatible /proc/stat. Only active when the -o linux |
244 | * mountflag is used. | | 245 | * mountflag is used. |
245 | */ | | 246 | */ |
246 | int | | 247 | int |
247 | procfs_docpustat(struct lwp *curl, struct proc *p, | | 248 | procfs_docpustat(struct lwp *curl, struct proc *p, |
248 | struct pfsnode *pfs, struct uio *uio) | | 249 | struct pfsnode *pfs, struct uio *uio) |
249 | { | | 250 | { |
250 | char *bf; | | 251 | char *bf; |
251 | int error; | | 252 | int error; |
252 | int len; | | 253 | int len; |
253 | #if defined(MULTIPROCESSOR) | | 254 | #if defined(MULTIPROCESSOR) |
254 | struct cpu_info *ci; | | 255 | struct cpu_info *ci; |
255 | CPU_INFO_ITERATOR cii; | | 256 | CPU_INFO_ITERATOR cii; |
256 | #endif | | 257 | #endif |
257 | int i; | | 258 | int i; |
258 | uint64_t nintr; | | 259 | uint64_t nintr; |
259 | uint64_t nswtch; | | 260 | uint64_t nswtch; |
260 | | | 261 | |
261 | error = ENAMETOOLONG; | | 262 | error = ENAMETOOLONG; |
262 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 263 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
263 | | | 264 | |
264 | len = snprintf(bf, LBFSZ, | | 265 | len = snprintf(bf, LBFSZ, |
265 | "cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", | | 266 | "cpu %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
266 | curcpu()->ci_schedstate.spc_cp_time[CP_USER], | | 267 | curcpu()->ci_schedstate.spc_cp_time[CP_USER], |
267 | curcpu()->ci_schedstate.spc_cp_time[CP_NICE], | | 268 | curcpu()->ci_schedstate.spc_cp_time[CP_NICE], |
268 | curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/, | | 269 | curcpu()->ci_schedstate.spc_cp_time[CP_SYS] /*+ [CP_INTR]*/, |
269 | curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]); | | 270 | curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]); |
270 | if (len == 0) | | 271 | if (len == 0) |
271 | goto out; | | 272 | goto out; |
272 | | | 273 | |
273 | #if defined(MULTIPROCESSOR) | | 274 | #if defined(MULTIPROCESSOR) |
274 | #define ALLCPUS CPU_INFO_FOREACH(cii, ci) | | 275 | #define ALLCPUS CPU_INFO_FOREACH(cii, ci) |
275 | #define CPUNAME ci | | 276 | #define CPUNAME ci |
276 | #else | | 277 | #else |
277 | #define ALLCPUS ; i < 1 ; | | 278 | #define ALLCPUS ; i < 1 ; |
278 | #define CPUNAME curcpu() | | 279 | #define CPUNAME curcpu() |
279 | #endif | | 280 | #endif |
280 | | | 281 | |
281 | i = 0; | | 282 | i = 0; |
282 | nintr = 0; | | 283 | nintr = 0; |
283 | nswtch = 0; | | 284 | nswtch = 0; |
284 | for (ALLCPUS) { | | 285 | for (ALLCPUS) { |
285 | len += snprintf(&bf[len], LBFSZ - len, | | 286 | len += snprintf(&bf[len], LBFSZ - len, |
286 | "cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 | | 287 | "cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 |
287 | "\n", i, | | 288 | "\n", i, |
288 | CPUNAME->ci_schedstate.spc_cp_time[CP_USER], | | 289 | CPUNAME->ci_schedstate.spc_cp_time[CP_USER], |
289 | CPUNAME->ci_schedstate.spc_cp_time[CP_NICE], | | 290 | CPUNAME->ci_schedstate.spc_cp_time[CP_NICE], |
290 | CPUNAME->ci_schedstate.spc_cp_time[CP_SYS], | | 291 | CPUNAME->ci_schedstate.spc_cp_time[CP_SYS], |
291 | CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]); | | 292 | CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]); |
292 | if (len >= LBFSZ) | | 293 | if (len >= LBFSZ) |
293 | goto out; | | 294 | goto out; |
294 | i += 1; | | 295 | i += 1; |
295 | nintr += CPUNAME->ci_data.cpu_nintr; | | 296 | nintr += CPUNAME->ci_data.cpu_nintr; |
296 | nswtch += CPUNAME->ci_data.cpu_nswtch; | | 297 | nswtch += CPUNAME->ci_data.cpu_nswtch; |
297 | } | | 298 | } |
298 | | | 299 | |
299 | len += snprintf(&bf[len], LBFSZ - len, | | 300 | len += snprintf(&bf[len], LBFSZ - len, |
300 | "disk 0 0 0 0\n" | | 301 | "disk 0 0 0 0\n" |
301 | "page %u %u\n" | | 302 | "page %u %u\n" |
302 | "swap %u %u\n" | | 303 | "swap %u %u\n" |
303 | "intr %"PRIu64"\n" | | 304 | "intr %"PRIu64"\n" |
304 | "ctxt %"PRIu64"\n" | | 305 | "ctxt %"PRIu64"\n" |
305 | "btime %"PRId64"\n", | | 306 | "btime %"PRId64"\n", |
306 | uvmexp.pageins, uvmexp.pdpageouts, | | 307 | uvmexp.pageins, uvmexp.pdpageouts, |
307 | uvmexp.pgswapin, uvmexp.pgswapout, | | 308 | uvmexp.pgswapin, uvmexp.pgswapout, |
308 | nintr, | | 309 | nintr, |
309 | nswtch, | | 310 | nswtch, |
310 | boottime.tv_sec); | | 311 | boottime.tv_sec); |
311 | if (len >= LBFSZ) | | 312 | if (len >= LBFSZ) |
312 | goto out; | | 313 | goto out; |
313 | | | 314 | |
314 | error = uiomove_frombuf(bf, len, uio); | | 315 | error = uiomove_frombuf(bf, len, uio); |
315 | out: | | 316 | out: |
316 | free(bf, M_TEMP); | | 317 | free(bf, M_TEMP); |
317 | return error; | | 318 | return error; |
318 | } | | 319 | } |
319 | | | 320 | |
320 | /* | | 321 | /* |
321 | * Linux compatible /proc/loadavg. Only active when the -o linux | | 322 | * Linux compatible /proc/loadavg. Only active when the -o linux |
322 | * mountflag is used. | | 323 | * mountflag is used. |
323 | */ | | 324 | */ |
324 | int | | 325 | int |
325 | procfs_doloadavg(struct lwp *curl, struct proc *p, | | 326 | procfs_doloadavg(struct lwp *curl, struct proc *p, |
326 | struct pfsnode *pfs, struct uio *uio) | | 327 | struct pfsnode *pfs, struct uio *uio) |
327 | { | | 328 | { |
328 | char *bf; | | 329 | char *bf; |
329 | int error; | | 330 | int error; |
330 | int len; | | 331 | int len; |
331 | | | 332 | |
332 | error = ENAMETOOLONG; | | 333 | error = ENAMETOOLONG; |
333 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 334 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
334 | | | 335 | |
335 | averunnable.fscale = FSCALE; | | 336 | averunnable.fscale = FSCALE; |
336 | len = snprintf(bf, LBFSZ, | | 337 | len = snprintf(bf, LBFSZ, |
337 | "%d.%02d %d.%02d %d.%02d %d/%d %d\n", | | 338 | "%d.%02d %d.%02d %d.%02d %d/%d %d\n", |
338 | (int)(averunnable.ldavg[0] / averunnable.fscale), | | 339 | (int)(averunnable.ldavg[0] / averunnable.fscale), |
339 | (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100), | | 340 | (int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100), |
340 | (int)(averunnable.ldavg[1] / averunnable.fscale), | | 341 | (int)(averunnable.ldavg[1] / averunnable.fscale), |
341 | (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100), | | 342 | (int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100), |
342 | (int)(averunnable.ldavg[2] / averunnable.fscale), | | 343 | (int)(averunnable.ldavg[2] / averunnable.fscale), |
343 | (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100), | | 344 | (int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100), |
344 | 1, /* number of ONPROC processes */ | | 345 | 1, /* number of ONPROC processes */ |
345 | nprocs, | | 346 | nprocs, |
346 | 30000); /* last pid */ | | 347 | 30000); /* last pid */ |
347 | if (len == 0) | | 348 | if (len == 0) |
348 | goto out; | | 349 | goto out; |
349 | | | 350 | |
350 | error = uiomove_frombuf(bf, len, uio); | | 351 | error = uiomove_frombuf(bf, len, uio); |
351 | out: | | 352 | out: |
352 | free(bf, M_TEMP); | | 353 | free(bf, M_TEMP); |
353 | return error; | | 354 | return error; |
354 | } | | 355 | } |
355 | | | 356 | |
356 | /* | | 357 | /* |
357 | * Linux compatible /proc/<pid>/statm. Only active when the -o linux | | 358 | * Linux compatible /proc/<pid>/statm. Only active when the -o linux |
358 | * mountflag is used. | | 359 | * mountflag is used. |
359 | */ | | 360 | */ |
360 | int | | 361 | int |
361 | procfs_do_pid_statm(struct lwp *curl, struct lwp *l, | | 362 | procfs_do_pid_statm(struct lwp *curl, struct lwp *l, |
362 | struct pfsnode *pfs, struct uio *uio) | | 363 | struct pfsnode *pfs, struct uio *uio) |
363 | { | | 364 | { |
364 | struct vmspace *vm; | | 365 | struct vmspace *vm; |
365 | struct proc *p = l->l_proc; | | 366 | struct proc *p = l->l_proc; |
366 | struct rusage *ru = &p->p_stats->p_ru; | | 367 | struct rusage *ru = &p->p_stats->p_ru; |
367 | char *bf; | | 368 | char *bf; |
368 | int error; | | 369 | int error; |
369 | int len; | | 370 | int len; |
370 | | | 371 | |
371 | error = ENAMETOOLONG; | | 372 | error = ENAMETOOLONG; |
372 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 373 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
373 | | | 374 | |
374 | /* XXX - we use values from vmspace, since dsl says that ru figures | | 375 | /* XXX - we use values from vmspace, since dsl says that ru figures |
375 | are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */ | | 376 | are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */ |
376 | if ((error = proc_vmspace_getref(p, &vm)) != 0) { | | 377 | if ((error = proc_vmspace_getref(p, &vm)) != 0) { |
377 | goto out; | | 378 | goto out; |
378 | } | | 379 | } |
379 | | | 380 | |
380 | len = snprintf(bf, LBFSZ, | | 381 | len = snprintf(bf, LBFSZ, |
381 | "%lu %lu %lu %lu %lu %lu %lu\n", | | 382 | "%lu %lu %lu %lu %lu %lu %lu\n", |
382 | (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ | | 383 | (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ |
383 | (unsigned long)(vm->vm_rssize), /* resident */ | | 384 | (unsigned long)(vm->vm_rssize), /* resident */ |
384 | (unsigned long)(ru->ru_ixrss), /* shared */ | | 385 | (unsigned long)(ru->ru_ixrss), /* shared */ |
385 | (unsigned long)(vm->vm_tsize), /* text size in pages */ | | 386 | (unsigned long)(vm->vm_tsize), /* text size in pages */ |
386 | (unsigned long)(vm->vm_dsize), /* data size in pages */ | | 387 | (unsigned long)(vm->vm_dsize), /* data size in pages */ |
387 | (unsigned long)(vm->vm_ssize), /* stack size in pages */ | | 388 | (unsigned long)(vm->vm_ssize), /* stack size in pages */ |
388 | (unsigned long) 0); | | 389 | (unsigned long) 0); |
389 | | | 390 | |
390 | uvmspace_free(vm); | | 391 | uvmspace_free(vm); |
391 | | | 392 | |
392 | if (len == 0) | | 393 | if (len == 0) |
393 | goto out; | | 394 | goto out; |
394 | | | 395 | |
395 | error = uiomove_frombuf(bf, len, uio); | | 396 | error = uiomove_frombuf(bf, len, uio); |
396 | out: | | 397 | out: |
397 | free(bf, M_TEMP); | | 398 | free(bf, M_TEMP); |
398 | return error; | | 399 | return error; |
399 | } | | 400 | } |
400 | | | 401 | |
401 | #define USEC_2_TICKS(x) ((x) / 10000) | | 402 | #define USEC_2_TICKS(x) ((x) / 10000) |
402 | | | 403 | |
403 | /* | | 404 | /* |
404 | * Linux compatible /proc/<pid>/stat. Only active when the -o linux | | 405 | * Linux compatible /proc/<pid>/stat. Only active when the -o linux |
405 | * mountflag is used. | | 406 | * mountflag is used. |
406 | */ | | 407 | */ |
407 | int | | 408 | int |
408 | procfs_do_pid_stat(struct lwp *curl, struct lwp *l, | | 409 | procfs_do_pid_stat(struct lwp *curl, struct lwp *l, |
409 | struct pfsnode *pfs, struct uio *uio) | | 410 | struct pfsnode *pfs, struct uio *uio) |
410 | { | | 411 | { |
411 | char *bf; | | 412 | char *bf; |
412 | struct proc *p = l->l_proc; | | 413 | struct proc *p = l->l_proc; |
413 | int len; | | 414 | int len; |
414 | struct tty *tty = p->p_session->s_ttyp; | | 415 | struct tty *tty = p->p_session->s_ttyp; |
415 | struct rusage *ru = &p->p_stats->p_ru; | | 416 | struct rusage *ru = &p->p_stats->p_ru; |
416 | struct rusage *cru = &p->p_stats->p_cru; | | 417 | struct rusage *cru = &p->p_stats->p_cru; |
417 | unsigned long stext = 0, etext = 0, sstack = 0; | | 418 | unsigned long stext = 0, etext = 0, sstack = 0; |
418 | struct timeval rt; | | 419 | struct timeval rt; |
419 | struct vmspace *vm; | | 420 | struct vmspace *vm; |
420 | int error = 0; | | 421 | int error = 0; |
421 | | | 422 | |
422 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 423 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
423 | | | 424 | |
424 | if ((error = proc_vmspace_getref(p, &vm)) != 0) { | | 425 | if ((error = proc_vmspace_getref(p, &vm)) != 0) { |
425 | goto out; | | 426 | goto out; |
426 | } | | 427 | } |
427 | | | 428 | |
428 | get_proc_size_info(l, &stext, &etext, &sstack); | | 429 | get_proc_size_info(l, &stext, &etext, &sstack); |
429 | | | 430 | |
430 | mutex_enter(proc_lock); | | 431 | mutex_enter(proc_lock); |
431 | mutex_enter(p->p_lock); | | 432 | mutex_enter(p->p_lock); |
432 | | | 433 | |
433 | calcru(p, NULL, NULL, NULL, &rt); | | 434 | calcru(p, NULL, NULL, NULL, &rt); |
434 | | | 435 | |
435 | len = snprintf(bf, LBFSZ, | | 436 | len = snprintf(bf, LBFSZ, |
436 | "%d (%s) %c %d %d %d %lld %d " | | 437 | "%d (%s) %c %d %d %d %lld %d " |
437 | "%u " | | 438 | "%u " |
438 | "%lu %lu %lu %lu %lu %lu %lu %lu " | | 439 | "%lu %lu %lu %lu %lu %lu %lu %lu " |
439 | "%d %d %d " | | 440 | "%d %d %d " |
440 | "%lld %lld %lu %lu %" PRIu64 " " | | 441 | "%lld %lld %lu %lu %" PRIu64 " " |
441 | "%lu %lu %lu " | | 442 | "%lu %lu %lu " |
442 | "%u %u " | | 443 | "%u %u " |
443 | "%u %u %u %u " | | 444 | "%u %u %u %u " |
444 | "%lu %lu %lu %d %d\n", | | 445 | "%lu %lu %lu %d %d\n", |
445 | | | 446 | |
446 | p->p_pid, | | 447 | p->p_pid, |
447 | p->p_comm, | | 448 | p->p_comm, |
448 | "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat], | | 449 | "0IR3SZD"[(p->p_stat > 6) ? 0 : (int)p->p_stat], |
449 | (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0, | | 450 | (p->p_pptr != NULL) ? p->p_pptr->p_pid : 0, |
450 | | | 451 | |
451 | p->p_pgid, | | 452 | p->p_pgid, |
452 | p->p_session->s_sid, | | 453 | p->p_session->s_sid, |
453 | (unsigned long long)(tty ? tty->t_dev : 0), | | 454 | (unsigned long long)(tty ? tty->t_dev : 0), |
454 | (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0, | | 455 | (tty && tty->t_pgrp) ? tty->t_pgrp->pg_id : 0, |
455 | | | 456 | |
456 | p->p_flag, | | 457 | p->p_flag, |
457 | | | 458 | |
458 | ru->ru_minflt, | | 459 | ru->ru_minflt, |
459 | cru->ru_minflt, | | 460 | cru->ru_minflt, |
460 | ru->ru_majflt, | | 461 | ru->ru_majflt, |
461 | cru->ru_majflt, | | 462 | cru->ru_majflt, |
462 | (long)USEC_2_TICKS(ru->ru_utime.tv_usec), | | 463 | (long)USEC_2_TICKS(ru->ru_utime.tv_usec), |
463 | (long)USEC_2_TICKS(ru->ru_stime.tv_usec), | | 464 | (long)USEC_2_TICKS(ru->ru_stime.tv_usec), |
464 | (long)USEC_2_TICKS(cru->ru_utime.tv_usec), | | 465 | (long)USEC_2_TICKS(cru->ru_utime.tv_usec), |
465 | (long)USEC_2_TICKS(cru->ru_stime.tv_usec), | | 466 | (long)USEC_2_TICKS(cru->ru_stime.tv_usec), |
466 | | | 467 | |
467 | l->l_priority, /* XXX: priority */ | | 468 | l->l_priority, /* XXX: priority */ |
468 | p->p_nice - 20, | | 469 | p->p_nice - 20, |
469 | 0, | | 470 | 0, |
470 | | | 471 | |
471 | (long long)rt.tv_sec, | | 472 | (long long)rt.tv_sec, |
472 | (long long)p->p_stats->p_start.tv_sec, | | 473 | (long long)p->p_stats->p_start.tv_sec, |
473 | (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ | | 474 | (unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */ |
474 | (unsigned long)(vm->vm_rssize), /* resident */ | | 475 | (unsigned long)(vm->vm_rssize), /* resident */ |
475 | p->p_rlimit[RLIMIT_RSS].rlim_cur, | | 476 | p->p_rlimit[RLIMIT_RSS].rlim_cur, |
476 | | | 477 | |
477 | stext, /* start code */ | | 478 | stext, /* start code */ |
478 | etext, /* end code */ | | 479 | etext, /* end code */ |
479 | sstack, /* mm start stack */ | | 480 | sstack, /* mm start stack */ |
480 | 0, /* XXX: pc */ | | 481 | 0, /* XXX: pc */ |
481 | 0, /* XXX: sp */ | | 482 | 0, /* XXX: sp */ |
482 | p->p_sigpend.sp_set.__bits[0], /* XXX: pending */ | | 483 | p->p_sigpend.sp_set.__bits[0], /* XXX: pending */ |
483 | 0, /* XXX: held */ | | 484 | 0, /* XXX: held */ |
484 | p->p_sigctx.ps_sigignore.__bits[0], /* ignored */ | | 485 | p->p_sigctx.ps_sigignore.__bits[0], /* ignored */ |
485 | p->p_sigctx.ps_sigcatch.__bits[0], /* caught */ | | 486 | p->p_sigctx.ps_sigcatch.__bits[0], /* caught */ |
486 | | | 487 | |
487 | (unsigned long)(intptr_t)l->l_wchan, | | 488 | (unsigned long)(intptr_t)l->l_wchan, |
488 | ru->ru_nvcsw, | | 489 | ru->ru_nvcsw, |
489 | ru->ru_nivcsw, | | 490 | ru->ru_nivcsw, |
490 | p->p_exitsig, | | 491 | p->p_exitsig, |
491 | 0); /* XXX: processor */ | | 492 | 0); /* XXX: processor */ |
492 | | | 493 | |
493 | mutex_exit(p->p_lock); | | 494 | mutex_exit(p->p_lock); |
494 | mutex_exit(proc_lock); | | 495 | mutex_exit(proc_lock); |
495 | | | 496 | |
496 | uvmspace_free(vm); | | 497 | uvmspace_free(vm); |
497 | | | 498 | |
498 | if (len == 0) | | 499 | if (len == 0) |
499 | goto out; | | 500 | goto out; |
500 | | | 501 | |
501 | error = uiomove_frombuf(bf, len, uio); | | 502 | error = uiomove_frombuf(bf, len, uio); |
502 | out: | | 503 | out: |
503 | free(bf, M_TEMP); | | 504 | free(bf, M_TEMP); |
504 | return error; | | 505 | return error; |
505 | } | | 506 | } |
506 | | | 507 | |
507 | int | | 508 | int |
508 | procfs_docpuinfo(struct lwp *curl, struct proc *p, | | 509 | procfs_docpuinfo(struct lwp *curl, struct proc *p, |
509 | struct pfsnode *pfs, struct uio *uio) | | 510 | struct pfsnode *pfs, struct uio *uio) |
510 | { | | 511 | { |
511 | int len = LBFSZ; | | 512 | int len = LBFSZ; |
512 | char *bf = malloc(len, M_TEMP, M_WAITOK); | | 513 | char *bf = malloc(len, M_TEMP, M_WAITOK); |
513 | int error; | | 514 | int error; |
514 | | | 515 | |
515 | if (procfs_getcpuinfstr(bf, &len) < 0) { | | 516 | if (procfs_getcpuinfstr(bf, &len) < 0) { |
516 | error = ENOSPC; | | 517 | error = ENOSPC; |
517 | goto done; | | 518 | goto done; |
518 | } | | 519 | } |
519 | | | 520 | |
520 | if (len == 0) { | | 521 | if (len == 0) { |
521 | error = 0; | | 522 | error = 0; |
522 | goto done; | | 523 | goto done; |
523 | } | | 524 | } |
524 | | | 525 | |
525 | error = uiomove_frombuf(bf, len, uio); | | 526 | error = uiomove_frombuf(bf, len, uio); |
526 | done: | | 527 | done: |
527 | free(bf, M_TEMP); | | 528 | free(bf, M_TEMP); |
528 | return error; | | 529 | return error; |
529 | } | | 530 | } |
530 | | | 531 | |
531 | int | | 532 | int |
532 | procfs_douptime(struct lwp *curl, struct proc *p, | | 533 | procfs_douptime(struct lwp *curl, struct proc *p, |
533 | struct pfsnode *pfs, struct uio *uio) | | 534 | struct pfsnode *pfs, struct uio *uio) |
534 | { | | 535 | { |
535 | char *bf; | | 536 | char *bf; |
536 | int len; | | 537 | int len; |
537 | struct timeval runtime; | | 538 | struct timeval runtime; |
538 | u_int64_t idle; | | 539 | u_int64_t idle; |
539 | int error = 0; | | 540 | int error = 0; |
540 | | | 541 | |
541 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 542 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
542 | | | 543 | |
543 | microuptime(&runtime); | | 544 | microuptime(&runtime); |
544 | idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]; | | 545 | idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE]; |
545 | len = snprintf(bf, LBFSZ, | | 546 | len = snprintf(bf, LBFSZ, |
546 | "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n", | | 547 | "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n", |
547 | (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000, | | 548 | (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000, |
548 | idle / hz, (((idle % hz) * 100) / hz) % 100); | | 549 | idle / hz, (((idle % hz) * 100) / hz) % 100); |
549 | | | 550 | |
550 | if (len == 0) | | 551 | if (len == 0) |
551 | goto out; | | 552 | goto out; |
552 | | | 553 | |
553 | error = uiomove_frombuf(bf, len, uio); | | 554 | error = uiomove_frombuf(bf, len, uio); |
554 | out: | | 555 | out: |
555 | free(bf, M_TEMP); | | 556 | free(bf, M_TEMP); |
556 | return error; | | 557 | return error; |
557 | } | | 558 | } |
558 | | | 559 | |
559 | int | | 560 | int |
560 | procfs_domounts(struct lwp *curl, struct proc *p, | | 561 | procfs_domounts(struct lwp *curl, struct proc *p, |
561 | struct pfsnode *pfs, struct uio *uio) | | 562 | struct pfsnode *pfs, struct uio *uio) |
562 | { | | 563 | { |
563 | char *bf, *mtab = NULL; | | 564 | char *bf, *mtab = NULL; |
564 | const char *fsname; | | 565 | const char *fsname; |
565 | size_t len, mtabsz = 0; | | 566 | size_t len, mtabsz = 0; |
566 | struct mount *mp, *nmp; | | 567 | struct mount *mp, *nmp; |
567 | struct statvfs *sfs; | | 568 | struct statvfs sfs; |
568 | int error = 0; | | 569 | int error = 0, suser; |
| | | 570 | |
| | | 571 | suser = kauth_authorize_generic(curl->l_cred, |
| | | 572 | KAUTH_GENERIC_ISSUSER, NULL) == 0; |
569 | | | 573 | |
570 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 574 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
571 | mutex_enter(&mountlist_lock); | | 575 | mutex_enter(&mountlist_lock); |
572 | for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; | | 576 | for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; |
573 | mp = nmp) { | | 577 | mp = nmp) { |
574 | if (vfs_busy(mp, &nmp)) { | | 578 | if (vfs_busy(mp, &nmp)) |
575 | continue; | | 579 | continue; |
576 | } | | | |
577 | | | 580 | |
578 | sfs = &mp->mnt_stat; | | 581 | if (dostatvfs(mp, &sfs, curl, MNT_WAIT, suser) != 0) |
| | | 582 | continue; |
579 | | | 583 | |
580 | /* Linux uses different names for some filesystems */ | | 584 | /* Linux uses different names for some filesystems */ |
581 | fsname = sfs->f_fstypename; | | 585 | fsname = sfs.f_fstypename; |
582 | if (strcmp(fsname, "procfs") == 0) | | 586 | if (strcmp(fsname, "procfs") == 0) |
583 | fsname = "proc"; | | 587 | fsname = "proc"; |
584 | else if (strcmp(fsname, "ext2fs") == 0) | | 588 | else if (strcmp(fsname, "ext2fs") == 0) |
585 | fsname = "ext2"; | | 589 | fsname = "ext2"; |
586 | | | 590 | |
587 | len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n", | | 591 | len = snprintf(bf, LBFSZ, "%s %s %s %s%s%s%s%s%s 0 0\n", |
588 | sfs->f_mntfromname, | | 592 | sfs.f_mntfromname, |
589 | sfs->f_mntonname, | | 593 | sfs.f_mntonname, |
590 | fsname, | | 594 | fsname, |
591 | (mp->mnt_flag & MNT_RDONLY) ? "ro" : "rw", | | 595 | (sfs.f_flag & ST_RDONLY) ? "ro" : "rw", |
592 | (mp->mnt_flag & MNT_NOSUID) ? ",nosuid" : "", | | 596 | (sfs.f_flag & ST_NOSUID) ? ",nosuid" : "", |
593 | (mp->mnt_flag & MNT_NOEXEC) ? ",noexec" : "", | | 597 | (sfs.f_flag & ST_NOEXEC) ? ",noexec" : "", |
594 | (mp->mnt_flag & MNT_NODEV) ? ",nodev" : "", | | 598 | (sfs.f_flag & ST_NODEV) ? ",nodev" : "", |
595 | (mp->mnt_flag & MNT_SYNCHRONOUS) ? ",sync" : "", | | 599 | (sfs.f_flag & ST_SYNCHRONOUS) ? ",sync" : "", |
596 | (mp->mnt_flag & MNT_NOATIME) ? ",noatime" : "" | | 600 | (sfs.f_flag & ST_NOATIME) ? ",noatime" : "" |
597 | ); | | 601 | ); |
598 | | | 602 | |
599 | mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK); | | 603 | mtab = realloc(mtab, mtabsz + len, M_TEMP, M_WAITOK); |
600 | memcpy(mtab + mtabsz, bf, len); | | 604 | memcpy(mtab + mtabsz, bf, len); |
601 | mtabsz += len; | | 605 | mtabsz += len; |
602 | | | 606 | |
603 | vfs_unbusy(mp, false, &nmp); | | 607 | vfs_unbusy(mp, false, &nmp); |
604 | } | | 608 | } |
605 | mutex_exit(&mountlist_lock); | | 609 | mutex_exit(&mountlist_lock); |
606 | free(bf, M_TEMP); | | 610 | free(bf, M_TEMP); |
607 | | | 611 | |
608 | if (mtabsz > 0) { | | 612 | if (mtabsz > 0) { |
609 | error = uiomove_frombuf(mtab, mtabsz, uio); | | 613 | error = uiomove_frombuf(mtab, mtabsz, uio); |
610 | free(mtab, M_TEMP); | | 614 | free(mtab, M_TEMP); |
611 | } | | 615 | } |
612 | | | 616 | |
613 | return error; | | 617 | return error; |
614 | } | | 618 | } |
615 | | | 619 | |
616 | /* | | 620 | /* |
617 | * Linux compatible /proc/version. Only active when the -o linux | | 621 | * Linux compatible /proc/version. Only active when the -o linux |
618 | * mountflag is used. | | 622 | * mountflag is used. |
619 | */ | | 623 | */ |
620 | int | | 624 | int |
621 | procfs_doversion(struct lwp *curl, struct proc *p, | | 625 | procfs_doversion(struct lwp *curl, struct proc *p, |
622 | struct pfsnode *pfs, struct uio *uio) | | 626 | struct pfsnode *pfs, struct uio *uio) |
623 | { | | 627 | { |
624 | char *bf; | | 628 | char *bf; |
625 | char lostype[20], losrelease[20], lversion[80]; | | 629 | char lostype[20], losrelease[20], lversion[80]; |
626 | const char *postype, *posrelease, *pversion; | | 630 | const char *postype, *posrelease, *pversion; |
627 | const char *emulname = curlwp->l_proc->p_emul->e_name; | | 631 | const char *emulname = curlwp->l_proc->p_emul->e_name; |
628 | int len; | | 632 | int len; |
629 | int error = 0; | | 633 | int error = 0; |
630 | int nm[4]; | | 634 | int nm[4]; |
631 | size_t buflen; | | 635 | size_t buflen; |
632 | | | 636 | |
633 | CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE); | | 637 | CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE); |
634 | CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE); | | 638 | CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE); |
635 | CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION); | | 639 | CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION); |
636 | | | 640 | |
637 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); | | 641 | bf = malloc(LBFSZ, M_TEMP, M_WAITOK); |
638 | | | 642 | |
639 | sysctl_lock(false); | | 643 | sysctl_lock(false); |
640 | | | 644 | |
641 | if (strncmp(emulname, "linux", 5) == 0) { | | 645 | if (strncmp(emulname, "linux", 5) == 0) { |
642 | /* | | 646 | /* |
643 | * Lookup the emulation ostype, osrelease, and version. | | 647 | * Lookup the emulation ostype, osrelease, and version. |
644 | * Since compat_linux and compat_linux32 can be built as | | 648 | * Since compat_linux and compat_linux32 can be built as |
645 | * modules, we use sysctl to obtain the values instead of | | 649 | * modules, we use sysctl to obtain the values instead of |
646 | * using the symbols directly. | | 650 | * using the symbols directly. |
647 | */ | | 651 | */ |
648 | | | 652 | |
649 | if (strcmp(emulname, "linux32") == 0) { | | 653 | if (strcmp(emulname, "linux32") == 0) { |
650 | nm[0] = CTL_EMUL; | | 654 | nm[0] = CTL_EMUL; |
651 | nm[1] = EMUL_LINUX32; | | 655 | nm[1] = EMUL_LINUX32; |
652 | nm[2] = EMUL_LINUX32_KERN; | | 656 | nm[2] = EMUL_LINUX32_KERN; |
653 | } else { | | 657 | } else { |
654 | nm[0] = CTL_EMUL; | | 658 | nm[0] = CTL_EMUL; |
655 | nm[1] = EMUL_LINUX; | | 659 | nm[1] = EMUL_LINUX; |
656 | nm[2] = EMUL_LINUX_KERN; | | 660 | nm[2] = EMUL_LINUX_KERN; |
657 | } | | 661 | } |
658 | | | 662 | |
659 | nm[3] = EMUL_LINUX_KERN_OSTYPE; | | 663 | nm[3] = EMUL_LINUX_KERN_OSTYPE; |
660 | buflen = sizeof(lostype); | | 664 | buflen = sizeof(lostype); |
661 | error = sysctl_dispatch(nm, __arraycount(nm), | | 665 | error = sysctl_dispatch(nm, __arraycount(nm), |
662 | lostype, &buflen, | | 666 | lostype, &buflen, |
663 | NULL, 0, NULL, NULL, NULL); | | 667 | NULL, 0, NULL, NULL, NULL); |
664 | if (error) | | 668 | if (error) |
665 | goto out; | | 669 | goto out; |
666 | | | 670 | |
667 | nm[3] = EMUL_LINUX_KERN_OSRELEASE; | | 671 | nm[3] = EMUL_LINUX_KERN_OSRELEASE; |
668 | buflen = sizeof(losrelease); | | 672 | buflen = sizeof(losrelease); |
669 | error = sysctl_dispatch(nm, __arraycount(nm), | | 673 | error = sysctl_dispatch(nm, __arraycount(nm), |
670 | losrelease, &buflen, | | 674 | losrelease, &buflen, |
671 | NULL, 0, NULL, NULL, NULL); | | 675 | NULL, 0, NULL, NULL, NULL); |
672 | if (error) | | 676 | if (error) |
673 | goto out; | | 677 | goto out; |
674 | | | 678 | |
675 | nm[3] = EMUL_LINUX_KERN_VERSION; | | 679 | nm[3] = EMUL_LINUX_KERN_VERSION; |
676 | buflen = sizeof(lversion); | | 680 | buflen = sizeof(lversion); |
677 | error = sysctl_dispatch(nm, __arraycount(nm), | | 681 | error = sysctl_dispatch(nm, __arraycount(nm), |
678 | lversion, &buflen, | | 682 | lversion, &buflen, |
679 | NULL, 0, NULL, NULL, NULL); | | 683 | NULL, 0, NULL, NULL, NULL); |
680 | if (error) | | 684 | if (error) |
681 | goto out; | | 685 | goto out; |
682 | | | 686 | |
683 | postype = lostype; | | 687 | postype = lostype; |
684 | posrelease = losrelease; | | 688 | posrelease = losrelease; |
685 | pversion = lversion; | | 689 | pversion = lversion; |
686 | } else { | | 690 | } else { |
687 | postype = ostype; | | 691 | postype = ostype; |
688 | posrelease = osrelease; | | 692 | posrelease = osrelease; |
689 | strlcpy(lversion, version, sizeof(lversion)); | | 693 | strlcpy(lversion, version, sizeof(lversion)); |
690 | if (strchr(lversion, '\n')) | | 694 | if (strchr(lversion, '\n')) |
691 | *strchr(lversion, '\n') = '\0'; | | 695 | *strchr(lversion, '\n') = '\0'; |
692 | pversion = lversion; | | 696 | pversion = lversion; |
693 | } | | 697 | } |
694 | | | 698 | |
695 | len = snprintf(bf, LBFSZ, | | 699 | len = snprintf(bf, LBFSZ, |
696 | "%s version %s (%s@localhost) (gcc version %s) %s\n", | | 700 | "%s version %s (%s@localhost) (gcc version %s) %s\n", |
697 | postype, posrelease, emulname, | | 701 | postype, posrelease, emulname, |
698 | #ifdef __VERSION__ | | 702 | #ifdef __VERSION__ |
699 | __VERSION__, | | 703 | __VERSION__, |
700 | #else | | 704 | #else |
701 | "unknown", | | 705 | "unknown", |
702 | #endif | | 706 | #endif |
703 | pversion); | | 707 | pversion); |
704 | | | 708 | |
705 | if (len == 0) | | 709 | if (len == 0) |
706 | goto out; | | 710 | goto out; |
707 | | | 711 | |
708 | error = uiomove_frombuf(bf, len, uio); | | 712 | error = uiomove_frombuf(bf, len, uio); |
709 | out: | | 713 | out: |
710 | free(bf, M_TEMP); | | 714 | free(bf, M_TEMP); |
711 | sysctl_unlock(); | | 715 | sysctl_unlock(); |
712 | return error; | | 716 | return error; |
713 | } | | 717 | } |