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