Home | History | Annotate | Line # | Download | only in procfs
procfs_linux.c revision 1.70.10.1
      1 /*      $NetBSD: procfs_linux.c,v 1.70.10.1 2020/01/21 19:23:38 martin 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.70.10.1 2020/01/21 19:23:38 martin 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 lwp *l, unsigned long *stext, unsigned long *etext, unsigned long *sstack)
     79 {
     80 	struct proc *p = l->l_proc;
     81 	struct vmspace *vm;
     82 	struct vm_map *map;
     83 	struct vm_map_entry *entry;
     84 
     85 	*stext = 0;
     86 	*etext = 0;
     87 	*sstack = 0;
     88 
     89 	proc_vmspace_getref(p, &vm);
     90 	map = &vm->vm_map;
     91 	vm_map_lock_read(map);
     92 
     93 	for (entry = map->header.next; entry != &map->header;
     94 	    entry = entry->next) {
     95 		if (UVM_ET_ISSUBMAP(entry))
     96 			continue;
     97 		/* assume text is the first entry */
     98 		if (*stext == *etext) {
     99 			*stext = entry->start;
    100 			*etext = entry->end;
    101 			break;
    102 		}
    103 	}
    104 #if defined(LINUX_USRSTACK32) && defined(USRSTACK32)
    105 	if (strcmp(p->p_emul->e_name, "linux32") == 0 &&
    106 	    LINUX_USRSTACK32 < USRSTACK32)
    107 		*sstack = (unsigned long)LINUX_USRSTACK32;
    108 	else
    109 #endif
    110 #ifdef LINUX_USRSTACK
    111 	if (strcmp(p->p_emul->e_name, "linux") == 0 &&
    112 	    LINUX_USRSTACK < USRSTACK)
    113 		*sstack = (unsigned long)LINUX_USRSTACK;
    114 	else
    115 #endif
    116 #ifdef	USRSTACK32
    117 	if (strstr(p->p_emul->e_name, "32") != NULL)
    118 		*sstack = (unsigned long)USRSTACK32;
    119 	else
    120 #endif
    121 		*sstack = (unsigned long)USRSTACK;
    122 
    123 	/*
    124 	 * jdk 1.6 compares low <= addr && addr < high
    125 	 * if we put addr == high, then the test fails
    126 	 * so eat one page.
    127 	 */
    128 	*sstack -= PAGE_SIZE;
    129 
    130 	vm_map_unlock_read(map);
    131 	uvmspace_free(vm);
    132 }
    133 
    134 /*
    135  * Linux compatible /proc/meminfo. Only active when the -o linux
    136  * mountflag is used.
    137  */
    138 int
    139 procfs_domeminfo(struct lwp *curl, struct proc *p,
    140     struct pfsnode *pfs, struct uio *uio)
    141 {
    142 	char *bf;
    143 	int len;
    144 	int error = 0;
    145 
    146 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    147 
    148 	len = snprintf(bf, LBFSZ,
    149 		"        total:    used:    free:  shared: buffers: cached:\n"
    150 		"Mem:  %8lu %8lu %8lu %8lu %8lu %8lu\n"
    151 		"Swap: %8lu %8lu %8lu\n"
    152 		"MemTotal:  %8lu kB\n"
    153 		"MemFree:   %8lu kB\n"
    154 		"MemShared: %8lu kB\n"
    155 		"Buffers:   %8lu kB\n"
    156 		"Cached:    %8lu kB\n"
    157 		"SwapTotal: %8lu kB\n"
    158 		"SwapFree:  %8lu kB\n",
    159 		PGTOB(uvmexp.npages),
    160 		PGTOB(uvmexp.npages - uvmexp.free),
    161 		PGTOB(uvmexp.free),
    162 		0L,
    163 		PGTOB(uvmexp.filepages),
    164 		PGTOB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    165 		PGTOB(uvmexp.swpages),
    166 		PGTOB(uvmexp.swpginuse),
    167 		PGTOB(uvmexp.swpages - uvmexp.swpginuse),
    168 		PGTOKB(uvmexp.npages),
    169 		PGTOKB(uvmexp.free),
    170 		0L,
    171 		PGTOKB(uvmexp.filepages),
    172 		PGTOKB(uvmexp.anonpages + uvmexp.filepages + uvmexp.execpages),
    173 		PGTOKB(uvmexp.swpages),
    174 		PGTOKB(uvmexp.swpages - uvmexp.swpginuse));
    175 
    176 	if (len == 0)
    177 		goto out;
    178 
    179 	error = uiomove_frombuf(bf, len, uio);
    180 out:
    181 	free(bf, M_TEMP);
    182 	return error;
    183 }
    184 
    185 /*
    186  * Linux compatible /proc/devices. Only active when the -o linux
    187  * mountflag is used.
    188  */
    189 int
    190 procfs_dodevices(struct lwp *curl, struct proc *p,
    191     struct pfsnode *pfs, struct uio *uio)
    192 {
    193 	char *bf;
    194 	int offset = 0;
    195 	int i, error = ENAMETOOLONG;
    196 
    197 	/* XXX elad - may need filtering. */
    198 
    199 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    200 
    201 	offset += snprintf(&bf[offset], LBFSZ - offset, "Character devices:\n");
    202 	if (offset >= LBFSZ)
    203 		goto out;
    204 
    205 	mutex_enter(&device_lock);
    206 	for (i = 0; i < max_devsw_convs; i++) {
    207 		if ((devsw_conv[i].d_name == NULL) ||
    208 		    (devsw_conv[i].d_cmajor == -1))
    209 			continue;
    210 
    211 		offset += snprintf(&bf[offset], LBFSZ - offset,
    212 		    "%3d %s\n", devsw_conv[i].d_cmajor, devsw_conv[i].d_name);
    213 		if (offset >= LBFSZ) {
    214 			mutex_exit(&device_lock);
    215 			goto out;
    216 		}
    217 	}
    218 
    219 	offset += snprintf(&bf[offset], LBFSZ - offset, "\nBlock devices:\n");
    220 	if (offset >= LBFSZ) {
    221 		mutex_exit(&device_lock);
    222 		goto out;
    223 	}
    224 
    225 	for (i = 0; i < max_devsw_convs; i++) {
    226 		if ((devsw_conv[i].d_name == NULL) ||
    227 		    (devsw_conv[i].d_bmajor == -1))
    228 			continue;
    229 
    230 		offset += snprintf(&bf[offset], LBFSZ - offset,
    231 		    "%3d %s\n", devsw_conv[i].d_bmajor, devsw_conv[i].d_name);
    232 		if (offset >= LBFSZ) {
    233 			mutex_exit(&device_lock);
    234 			goto out;
    235 		}
    236 	}
    237 	mutex_exit(&device_lock);
    238 
    239 	error = uiomove_frombuf(bf, offset, uio);
    240 out:
    241 	free(bf, M_TEMP);
    242 	return error;
    243 }
    244 
    245 /*
    246  * Linux compatible /proc/stat. Only active when the -o linux
    247  * mountflag is used.
    248  */
    249 int
    250 procfs_docpustat(struct lwp *curl, struct proc *p,
    251     struct pfsnode *pfs, struct uio *uio)
    252 {
    253 	char		*bf;
    254 	int	 	 error;
    255 	int	 	 len;
    256 #if defined(MULTIPROCESSOR)
    257         struct cpu_info *ci;
    258         CPU_INFO_ITERATOR cii;
    259 #endif
    260 	int	 	 i;
    261 	uint64_t	nintr;
    262 	uint64_t	nswtch;
    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 	nintr = 0;
    286 	nswtch = 0;
    287 	for (ALLCPUS) {
    288 		len += snprintf(&bf[len], LBFSZ - len,
    289 			"cpu%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
    290 			"\n", i,
    291 			CPUNAME->ci_schedstate.spc_cp_time[CP_USER],
    292 			CPUNAME->ci_schedstate.spc_cp_time[CP_NICE],
    293 			CPUNAME->ci_schedstate.spc_cp_time[CP_SYS],
    294 			CPUNAME->ci_schedstate.spc_cp_time[CP_IDLE]);
    295 		if (len >= LBFSZ)
    296 			goto out;
    297 		i += 1;
    298 		nintr += CPUNAME->ci_data.cpu_nintr;
    299 		nswtch += CPUNAME->ci_data.cpu_nswtch;
    300 	}
    301 
    302 	len += snprintf(&bf[len], LBFSZ - len,
    303 			"disk 0 0 0 0\n"
    304 			"page %u %u\n"
    305 			"swap %u %u\n"
    306 			"intr %"PRIu64"\n"
    307 			"ctxt %"PRIu64"\n"
    308 			"btime %"PRId64"\n",
    309 			uvmexp.pageins, uvmexp.pdpageouts,
    310 			uvmexp.pgswapin, uvmexp.pgswapout,
    311 			nintr,
    312 			nswtch,
    313 			boottime.tv_sec);
    314 	if (len >= LBFSZ)
    315 		goto out;
    316 
    317 	error = uiomove_frombuf(bf, len, uio);
    318 out:
    319 	free(bf, M_TEMP);
    320 	return error;
    321 }
    322 
    323 /*
    324  * Linux compatible /proc/loadavg. Only active when the -o linux
    325  * mountflag is used.
    326  */
    327 int
    328 procfs_doloadavg(struct lwp *curl, struct proc *p,
    329     struct pfsnode *pfs, struct uio *uio)
    330 {
    331 	char	*bf;
    332 	int 	 error;
    333 	int 	 len;
    334 
    335 	error = ENAMETOOLONG;
    336 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    337 
    338 	averunnable.fscale = FSCALE;
    339 	len = snprintf(bf, LBFSZ,
    340 	        "%d.%02d %d.%02d %d.%02d %d/%d %d\n",
    341 		(int)(averunnable.ldavg[0] / averunnable.fscale),
    342 		(int)(averunnable.ldavg[0] * 100 / averunnable.fscale % 100),
    343 		(int)(averunnable.ldavg[1] / averunnable.fscale),
    344 		(int)(averunnable.ldavg[1] * 100 / averunnable.fscale % 100),
    345 		(int)(averunnable.ldavg[2] / averunnable.fscale),
    346 		(int)(averunnable.ldavg[2] * 100 / averunnable.fscale % 100),
    347 		1,		/* number of ONPROC processes */
    348 		nprocs,
    349 		30000);		/* last pid */
    350 	if (len == 0)
    351 		goto out;
    352 
    353 	error = uiomove_frombuf(bf, len, uio);
    354 out:
    355 	free(bf, M_TEMP);
    356 	return error;
    357 }
    358 
    359 /*
    360  * Linux compatible /proc/<pid>/statm. Only active when the -o linux
    361  * mountflag is used.
    362  */
    363 int
    364 procfs_do_pid_statm(struct lwp *curl, struct lwp *l,
    365     struct pfsnode *pfs, struct uio *uio)
    366 {
    367 	struct vmspace	*vm;
    368 	struct proc	*p = l->l_proc;
    369 	struct rusage	*ru = &p->p_stats->p_ru;
    370 	char		*bf;
    371 	int	 	 error;
    372 	int	 	 len;
    373 
    374 	error = ENAMETOOLONG;
    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 	len = snprintf(bf, LBFSZ,
    384 	        "%lu %lu %lu %lu %lu %lu %lu\n",
    385 		(unsigned long)(vm->vm_tsize + vm->vm_dsize + vm->vm_ssize), /* size */
    386 		(unsigned long)(vm->vm_rssize),	/* resident */
    387 		(unsigned long)(ru->ru_ixrss),	/* shared */
    388 		(unsigned long)(vm->vm_tsize),	/* text size in pages */
    389 		(unsigned long)(vm->vm_dsize),	/* data size in pages */
    390 		(unsigned long)(vm->vm_ssize),	/* stack size in pages */
    391 		(unsigned long) 0);
    392 
    393 	uvmspace_free(vm);
    394 
    395 	if (len == 0)
    396 		goto out;
    397 
    398 	error = uiomove_frombuf(bf, len, uio);
    399 out:
    400 	free(bf, M_TEMP);
    401 	return error;
    402 }
    403 
    404 #define UTIME2TICKS(s,u)	(((uint64_t)(s) * 1000000 + (u)) / 10000)
    405 
    406 /*
    407  * Linux compatible /proc/<pid>/stat. Only active when the -o linux
    408  * mountflag is used.
    409  */
    410 int
    411 procfs_do_pid_stat(struct lwp *curl, struct lwp *l,
    412     struct pfsnode *pfs, struct uio *uio)
    413 {
    414 	char *bf;
    415 	struct proc *p = l->l_proc;
    416 	int len;
    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 	struct kinfo_proc2 ki;
    422 	int error = 0;
    423 
    424 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    425 
    426 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
    427 		goto out;
    428 	}
    429 
    430 	get_proc_size_info(l, &stext, &etext, &sstack);
    431 
    432 	mutex_enter(proc_lock);
    433 	mutex_enter(p->p_lock);
    434 
    435 	memset(&ki, 0, sizeof(ki));
    436 	fill_kproc2(p, &ki, false);
    437 	calcru(p, NULL, NULL, NULL, &rt);
    438 
    439 	len = snprintf(bf, LBFSZ,
    440 	    "%d (%s) %c %d %d %d %u %d "
    441 	    "%u "
    442 	    "%"PRIu64" %lu %"PRIu64" %lu %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" "
    443 	    "%d %d %"PRIu64" "
    444 	    "%lld %"PRIu64" %"PRId64" %lu %"PRIu64" "
    445 	    "%lu %lu %lu "
    446 	    "%u %u "
    447 	    "%u %u %u %u "
    448 	    "%"PRIu64" %"PRIu64" %"PRIu64" %d %"PRIu64"\n",
    449 
    450 	    ki.p_pid,						/* 1 pid */
    451 	    ki.p_comm,						/* 2 tcomm */
    452 	    "0RRSTZXR8"[(ki.p_stat > 8) ? 0 : (int)ki.p_stat],	/* 3 state */
    453 	    ki.p_ppid,						/* 4 ppid */
    454 	    ki.p__pgid,						/* 5 pgrp */
    455 	    ki.p_sid,						/* 6 sid */
    456 	    (ki.p_tdev != (uint32_t)NODEV) ? ki.p_tdev : 0,	/* 7 tty_nr */
    457 	    ki.p_tpgid,						/* 8 tty_pgrp */
    458 
    459 	    ki.p_flag,						/* 9 flags */
    460 
    461 	    ki.p_uru_minflt,					/* 10 min_flt */
    462 	    cru->ru_minflt,
    463 	    ki.p_uru_majflt,					/* 12 maj_flt */
    464 	    cru->ru_majflt,
    465 	    UTIME2TICKS(ki.p_uutime_sec, ki.p_uutime_usec),	/* 14 utime */
    466 	    UTIME2TICKS(ki.p_ustime_sec, ki.p_ustime_usec),	/* 15 stime */
    467 	    UTIME2TICKS(cru->ru_utime.tv_sec, cru->ru_utime.tv_usec), /* 16 cutime */
    468 	    UTIME2TICKS(cru->ru_stime.tv_sec, cru->ru_stime.tv_usec), /* 17 cstime */
    469 
    470 	    ki.p_priority,				/* XXX: 18 priority */
    471 	    ki.p_nice - NZERO,				/* 19 nice */
    472 	    ki.p_nlwps,					/* 20 num_threads */
    473 
    474 	    (long long)rt.tv_sec,
    475 	    UTIME2TICKS(ki.p_ustart_sec, ki.p_ustart_usec), /* 22 start_time */
    476 	    ki.p_vm_msize,				/* 23 vsize */
    477 	    PGTOKB(ki.p_vm_rssize),			/* 24 rss */
    478 	    p->p_rlimit[RLIMIT_RSS].rlim_cur,		/* 25 rsslim */
    479 
    480 	    stext,					/* 26 start_code */
    481 	    etext,					/* 27 end_code */
    482 	    sstack,					/* 28 start_stack */
    483 
    484 	    0,						/* XXX: 29 esp */
    485 	    0,						/* XXX: 30 eip */
    486 
    487 	    ki.p_siglist.__bits[0],			/* XXX: 31 pending */
    488 	    0,						/* XXX: 32 blocked */
    489 	    ki.p_sigignore.__bits[0],		/* 33 sigign */
    490 	    ki.p_sigcatch.__bits[0],		/* 34 sigcatch */
    491 
    492 	    ki.p_wchan,					/* 35 wchan */
    493 	    ki.p_uru_nvcsw,
    494 	    ki.p_uru_nivcsw,
    495 	    ki.p_exitsig,				/* 38 exit_signal */
    496 	    ki.p_cpuid);				/* 39 task_cpu */
    497 
    498 	mutex_exit(p->p_lock);
    499 	mutex_exit(proc_lock);
    500 
    501 	uvmspace_free(vm);
    502 
    503 	if (len == 0)
    504 		goto out;
    505 
    506 	error = uiomove_frombuf(bf, len, uio);
    507 out:
    508 	free(bf, M_TEMP);
    509 	return error;
    510 }
    511 
    512 int
    513 procfs_docpuinfo(struct lwp *curl, struct proc *p,
    514     struct pfsnode *pfs, struct uio *uio)
    515 {
    516 	size_t len = LBFSZ;
    517 	char *bf = NULL;
    518 	int error;
    519 
    520 	do {
    521 		if (bf)
    522 			free(bf, M_TEMP);
    523 		bf = malloc(len, M_TEMP, M_WAITOK);
    524 	} while (procfs_getcpuinfstr(bf, &len) < 0);
    525 
    526 	if (len == 0) {
    527 		error = 0;
    528 		goto done;
    529 	}
    530 
    531 	error = uiomove_frombuf(bf, len, uio);
    532 done:
    533 	free(bf, M_TEMP);
    534 	return error;
    535 }
    536 
    537 int
    538 procfs_douptime(struct lwp *curl, struct proc *p,
    539     struct pfsnode *pfs, struct uio *uio)
    540 {
    541 	char *bf;
    542 	int len;
    543 	struct timeval runtime;
    544 	u_int64_t idle;
    545 	int error = 0;
    546 
    547 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    548 
    549 	microuptime(&runtime);
    550 	idle = curcpu()->ci_schedstate.spc_cp_time[CP_IDLE];
    551 	len = snprintf(bf, LBFSZ,
    552 	    "%lld.%02lu %" PRIu64 ".%02" PRIu64 "\n",
    553 	    (long long)runtime.tv_sec, (long)runtime.tv_usec / 10000,
    554 	    idle / hz, (((idle % hz) * 100) / hz) % 100);
    555 
    556 	if (len == 0)
    557 		goto out;
    558 
    559 	error = uiomove_frombuf(bf, len, uio);
    560 out:
    561 	free(bf, M_TEMP);
    562 	return error;
    563 }
    564 
    565 static int
    566 procfs_format_sfs(char **mtab, size_t *mlen, char *buf, size_t blen,
    567     const struct statvfs *sfs, struct lwp *curl, int suser)
    568 {
    569 	const char *fsname;
    570 
    571 	/* Linux uses different names for some filesystems */
    572 	fsname = sfs->f_fstypename;
    573 	if (strcmp(fsname, "procfs") == 0)
    574 		fsname = "proc";
    575 	else if (strcmp(fsname, "ext2fs") == 0)
    576 		fsname = "ext2";
    577 
    578 	blen = snprintf(buf, blen, "%s %s %s %s%s%s%s%s%s 0 0\n",
    579 	    sfs->f_mntfromname, sfs->f_mntonname, fsname,
    580 	    (sfs->f_flag & ST_RDONLY) ? "ro" : "rw",
    581 	    (sfs->f_flag & ST_NOSUID) ? ",nosuid" : "",
    582 	    (sfs->f_flag & ST_NOEXEC) ? ",noexec" : "",
    583 	    (sfs->f_flag & ST_NODEV) ? ",nodev" : "",
    584 	    (sfs->f_flag & ST_SYNCHRONOUS) ? ",sync" : "",
    585 	    (sfs->f_flag & ST_NOATIME) ? ",noatime" : "");
    586 
    587 	*mtab = realloc(*mtab, *mlen + blen, M_TEMP, M_WAITOK);
    588 	memcpy(*mtab + *mlen, buf, blen);
    589 	*mlen += blen;
    590 	return sfs->f_mntonname[0] == '/' && sfs->f_mntonname[1] == '\0';
    591 }
    592 
    593 int
    594 procfs_domounts(struct lwp *curl, struct proc *p,
    595     struct pfsnode *pfs, struct uio *uio)
    596 {
    597 	char *bf, *mtab = NULL;
    598 	size_t mtabsz = 0;
    599 	struct mount *mp, *nmp;
    600 	int error = 0, root = 0;
    601 	struct cwdinfo *cwdi = curl->l_proc->p_cwdi;
    602 
    603 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    604 
    605 	mutex_enter(&mountlist_lock);
    606 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
    607 		struct statvfs sfs;
    608 
    609 		if (vfs_busy(mp, &nmp))
    610 			continue;
    611 
    612 		if ((error = dostatvfs(mp, &sfs, curl, MNT_WAIT, 0)) == 0)
    613 			root |= procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
    614 			    &sfs, curl, 0);
    615 
    616 		vfs_unbusy(mp, false, &nmp);
    617 	}
    618 	mutex_exit(&mountlist_lock);
    619 
    620 	/*
    621 	 * If we are inside a chroot that is not itself a mount point,
    622 	 * fake a root entry.
    623 	 */
    624 	if (!root && cwdi->cwdi_rdir)
    625 		(void)procfs_format_sfs(&mtab, &mtabsz, bf, LBFSZ,
    626 		    &cwdi->cwdi_rdir->v_mount->mnt_stat, curl, 1);
    627 
    628 	free(bf, M_TEMP);
    629 
    630 	if (mtabsz > 0) {
    631 		error = uiomove_frombuf(mtab, mtabsz, uio);
    632 		free(mtab, M_TEMP);
    633 	}
    634 
    635 	return error;
    636 }
    637 
    638 /*
    639  * Linux compatible /proc/version. Only active when the -o linux
    640  * mountflag is used.
    641  */
    642 int
    643 procfs_doversion(struct lwp *curl, struct proc *p,
    644     struct pfsnode *pfs, struct uio *uio)
    645 {
    646 	char *bf;
    647 	char lostype[20], losrelease[20], lversion[80];
    648 	const char *postype, *posrelease, *pversion;
    649 	const char *emulname = curlwp->l_proc->p_emul->e_name;
    650 	int len;
    651 	int error = 0;
    652 	int nm[4];
    653 	size_t buflen;
    654 
    655 	CTASSERT(EMUL_LINUX_KERN_OSTYPE == EMUL_LINUX32_KERN_OSTYPE);
    656 	CTASSERT(EMUL_LINUX_KERN_OSRELEASE == EMUL_LINUX32_KERN_OSRELEASE);
    657 	CTASSERT(EMUL_LINUX_KERN_VERSION == EMUL_LINUX32_KERN_VERSION);
    658 
    659 	bf = malloc(LBFSZ, M_TEMP, M_WAITOK);
    660 
    661 	sysctl_lock(false);
    662 
    663 	if (strncmp(emulname, "linux", 5) == 0) {
    664 		/*
    665 		 * Lookup the emulation ostype, osrelease, and version.
    666 		 * Since compat_linux and compat_linux32 can be built as
    667 		 * modules, we use sysctl to obtain the values instead of
    668 		 * using the symbols directly.
    669 		 */
    670 
    671 		if (strcmp(emulname, "linux32") == 0) {
    672 			nm[0] = CTL_EMUL;
    673 			nm[1] = EMUL_LINUX32;
    674 			nm[2] = EMUL_LINUX32_KERN;
    675 		} else {
    676 			nm[0] = CTL_EMUL;
    677 			nm[1] = EMUL_LINUX;
    678 			nm[2] = EMUL_LINUX_KERN;
    679 		}
    680 
    681 		nm[3] = EMUL_LINUX_KERN_OSTYPE;
    682 		buflen = sizeof(lostype);
    683 		error = sysctl_dispatch(nm, __arraycount(nm),
    684 		    lostype, &buflen,
    685 		    NULL, 0, NULL, NULL, NULL);
    686 		if (error)
    687 			goto out;
    688 
    689 		nm[3] = EMUL_LINUX_KERN_OSRELEASE;
    690 		buflen = sizeof(losrelease);
    691 		error = sysctl_dispatch(nm, __arraycount(nm),
    692 		    losrelease, &buflen,
    693 		    NULL, 0, NULL, NULL, NULL);
    694 		if (error)
    695 			goto out;
    696 
    697 		nm[3] = EMUL_LINUX_KERN_VERSION;
    698 		buflen = sizeof(lversion);
    699 		error = sysctl_dispatch(nm, __arraycount(nm),
    700 		    lversion, &buflen,
    701 		    NULL, 0, NULL, NULL, NULL);
    702 		if (error)
    703 			goto out;
    704 
    705 		postype = lostype;
    706 		posrelease = losrelease;
    707 		pversion = lversion;
    708 	} else {
    709 		postype = ostype;
    710 		posrelease = osrelease;
    711 		strlcpy(lversion, version, sizeof(lversion));
    712 		if (strchr(lversion, '\n'))
    713 			*strchr(lversion, '\n') = '\0';
    714 		pversion = lversion;
    715 	}
    716 
    717 	len = snprintf(bf, LBFSZ,
    718 		"%s version %s (%s@localhost) (gcc version %s) %s\n",
    719 		postype, posrelease, emulname,
    720 #ifdef __VERSION__
    721 		__VERSION__,
    722 #else
    723 		"unknown",
    724 #endif
    725 		pversion);
    726 
    727 	if (len == 0)
    728 		goto out;
    729 
    730 	error = uiomove_frombuf(bf, len, uio);
    731 out:
    732 	free(bf, M_TEMP);
    733 	sysctl_unlock();
    734 	return error;
    735 }
    736