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