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