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