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