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