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