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