Home | History | Annotate | Line # | Download | only in sysinst
util.c revision 1.60
      1 /*	$NetBSD: util.c,v 1.60 2021/09/25 08:54:31 maya Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Philip A. Nelson for Piermont Information 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. The name of Piermont Information Systems Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /* util.c -- routines that don't really fit anywhere else... */
     36 
     37 #include <assert.h>
     38 #include <inttypes.h>
     39 #include <stdio.h>
     40 #include <stdarg.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 #include <sys/mount.h>
     44 #include <sys/dkio.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #include <sys/sysctl.h>
     49 #include <sys/stat.h>
     50 #include <sys/statvfs.h>
     51 #include <isofs/cd9660/iso.h>
     52 #include <curses.h>
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <dirent.h>
     56 #include <util.h>
     57 #include "defs.h"
     58 #include "md.h"
     59 #include "defsizes.h"
     60 #include "msg_defs.h"
     61 #include "menu_defs.h"
     62 #ifdef MD_MAY_SWAP_TO
     63 #include <sys/drvctlio.h>
     64 #endif
     65 #ifdef CHECK_ENTROPY
     66 #include <sha2.h>
     67 #include <paths.h>
     68 #endif
     69 
     70 #define MAX_CD_DEVS	256	/* how many cd drives do we expect to attach */
     71 #define ISO_BLKSIZE	ISO_DEFAULT_BLOCK_SIZE
     72 
     73 static const char *msg_yes, *msg_no, *msg_all, *msg_some, *msg_none;
     74 static int select_menu_width;
     75 
     76 static uint8_t set_status[SET_GROUP_END];
     77 #define SET_VALID	0x01
     78 #define SET_SELECTED	0x02
     79 #define SET_SKIPPED	0x04
     80 #define SET_INSTALLED	0x08
     81 #define	SET_NO_EXTRACT	0x10
     82 
     83 struct  tarstats {
     84 	int nselected;
     85 	int nfound;
     86 	int nnotfound;
     87 	int nerror;
     88 	int nsuccess;
     89 	int nskipped;
     90 } tarstats;
     91 
     92 distinfo dist_list[] = {
     93 #ifdef SET_KERNEL_1_NAME
     94 	{SET_KERNEL_1_NAME,	SET_KERNEL_1,		false, MSG_set_kernel_1, NULL},
     95 #endif
     96 #ifdef SET_KERNEL_2_NAME
     97 	{SET_KERNEL_2_NAME,	SET_KERNEL_2,		false, MSG_set_kernel_2, NULL},
     98 #endif
     99 #ifdef SET_KERNEL_3_NAME
    100 	{SET_KERNEL_3_NAME,	SET_KERNEL_3,		false, MSG_set_kernel_3, NULL},
    101 #endif
    102 #ifdef SET_KERNEL_4_NAME
    103 	{SET_KERNEL_4_NAME,	SET_KERNEL_4,		false, MSG_set_kernel_4, NULL},
    104 #endif
    105 #ifdef SET_KERNEL_5_NAME
    106 	{SET_KERNEL_5_NAME,	SET_KERNEL_5,		false, MSG_set_kernel_5, NULL},
    107 #endif
    108 #ifdef SET_KERNEL_6_NAME
    109 	{SET_KERNEL_6_NAME,	SET_KERNEL_6,		false, MSG_set_kernel_6, NULL},
    110 #endif
    111 #ifdef SET_KERNEL_7_NAME
    112 	{SET_KERNEL_7_NAME,	SET_KERNEL_7,		false, MSG_set_kernel_7, NULL},
    113 #endif
    114 #ifdef SET_KERNEL_8_NAME
    115 	{SET_KERNEL_8_NAME,	SET_KERNEL_8,		false, MSG_set_kernel_8, NULL},
    116 #endif
    117 #ifdef SET_KERNEL_9_NAME
    118 	{SET_KERNEL_9_NAME,	SET_KERNEL_9,		false, MSG_set_kernel_9, NULL},
    119 #endif
    120 
    121 #ifdef HAVE_MODULES
    122 	{"modules",		SET_MODULES,		false, MSG_set_modules, NULL},
    123 #endif
    124 	{"base",		SET_BASE,		false, MSG_set_base, NULL},
    125 #ifdef HAVE_DTB
    126 	{"dtb",			SET_DTB,		false, MSG_set_dtb, NULL},
    127 #endif
    128 	{"etc",			SET_ETC,		false, MSG_set_system, NULL},
    129 	{"comp",		SET_COMPILER,		false, MSG_set_compiler, NULL},
    130 	{"games",		SET_GAMES,		false, MSG_set_games, NULL},
    131 #ifdef HAVE_GPUFW
    132 	{"gpufw",		SET_GPUFW,		false, MSG_set_gpufw, NULL},
    133 #endif
    134 	{"man",			SET_MAN_PAGES,		false, MSG_set_man_pages, NULL},
    135 	{"misc",		SET_MISC,		false, MSG_set_misc, NULL},
    136 	{"rescue",		SET_RESCUE,		false, MSG_set_rescue, NULL},
    137 	{"tests",		SET_TESTS,		false, MSG_set_tests, NULL},
    138 	{"text",		SET_TEXT_TOOLS,		false, MSG_set_text_tools, NULL},
    139 
    140 	{NULL,			SET_GROUP,		false, MSG_set_X11, NULL},
    141 	{"xbase",		SET_X11_BASE,		false, MSG_set_X11_base, NULL},
    142 	{"xcomp",		SET_X11_PROG,		false, MSG_set_X11_prog, NULL},
    143 	{"xetc",		SET_X11_ETC,		false, MSG_set_X11_etc, NULL},
    144 	{"xfont",		SET_X11_FONTS,		false, MSG_set_X11_fonts, NULL},
    145 	{"xserver",		SET_X11_SERVERS,	false, MSG_set_X11_servers, NULL},
    146 	{NULL,			SET_GROUP_END,		false, NULL, NULL},
    147 
    148 #ifdef SET_MD_1_NAME
    149 	{SET_MD_1_NAME,		SET_MD_1,		false, MSG_set_md_1, NULL},
    150 #endif
    151 #ifdef SET_MD_2_NAME
    152 	{SET_MD_2_NAME,		SET_MD_2,		false, MSG_set_md_2, NULL},
    153 #endif
    154 #ifdef SET_MD_3_NAME
    155 	{SET_MD_3_NAME,		SET_MD_3,		false, MSG_set_md_3, NULL},
    156 #endif
    157 #ifdef SET_MD_4_NAME
    158 	{SET_MD_4_NAME,		SET_MD_4,		false, MSG_set_md_4, NULL},
    159 #endif
    160 
    161 	{NULL,			SET_GROUP,		true, MSG_set_source, NULL},
    162 	{"syssrc",		SET_SYSSRC,		true, MSG_set_syssrc, NULL},
    163 	{"src",			SET_SRC,		true, MSG_set_src, NULL},
    164 	{"sharesrc",		SET_SHARESRC,		true, MSG_set_sharesrc, NULL},
    165 	{"gnusrc",		SET_GNUSRC,		true, MSG_set_gnusrc, NULL},
    166 	{"xsrc",		SET_XSRC,		true, MSG_set_xsrc, NULL},
    167 	{"debug",		SET_DEBUG,		false, MSG_set_debug, NULL},
    168 	{"xdebug",		SET_X11_DEBUG,		false, MSG_set_xdebug, NULL},
    169 	{NULL,			SET_GROUP_END,		false, NULL, NULL},
    170 
    171 	{NULL,			SET_LAST,		false, NULL, NULL},
    172 };
    173 
    174 #define MAX_CD_INFOS	16	/* how many media can be found? */
    175 struct cd_info {
    176 	char device_name[16];
    177 	char menu[100];
    178 };
    179 static struct cd_info cds[MAX_CD_INFOS];
    180 
    181 /* flags whether to offer the respective options (depending on helper
    182    programs available on install media */
    183 int have_raid, have_vnd, have_cgd, have_lvm, have_gpt, have_dk;
    184 
    185 /*
    186  * local prototypes
    187  */
    188 
    189 static int check_for(unsigned int mode, const char *pathname);
    190 static int get_iso9660_volname(int dev, int sess, char *volname,
    191 		size_t volnamelen);
    192 static int get_available_cds(void);
    193 static int binary_available(const char *prog);
    194 
    195 void
    196 init_set_status(int flags)
    197 {
    198 	static const uint8_t sets_valid[] = {MD_SETS_VALID};
    199 	static const uint8_t sets_selected_full[] = {MD_SETS_SELECTED};
    200 	static const uint8_t sets_selected_minimal[] = {MD_SETS_SELECTED_MINIMAL};
    201 	static const uint8_t sets_selected_nox[] = {MD_SETS_SELECTED_NOX};
    202 	static const uint8_t *sets_selected;
    203 	unsigned int nelem_selected;
    204 	unsigned int i, len;
    205 	const char *longest;
    206 
    207 	if (flags & SFLAG_MINIMAL) {
    208 		sets_selected = sets_selected_minimal;
    209 		nelem_selected = __arraycount(sets_selected_minimal);
    210 	} else if (flags & SFLAG_NOX) {
    211 		sets_selected = sets_selected_nox;
    212 		nelem_selected = __arraycount(sets_selected_nox);
    213 	} else {
    214 		sets_selected = sets_selected_full;
    215 		nelem_selected = __arraycount(sets_selected_full);
    216 	}
    217 
    218 	for (i = 0; i < __arraycount(sets_valid); i++)
    219 		set_status[sets_valid[i]] = SET_VALID;
    220 	for (i = 0; i < nelem_selected; i++)
    221 		set_status[sets_selected[i]] |= SET_SELECTED;
    222 
    223 	set_status[SET_GROUP] = SET_VALID;
    224 
    225 	/* Lookup some strings we need lots of times */
    226 	msg_yes = msg_string(MSG_Yes);
    227 	msg_no = msg_string(MSG_No);
    228 	msg_all = msg_string(MSG_All);
    229 	msg_some = msg_string(MSG_Some);
    230 	msg_none = msg_string(MSG_None);
    231 
    232 	/* Find longest and use it to determine width of selection menu */
    233 	len = strlen(msg_no); longest = msg_no;
    234 	i = strlen(msg_yes); if (i > len) {len = i; longest = msg_yes; }
    235 	i = strlen(msg_all); if (i > len) {len = i; longest = msg_all; }
    236 	i = strlen(msg_some); if (i > len) {len = i; longest = msg_some; }
    237 	i = strlen(msg_none); if (i > len) {len = i; longest = msg_none; }
    238 	select_menu_width = snprintf(NULL, 0, "%-30s %s", "", longest);
    239 
    240 	/* Give the md code a chance to choose the right kernel, etc. */
    241 	md_init_set_status(flags);
    242 }
    243 
    244 int
    245 dir_exists_p(const char *path)
    246 {
    247 
    248 	return file_mode_match(path, S_IFDIR);
    249 }
    250 
    251 int
    252 file_exists_p(const char *path)
    253 {
    254 
    255 	return file_mode_match(path, S_IFREG);
    256 }
    257 
    258 int
    259 file_mode_match(const char *path, unsigned int mode)
    260 {
    261 	struct stat st;
    262 
    263 	return (stat(path, &st) == 0 && (st.st_mode & S_IFMT) == mode);
    264 }
    265 
    266 /* return ram size in MB */
    267 uint64_t
    268 get_ramsize(void)
    269 {
    270 	static uint64_t ramsize;
    271 
    272 	if (ramsize == 0) {
    273 		size_t len = sizeof ramsize;
    274 		int mib[2] = {CTL_HW, HW_PHYSMEM64};
    275 
    276 		sysctl(mib, 2, &ramsize, &len, NULL, 0);
    277 	}
    278 
    279 	/* Find out how many Megs ... round up. */
    280 	return (ramsize + MEG - 1) / MEG;
    281 }
    282 
    283 void
    284 run_makedev(void)
    285 {
    286 	char *owd;
    287 
    288 	msg_display_add("\n\n");
    289 	msg_display_add(MSG_makedev);
    290 
    291 	owd = getcwd(NULL, 0);
    292 
    293 	/* make /dev, in case the user  didn't extract it. */
    294 	make_target_dir("/dev");
    295 	target_chdir_or_die("/dev");
    296 	run_program(0, "/bin/sh MAKEDEV all");
    297 
    298 	chdir(owd);
    299 	free(owd);
    300 }
    301 
    302 /*
    303  * Performs in-place replacement of a set of patterns in a file that lives
    304  * inside the installed system.  The patterns must be separated by a semicolon.
    305  * For example:
    306  *
    307  * replace("/etc/some-file.conf", "s/prop1=NO/prop1=YES/;s/foo/bar/");
    308  */
    309 void
    310 replace(const char *path, const char *patterns, ...)
    311 {
    312 	char *spatterns;
    313 	va_list ap;
    314 
    315 	va_start(ap, patterns);
    316 	vasprintf(&spatterns, patterns, ap);
    317 	va_end(ap);
    318 	if (spatterns == NULL)
    319 		err(1, "vasprintf(&spatterns, \"%s\", ...)", patterns);
    320 
    321 	run_program(RUN_CHROOT, "sed -an -e '%s;H;$!d;g;w %s' %s", spatterns,
    322 	    path, path);
    323 
    324 	free(spatterns);
    325 }
    326 
    327 static int
    328 floppy_fetch(const char *set_name)
    329 {
    330 	char post[4];
    331 	msg errmsg;
    332 	int menu;
    333 	int status;
    334 	const char *write_mode = ">";
    335 
    336 	strcpy(post, "aa");
    337 
    338 	errmsg = "";
    339 	menu = MENU_fdok;
    340 	for (;;) {
    341 		umount_mnt2();
    342 		msg_display(errmsg);
    343 		msg_fmt_display_add(MSG_fdmount, "%s%s", set_name, post);
    344 		process_menu(menu, &status);
    345 		if (status != SET_CONTINUE)
    346 			return status;
    347 		menu = MENU_fdremount;
    348 		errmsg = MSG_fdremount;
    349 		if (run_program(0, "/sbin/mount -r -t %s %s /mnt2",
    350 							fd_type, fd_dev))
    351 			continue;
    352 		mnt2_mounted = 1;
    353 		errmsg = MSG_fdnotfound;
    354 
    355 		/* Display this because it might take a while.... */
    356 		if (run_program(RUN_DISPLAY,
    357 			    "sh -c '/bin/cat /mnt2/%s.%s %s %s/%s/%s%s'",
    358 			    set_name, post, write_mode,
    359 			    target_prefix(), xfer_dir, set_name,
    360 			    set_postfix(set_name)))
    361 			/* XXX: a read error will give a corrupt file! */
    362 			continue;
    363 
    364 		/* We got that file, advance to next fragment */
    365 		if (post[1] < 'z')
    366 			post[1]++;
    367 		else
    368 			post[1] = 'a', post[0]++;
    369 		write_mode = ">>";
    370 		errmsg = "";
    371 		menu = MENU_fdok;
    372 	}
    373 }
    374 
    375 /*
    376  * Load files from floppy.  Requires a /mnt2 directory for mounting them.
    377  */
    378 int
    379 get_via_floppy(void)
    380 {
    381 	int rv = -1;
    382 
    383 	process_menu(MENU_floppysource, &rv);
    384 	if (rv == SET_RETRY)
    385 		return SET_RETRY;
    386 
    387 	fetch_fn = floppy_fetch;
    388 
    389 	/* Set ext_dir for absolute path. */
    390 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(), xfer_dir);
    391 	snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(), xfer_dir);
    392 
    393 	return SET_OK;
    394 }
    395 
    396 /*
    397  * Get the volume name of a ISO9660 file system
    398  */
    399 static int
    400 get_iso9660_volname(int dev, int sess, char *volname, size_t volnamelen)
    401 {
    402 	int blkno, error, last;
    403 	char buf[ISO_BLKSIZE];
    404 	struct iso_volume_descriptor *vd = NULL;
    405 	struct iso_primary_descriptor *pd = NULL;
    406 
    407 	for (blkno = sess+16; blkno < sess+16+100; blkno++) {
    408 		error = pread(dev, buf, ISO_BLKSIZE, blkno*ISO_BLKSIZE);
    409 		if (error == -1)
    410 			return -1;
    411 		vd = (struct iso_volume_descriptor *)&buf;
    412 		if (memcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
    413 			return -1;
    414 		if (isonum_711((const unsigned char *)&vd->type)
    415 		     == ISO_VD_PRIMARY) {
    416 			pd = (struct iso_primary_descriptor*)buf;
    417 			strncpy(volname, pd->volume_id, volnamelen - 1);
    418 			volname[volnamelen - 1] = '\0';
    419 			last = volnamelen - 1;
    420 			while (last >= 0
    421 			    && (volname[last] == ' ' || volname[last] == 0))
    422 				last--;
    423 			volname[last+1] = 0;
    424 			return 0;
    425 		}
    426 	}
    427 	return -1;
    428 }
    429 
    430 /*
    431  * Local state while iterating CDs and collecting volumes
    432  */
    433 struct get_available_cds_state {
    434 	size_t num_mounted;
    435 	struct statvfs *mounted;
    436 	struct cd_info *info;
    437 	size_t count;
    438 };
    439 
    440 /*
    441  * Callback function: if this is a CD, enumerate all volumes on it
    442  */
    443 static bool
    444 get_available_cds_helper(void *arg, const char *device)
    445 {
    446 	struct get_available_cds_state *state = arg;
    447 	char dname[16], tname[16], volname[80], *t;
    448 	struct disklabel label;
    449 	int part, dev, error, sess, ready, tlen;
    450 
    451 	if (!is_cdrom_device(device, false))
    452 		return true;
    453 
    454 	sprintf(dname, "/dev/r%s%c", device, 'a'+RAW_PART);
    455 	tlen = sprintf(tname, "/dev/%s", device);
    456 
    457 	/* check if this is mounted already */
    458 	for (size_t i = 0; i < state->num_mounted; i++) {
    459 		if (strncmp(state->mounted[i].f_mntfromname, tname, tlen)
    460 		    == 0) {
    461 			t = state->mounted[i].f_mntfromname + tlen;
    462 			if (t[0] >= 'a' && t[0] <= 'z' && t[1] == 0)
    463 				return true;
    464 		}
    465 	}
    466 
    467 	dev = open(dname, O_RDONLY, 0);
    468 	if (dev == -1)
    469 		return true;
    470 
    471 	ready = 0;
    472 	error = ioctl(dev, DIOCTUR, &ready);
    473 	if (error != 0 || ready == 0) {
    474 		close(dev);
    475 		return true;
    476 	}
    477 	error = ioctl(dev, DIOCGDINFO, &label);
    478 	close(dev);
    479 	if (error != 0)
    480 		return true;
    481 
    482 	for (part = 0; part < label.d_npartitions; part++) {
    483 
    484 		if (label.d_partitions[part].p_fstype == FS_UNUSED
    485 		    || label.d_partitions[part].p_size == 0)
    486 			continue;
    487 
    488 		if (label.d_partitions[part].p_fstype == FS_ISO9660) {
    489 			sess = label.d_partitions[part].p_cdsession;
    490 			sprintf(dname, "/dev/r%s%c", device, 'a'+part);
    491 			dev = open(dname, O_RDONLY, 0);
    492 			if (dev == -1)
    493 				continue;
    494 			error = get_iso9660_volname(dev, sess, volname,
    495 			    sizeof volname);
    496 			close(dev);
    497 			if (error)
    498 				continue;
    499 			sprintf(state->info->device_name,
    500 			    "%s%c", device, 'a'+part);
    501 			sprintf(state->info->menu, "%s (%s)",
    502 			    state->info->device_name, volname);
    503 		} else {
    504 			/*
    505 			 * All install CDs use partition
    506 			 * a for the sets.
    507 			 */
    508 			if (part > 0)
    509 				continue;
    510 			sprintf(state->info->device_name,
    511 			    "%s%c", device, 'a'+part);
    512 			strcpy(state->info->menu, state->info->device_name);
    513 		}
    514 		state->info++;
    515 		if (++state->count >= MAX_CD_INFOS)
    516 			return false;
    517 	}
    518 
    519 	return true;
    520 }
    521 
    522 /*
    523  * Get a list of all available CD media (not drives!), return
    524  * the number of entries collected.
    525  */
    526 static int
    527 get_available_cds(void)
    528 {
    529 	struct get_available_cds_state data;
    530 	int n, m;
    531 
    532 	memset(&data, 0, sizeof data);
    533 	data.info = cds;
    534 
    535 	n = getvfsstat(NULL, 0, ST_NOWAIT);
    536 	if (n > 0) {
    537 		data.mounted = calloc(n, sizeof(*data.mounted));
    538 		m = getvfsstat(data.mounted, n*sizeof(*data.mounted),
    539 		    ST_NOWAIT);
    540 		assert(m >= 0 && m <= n);
    541 		data.num_mounted = m;
    542 	}
    543 
    544 	enumerate_disks(&data, get_available_cds_helper);
    545 
    546 	free(data.mounted);
    547 
    548 	return data.count;
    549 }
    550 
    551 static int
    552 cd_has_sets(void)
    553 {
    554 
    555 	/* sanity check */
    556 	if (cdrom_dev[0] == 0)
    557 		return 0;
    558 
    559 	/* Mount it */
    560 	if (run_program(RUN_SILENT, "/sbin/mount -rt cd9660 /dev/%s /mnt2",
    561 	    cdrom_dev) != 0)
    562 		return 0;
    563 
    564 	mnt2_mounted = 1;
    565 
    566 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", "/mnt2", set_dir_bin);
    567 	snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", "/mnt2", set_dir_src);
    568 	return dir_exists_p(ext_dir_bin);
    569 }
    570 
    571 /*
    572  * Check whether we can remove the boot media.
    573  * If it is not a local filesystem, return -1.
    574  * If we can not decide for sure (can not tell MD content from plain ffs
    575  * on hard disk, for example), return 0.
    576  * If it is a CD/DVD, return 1.
    577  */
    578 int
    579 boot_media_still_needed(void)
    580 {
    581 	struct statvfs sb;
    582 
    583 	if (statvfs("/", &sb) == 0) {
    584 		if (!(sb.f_flag & ST_LOCAL))
    585 			return -1;
    586 		if (strcmp(sb.f_fstypename, MOUNT_CD9660) == 0
    587 			   || strcmp(sb.f_fstypename, MOUNT_UDF) == 0)
    588 			return 1;
    589 	}
    590 
    591 	return 0;
    592 }
    593 
    594 bool
    595 root_is_read_only(void)
    596 {
    597 	struct statvfs sb;
    598 
    599 	if (statvfs("/", &sb) == 0)
    600 		return sb.f_flag & ST_RDONLY;
    601 
    602 	return false;
    603 }
    604 
    605 /*
    606  * Get from a CDROM distribution.
    607  * Also used on "installation using bootable install media"
    608  * as the default option in the "distmedium" menu.
    609  */
    610 int
    611 get_via_cdrom(void)
    612 {
    613 	menu_ent cd_menu[MAX_CD_INFOS];
    614 	struct stat sb;
    615 	int rv, num_cds, menu_cd, i, selected_cd = 0;
    616 	int mib[2];
    617 	char rootdev[SSTRSIZE] = "";
    618 	size_t varlen;
    619 
    620 	/* If root is not md(4) and we have set dir, skip this step. */
    621 	mib[0] = CTL_KERN;
    622 	mib[1] = KERN_ROOT_DEVICE;
    623 	varlen = sizeof(rootdev);
    624 	(void)sysctl(mib, 2, rootdev, &varlen, NULL, 0);
    625 	if (stat(set_dir_bin, &sb) == 0 && S_ISDIR(sb.st_mode) &&
    626 	    strncmp("md", rootdev, 2) != 0) {
    627 	    	strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
    628 	    	strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
    629 		return SET_OK;
    630 	}
    631 
    632 	memset(cd_menu, 0, sizeof(cd_menu));
    633 	num_cds = get_available_cds();
    634 	if (num_cds <= 0) {
    635 		msg_display(MSG_No_cd_found);
    636 		cdrom_dev[0] = 0;
    637 	} else if (num_cds == 1) {
    638 		/* single CD found, check for sets on it */
    639 		strcpy(cdrom_dev, cds[0].device_name);
    640 		if (cd_has_sets())
    641 			return SET_OK;
    642 	} else {
    643 		for (i = 0; i< num_cds; i++) {
    644 			cd_menu[i].opt_name = cds[i].menu;
    645 			cd_menu[i].opt_flags = OPT_EXIT;
    646 			cd_menu[i].opt_action = set_menu_select;
    647 		}
    648 		/* create a menu offering available choices */
    649 		menu_cd = new_menu(MSG_Available_cds,
    650 			cd_menu, num_cds, -1, 4, 0, 0,
    651 			MC_SCROLL | MC_NOEXITOPT,
    652 			NULL, NULL, NULL, NULL, NULL);
    653 		if (menu_cd == -1)
    654 			return SET_RETRY;
    655 		msg_display(MSG_ask_cd);
    656 		process_menu(menu_cd, &selected_cd);
    657 		free_menu(menu_cd);
    658 		strcpy(cdrom_dev, cds[selected_cd].device_name);
    659 		if (cd_has_sets())
    660 			return SET_OK;
    661 	}
    662 
    663 	if (num_cds >= 1 && mnt2_mounted) {
    664 		umount_mnt2();
    665 		hit_enter_to_continue(MSG_cd_path_not_found, NULL);
    666 	}
    667 
    668 	/* ask for paths on the CD */
    669 	rv = -1;
    670 	process_menu(MENU_cdromsource, &rv);
    671 	if (rv == SET_RETRY || rv == SET_ABANDON)
    672 		return rv;
    673 
    674 	if (cd_has_sets())
    675 		return SET_OK;
    676 
    677 	return SET_RETRY;
    678 }
    679 
    680 
    681 /*
    682  * Get from a pathname inside an unmounted local filesystem
    683  * (e.g., where sets were preloaded onto a local DOS partition)
    684  */
    685 int
    686 get_via_localfs(void)
    687 {
    688 	int rv = -1;
    689 
    690 	/* Get device, filesystem, and filepath */
    691 	process_menu (MENU_localfssource, &rv);
    692 	if (rv == SET_RETRY)
    693 		return SET_RETRY;
    694 
    695 	/* Mount it */
    696 	if (run_program(0, "/sbin/mount -rt %s /dev/%s /mnt2",
    697 	    localfs_fs, localfs_dev))
    698 		return SET_RETRY;
    699 
    700 	mnt2_mounted = 1;
    701 
    702 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s/%s",
    703 		"/mnt2", localfs_dir, set_dir_bin);
    704 	snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s/%s",
    705 		"/mnt2", localfs_dir, set_dir_src);
    706 
    707 	return SET_OK;
    708 }
    709 
    710 /*
    711  * Get from an already-mounted pathname.
    712  */
    713 
    714 int
    715 get_via_localdir(void)
    716 {
    717 	int rv = -1;
    718 
    719 	/* Get filepath */
    720 	process_menu(MENU_localdirsource, &rv);
    721 	if (rv == SET_RETRY)
    722 		return SET_RETRY;
    723 
    724 	/*
    725 	 * We have to have an absolute path ('cos pax runs in a
    726 	 * different directory), make it so.
    727 	 */
    728 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "/%s/%s", localfs_dir, set_dir_bin);
    729 	snprintf(ext_dir_src, sizeof ext_dir_src, "/%s/%s", localfs_dir, set_dir_src);
    730 
    731 	return SET_OK;
    732 }
    733 
    734 
    735 /*
    736  * Support for custom distribution fetches / unpacks.
    737  */
    738 
    739 unsigned int
    740 set_X11_selected(void)
    741 {
    742 	int i;
    743 
    744 	for (i = SET_X11_FIRST; ++i < SET_X11_LAST;)
    745 		if (set_status[i] & SET_SELECTED)
    746 			return 1;
    747 	return 0;
    748 }
    749 
    750 unsigned int
    751 get_kernel_set(void)
    752 {
    753 	int i;
    754 
    755 	for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
    756 		if (set_status[i] & SET_SELECTED)
    757 			return i;
    758 	return SET_NONE;
    759 }
    760 
    761 void
    762 set_kernel_set(unsigned int kernel_set)
    763 {
    764 	int i;
    765 
    766 	/* only one kernel set is allowed */
    767 	for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
    768 		set_status[i] &= ~SET_SELECTED;
    769 	set_status[kernel_set] |= SET_SELECTED;
    770 }
    771 
    772 void
    773 set_noextract_set(unsigned int set)
    774 {
    775 
    776 	set_status[set] |= SET_NO_EXTRACT;
    777 }
    778 
    779 static int
    780 set_toggle(menudesc *menu, void *arg)
    781 {
    782 	distinfo **distp = arg;
    783 	int set = distp[menu->cursel]->set;
    784 
    785 	if (set > SET_KERNEL_FIRST && set < SET_KERNEL_LAST &&
    786 	    !(set_status[set] & SET_SELECTED))
    787 		set_kernel_set(set);
    788 	else
    789 		set_status[set] ^= SET_SELECTED;
    790 	return 0;
    791 }
    792 
    793 static int
    794 set_all_none(menudesc *menu, void *arg, int set, int clr)
    795 {
    796 	distinfo **distp = arg;
    797 	distinfo *dist = *distp;
    798 	int nested;
    799 
    800 	for (nested = 0; dist->set != SET_GROUP_END || nested--; dist++) {
    801 		if (dist->set == SET_GROUP) {
    802 			nested++;
    803 			continue;
    804 		}
    805 		set_status[dist->set] = (set_status[dist->set] & ~clr) | set;
    806 	}
    807 	return 0;
    808 }
    809 
    810 static int
    811 set_all(menudesc *menu, void *arg)
    812 {
    813 	return set_all_none(menu, arg, SET_SELECTED, 0);
    814 }
    815 
    816 static int
    817 set_none(menudesc *menu, void *arg)
    818 {
    819 	return set_all_none(menu, arg, 0, SET_SELECTED);
    820 }
    821 
    822 static void
    823 set_label(menudesc *menu, int opt, void *arg)
    824 {
    825 	distinfo **distp = arg;
    826 	distinfo *dist = distp[opt];
    827 	const char *selected;
    828 	const char *desc;
    829 	int nested;
    830 
    831 	desc = dist->desc;
    832 
    833 	if (dist->set != SET_GROUP)
    834 		selected = set_status[dist->set] & SET_SELECTED ? msg_yes : msg_no;
    835 	else {
    836 		/* sub menu - display None/Some/All */
    837 		nested = 0;
    838 		selected = "unknown";
    839 		while ((++dist)->set != SET_GROUP_END || nested--) {
    840 			if (dist->set == SET_GROUP) {
    841 				nested++;
    842 				continue;
    843 			}
    844 			if (!(set_status[dist->set] & SET_VALID))
    845 				continue;
    846 			if (set_status[dist->set] & SET_SELECTED) {
    847 				if (selected == msg_none) {
    848 					selected = msg_some;
    849 					break;
    850 				}
    851 				selected = msg_all;
    852 			} else {
    853 				if (selected == msg_all) {
    854 					selected = msg_some;
    855 					break;
    856 				}
    857 				selected = msg_none;
    858 			}
    859 		}
    860 	}
    861 
    862 	wprintw(menu->mw, "%-30s %s", msg_string(desc), selected);
    863 }
    864 
    865 static int set_sublist(menudesc *menu, void *arg);
    866 
    867 static int
    868 initialise_set_menu(distinfo *dist, menu_ent *me, distinfo **de, int all_none)
    869 {
    870 	int set;
    871 	int sets;
    872 	int nested;
    873 
    874 	for (sets = 0; ; dist++) {
    875 		set = dist->set;
    876 		if (set == SET_LAST || set == SET_GROUP_END)
    877 			break;
    878 		if (!(set_status[set] & SET_VALID))
    879 			continue;
    880 		*de = dist;
    881 		memset(me, 0, sizeof(*me));
    882 		if (set != SET_GROUP)
    883 			me->opt_action = set_toggle;
    884 		else {
    885 			/* Collapse sublist */
    886 			nested = 0;
    887 			while ((++dist)->set != SET_GROUP_END || nested--) {
    888 				if (dist->set == SET_GROUP)
    889 					nested++;
    890 			}
    891 			me->opt_action = set_sublist;
    892 		}
    893 		sets++;
    894 		de++;
    895 		me++;
    896 	}
    897 
    898 	if (all_none) {
    899 		me->opt_name = MSG_select_all;
    900 		me->opt_action = set_all;
    901 		me++;
    902 		me->opt_name = MSG_select_none;
    903 		me->opt_action = set_none;
    904 		sets += 2;
    905 	}
    906 
    907 	return sets;
    908 }
    909 
    910 static int
    911 set_sublist(menudesc *menu, void *arg)
    912 {
    913 	distinfo *de[SET_LAST];
    914 	menu_ent me[SET_LAST];
    915 	distinfo **dist = arg;
    916 	int menu_no;
    917 	int sets;
    918 
    919 	memset(me, 0, sizeof(me));
    920 	sets = initialise_set_menu(dist[menu->cursel] + 1, me, de, 1);
    921 
    922 	menu_no = new_menu(NULL, me, sets, 20, 10, 0, select_menu_width,
    923 		MC_SUBMENU | MC_SCROLL | MC_DFLTEXIT,
    924 		NULL, set_label, NULL, NULL,
    925 		MSG_install_selected_sets);
    926 
    927 	process_menu(menu_no, de);
    928 	free_menu(menu_no);
    929 
    930 	return 0;
    931 }
    932 
    933 void
    934 customise_sets(void)
    935 {
    936 	distinfo *de[SET_LAST];
    937 	menu_ent me[SET_LAST];
    938 	int sets;
    939 	int menu_no;
    940 
    941 	msg_display(MSG_cur_distsets);
    942 	msg_table_add(MSG_cur_distsets_header);
    943 
    944 	memset(me, 0, sizeof(me));
    945 	sets = initialise_set_menu(dist_list, me, de, 0);
    946 
    947 	menu_no = new_menu(NULL, me, sets, 0, 5, 0, select_menu_width,
    948 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT | MC_NOCLEAR,
    949 		NULL, set_label, NULL, NULL,
    950 		MSG_install_selected_sets);
    951 
    952 	process_menu(menu_no, de);
    953 	free_menu(menu_no);
    954 }
    955 
    956 /*
    957  * Extract_file **REQUIRES** an absolute path in ext_dir.  Any code
    958  * that sets up xfer_dir for use by extract_file needs to put in the
    959  * full path name to the directory.
    960  */
    961 
    962 int
    963 extract_file(distinfo *dist, int update)
    964 {
    965 	const char *dest_dir = NULL;
    966 
    967 	if (update && (dist->set == SET_ETC || dist->set == SET_X11_ETC)) {
    968 		dest_dir = "/.sysinst";
    969 		make_target_dir(dest_dir);
    970 	} else if (dist->set == SET_PKGSRC)
    971 		dest_dir = "/usr";
    972 	else
    973 		dest_dir = "/";
    974 
    975 	return extract_file_to(dist, update, dest_dir, NULL, true);
    976 }
    977 
    978 int
    979 extract_file_to(distinfo *dist, int update, const char *dest_dir,
    980     const char *extr_pattern, bool do_stats)
    981 {
    982 	char path[STRSIZE];
    983 	char *owd;
    984 	int   rval;
    985 
    986 	/* If we might need to tidy up, ensure directory exists */
    987 	if (fetch_fn != NULL)
    988 		make_target_dir(xfer_dir);
    989 
    990 	(void)snprintf(path, sizeof path, "%s/%s%s",
    991 	    ext_dir_for_set(dist->name), dist->name, set_postfix(dist->name));
    992 
    993 	owd = getcwd(NULL, 0);
    994 
    995 	/* Do we need to fetch the file now? */
    996 	if (fetch_fn != NULL) {
    997 		rval = fetch_fn(dist->name);
    998 		if (rval != SET_OK)
    999 			return rval;
   1000 	}
   1001 
   1002 	/* check tarfile exists */
   1003 	if (!file_exists_p(path)) {
   1004 
   1005 #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
   1006 		/*
   1007 		 * Update path to use dist->name truncated to the first eight
   1008 		 * characters and check again
   1009 		 */
   1010 		(void)snprintf(path, sizeof path,
   1011 		    "%s/%.8s%.4s", /* 4 as includes '.' */
   1012 		    ext_dir_for_set(dist->name), dist->name,
   1013 		    set_postfix(dist->name));
   1014 
   1015 		if (!file_exists_p(path)) {
   1016 #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
   1017 			if (do_stats)
   1018 				tarstats.nnotfound++;
   1019 
   1020 			char *err = str_arg_subst(msg_string(MSG_notarfile),
   1021 			    1, &dist->name);
   1022 			hit_enter_to_continue(err, NULL);
   1023 			free(err);
   1024 			free(owd);
   1025 			return SET_RETRY;
   1026 		}
   1027 #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
   1028 	}
   1029 #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
   1030 
   1031 	if (do_stats)
   1032 		tarstats.nfound++;
   1033 	/* cd to the target root. */
   1034 	target_chdir_or_die(dest_dir);
   1035 
   1036 	/*
   1037 	 * /usr/X11R7/lib/X11/xkb/symbols/pc was a directory in 5.0
   1038 	 * but is a file in 5.1 and beyond, so on upgrades we need to
   1039 	 * delete it before extracting the xbase set.
   1040 	 */
   1041 	if (update && dist->set == SET_X11_BASE)
   1042 		run_program(0, "rm -rf usr/X11R7/lib/X11/xkb/symbols/pc");
   1043 
   1044 	/* now extract set files into "./". */
   1045 	if (extr_pattern != NULL) {
   1046 		rval = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1047 				"progress -zf %s tar --chroot "
   1048 				TAR_EXTRACT_FLAGS " - '%s'",
   1049 				path, extr_pattern);
   1050 	} else {
   1051 		rval = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1052 				"progress -zf %s tar --chroot "
   1053 				TAR_EXTRACT_FLAGS " -", path);
   1054 	}
   1055 
   1056 	chdir(owd);
   1057 	free(owd);
   1058 
   1059 	/* Check rval for errors and give warning. */
   1060 	if (rval != 0) {
   1061 		if (do_stats)
   1062 			tarstats.nerror++;
   1063 		msg_fmt_display(MSG_tarerror, "%s", path);
   1064 		hit_enter_to_continue(NULL, NULL);
   1065 		return SET_RETRY;
   1066 	}
   1067 
   1068 	if (fetch_fn != NULL && clean_xfer_dir) {
   1069 		run_program(0, "rm %s", path);
   1070 		/* Plausibly we should unlink an empty xfer_dir as well */
   1071 	}
   1072 
   1073 	set_status[dist->set] |= SET_INSTALLED;
   1074 	if (do_stats)
   1075 		tarstats.nsuccess++;
   1076 	return SET_OK;
   1077 }
   1078 
   1079 static void
   1080 skip_set(distinfo *dist, int skip_type)
   1081 {
   1082 	int nested;
   1083 	int set;
   1084 
   1085 	nested = 0;
   1086 	while ((++dist)->set != SET_GROUP_END || nested--) {
   1087 		set = dist->set;
   1088 		if (set == SET_GROUP) {
   1089 			nested++;
   1090 			continue;
   1091 		}
   1092 		if (set == SET_LAST)
   1093 			break;
   1094 		if (set_status[set] == (SET_SELECTED | SET_VALID))
   1095 			set_status[set] |= SET_SKIPPED;
   1096 		tarstats.nskipped++;
   1097 	}
   1098 }
   1099 
   1100 distinfo*
   1101 get_set_distinfo(int opt)
   1102 {
   1103 	distinfo *dist;
   1104 	int set;
   1105 
   1106 	for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
   1107 		if (set != opt)
   1108 			continue;
   1109 		if (dist->name == NULL)
   1110 			continue;
   1111 		if ((set_status[set] & (SET_VALID | SET_SELECTED))
   1112 		    != (SET_VALID | SET_SELECTED))
   1113 			continue;
   1114 		return dist;
   1115 	}
   1116 
   1117 	return NULL;
   1118 }
   1119 
   1120 #ifdef CHECK_ENTROPY
   1121 
   1122 char entropy_file[PATH_MAX];
   1123 
   1124 /*
   1125  * Are we short of entropy?
   1126  */
   1127 static size_t
   1128 entropy_needed(void)
   1129 {
   1130 	int needed;
   1131 	size_t len;
   1132 
   1133 	len = sizeof(needed);
   1134 	if (sysctlbyname("kern.entropy.needed", &needed, &len, NULL, 0))
   1135 		return 0;
   1136 
   1137 	if (needed < 0)
   1138 		return 0;
   1139 
   1140 	return needed;
   1141 }
   1142 
   1143 static void
   1144 entropy_write_to_kernel(const uint8_t *data, size_t len)
   1145 {
   1146 	int fd;
   1147 
   1148 	fd = open(_PATH_RANDOM, O_RDWR, 0);
   1149 	if (fd >= 0) {
   1150 		write(fd, data, len);
   1151 		close(fd);
   1152 	}
   1153 }
   1154 
   1155 static void
   1156 entropy_add_manual(void)
   1157 {
   1158 	SHA256_CTX ctx;
   1159 	char buf[256], line[25];
   1160 	size_t line_no, l;
   1161 	uint8_t digest[SHA256_DIGEST_LENGTH];
   1162 	bool ok = false;
   1163 
   1164 	msg_display(MSG_entropy_enter_manual1);
   1165 	msg_printf("\n\n");
   1166 	msg_display_add(MSG_entropy_enter_manual2);
   1167 	msg_printf("\n\n   dd if=/dev/random bs=32 count=16 | openssl base64\n\n");
   1168 	msg_display_add(MSG_entropy_enter_manual3);
   1169 	msg_printf("\n\n");
   1170 	SHA256_Init(&ctx);
   1171 	line_no = 1;
   1172 	do {
   1173 		sprintf(line, "%zu", line_no);
   1174 		msg_prompt_win(line, -1, 15, 0, 0, "", buf, sizeof(buf));
   1175 		l = strlen(buf);
   1176 		if (l > 0)
   1177 			SHA256_Update(&ctx, (const uint8_t*)buf, l);
   1178 		line_no++;
   1179 	} while(buf[0] != 0);
   1180 	ok = ctx.bitcount >= 256;
   1181 	SHA256_Final(digest, &ctx);
   1182 
   1183 	if (ok)
   1184 		entropy_write_to_kernel(digest, sizeof digest);
   1185 	else
   1186 		hit_enter_to_continue(NULL, MSG_entropy_manual_not_enough);
   1187 }
   1188 
   1189 /*
   1190  * Get a file by some means and return a (potentially only
   1191  * temporary valid) path to the local copy.
   1192  * If mountpt is nonempty, the caller should unmount that
   1193  * directory after processing the file.
   1194  * Return success if the file is available, or failure if
   1195  * the user cancelled the request or network transfer failed.
   1196  */
   1197 static bool
   1198 entropy_get_file(bool use_netbsd_seed, char *path)
   1199 {
   1200 	static struct ftpinfo server = { .user = "ftp" };
   1201 	char url[STRSIZE], tmpf[PATH_MAX], mountpt[PATH_MAX];
   1202 	const char *ftp_opt;
   1203 	arg_rv arg;
   1204 	int rv = 0;
   1205 	const char *file_desc = msg_string(use_netbsd_seed ?
   1206 	    MSG_entropy_seed : MSG_entropy_data);
   1207 	char *dir;
   1208 
   1209 	path[0] = 0;
   1210 	mountpt[0] = 0;
   1211 
   1212 	sprintf(tmpf, "/tmp/entr.%06x", getpid());
   1213 
   1214 	msg_display(use_netbsd_seed ?
   1215 	    MSG_entropy_seed_hdr : MSG_entropy_data_hdr);
   1216 	msg_printf("\n\n    %s\n\n",
   1217 	    use_netbsd_seed ?
   1218 	    "rndctl -S /tmp/entropy-file" :
   1219 	    "dd if=/dev/random bs=32 count=1 of=/tmp/random.tmp");
   1220 	strcpy(entropy_file, use_netbsd_seed ?
   1221 	    "entropy-file" : "random.tmp");
   1222 	process_menu(MENU_entropy_select_file, &rv);
   1223 	switch (rv) {
   1224 	case 1:
   1225 	case 2:
   1226 #ifndef DEBUG
   1227 		if (!network_up)
   1228 			config_network();
   1229 #endif
   1230 		server.xfer = rv == 1 ? XFER_HTTP : XFER_FTP;
   1231 		arg.arg = &server;
   1232 		arg.rv = -1;
   1233 		msg_display_add_subst(MSG_entropy_via_download, 1, file_desc);
   1234 		msg_printf("\n\n");
   1235 		process_menu(MENU_entropy_ftpsource, &arg);
   1236 		if (arg.rv == SET_RETRY)
   1237 			return false;
   1238 		make_url(url, &server, entropy_file);
   1239 		if (server.xfer == XFER_FTP &&
   1240 		    strcmp("ftp", server.user) == 0 && server.pass[0] == 0) {
   1241 			/* do anon ftp */
   1242 			ftp_opt = "-a ";
   1243 		} else {
   1244 			ftp_opt = "";
   1245 		}
   1246 		rv = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1247 		    "/usr/bin/ftp %s -o %s %s",
   1248 		    ftp_opt, tmpf, url);
   1249 		strcpy(path, tmpf);
   1250 		return rv == 0;
   1251 	case 3:
   1252 #ifndef DEBUG
   1253 		if (!network_up)
   1254 			config_network();
   1255 #endif
   1256 		rv = -1;
   1257 		msg_display_add_subst(MSG_entropy_via_nfs, 1, file_desc);
   1258 		msg_printf("\n\n");
   1259 		process_menu(MENU_entropy_nfssource, &rv);
   1260 		if (rv == SET_RETRY)
   1261 			return false;
   1262 		if (nfs_host[0] != 0 && nfs_dir[0] != 0 &&
   1263 		    entropy_file[0] != 0) {
   1264 			strcpy(mountpt, "/tmp/ent-mnt.XXXXXX");
   1265 			dir = mkdtemp(mountpt);
   1266 			if (dir == NULL)
   1267 				return false;
   1268 			sprintf(path, "%s/%s", mountpt, entropy_file);
   1269 			if (run_program(RUN_SILENT,
   1270 			    "mount -t nfs -r %s:/%s %s",
   1271 			    nfs_host, nfs_dir, mountpt) == 0) {
   1272 				run_program(RUN_SILENT,
   1273 				    "cp %s %s", path, tmpf);
   1274 				run_program(RUN_SILENT,
   1275 				    "umount %s", mountpt);
   1276 				rmdir(mountpt);
   1277 				strcpy(path, tmpf);
   1278 			}
   1279 		}
   1280 		break;
   1281 	case 4:
   1282 		rv = -1;
   1283 		/* Get device, filesystem, and filepath */
   1284 		process_menu (MENU_entropy_localfs, &rv);
   1285 		if (rv == SET_RETRY)
   1286 			return false;
   1287 		if (localfs_dev[0] != 0 && localfs_fs[0] != 0 &&
   1288 		    entropy_file[0] != 0) {
   1289 			strcpy(mountpt, "/tmp/ent-mnt.XXXXXX");
   1290 			dir = mkdtemp(mountpt);
   1291 			if (dir == NULL)
   1292 				return false;
   1293 			sprintf(path, "%s/%s", mountpt, entropy_file);
   1294 			if (run_program(RUN_SILENT,
   1295 			    "mount -t %s -r /dev/%s %s",
   1296 			    localfs_fs, localfs_dev, mountpt) == 0) {
   1297 				run_program(RUN_SILENT,
   1298 				    "cp %s %s", path, tmpf);
   1299 				run_program(RUN_SILENT,
   1300 				    "umount %s", mountpt);
   1301 				rmdir(mountpt);
   1302 				strcpy(path, tmpf);
   1303 			}
   1304 		}
   1305 		break;
   1306 	}
   1307 	return path[0] != 0;
   1308 }
   1309 
   1310 static void
   1311 entropy_add_bin_file(void)
   1312 {
   1313 	char fname[PATH_MAX];
   1314 
   1315 	if (!entropy_get_file(false, fname))
   1316 		return;
   1317 	if (access(fname, R_OK) == 0)
   1318 		run_program(RUN_SILENT, "dd if=%s of=" _PATH_RANDOM,
   1319 		    fname);
   1320 }
   1321 
   1322 static void
   1323 entropy_add_seed(void)
   1324 {
   1325 	char fname[PATH_MAX];
   1326 
   1327 	if (!entropy_get_file(true, fname))
   1328 		return;
   1329 	if (access(fname, R_OK) == 0)
   1330 		run_program(RUN_SILENT, "rndctl -L %s", fname);
   1331 }
   1332 
   1333 /*
   1334  * return true if we have enough entropy
   1335  */
   1336 bool
   1337 do_check_entropy(void)
   1338 {
   1339 	int rv;
   1340 
   1341 	if (entropy_needed() == 0)
   1342 		return true;
   1343 
   1344 	for (;;) {
   1345 		if (entropy_needed() == 0)
   1346 			return true;
   1347 
   1348 		msg_clear();
   1349 		rv = 0;
   1350 		process_menu(MENU_not_enough_entropy, &rv);
   1351 		switch (rv) {
   1352 		case 0:
   1353 			return false;
   1354 		case 1:
   1355 			entropy_add_manual();
   1356 			break;
   1357 		case 2:
   1358 			entropy_add_seed();
   1359 			break;
   1360 		case 3:
   1361 			entropy_add_bin_file();
   1362 			break;
   1363 		default:
   1364 			/*
   1365 			 * retry after small delay to give a new USB device
   1366 			 * a chance to attach and do deliver some
   1367 			 * entropy
   1368 			 */
   1369 			msg_display(".");
   1370 			for (size_t i = 0; i < 10; i++) {
   1371 				if (entropy_needed() == 0)
   1372 					return true;
   1373 				sleep(1);
   1374 				msg_display_add(".");
   1375 			}
   1376 		}
   1377 	}
   1378 }
   1379 #endif
   1380 
   1381 
   1382 
   1383 /*
   1384  * Get and unpack the distribution.
   1385  * Show success_msg if installation completes.
   1386  * Otherwise show failure_msg and wait for the user to ack it before continuing.
   1387  * success_msg and failure_msg must both be 0-adic messages.
   1388  */
   1389 int
   1390 get_and_unpack_sets(int update, msg setupdone_msg, msg success_msg, msg failure_msg)
   1391 {
   1392 	distinfo *dist;
   1393 	int status;
   1394 	int set, olderror, oldfound;
   1395 	bool entropy_loaded = false;
   1396 
   1397 	/* Ensure mountpoint for distribution files exists in current root. */
   1398 	(void)mkdir("/mnt2", S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
   1399 	if (script)
   1400 		(void)fprintf(script, "mkdir -m 755 /mnt2\n");
   1401 
   1402 	/* reset failure/success counters */
   1403 	memset(&tarstats, 0, sizeof(tarstats));
   1404 
   1405 	/* Find out which files to "get" if we get files. */
   1406 
   1407 	/* Accurately count selected sets */
   1408 	for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
   1409 		if (dist->name == NULL)
   1410 			continue;
   1411 		if (set_status[set] & SET_NO_EXTRACT)
   1412 			continue;
   1413 		if ((set_status[set] & (SET_VALID | SET_SELECTED))
   1414 		    == (SET_VALID | SET_SELECTED))
   1415 			tarstats.nselected++;
   1416 	}
   1417 
   1418 	status = SET_RETRY;
   1419 	for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
   1420 		if (dist->name == NULL)
   1421 			continue;
   1422 		if (set_status[set] != (SET_VALID | SET_SELECTED))
   1423 			continue;
   1424 
   1425 		/* save stats, in case we will retry */
   1426 		oldfound = tarstats.nfound;
   1427 		olderror = tarstats.nerror;
   1428 
   1429 		if (status != SET_OK) {
   1430 			/* This might force a redraw.... */
   1431 			clearok(curscr, 1);
   1432 			touchwin(stdscr);
   1433 			wrefresh(stdscr);
   1434 			/* Sort out the location of the set files */
   1435 			do {
   1436 				umount_mnt2();
   1437 				msg_fmt_display(MSG_distmedium, "%d%d%s",
   1438 				    tarstats.nselected,
   1439 				    tarstats.nsuccess + tarstats.nskipped,
   1440 				    dist->name);
   1441 				fetch_fn = NULL;
   1442 				process_menu(MENU_distmedium, &status);
   1443 			} while (status == SET_RETRY);
   1444 
   1445 			if (status == SET_SKIP) {
   1446 				set_status[set] |= SET_SKIPPED;
   1447 				tarstats.nskipped++;
   1448 				continue;
   1449 			}
   1450 			if (status == SET_SKIP_GROUP) {
   1451 				skip_set(dist, status);
   1452 				continue;
   1453 			}
   1454 			if (status != SET_OK) {
   1455 				hit_enter_to_continue(failure_msg, NULL);
   1456 				return 1;
   1457 			}
   1458 		}
   1459 
   1460 		if (set_status[set] & SET_NO_EXTRACT)
   1461 			continue;
   1462 
   1463 		/* Try to extract this set */
   1464 		status = extract_file(dist, update);
   1465 		if (status == SET_RETRY) {
   1466 			/* do this set again */
   1467 			dist--;
   1468 			/* and reset statistics to what we had before this
   1469 			 * set */
   1470 			tarstats.nfound = oldfound;
   1471 			tarstats.nerror = olderror;
   1472 		}
   1473 	}
   1474 
   1475 #ifdef MD_SET_EXTRACT_FINALIZE
   1476 	MD_SET_EXTRACT_FINALIZE(update);
   1477 #endif
   1478 
   1479 	if (tarstats.nerror == 0 && tarstats.nsuccess == tarstats.nselected) {
   1480 		msg_display(MSG_endtarok);
   1481 		/* Give user a chance to see the success message */
   1482 		sleep(1);
   1483 	} else {
   1484 		/* We encountered errors. Let the user know. */
   1485 		msg_fmt_display(MSG_endtar, "%d%d%d%d%d%d",
   1486 		    tarstats.nselected, tarstats.nnotfound, tarstats.nskipped,
   1487 		    tarstats.nfound, tarstats.nsuccess, tarstats.nerror);
   1488 		hit_enter_to_continue(NULL, NULL);
   1489 	}
   1490 
   1491 	/*
   1492 	 * postinstall needs to be run after extracting all sets, because
   1493 	 * otherwise /var/db/obsolete will only have current information
   1494 	 * from the base, comp, and etc sets.
   1495 	 */
   1496 	if (update && (set_status[SET_ETC] & SET_INSTALLED)) {
   1497 		int oldsendmail;
   1498 		oldsendmail = run_program(RUN_DISPLAY | RUN_CHROOT |
   1499 					  RUN_ERROR_OK | RUN_PROGRESS,
   1500 					  "/usr/sbin/postinstall -s /.sysinst -d / check mailerconf");
   1501 		if (oldsendmail == 1) {
   1502 			msg_display(MSG_oldsendmail);
   1503 			if (ask_yesno(NULL)) {
   1504 				run_program(RUN_DISPLAY | RUN_CHROOT,
   1505 					    "/usr/sbin/postinstall -s /.sysinst -d / fix mailerconf");
   1506 			}
   1507 		}
   1508 		run_program(RUN_DISPLAY | RUN_CHROOT,
   1509 			"/usr/sbin/postinstall -s /.sysinst -d / fix");
   1510 
   1511 		/* Don't discard the system's old entropy if any */
   1512 		run_program(RUN_CHROOT | RUN_SILENT,
   1513 		    "/etc/rc.d/random_seed start");
   1514 		entropy_loaded = true;
   1515 	}
   1516 
   1517 	/* Configure the system */
   1518 	if (set_status[SET_BASE] & SET_INSTALLED)
   1519 		run_makedev();
   1520 
   1521 	if (update) {
   1522 #ifdef CHECK_ENTROPY
   1523 		if (!do_check_entropy()) {
   1524 			hit_enter_to_continue(NULL, MSG_abortupgr);
   1525 			return 1;
   1526 		}
   1527 #endif
   1528 	} else {
   1529 		struct stat sb1, sb2;
   1530 
   1531 		if (stat(target_expand("/"), &sb1) == 0
   1532 		    && stat(target_expand("/var"), &sb2) == 0
   1533 		    && sb1.st_dev != sb2.st_dev) {
   1534 			add_rc_conf("random_file=/etc/entropy-file\n");
   1535 			if (target_file_exists_p("/boot.cfg")) {
   1536 				run_program(RUN_CHROOT|RUN_FATAL,
   1537 					    "sh -c 'sed -e s./var/db/./etc/. "
   1538 					    "< /boot.cfg "
   1539 					    "> /tmp/boot.cfg.tmp'");
   1540 				mv_within_target_or_die("/tmp/boot.cfg.tmp",
   1541 							"/boot.cfg");
   1542 
   1543 			}
   1544 		}
   1545 
   1546 #ifdef MD_BOOT_CFG_FINALIZE
   1547 		if (target_file_exists_p("/boot.cfg")) {
   1548 			MD_BOOT_CFG_FINALIZE("/boot.cfg");
   1549 		}
   1550 #endif
   1551 
   1552 		/* Save keyboard type */
   1553 		save_kb_encoding();
   1554 
   1555 		/* Other configuration. */
   1556 		mnt_net_config();
   1557 	}
   1558 
   1559 	/* Mounted dist dir? */
   1560 	umount_mnt2();
   1561 
   1562 #ifdef CHECK_ENTROPY
   1563 	entropy_loaded |= entropy_needed() == 0;
   1564 #endif
   1565 
   1566 	/* Save entropy -- on some systems it's ~all we'll ever get */
   1567 	if (!update || entropy_loaded)
   1568 		run_program(RUN_SILENT | RUN_CHROOT | RUN_ERROR_OK,
   1569 		    "/etc/rc.d/random_seed stop");
   1570 	/* Install/Upgrade complete ... reboot or exit to script */
   1571 	hit_enter_to_continue(success_msg, NULL);
   1572 	return 0;
   1573 }
   1574 
   1575 void
   1576 umount_mnt2(void)
   1577 {
   1578 	if (!mnt2_mounted)
   1579 		return;
   1580 	run_program(RUN_SILENT, "/sbin/umount /mnt2");
   1581 	mnt2_mounted = 0;
   1582 }
   1583 
   1584 
   1585 /*
   1586  * Do a quick sanity check that  the target can reboot.
   1587  * return 1 if everything OK, 0 if there is a problem.
   1588  * Uses a table of files we expect to find after a base install/upgrade.
   1589  */
   1590 
   1591 /* test flag and pathname to check for after unpacking. */
   1592 struct check_table { unsigned int mode; const char *path;} checks[] = {
   1593   { S_IFREG, "/netbsd" },
   1594   { S_IFDIR, "/etc" },
   1595   { S_IFREG, "/etc/fstab" },
   1596   { S_IFREG, "/sbin/init" },
   1597   { S_IFREG, "/bin/sh" },
   1598   { S_IFREG, "/etc/rc" },
   1599   { S_IFREG, "/etc/rc.subr" },
   1600   { S_IFREG, "/etc/rc.conf" },
   1601   { S_IFDIR, "/dev" },
   1602   { S_IFCHR, "/dev/console" },
   1603 /* XXX check for rootdev in target /dev? */
   1604   { S_IFREG, "/sbin/fsck" },
   1605   { S_IFREG, "/sbin/fsck_ffs" },
   1606   { S_IFREG, "/sbin/mount" },
   1607   { S_IFREG, "/sbin/mount_ffs" },
   1608   { S_IFREG, "/sbin/mount_nfs" },
   1609 #if defined(DEBUG) || defined(DEBUG_CHECK)
   1610   { S_IFREG, "/foo/bar" },		/* bad entry to exercise warning */
   1611 #endif
   1612   { 0, 0 }
   1613 
   1614 };
   1615 
   1616 /*
   1617  * Check target for a single file.
   1618  */
   1619 static int
   1620 check_for(unsigned int mode, const char *pathname)
   1621 {
   1622 	int found;
   1623 
   1624 	found = (target_test(mode, pathname) == 0);
   1625 	if (found == 0)
   1626 		msg_fmt_display(MSG_rootmissing, "%s", pathname);
   1627 	return found;
   1628 }
   1629 
   1630 /*
   1631  * Check that all the files in check_table are present in the
   1632  * target root. Warn if not found.
   1633  */
   1634 int
   1635 sanity_check(void)
   1636 {
   1637 	int target_ok = 1;
   1638 	struct check_table *p;
   1639 
   1640 	for (p = checks; p->path; p++) {
   1641 		target_ok = target_ok && check_for(p->mode, p->path);
   1642 	}
   1643 	if (target_ok)
   1644 		return 0;
   1645 
   1646 	/* Uh, oh. Something's missing. */
   1647 	hit_enter_to_continue(MSG_badroot, NULL);
   1648 	return 1;
   1649 }
   1650 
   1651 /*
   1652  * Some globals to pass things back from callbacks
   1653  */
   1654 static char zoneinfo_dir[STRSIZE];
   1655 static int zonerootlen;
   1656 static char *tz_selected;	/* timezonename (relative to share/zoneinfo */
   1657 const char *tz_default;		/* UTC, or whatever /etc/localtime points to */
   1658 static char tz_env[STRSIZE];
   1659 static int save_cursel, save_topline;
   1660 static int time_menu = -1;
   1661 
   1662 static void
   1663 update_time_display(void)
   1664 {
   1665 	time_t t;
   1666 	struct tm *tm;
   1667 	char cur_time[STRSIZE], *p;
   1668 
   1669 	t = time(NULL);
   1670 	tm = localtime(&t);
   1671 	strlcpy(cur_time, safectime(&t), sizeof cur_time);
   1672 	p = strchr(cur_time, '\n');
   1673 	if (p != NULL)
   1674 		*p = 0;
   1675 
   1676 	msg_clear();
   1677 	msg_fmt_table_add(MSG_choose_timezone, "%s%s%s%s",
   1678 	    tz_default, tz_selected, cur_time, tm ? tm->tm_zone : "?");
   1679 }
   1680 
   1681 /*
   1682  * Callback from timezone menu
   1683  */
   1684 static int
   1685 set_tz_select(menudesc *m, void *arg)
   1686 {
   1687 	char *new;
   1688 
   1689 	if (m && strcmp(tz_selected, m->opts[m->cursel].opt_name) != 0) {
   1690 		/* Change the displayed timezone */
   1691 		new = strdup(m->opts[m->cursel].opt_name);
   1692 		if (new == NULL)
   1693 			return 0;
   1694 		free(tz_selected);
   1695 		tz_selected = new;
   1696 		snprintf(tz_env, sizeof tz_env, "%.*s%s",
   1697 			 zonerootlen, zoneinfo_dir, tz_selected);
   1698 		setenv("TZ", tz_env, 1);
   1699 	}
   1700 	if (m)
   1701 		/* Warp curser to 'Exit' line on menu */
   1702 		m->cursel = -1;
   1703 
   1704 	update_time_display();
   1705 	if (time_menu >= 1) {
   1706 		WINDOW *w = get_menudesc(time_menu)->mw;
   1707 		if (w != NULL) {
   1708 			touchwin(w);
   1709 			wrefresh(w);
   1710 		}
   1711 	}
   1712 	return 0;
   1713 }
   1714 
   1715 static int
   1716 set_tz_back(menudesc *m, void *arg)
   1717 {
   1718 
   1719 	zoneinfo_dir[zonerootlen] = 0;
   1720 	m->cursel = save_cursel;
   1721 	m->topline = save_topline;
   1722 	return 0;
   1723 }
   1724 
   1725 static int
   1726 set_tz_dir(menudesc *m, void *arg)
   1727 {
   1728 
   1729 	strlcpy(zoneinfo_dir + zonerootlen, m->opts[m->cursel].opt_name,
   1730 		sizeof zoneinfo_dir - zonerootlen);
   1731 	save_cursel = m->cursel;
   1732 	save_topline = m->topline;
   1733 	m->cursel = 0;
   1734 	m->topline = 0;
   1735 	return 0;
   1736 }
   1737 
   1738 /*
   1739  * Alarm-handler to update example-display
   1740  */
   1741 static void
   1742 /*ARGSUSED*/
   1743 timezone_sig(int sig)
   1744 {
   1745 
   1746 	set_tz_select(NULL, NULL);
   1747 	alarm(60);
   1748 }
   1749 
   1750 static int
   1751 tz_sort(const void *a, const void *b)
   1752 {
   1753 	return strcmp(((const menu_ent *)a)->opt_name, ((const menu_ent *)b)->opt_name);
   1754 }
   1755 
   1756 static void
   1757 tzm_set_names(menudesc *m, void *arg)
   1758 {
   1759 	DIR *dir;
   1760 	struct dirent *dp;
   1761 	static int nfiles;
   1762 	static int maxfiles = 32;
   1763 	static menu_ent *tz_menu;
   1764 	static char **tz_names;
   1765 	void *p;
   1766 	int maxfname;
   1767 	char *fp;
   1768 	struct stat sb;
   1769 
   1770 	if (tz_menu == NULL)
   1771 		tz_menu = calloc(maxfiles, sizeof *tz_menu);
   1772 	if (tz_names == NULL)
   1773 		tz_names = malloc(maxfiles * sizeof *tz_names);
   1774 	if (tz_menu == NULL || tz_names == NULL)
   1775 		return;	/* error - skip timezone setting */
   1776 	while (nfiles > 0)
   1777 		free(tz_names[--nfiles]);
   1778 
   1779 	dir = opendir(zoneinfo_dir);
   1780 	fp = strchr(zoneinfo_dir, 0);
   1781 	if (fp != zoneinfo_dir + zonerootlen) {
   1782 		tz_names[0] = 0;
   1783 		tz_menu[0].opt_name = msg_string(MSG_tz_back);
   1784 		tz_menu[0].opt_action = set_tz_back;
   1785 		nfiles = 1;
   1786 	}
   1787 	maxfname = zoneinfo_dir + sizeof zoneinfo_dir - fp - 1;
   1788 	if (dir != NULL) {
   1789 		while ((dp = readdir(dir)) != NULL) {
   1790 			if (dp->d_namlen > maxfname || dp->d_name[0] == '.')
   1791 				continue;
   1792 			strlcpy(fp, dp->d_name, maxfname);
   1793 			if (stat(zoneinfo_dir, &sb) == -1)
   1794 				continue;
   1795 			if (nfiles >= maxfiles) {
   1796 				p = realloc(tz_menu,
   1797 				    2 * maxfiles * sizeof *tz_menu);
   1798 				if (p == NULL)
   1799 					break;
   1800 				tz_menu = p;
   1801 				memset(tz_menu + maxfiles, 0,
   1802 				    maxfiles * sizeof *tz_menu);
   1803 				p = realloc(tz_names,
   1804 				    2 * maxfiles * sizeof *tz_names);
   1805 				if (p == NULL)
   1806 					break;
   1807 				tz_names = p;
   1808 				memset(tz_names + maxfiles, 0,
   1809 				    maxfiles * sizeof *tz_names);
   1810 				maxfiles *= 2;
   1811 			}
   1812 			if (S_ISREG(sb.st_mode))
   1813 				tz_menu[nfiles].opt_action = set_tz_select;
   1814 			else if (S_ISDIR(sb.st_mode)) {
   1815 				tz_menu[nfiles].opt_action = set_tz_dir;
   1816 				strlcat(fp, "/",
   1817 				    sizeof(zoneinfo_dir) - (fp - zoneinfo_dir));
   1818 			} else
   1819 				continue;
   1820 			tz_names[nfiles] = strdup(zoneinfo_dir + zonerootlen);
   1821 			tz_menu[nfiles].opt_name = tz_names[nfiles];
   1822 			nfiles++;
   1823 		}
   1824 		closedir(dir);
   1825 	}
   1826 	*fp = 0;
   1827 
   1828 	m->opts = tz_menu;
   1829 	m->numopts = nfiles;
   1830 	qsort(tz_menu, nfiles, sizeof *tz_menu, tz_sort);
   1831 }
   1832 
   1833 void
   1834 get_tz_default(void)
   1835 {
   1836 	char localtime_link[STRSIZE];
   1837 	static char localtime_target[STRSIZE];
   1838 	int rc;
   1839 
   1840 	strlcpy(localtime_link, target_expand("/etc/localtime"),
   1841 	    sizeof localtime_link);
   1842 
   1843 	/* Add sanity check that /mnt/usr/share/zoneinfo contains
   1844 	 * something useful
   1845 	 */
   1846 
   1847 	rc = readlink(localtime_link, localtime_target,
   1848 		      sizeof(localtime_target) - 1);
   1849 	if (rc < 0) {
   1850 		/* error, default to UTC */
   1851 		tz_default = "UTC";
   1852 	} else {
   1853 		localtime_target[rc] = '\0';
   1854 		tz_default = strchr(strstr(localtime_target, "zoneinfo"), '/') + 1;
   1855 	}
   1856 }
   1857 
   1858 /*
   1859  * Choose from the files in usr/share/zoneinfo and set etc/localtime
   1860  */
   1861 int
   1862 set_timezone(void)
   1863 {
   1864 	char localtime_link[STRSIZE];
   1865 	char localtime_target[STRSIZE];
   1866 	int menu_no;
   1867 
   1868 	strlcpy(zoneinfo_dir, target_expand("/usr/share/zoneinfo/"),
   1869 	    sizeof zoneinfo_dir - 1);
   1870 	zonerootlen = strlen(zoneinfo_dir);
   1871 
   1872 	get_tz_default();
   1873 
   1874 	tz_selected = strdup(tz_default);
   1875 	snprintf(tz_env, sizeof(tz_env), "%s%s", zoneinfo_dir, tz_selected);
   1876 	setenv("TZ", tz_env, 1);
   1877 	update_time_display();
   1878 
   1879 	signal(SIGALRM, timezone_sig);
   1880 	alarm(60);
   1881 
   1882 	menu_no = new_menu(NULL, NULL, 14, 23, 9,
   1883 			   12, 32, MC_ALWAYS_SCROLL | MC_NOSHORTCUT,
   1884 			   tzm_set_names, NULL, NULL,
   1885 			   "\nPlease consult the install documents.",
   1886 			   MSG_exit_menu_generic);
   1887 	if (menu_no >= 0) {
   1888 		time_menu = menu_no;
   1889 		process_menu(menu_no, NULL);
   1890 		time_menu = -1;
   1891 
   1892 		free_menu(menu_no);
   1893 	}
   1894 
   1895 	alarm(0);
   1896 	signal(SIGALRM, SIG_IGN);
   1897 
   1898 	if (menu_no >= 0) {
   1899 		snprintf(localtime_target, sizeof(localtime_target),
   1900 			 "/usr/share/zoneinfo/%s", tz_selected);
   1901 		strlcpy(localtime_link, target_expand("/etc/localtime"),
   1902 		    sizeof localtime_link);
   1903 		unlink(localtime_link);
   1904 		symlink(localtime_target, localtime_link);
   1905 	}
   1906 
   1907 	return 1;
   1908 }
   1909 
   1910 void
   1911 scripting_vfprintf(FILE *f, const char *fmt, va_list ap)
   1912 {
   1913 	va_list ap2;
   1914 
   1915 	va_copy(ap2, ap);
   1916 	if (f)
   1917 		(void)vfprintf(f, fmt, ap);
   1918 	if (script)
   1919 		(void)vfprintf(script, fmt, ap2);
   1920 }
   1921 
   1922 void
   1923 scripting_fprintf(FILE *f, const char *fmt, ...)
   1924 {
   1925 	va_list ap;
   1926 
   1927 	va_start(ap, fmt);
   1928 	scripting_vfprintf(f, fmt, ap);
   1929 	va_end(ap);
   1930 }
   1931 
   1932 void
   1933 add_rc_conf(const char *fmt, ...)
   1934 {
   1935 	FILE *f;
   1936 	va_list ap;
   1937 
   1938 	va_start(ap, fmt);
   1939 	f = target_fopen("/etc/rc.conf", "a");
   1940 	if (f != 0) {
   1941 		scripting_fprintf(NULL, "cat <<EOF >>%s/etc/rc.conf\n",
   1942 		    target_prefix());
   1943 		scripting_vfprintf(f, fmt, ap);
   1944 		fclose(f);
   1945 		scripting_fprintf(NULL, "EOF\n");
   1946 	}
   1947 	va_end(ap);
   1948 }
   1949 
   1950 int
   1951 del_rc_conf(const char *value)
   1952 {
   1953 	FILE *fp, *nfp;
   1954 	char buf[4096]; /* Ridiculously high, but should be enough in any way */
   1955 	char *rcconf, *tempname = NULL, *bakname = NULL;
   1956 	char *cp;
   1957 	int done = 0;
   1958 	int fd;
   1959 	int retval = 0;
   1960 
   1961 	/* The paths might seem strange, but using /tmp would require copy instead
   1962 	 * of rename operations. */
   1963 	if (asprintf(&rcconf, "%s", target_expand("/etc/rc.conf")) < 0
   1964 			|| asprintf(&tempname, "%s", target_expand("/etc/rc.conf.tmp.XXXXXX")) < 0
   1965 			|| asprintf(&bakname, "%s", target_expand("/etc/rc.conf.bak.XXXXXX")) < 0) {
   1966 		if (rcconf)
   1967 			free(rcconf);
   1968 		if (tempname)
   1969 			free(tempname);
   1970 		msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
   1971 		hit_enter_to_continue(NULL, NULL);
   1972 		return -1;
   1973 	}
   1974 
   1975 	if ((fd = mkstemp(bakname)) < 0) {
   1976 		msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
   1977 		hit_enter_to_continue(NULL, NULL);
   1978 		return -1;
   1979 	}
   1980 	close(fd);
   1981 
   1982 	if (!(fp = fopen(rcconf, "r+")) || (fd = mkstemp(tempname)) < 0) {
   1983 		if (fp)
   1984 			fclose(fp);
   1985 		msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
   1986 		hit_enter_to_continue(NULL, NULL);
   1987 		return -1;
   1988 	}
   1989 
   1990 	nfp = fdopen(fd, "w");
   1991 	if (!nfp) {
   1992 		fclose(fp);
   1993 		close(fd);
   1994 		msg_fmt_display(MSG_rcconf_delete_failed, "%s", value);
   1995 		hit_enter_to_continue(NULL, NULL);
   1996 		return -1;
   1997 	}
   1998 
   1999 	while (fgets(buf, sizeof buf, fp) != NULL) {
   2000 
   2001 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
   2002 		if (strncmp(cp, value, strlen(value)) == 0) {
   2003 			cp += strlen(value);
   2004 			if (*cp != '=')
   2005 				scripting_fprintf(nfp, "%s", buf);
   2006 			else
   2007 				done = 1;
   2008 		} else {
   2009 			scripting_fprintf(nfp, "%s", buf);
   2010 		}
   2011 	}
   2012 	fclose(fp);
   2013 	fclose(nfp);
   2014 
   2015 	if (done) {
   2016 		if (rename(rcconf, bakname)) {
   2017 			msg_display(MSG_rcconf_backup_failed);
   2018 			if (!ask_noyes(NULL)) {
   2019 				retval = -1;
   2020 				goto done;
   2021 			}
   2022 		}
   2023 
   2024 		if (rename(tempname, rcconf)) {
   2025 			if (rename(bakname, rcconf)) {
   2026 				hit_enter_to_continue(MSG_rcconf_restore_failed,
   2027 				    NULL);
   2028 			} else {
   2029 				hit_enter_to_continue(MSG_rcconf_delete_failed,
   2030 				    NULL);
   2031 			}
   2032 		} else {
   2033 			(void)unlink(bakname);
   2034 		}
   2035 	}
   2036 
   2037 done:
   2038 	(void)unlink(tempname);
   2039 	free(rcconf);
   2040 	free(tempname);
   2041 	free(bakname);
   2042 	return retval;
   2043 }
   2044 
   2045 void
   2046 add_sysctl_conf(const char *fmt, ...)
   2047 {
   2048 	FILE *f;
   2049 	va_list ap;
   2050 
   2051 	va_start(ap, fmt);
   2052 	f = target_fopen("/etc/sysctl.conf", "a");
   2053 	if (f != 0) {
   2054 		scripting_fprintf(NULL, "cat <<EOF >>%s/etc/sysctl.conf\n",
   2055 		    target_prefix());
   2056 		scripting_vfprintf(f, fmt, ap);
   2057 		fclose(f);
   2058 		scripting_fprintf(NULL, "EOF\n");
   2059 	}
   2060 	va_end(ap);
   2061 }
   2062 
   2063 void
   2064 enable_rc_conf(void)
   2065 {
   2066 
   2067 	replace("/etc/rc.conf", "s/^rc_configured=NO/rc_configured=YES/");
   2068 }
   2069 
   2070 int
   2071 check_lfs_progs(void)
   2072 {
   2073 
   2074 #ifndef NO_LFS
   2075 	return binary_available("fsck_lfs") && binary_available("mount_lfs")
   2076 	    && binary_available("newfs_lfs");
   2077 #else
   2078 	return 0;
   2079 #endif
   2080 }
   2081 
   2082 int
   2083 set_is_source(const char *set_name) {
   2084 	int len = strlen(set_name);
   2085 	return len >= 3 && memcmp(set_name + len - 3, "src", 3) == 0;
   2086 }
   2087 
   2088 const char *
   2089 set_dir_for_set(const char *set_name) {
   2090 	if (strcmp(set_name, "pkgsrc") == 0)
   2091 		return pkgsrc_dir;
   2092 	return set_is_source(set_name) ? set_dir_src : set_dir_bin;
   2093 }
   2094 
   2095 const char *
   2096 ext_dir_for_set(const char *set_name) {
   2097 	if (strcmp(set_name, "pkgsrc") == 0)
   2098 		return ext_dir_pkgsrc;
   2099 	return set_is_source(set_name) ? ext_dir_src : ext_dir_bin;
   2100 }
   2101 
   2102 void
   2103 do_coloring(unsigned int fg, unsigned int bg) {
   2104 	if (bg > COLOR_WHITE)
   2105 		bg = COLOR_BLUE;
   2106 	if (fg > COLOR_WHITE)
   2107 		fg = COLOR_WHITE;
   2108 	if (fg != bg && has_colors()) {
   2109 		init_pair(1, fg, bg);
   2110 		wbkgd(stdscr, COLOR_PAIR(1));
   2111 		wbkgd(mainwin, COLOR_PAIR(1));
   2112 		wattrset(stdscr, COLOR_PAIR(1));
   2113 		wattrset(mainwin, COLOR_PAIR(1));
   2114 	}
   2115 	/* redraw screen */
   2116 	touchwin(stdscr);
   2117 	touchwin(mainwin);
   2118 	wrefresh(stdscr);
   2119 	wrefresh(mainwin);
   2120 	return;
   2121 }
   2122 
   2123 int
   2124 set_menu_select(menudesc *m, void *arg)
   2125 {
   2126 	*(int *)arg = m->cursel;
   2127 	return 1;
   2128 }
   2129 
   2130 /*
   2131  * check wether a binary is available somewhere in PATH,
   2132  * return 1 if found, 0 if not.
   2133  */
   2134 static int
   2135 binary_available(const char *prog)
   2136 {
   2137         char *p, tmp[MAXPATHLEN], *path = getenv("PATH"), *opath;
   2138 
   2139         if (path == NULL)
   2140                 return access(prog, X_OK) == 0;
   2141         path = strdup(path);
   2142         if (path == NULL)
   2143                 return 0;
   2144 
   2145 	opath = path;
   2146 
   2147         while ((p = strsep(&path, ":")) != NULL) {
   2148                 if (strlcpy(tmp, p, MAXPATHLEN) >= MAXPATHLEN)
   2149                         continue;
   2150                 if (strlcat(tmp, "/", MAXPATHLEN) >= MAXPATHLEN)
   2151                         continue;
   2152                 if (strlcat(tmp, prog, MAXPATHLEN) >= MAXPATHLEN)
   2153                         continue;
   2154                 if (access(tmp, X_OK) == 0) {
   2155                         free(opath);
   2156                         return 1;
   2157                 }
   2158         }
   2159         free(opath);
   2160         return 0;
   2161 }
   2162 
   2163 const char *
   2164 safectime(time_t *t)
   2165 {
   2166 	const char *s = ctime(t);
   2167 	if (s != NULL)
   2168 		return s;
   2169 
   2170 	// Debugging.
   2171 	fprintf(stderr, "Can't convert to localtime 0x%jx (%s)\n",
   2172 	    (intmax_t)*t, strerror(errno));
   2173 	      /*123456789012345678901234*/
   2174 	return "preposterous clock time\n";
   2175 }
   2176 
   2177 int
   2178 ask_yesno(const char* msgtxt)
   2179 {
   2180 	arg_rv p;
   2181 
   2182 	p.arg = __UNCONST(msgtxt);
   2183 	p.rv = -1;
   2184 
   2185 	process_menu(MENU_yesno, &p);
   2186 	return p.rv;
   2187 }
   2188 
   2189 int
   2190 ask_noyes(const char *msgtxt)
   2191 {
   2192 	arg_rv p;
   2193 
   2194 	p.arg = __UNCONST(msgtxt);
   2195 	p.rv = -1;
   2196 
   2197 	process_menu(MENU_noyes, &p);
   2198 	return p.rv;
   2199 }
   2200 
   2201 int
   2202 ask_reedit(const struct disk_partitions *parts)
   2203 {
   2204 	const char *args[2];
   2205 	arg_rep_int arg;
   2206 
   2207 	args[0] = msg_string(parts->pscheme->name);
   2208 	args[1] = msg_string(parts->pscheme->short_name);
   2209 	arg.args.argv = args;
   2210 	arg.args.argc = 2;
   2211 	arg.rv = 0;
   2212 	process_menu(MENU_reedit, &arg);
   2213 
   2214 	return arg.rv;
   2215 }
   2216 
   2217 bool
   2218 use_tgz_for_set(const char *set_name)
   2219 {
   2220 	const struct distinfo *dist;
   2221 
   2222 	for (dist = dist_list; dist->set != SET_LAST; dist++) {
   2223 		if (dist->name == NULL)
   2224 			continue;
   2225 		if (strcmp(set_name, dist->name) == 0)
   2226 			return dist->force_tgz;
   2227 	}
   2228 
   2229 	return true;
   2230 }
   2231 
   2232 /* Return the postfix used for a given set */
   2233 const char *
   2234 set_postfix(const char *set_name)
   2235 {
   2236 	return use_tgz_for_set(set_name) ? dist_tgz_postfix : dist_postfix;
   2237 }
   2238 
   2239 /*
   2240  * Replace positional arguments (encoded as $0 .. $N) in the string
   2241  * passed by the contents of the passed argument array.
   2242  * Caller must free() the result string.
   2243  */
   2244 char*
   2245 str_arg_subst(const char *src, size_t argc, const char **argv)
   2246 {
   2247 	const char *p, *last;
   2248 	char *out, *t;
   2249 	size_t len;
   2250 
   2251 	len = strlen(src);
   2252 	for (p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
   2253 		char *endp = NULL;
   2254 		size_t n;
   2255 		int e;
   2256 
   2257 		/* $ followed by a correct numeric position? */
   2258 		n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
   2259 		if ((e == 0 || e == ENOTSUP) && n < argc) {
   2260 			len += strlen(argv[n]);
   2261 			len -= endp-p;
   2262 			p = endp-1;
   2263 		}
   2264 	}
   2265 
   2266 	out = malloc(len+1);
   2267 	if (out == NULL)
   2268 		return NULL;
   2269 
   2270 	t = out;
   2271 	for (last = src, p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
   2272 		char *endp = NULL;
   2273 		size_t n;
   2274 		int e;
   2275 
   2276 		/* $ followed by a correct numeric position? */
   2277 		n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
   2278 		if ((e == 0 || e == ENOTSUP) && n < argc) {
   2279 			size_t l = p-last;
   2280 			memcpy(t, last, l);
   2281 			t += l;
   2282 			strcpy(t, argv[n]);
   2283 			t += strlen(argv[n]);
   2284 			last = endp;
   2285 		}
   2286 	}
   2287 	if (*last) {
   2288 		strcpy(t, last);
   2289 		t += strlen(last);
   2290 	} else {
   2291 		*t = 0;
   2292 	}
   2293 	assert((size_t)(t-out) == len);
   2294 
   2295 	return out;
   2296 }
   2297 
   2298 /*
   2299  * Does this string have any positional args that need expanding?
   2300  */
   2301 bool
   2302 needs_expanding(const char *src, size_t argc)
   2303 {
   2304 	const char *p;
   2305 
   2306 	for (p = strchr(src, '$'); p; p = strchr(p+1, '$')) {
   2307 		char *endp = NULL;
   2308 		size_t n;
   2309 		int e;
   2310 
   2311 		/* $ followed by a correct numeric position? */
   2312 		n = strtou(p+1, &endp, 10, 0, INT_MAX, &e);
   2313 		if ((e == 0 || e == ENOTSUP) && n < argc)
   2314 			return true;
   2315 	}
   2316 	return false;
   2317 }
   2318 
   2319 /*
   2320  * Replace positional arguments (encoded as $0 .. $N) in the
   2321  * message by the strings passed as ... and call outfunc
   2322  * with the result.
   2323  */
   2324 static void
   2325 msg_display_subst_internal(void (*outfunc)(msg),
   2326     const char *master, size_t argc, va_list ap)
   2327 {
   2328 	const char **args, **arg;
   2329 	char *out;
   2330 
   2331 	args = calloc(argc, sizeof(*args));
   2332 	if (args == NULL)
   2333 		return;
   2334 
   2335 	arg = args;
   2336 	for (size_t i = 0; i < argc; i++)
   2337 		*arg++ = va_arg(ap, const char*);
   2338 
   2339 	out = str_arg_subst(msg_string(master), argc, args);
   2340 	if (out != NULL) {
   2341 		outfunc(out);
   2342 		free(out);
   2343 	}
   2344 	free(args);
   2345 }
   2346 
   2347 /*
   2348  * Replace positional arguments (encoded as $0 .. $N) in the
   2349  * message by the strings passed as ...
   2350  */
   2351 void
   2352 msg_display_subst(const char *master, size_t argc, ...)
   2353 {
   2354 	va_list ap;
   2355 
   2356 	va_start(ap, argc);
   2357 	msg_display_subst_internal(msg_display, master, argc, ap);
   2358 	va_end(ap);
   2359 }
   2360 
   2361 /*
   2362  * Same as above, but add to message instead of starting a new one
   2363  */
   2364 void
   2365 msg_display_add_subst(const char *master, size_t argc, ...)
   2366 {
   2367 	va_list ap;
   2368 
   2369 	va_start(ap, argc);
   2370 	msg_display_subst_internal(msg_display_add, master, argc, ap);
   2371 	va_end(ap);
   2372 }
   2373 
   2374 /* initialize have_* variables */
   2375 void
   2376 check_available_binaries()
   2377 {
   2378 	static int did_test = false;
   2379 
   2380 	if (did_test) return;
   2381 	did_test = 1;
   2382 
   2383 	have_raid = binary_available("raidctl");
   2384 	have_vnd = binary_available("vndconfig");
   2385 	have_cgd = binary_available("cgdconfig");
   2386 	have_lvm = binary_available("lvm");
   2387 	have_gpt = binary_available("gpt");
   2388 	have_dk = binary_available("dkctl");
   2389 }
   2390 
   2391 /*
   2392  * Wait for enter and immediately clear the screen after user response
   2393  * (in case some longer action follows, so the user has instant feedback)
   2394  */
   2395 void
   2396 hit_enter_to_continue(const char *prompt, const char *title)
   2397 {
   2398 	if (prompt != NULL)
   2399 		msg_display(prompt);
   2400         process_menu(MENU_ok, __UNCONST(title));
   2401 	msg_clear();
   2402 	wrefresh(mainwin);
   2403 }
   2404 
   2405 /*
   2406  * On auto pilot:
   2407  * convert an existing set of partitions ot a list of part_usage_info
   2408  * so that we "want" exactly what is there already.
   2409  */
   2410 static bool
   2411 usage_info_list_from_parts(struct part_usage_info **list, size_t *count,
   2412     struct disk_partitions *parts)
   2413 {
   2414 	struct disk_part_info info;
   2415 	part_id pno;
   2416 	size_t no, num;
   2417 
   2418 	num = 0;
   2419 	for (pno = 0; pno < parts->num_part; pno++) {
   2420 		if (!parts->pscheme->get_part_info(parts, pno, &info))
   2421 			continue;
   2422 		num++;
   2423 	}
   2424 
   2425 	*list = calloc(num, sizeof(**list));
   2426 	if (*list == NULL)
   2427 		return false;
   2428 
   2429 	*count = num;
   2430 	for (no = pno = 0; pno < parts->num_part && no < num; pno++) {
   2431 		if (!parts->pscheme->get_part_info(parts, pno, &info))
   2432 			continue;
   2433 		(*list)[no].size = info.size;
   2434 		if (info.last_mounted != NULL && *info.last_mounted != 0) {
   2435 			strlcpy((*list)[no].mount, info.last_mounted,
   2436 			    sizeof((*list)[no].mount));
   2437 			(*list)[no].instflags |= PUIINST_MOUNT;
   2438 		}
   2439 		(*list)[no].parts = parts;
   2440 		(*list)[no].cur_part_id = pno;
   2441 		(*list)[no].cur_start = info.start;
   2442 		(*list)[no].cur_flags = info.flags;
   2443 		(*list)[no].type = info.nat_type->generic_ptype;
   2444 		if ((*list)[no].type == PT_swap) {
   2445 			(*list)[no].fs_type = FS_SWAP;
   2446 			(*list)[no].fs_version = 0;
   2447 		} else {
   2448 			(*list)[no].fs_type = info.fs_type;
   2449 			(*list)[no].fs_version = info.fs_sub_type;
   2450 		}
   2451 		(*list)[no].fs_opt1 = info.fs_opt1;
   2452 		(*list)[no].fs_opt2 = info.fs_opt2;
   2453 		(*list)[no].fs_opt3 = info.fs_opt3;
   2454 		no++;
   2455 	}
   2456 	return true;
   2457 }
   2458 
   2459 bool
   2460 usage_set_from_parts(struct partition_usage_set *wanted,
   2461     struct disk_partitions *parts)
   2462 {
   2463 	memset(wanted, 0, sizeof(*wanted));
   2464 	wanted->parts = parts;
   2465 
   2466 	return usage_info_list_from_parts(&wanted->infos, &wanted->num, parts);
   2467 }
   2468 
   2469 struct disk_partitions *
   2470 get_inner_parts(struct disk_partitions *parts)
   2471 {
   2472 	daddr_t start, size;
   2473 	part_id pno;
   2474 	struct disk_part_info info;
   2475 
   2476 	if (parts->pscheme->secondary_scheme == NULL)
   2477 		return NULL;
   2478 
   2479 	start = -1;
   2480 	size = -1;
   2481 	if (parts->pscheme->guess_install_target == NULL ||
   2482 	    !parts->pscheme->guess_install_target(parts, &start, &size)) {
   2483 		for (pno = 0; pno < parts->num_part; pno++) {
   2484 			if (!parts->pscheme->get_part_info(parts, pno, &info))
   2485 				continue;
   2486 			if (!(info.flags & PTI_SEC_CONTAINER))
   2487 				continue;
   2488 			start = info.start;
   2489 			size = info.size;
   2490 		}
   2491 	}
   2492 
   2493 	if (size > 0)
   2494 		return parts->pscheme->secondary_partitions(parts, start,
   2495 		    false);
   2496 
   2497 	return NULL;
   2498 }
   2499 
   2500 bool
   2501 install_desc_from_parts(struct install_partition_desc *install,
   2502     struct disk_partitions *parts)
   2503 {
   2504 	struct disk_partitions *inner_parts;
   2505 
   2506 	memset(install, 0, sizeof(*install));
   2507 	inner_parts = get_inner_parts(parts);
   2508 	if (inner_parts != NULL)
   2509 		parts = inner_parts;
   2510 
   2511 	return usage_info_list_from_parts(&install->infos, &install->num,
   2512 	    parts);
   2513 }
   2514 
   2515 void
   2516 free_usage_set(struct partition_usage_set *wanted)
   2517 {
   2518 	/* XXX - free parts? free clone src? */
   2519 	free(wanted->write_back);
   2520 	free(wanted->menu_opts);
   2521 	free(wanted->infos);
   2522 }
   2523 
   2524 void
   2525 free_install_desc(struct install_partition_desc *install)
   2526 {
   2527 #ifndef NO_CLONES
   2528 	size_t i, j;
   2529 
   2530 	for (i = 0; i < install->num; i++) {
   2531 		struct selected_partitions *src = install->infos[i].clone_src;
   2532 		if (!(install->infos[i].flags & PUIFLG_CLONE_PARTS) ||
   2533 		    src == NULL)
   2534 			continue;
   2535 		free_selected_partitions(src);
   2536 		for (j = i+1; j < install->num; j++)
   2537 			if (install->infos[j].clone_src == src)
   2538 				install->infos[j].clone_src = NULL;
   2539 	}
   2540 #endif
   2541 	free(install->write_back);
   2542 	free(install->infos);
   2543 }
   2544 
   2545 #ifdef MD_MAY_SWAP_TO
   2546 bool
   2547 may_swap_if_not_sdmmc(const char *disk)
   2548 {
   2549 	int fd, res;
   2550 	prop_dictionary_t command_dict, args_dict, results_dict, data_dict;
   2551 	prop_string_t string;
   2552 	prop_number_t number;
   2553 	const char *parent = "";
   2554 
   2555 	fd = open(DRVCTLDEV, O_RDONLY, 0);
   2556 	if (fd == -1)
   2557 		return true;
   2558 
   2559 	command_dict = prop_dictionary_create();
   2560 	args_dict = prop_dictionary_create();
   2561 
   2562 	string = prop_string_create_nocopy("get-properties");
   2563 	prop_dictionary_set(command_dict, "drvctl-command", string);
   2564 	prop_object_release(string);
   2565 
   2566 	string = prop_string_create_copy(disk);
   2567 	prop_dictionary_set(args_dict, "device-name", string);
   2568 	prop_object_release(string);
   2569 
   2570 	prop_dictionary_set(command_dict, "drvctl-arguments",
   2571 	    args_dict);
   2572 	prop_object_release(args_dict);
   2573 
   2574 	res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
   2575 	    DRVCTLCOMMAND, &results_dict);
   2576 	prop_object_release(command_dict);
   2577 	close(fd);
   2578 	if (res)
   2579 		return true;
   2580 
   2581 	number = prop_dictionary_get(results_dict, "drvctl-error");
   2582 	if (prop_number_signed_value(number) == 0) {
   2583 		data_dict = prop_dictionary_get(results_dict,
   2584 		    "drvctl-result-data");
   2585 		if (data_dict != NULL) {
   2586 			string = prop_dictionary_get(data_dict,
   2587 			    "device-parent");
   2588 			if (string != NULL)
   2589 				parent = prop_string_value(string);
   2590 		}
   2591 	}
   2592 
   2593 	prop_object_release(results_dict);
   2594 
   2595 	if (parent == NULL)
   2596 		return true;
   2597 
   2598 	return strncmp(parent, "sdmmc", 5) != 0;
   2599 }
   2600 #endif
   2601