xref: /netbsd-src/sys/miscfs/procfs/procfs_linux.c (revision a98966d3dc31ab749747143261d1ce16c39a7633)
1 /*      $NetBSD: procfs_linux.c,v 1.77 2019/12/16 22:47:55 ad 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.77 2019/12/16 22:47:55 ad 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 = uvmexp.free;
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 	len += snprintf(&bf[len], LBFSZ - len,
301 			"disk 0 0 0 0\n"
302 			"page %u %u\n"
303 			"swap %u %u\n"
304 			"intr %"PRId64"\n"
305 			"ctxt %"PRId64"\n"
306 			"btime %"PRId64"\n",
307 			uvmexp.pageins, uvmexp.pdpageouts,
308 			uvmexp.pgswapin, uvmexp.pgswapout,
309 			cpu_count_get(CPU_COUNT_NINTR),
310 			cpu_count_get(CPU_COUNT_NSWTCH),
311 			boottime.tv_sec);
312 	if (len >= LBFSZ)
313 		goto out;
314 
315 	error = uiomove_frombuf(bf, len, uio);
316 out:
317 	free(bf, M_TEMP);
318 	return error;
319 }
320 
321 /*
322  * Linux compatible /proc/loadavg. Only active when the -o linux
323  * mountflag is used.
324  */
325 int
326 procfs_doloadavg(struct lwp *curl, struct proc *p,
327     struct pfsnode *pfs, struct uio *uio)
328 {
329 	char	*bf;
330 	int 	 error;
331 	int 	 len;
332 
333 	error = ENAMETOOLONG;
334 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
335 
336 	averunnable.fscale = FSCALE;
337 	len = snprintf(bf, LBFSZ,
338 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
339 		(int)(averunnable.ldavg[0] / averunnable.fscale),
340 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
341 		(int)(averunnable.ldavg[1] / averunnable.fscale),
342 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
343 		(int)(averunnable.ldavg[2] / averunnable.fscale),
344 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
345 		1,		/* number of ONPROC processes */
346 		nprocs,
347 		30000);		/* last pid */
348 	if (len == 0)
349 		goto out;
350 
351 	error = uiomove_frombuf(bf, len, uio);
352 out:
353 	free(bf, M_TEMP);
354 	return error;
355 }
356 
357 /*
358  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
359  * mountflag is used.
360  */
361 int
362 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
363     struct pfsnode *pfs, struct uio *uio)
364 {
365 	struct vmspace	*vm;
366 	struct proc	*p = l->l_proc;
367 	char		*bf;
368 	int	 	 error;
369 	int	 	 len;
370 	struct kinfo_proc2 ki;
371 
372 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
373 
374 	/* XXX - we use values from vmspace, since dsl says that ru figures
375 	   are always 0 except for zombies. See kvm_proc.c::kvm_getproc2() */
376 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
377 		goto out;
378 	}
379 
380 	mutex_enter(proc_lock);
381 	mutex_enter(p->p_lock);
382 
383 	/* retrieve RSS size */
384 	memset(&ki, 0, sizeof(ki));
385 	fill_kproc2(p, &ki, false, false);
386 
387 	mutex_exit(p->p_lock);
388 	mutex_exit(proc_lock);
389 
390 	uvmspace_free(vm);
391 
392 	len = snprintf(bf, LBFSZ,
393 	        "%lu %lu %lu %lu %lu %lu %lu\n",
394 		(unsigned long)(ki.p_vm_msize),	/* size */
395 		(unsigned long)(ki.p_vm_rssize),/* resident */
396 		(unsigned long)(ki.p_uru_ixrss),/* shared */
397 		(unsigned long)(ki.p_vm_tsize),	/* text */
398 		(unsigned long) 0,		/* library (unused) */
399 		(unsigned long)(ki.p_vm_dsize + ki.p_vm_ssize),	/* data+stack */
400 		(unsigned long) 0);		/* dirty */
401 
402 	if (len == 0)
403 		goto out;
404 
405 	error = uiomove_frombuf(bf, len, uio);
406 out:
407 	free(bf, M_TEMP);
408 	return error;
409 }
410 
411 #define UTIME2TICKS(s,u)	(((uint64_t)(s) * 1000000 + (u)) / 10000)
412 
413 /*
414  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
415  * mountflag is used.
416  */
417 int
418 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
419     struct pfsnode *pfs, struct uio *uio)
420 {
421 	char *bf;
422 	struct proc *p = l->l_proc;
423 	int len;
424 	struct rusage *cru = &p->p_stats->p_cru;
425 	unsigned long stext = 0, etext = 0, sstack = 0;
426 	struct timeval rt;
427 	struct vmspace	*vm;
428 	struct kinfo_proc2 ki;
429 	int error;
430 
431 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
432 
433 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
434 		goto out;
435 	}
436 
437 	get_proc_size_info(p, &vm->vm_map, &stext, &etext, &sstack);
438 
439 	mutex_enter(proc_lock);
440 	mutex_enter(p->p_lock);
441 
442 	memset(&ki, 0, sizeof(ki));
443 	fill_kproc2(p, &ki, false, false);
444 	calcru(p, NULL, NULL, NULL, &rt);
445 
446 	len = snprintf(bf, LBFSZ,
447 	    "%d (%s) %c %d %d %d %u %d "
448 	    "%u "
449 	    "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
450 	    "%d %d %"PRIu64" "
451 	    "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
452 	    "%lu %lu %lu "
453 	    "%u %u "
454 	    "%u %u %u %u "
455 	    "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
456 
457 	    ki.p_pid,						/* 1 pid */
458 	    ki.p_comm,						/* 2 tcomm */
459 	    "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat],	/* 3 state */
460 	    ki.p_ppid,						/* 4 ppid */
461 	    ki.p__pgid,						/* 5 pgrp */
462 	    ki.p_sid,						/* 6 sid */
463 	    (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0,	/* 7 tty_nr */
464 	    ki.p_tpgid,						/* 8 tty_pgrp */
465 
466 	    ki.p_flag,						/* 9 flags */
467 
468 	    ki.p_uru_minflt,					/* 10 min_flt */
469 	    cru->ru_minflt,
470 	    ki.p_uru_majflt,					/* 12 maj_flt */
471 	    cru->ru_majflt,
472 	    UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec),	/* 14 utime */
473 	    UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec),	/* 15 stime */
474 	    UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
475 	    UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
476 
477 	    ki.p_priority,				/* XXX: 18 priority */
478 	    ki.p_nice - NZERO,				/* 19 nice */
479 	    ki.p_nlwps,					/* 20 num_threads */
480 
481 	    (long long)rt.tv_sec,
482 	    UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
483 	    ki.p_vm_msize,				/* 23 vsize */
484 	    PGTOKB(ki.p_vm_rssize),			/* 24 rss */
485 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,		/* 25 rsslim */
486 
487 	    stext,					/* 26 start_code */
488 	    etext,					/* 27 end_code */
489 	    sstack,					/* 28 start_stack */
490 
491 	    0,						/* XXX: 29 esp */
492 	    0,						/* XXX: 30 eip */
493 
494 	    ki.p_siglist.__bits[0],			/* XXX: 31 pending */
495 	    0,						/* XXX: 32 blocked */
496 	    ki.p_sigignore.__bits[0],		/* 33 sigign */
497 	    ki.p_sigcatch.__bits[0],		/* 34 sigcatch */
498 
499 	    ki.p_wchan,					/* 35 wchan */
500 	    ki.p_uru_nvcsw,
501 	    ki.p_uru_nivcsw,
502 	    ki.p_exitsig,				/* 38 exit_signal */
503 	    ki.p_cpuid);				/* 39 task_cpu */
504 
505 	mutex_exit(p->p_lock);
506 	mutex_exit(proc_lock);
507 
508 	uvmspace_free(vm);
509 
510 	if (len == 0)
511 		goto out;
512 
513 	error = uiomove_frombuf(bf, len, uio);
514 out:
515 	free(bf, M_TEMP);
516 	return error;
517 }
518 
519 int
520 procfs_docpuinfo(struct lwp *curl, struct proc *p,
521     struct pfsnode *pfs, struct uio *uio)
522 {
523 	size_t len = LBFSZ;
524 	char *bf = NULL;
525 	int error;
526 
527 	do {
528 		if (bf)
529 			free(bf, M_TEMP);
530 		bf = malloc(len, M_TEMP, M_WAITOK);
531 	} while (procfs_getcpuinfstr(bf, &len) < 0);
532 
533 	if (len == 0) {
534 		error = 0;
535 		goto done;
536 	}
537 
538 	error = uiomove_frombuf(bf, len, uio);
539 done:
540 	free(bf, M_TEMP);
541 	return error;
542 }
543 
544 int
545 procfs_douptime(struct lwp *curl, struct proc *p,
546     struct pfsnode *pfs, struct uio *uio)
547 {
548 	char *bf;
549 	int len;
550 	struct timeval runtime;
551 	u_int64_t idle;
552 	int error = 0;
553 
554 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
555 
556 	microuptime(&runtime);
557 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
558 	len = snprintf(bf, LBFSZ,
559 	    "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
560 	    (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
561 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
562 
563 	if (len == 0)
564 		goto out;
565 
566 	error = uiomove_frombuf(bf, len, uio);
567 out:
568 	free(bf, M_TEMP);
569 	return error;
570 }
571 
572 static int
573 procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
574     const struct statvfs *sfs, struct lwp *curl, int suser)
575 {
576 	const char *fsname;
577 
578 	/* Linux uses different names for some filesystems */
579 	fsname = sfs->f_fstypename;
580 	if (strcmp(fsname, "procfs") == 0)
581 		fsname = "proc";
582 	else if (strcmp(fsname, "ext2fs") == 0)
583 		fsname = "ext2";
584 
585 	blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
586 	    sfs->f_mntfromname, sfs->f_mntonname, fsname,
587 	    (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
588 	    (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
589 	    (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
590 	    (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
591 	    (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
592 	    (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
593 
594 	*mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
595 	memcpy(*mtab + *mlen, buf, blen);
596 	*mlen += blen;
597 	return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
598 }
599 
600 int
601 procfs_domounts(struct lwp *curl, struct proc *p,
602     struct pfsnode *pfs, struct uio *uio)
603 {
604 	char *bf, *mtab = NULL;
605 	size_t mtabsz = 0;
606 	mount_iterator_t *iter;
607 	struct mount *mp;
608 	int error = 0, root = 0;
609 	struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
610 
611 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
612 
613 	mountlist_iterator_init(&iter);
614 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
615 		struct statvfs sfs;
616 
617 		if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
618 			root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
619 			    &sfs, curl, 0);
620 	}
621 	mountlist_iterator_destroy(iter);
622 
623 	/*
624 	 * If we are inside a chroot that is not itself a mount point,
625 	 * fake a root entry.
626 	 */
627 	if (!root && cwdi->cwdi_rdir)
628 		(void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
629 		    &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
630 
631 	free(bf, M_TEMP);
632 
633 	if (mtabsz > 0) {
634 		error = uiomove_frombuf(mtab, mtabsz, uio);
635 		free(mtab, M_TEMP);
636 	}
637 
638 	return error;
639 }
640 
641 /*
642  * Linux compatible /proc/version. Only active when the -o linux
643  * mountflag is used.
644  */
645 int
646 procfs_doversion(struct lwp *curl, struct proc *p,
647     struct pfsnode *pfs, struct uio *uio)
648 {
649 	char *bf;
650 	char lostype[20], losrelease[20], lversion[80];
651 	const char *postype, *posrelease, *pversion;
652 	const char *emulname = curlwp->l_proc->p_emul->e_name;
653 	int len;
654 	int error = 0;
655 	int nm[4];
656 	size_t buflen;
657 
658 	CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
659 	CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
660 	CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
661 
662 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
663 
664 	sysctl_lock(false);
665 
666 	if (strncmp(emulname, "linux", 5) == 0) {
667 		/*
668 		 * Lookup the emulation ostype, osrelease, and version.
669 		 * Since compat_linux and compat_linux32 can be built as
670 		 * modules, we use sysctl to obtain the values instead of
671 		 * using the symbols directly.
672 		 */
673 
674 		if (strcmp(emulname, "linux32") == 0) {
675 			nm[0] = CTL_EMUL;
676 			nm[1] = EMUL_LINUX32;
677 			nm[2] = EMUL_LINUX32_KERN;
678 		} else {
679 			nm[0] = CTL_EMUL;
680 			nm[1] = EMUL_LINUX;
681 			nm[2] = EMUL_LINUX_KERN;
682 		}
683 
684 		nm[3] = EMUL_LINUX_KERN_OSTYPE;
685 		buflen = sizeof(lostype);
686 		error = sysctl_dispatch(nm, __arraycount(nm),
687 		    lostype, &buflen,
688 		    NULL, 0, NULL, NULL, NULL);
689 		if (error)
690 			goto out;
691 
692 		nm[3] = EMUL_LINUX_KERN_OSRELEASE;
693 		buflen = sizeof(losrelease);
694 		error = sysctl_dispatch(nm, __arraycount(nm),
695 		    losrelease, &buflen,
696 		    NULL, 0, NULL, NULL, NULL);
697 		if (error)
698 			goto out;
699 
700 		nm[3] = EMUL_LINUX_KERN_VERSION;
701 		buflen = sizeof(lversion);
702 		error = sysctl_dispatch(nm, __arraycount(nm),
703 		    lversion, &buflen,
704 		    NULL, 0, NULL, NULL, NULL);
705 		if (error)
706 			goto out;
707 
708 		postype = lostype;
709 		posrelease = losrelease;
710 		pversion = lversion;
711 	} else {
712 		postype = ostype;
713 		posrelease = osrelease;
714 		strlcpy(lversion, version, sizeof(lversion));
715 		if (strchr(lversion, '\n'))
716 			*strchr(lversion, '\n') = '\0';
717 		pversion = lversion;
718 	}
719 
720 	len = snprintf(bf, LBFSZ,
721 		"%s version %s (%s@localhost) (gcc version %s) %s\n",
722 		postype, posrelease, emulname,
723 #ifdef __VERSION__
724 		__VERSION__,
725 #else
726 		"unknown",
727 #endif
728 		pversion);
729 
730 	if (len == 0)
731 		goto out;
732 
733 	error = uiomove_frombuf(bf, len, uio);
734 out:
735 	free(bf, M_TEMP);
736 	sysctl_unlock();
737 	return error;
738 }
739