1 /* $NetBSD: main.c,v 1.30 2022/07/10 10:52:40 martin Exp $ */ 2 3 /* 4 * Copyright 1997 Piermont Information Systems Inc. 5 * All rights reserved. 6 * 7 * Written by Philip A. Nelson for Piermont Information Systems Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of Piermont Information Systems Inc. may not be used to endorse 18 * or promote products derived from this software without specific prior 19 * written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 /* main sysinst program. */ 36 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <sys/syslimits.h> 40 #include <sys/uio.h> 41 #include <stdio.h> 42 #include <signal.h> 43 #include <curses.h> 44 #include <unistd.h> 45 #include <fcntl.h> 46 #include <dirent.h> 47 #include <locale.h> 48 49 #include "defs.h" 50 #include "md.h" 51 #include "msg_defs.h" 52 #include "menu_defs.h" 53 #include "txtwalk.h" 54 55 int debug; 56 char machine[SSTRSIZE]; 57 int ignorerror; 58 int ttysig_ignore; 59 pid_t ttysig_forward; 60 uint sizemult; 61 int partman_go; 62 FILE *logfp; 63 FILE *script; 64 daddr_t root_limit; 65 struct pm_head_t pm_head; 66 struct pm_devs *pm; 67 struct pm_devs *pm_new; 68 char xfer_dir[STRSIZE]; 69 int clean_xfer_dir; 70 char ext_dir_bin[STRSIZE]; 71 char ext_dir_src[STRSIZE]; 72 char ext_dir_pkgsrc[STRSIZE]; 73 char set_dir_bin[STRSIZE]; 74 char set_dir_src[STRSIZE]; 75 char pkg_dir[STRSIZE]; 76 char pkgsrc_dir[STRSIZE]; 77 const char *ushell; 78 struct ftpinfo ftp, pkg, pkgsrc; 79 int (*fetch_fn)(const char *); 80 char nfs_host[STRSIZE]; 81 char nfs_dir[STRSIZE]; 82 char cdrom_dev[SSTRSIZE]; 83 char fd_dev[SSTRSIZE]; 84 const char *fd_type; 85 char localfs_dev[SSTRSIZE]; 86 char localfs_fs[SSTRSIZE]; 87 char localfs_dir[STRSIZE]; 88 char targetroot_mnt[SSTRSIZE]; 89 int mnt2_mounted; 90 char dist_postfix[SSTRSIZE]; 91 char dist_tgz_postfix[SSTRSIZE]; 92 WINDOW *mainwin; 93 94 static void select_language(const char*); 95 __dead static void usage(void); 96 __dead static void miscsighandler(int); 97 static void ttysighandler(int); 98 static void cleanup(void); 99 static void process_f_flag(char *); 100 101 static int exit_cleanly = 0; /* Did we finish nicely? */ 102 FILE *logfp; /* log file */ 103 FILE *script; /* script file */ 104 105 const char *multname; 106 const char *err_outofmem; 107 108 #ifdef DEBUG 109 extern int log_flip(void); 110 #endif 111 112 /* Definion for colors */ 113 114 struct { 115 unsigned int bg; 116 unsigned int fg; 117 } clr_arg; 118 119 /* String defaults and stuff for processing the -f file argument. */ 120 121 struct f_arg { 122 const char *name; 123 const char *dflt; 124 char *var; 125 int size; 126 }; 127 128 static const struct f_arg fflagopts[] = { 129 {"release", REL, NULL, 0}, 130 {"machine", MACH, machine, sizeof machine}, 131 {"xfer dir", "/usr/INSTALL", xfer_dir, sizeof xfer_dir}, 132 {"ext dir", "", ext_dir_bin, sizeof ext_dir_bin}, 133 {"ext src dir", "", ext_dir_src, sizeof ext_dir_src}, 134 {"ftp host", SYSINST_FTP_HOST, ftp.xfer_host[XFER_FTP], sizeof ftp.xfer_host[XFER_FTP]}, 135 {"http host", SYSINST_HTTP_HOST, ftp.xfer_host[XFER_HTTP], sizeof ftp.xfer_host[XFER_HTTP]}, 136 {"ftp dir", SYSINST_FTP_DIR, ftp.dir, sizeof ftp.dir}, 137 {"ftp prefix", "/" ARCH_SUBDIR "/binary/sets", set_dir_bin, sizeof set_dir_bin}, 138 {"ftp src prefix", "/source/sets", set_dir_src, sizeof set_dir_src}, 139 {"ftp user", "ftp", ftp.user, sizeof ftp.user}, 140 {"ftp pass", "", ftp.pass, sizeof ftp.pass}, 141 {"ftp proxy", "", ftp.proxy, sizeof ftp.proxy}, 142 {"nfs host", "", nfs_host, sizeof nfs_host}, 143 {"nfs dir", "/bsd/release", nfs_dir, sizeof nfs_dir}, 144 {"cd dev", 0, cdrom_dev, sizeof cdrom_dev}, /* default filled in init */ 145 {"fd dev", "/dev/fd0a", fd_dev, sizeof fd_dev}, 146 {"local dev", "", localfs_dev, sizeof localfs_dev}, 147 {"local fs", "ffs", localfs_fs, sizeof localfs_fs}, 148 {"local dir", "release", localfs_dir, sizeof localfs_dir}, 149 {"targetroot mount", "/targetroot", targetroot_mnt, sizeof targetroot_mnt}, 150 {"dist postfix", "." SETS_TAR_SUFF, dist_postfix, sizeof dist_postfix}, 151 {"dist tgz postfix", ".tgz", dist_tgz_postfix, sizeof dist_tgz_postfix}, 152 {"pkg host", SYSINST_PKG_HOST, pkg.xfer_host[XFER_FTP], sizeof pkg.xfer_host[XFER_FTP]}, 153 {"pkg http host", SYSINST_PKG_HTTP_HOST, pkg.xfer_host[XFER_HTTP], sizeof pkg.xfer_host[XFER_HTTP]}, 154 {"pkg dir", SYSINST_PKG_DIR, pkg.dir, sizeof pkg.dir}, 155 {"pkg prefix", "/" PKG_ARCH_SUBDIR "/" PKG_SUBDIR "/All", pkg_dir, sizeof pkg_dir}, 156 {"pkg user", "ftp", pkg.user, sizeof pkg.user}, 157 {"pkg pass", "", pkg.pass, sizeof pkg.pass}, 158 {"pkg proxy", "", pkg.proxy, sizeof pkg.proxy}, 159 {"pkgsrc host", SYSINST_PKGSRC_HOST, pkgsrc.xfer_host[XFER_FTP], sizeof pkgsrc.xfer_host[XFER_FTP]}, 160 {"pkgsrc http host", SYSINST_PKGSRC_HTTP_HOST, pkgsrc.xfer_host[XFER_HTTP], sizeof pkgsrc.xfer_host[XFER_HTTP]}, 161 {"pkgsrc dir", "", pkgsrc.dir, sizeof pkgsrc.dir}, 162 {"pkgsrc prefix", "pub/pkgsrc/stable", pkgsrc_dir, sizeof pkgsrc_dir}, 163 {"pkgsrc user", "ftp", pkgsrc.user, sizeof pkgsrc.user}, 164 {"pkgsrc pass", "", pkgsrc.pass, sizeof pkgsrc.pass}, 165 {"pkgsrc proxy", "", pkgsrc.proxy, sizeof pkgsrc.proxy}, 166 167 {NULL, NULL, NULL, 0} 168 }; 169 170 static void 171 init(void) 172 { 173 const struct f_arg *arg; 174 175 sizemult = 1; 176 clean_xfer_dir = 0; 177 mnt2_mounted = 0; 178 fd_type = "msdos"; 179 180 pm_head = (struct pm_head_t) SLIST_HEAD_INITIALIZER(pm_head); 181 SLIST_INIT(&pm_head); 182 pm_new = malloc(sizeof (struct pm_devs)); 183 memset(pm_new, 0, sizeof *pm_new); 184 185 for (arg = fflagopts; arg->name != NULL; arg++) { 186 if (arg->var == NULL) 187 continue; 188 if (arg->var == cdrom_dev) 189 get_default_cdrom(arg->var, arg->size); 190 else 191 strlcpy(arg->var, arg->dflt, arg->size); 192 } 193 pkg.xfer = pkgsrc.xfer = XFER_HTTP; 194 195 clr_arg.bg=COLOR_BLUE; 196 clr_arg.fg=COLOR_WHITE; 197 } 198 199 static void 200 init_lang(void) 201 { 202 sizemult = 1; 203 err_outofmem = msg_string(MSG_out_of_memory); 204 multname = msg_string(MSG_secname); 205 } 206 207 int 208 main(int argc, char **argv) 209 { 210 int ch; 211 const char *msg_cat_dir = NULL; 212 213 init(); 214 215 #ifdef DEBUG 216 log_flip(); 217 #endif 218 219 /* Check for TERM ... */ 220 if (!getenv("TERM")) { 221 (void)fprintf(stderr, 222 "sysinst: environment variable TERM not set.\n"); 223 exit(4); 224 } 225 226 /* argv processing */ 227 while ((ch = getopt(argc, argv, "Dr:f:C:m:" 228 #ifndef NO_PARTMAN 229 "p" 230 #endif 231 )) != -1) 232 switch(ch) { 233 case 'D': /* set to get past certain errors in testing */ 234 debug = 1; 235 break; 236 case 'r': 237 /* Release name - ignore for compatibility with older versions */ 238 break; 239 case 'f': 240 /* Definition file to read. */ 241 process_f_flag(optarg); 242 break; 243 case 'C': 244 /* Define colors */ 245 sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg); 246 break; 247 case 'm': 248 /* set message catalog directory */ 249 msg_cat_dir = optarg; 250 break; 251 #ifndef NO_PARTMAN 252 case 'p': 253 /* Partition tool */ 254 partman_go = 1; 255 break; 256 #endif 257 case '?': 258 default: 259 usage(); 260 } 261 262 md_init(); 263 264 /* Initialize the partitioning subsystem */ 265 partitions_init(); 266 267 /* initialize message window */ 268 if (menu_init()) { 269 __menu_initerror(); 270 exit(4); 271 } 272 273 /* 274 * Put 'messages' in a window that has a one-character border 275 * on the real screen. 276 */ 277 mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1); 278 if (mainwin == NULL) { 279 (void)fprintf(stderr, 280 "sysinst: screen too small\n"); 281 exit(1); 282 } 283 if (has_colors()) { 284 start_color(); 285 do_coloring(clr_arg.fg,clr_arg.bg); 286 } else { 287 remove_color_options(); 288 } 289 msg_window(mainwin); 290 291 /* Watch for signals and clean up */ 292 (void)atexit(cleanup); 293 (void)signal(SIGINT, ttysighandler); 294 (void)signal(SIGQUIT, ttysighandler); 295 (void)signal(SIGHUP, miscsighandler); 296 297 /* redraw screen */ 298 touchwin(stdscr); 299 refresh(); 300 301 /* Ensure we have mountpoint for target filesystems */ 302 mkdir(targetroot_mnt, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); 303 304 select_language(msg_cat_dir); 305 get_kb_encoding(); 306 init_lang(); 307 308 /* Menu processing */ 309 if (partman_go) 310 partman(NULL); 311 else 312 process_menu(MENU_netbsd, NULL); 313 314 #ifndef NO_PARTMAN 315 /* clean up internal storage */ 316 pm_destroy_all(); 317 #endif 318 319 partitions_cleanup(); 320 321 exit_cleanly = 1; 322 return 0; 323 } 324 325 static int 326 set_language(menudesc *m, void *arg) 327 { 328 char **fnames = arg; 329 330 msg_file(fnames[m->cursel]); 331 return 1; 332 } 333 334 /* 335 * Search for sysinstmsg.* files in the given dir, collect 336 * their names and return the number of files found. 337 * fnames[0] is preallocated and duplicates are ignored. 338 */ 339 struct found_msgs { 340 char **lang_msg, **fnames; 341 int max_lang, num_lang; 342 343 }; 344 static void 345 find_language_files(const char *path, struct found_msgs *res) 346 { 347 DIR *dir; 348 struct dirent *dirent; 349 char fname[PATH_MAX]; 350 const char *cp; 351 352 res->num_lang = 0; 353 dir = opendir(path); 354 if (!dir) 355 return; 356 357 while ((dirent = readdir(dir)) != 0) { 358 if (memcmp(dirent->d_name, "sysinstmsgs.", 12)) 359 continue; 360 361 if (res->num_lang == 0) 362 res->num_lang = 1; 363 strcpy(fname, path); 364 strcat(fname, "/"); 365 strcat(fname, dirent->d_name); 366 if (msg_file(fname)) 367 continue; 368 cp = msg_string(MSG_sysinst_message_language); 369 if (!strcmp(cp, res->lang_msg[0])) 370 continue; 371 if (res->num_lang == res->max_lang) { 372 char **new; 373 res->max_lang *= 2; 374 new = realloc(res->lang_msg, 375 res->max_lang * sizeof *res->lang_msg); 376 if (!new) 377 break; 378 res->lang_msg = new; 379 new = realloc(res->fnames, 380 res->max_lang * sizeof *res->fnames); 381 if (!new) 382 break; 383 res->fnames = new; 384 } 385 res->fnames[res->num_lang] = strdup(fname); 386 res->lang_msg[res->num_lang++] = strdup(cp); 387 } 388 389 closedir(dir); 390 } 391 392 static void 393 select_language(const char *msg_cat_path) 394 { 395 struct found_msgs found; 396 menu_ent *opt = 0; 397 const char *cp; 398 int lang_menu = -1; 399 int lang; 400 401 found.max_lang = 16; 402 found.num_lang = 0; 403 found.lang_msg = malloc(found.max_lang * sizeof *found.lang_msg); 404 found.fnames = malloc(found.max_lang * sizeof *found.fnames); 405 if (!found.lang_msg || !found.fnames) 406 goto done; 407 found.lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language)); 408 found.fnames[0] = NULL; 409 410 if (msg_cat_path != NULL) 411 find_language_files(msg_cat_path, &found); 412 if (found.num_lang == 0) 413 find_language_files(".", &found); 414 #ifdef CATALOG_DIR 415 if (found.num_lang == 0) 416 find_language_files(CATALOG_DIR, &found); 417 #endif 418 419 msg_file(0); 420 421 if (found.num_lang <= 1) 422 goto done; 423 424 opt = calloc(found.num_lang, sizeof *opt); 425 if (!opt) 426 goto done; 427 428 for (lang = 0; lang < found.num_lang; lang++) { 429 opt[lang].opt_name = found.lang_msg[lang]; 430 opt[lang].opt_action = set_language; 431 } 432 433 lang_menu = new_menu(NULL, opt, found.num_lang, -1, 12, 0, 0, 434 MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL); 435 436 if (lang_menu != -1) { 437 msg_display(MSG_hello); 438 process_menu(lang_menu, found.fnames); 439 } 440 441 done: 442 if (lang_menu != -1) 443 free_menu(lang_menu); 444 free(opt); 445 for (int i = 0; i < found.num_lang; i++) { 446 free(found.lang_msg[i]); 447 free(found.fnames[i]); 448 } 449 free(found.lang_msg); 450 free(found.fnames); 451 452 /* set locale according to selected language */ 453 cp = msg_string(MSG_sysinst_message_locale); 454 if (cp) { 455 setlocale(LC_CTYPE, cp); 456 setenv("LC_CTYPE", cp, 1); 457 } 458 } 459 460 #ifndef md_may_remove_boot_medium 461 #define md_may_remove_boot_medium() (boot_media_still_needed()<=0) 462 #endif 463 464 /* toplevel menu handler ... */ 465 void 466 toplevel(void) 467 { 468 /* 469 * Undo any stateful side-effects of previous menu choices. 470 * XXX must be idempotent, since we get run each time the main 471 * menu is displayed. 472 */ 473 char *home = getenv("HOME"); 474 if (home != NULL) 475 if (chdir(home) != 0) 476 (void)chdir("/"); 477 unwind_mounts(); 478 clear_swap(); 479 480 /* Display banner message in (english, francais, deutsch..) */ 481 msg_display(MSG_hello); 482 msg_display_add(MSG_md_hello); 483 if (md_may_remove_boot_medium()) 484 msg_display_add(MSG_md_may_remove_boot_medium); 485 msg_display_add(MSG_thanks); 486 } 487 488 489 /* The usage ... */ 490 491 static void 492 usage(void) 493 { 494 495 (void)fprintf(stderr, "usage: sysinst [-C bg:fg] [-D" 496 #ifndef NO_PARTMAN 497 "p" 498 #endif 499 "] [-f definition_file] " 500 "[-m message_catalog_dir]" 501 "\n" 502 "where:\n" 503 "\t-C bg:fg\n\t\tuse different color scheme\n" 504 "\t-D\n\t\trun in debug mode\n" 505 "\t-f definition_file\n\t\toverride built-in defaults from file\n" 506 "\t-m msg_catalog_dir\n\t\tuse translation files from msg_catalog_dir\n" 507 #ifndef NO_PARTMAN 508 "\t-p\n\t\tonly run the partition editor, no installation\n" 509 #endif 510 ); 511 512 exit(1); 513 } 514 515 /* ARGSUSED */ 516 static void 517 miscsighandler(int signo) 518 { 519 520 /* 521 * we need to cleanup(), but it was already scheduled with atexit(), 522 * so it'll be invoked on exit(). 523 */ 524 exit(1); 525 } 526 527 static void 528 ttysighandler(int signo) 529 { 530 531 /* 532 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we 533 * just return. If we want to forward a TTY signal, we forward it 534 * to the specified process group. 535 * 536 * This functionality is used when setting up and displaying child 537 * output so that the child gets the signal and presumably dies, 538 * but sysinst continues. We use this rather than actually ignoring 539 * the signals, because that will be passed on to a child 540 * through fork/exec, whereas special handlers get reset on exec.. 541 */ 542 if (ttysig_ignore) 543 return; 544 if (ttysig_forward) { 545 killpg(ttysig_forward, signo); 546 return; 547 } 548 549 /* 550 * we need to cleanup(), but it was already scheduled with atexit(), 551 * so it'll be invoked on exit(). 552 */ 553 exit(1); 554 } 555 556 static void 557 cleanup(void) 558 { 559 time_t tloc; 560 561 (void)time(&tloc); 562 563 #if 0 564 restore_etc(); 565 #endif 566 /* Ensure we aren't inside the target tree */ 567 chdir(getenv("HOME")); 568 unwind_mounts(); 569 umount_mnt2(); 570 clear_swap(); 571 572 endwin(); 573 574 if (logfp) { 575 fprintf(logfp, "Log ended at: %s\n", safectime(&tloc)); 576 fflush(logfp); 577 fclose(logfp); 578 logfp = NULL; 579 } 580 if (script) { 581 fprintf(script, "# Script ended at: %s\n", safectime(&tloc)); 582 fflush(script); 583 fclose(script); 584 script = NULL; 585 } 586 587 if (!exit_cleanly) 588 fprintf(stderr, "\n\nsysinst terminated.\n"); 589 } 590 591 592 /* process function ... */ 593 594 void 595 process_f_flag(char *f_name) 596 { 597 char buffer[STRSIZE]; 598 int len; 599 const struct f_arg *arg; 600 FILE *fp; 601 char *cp, *cp1, *err; 602 603 /* open the file */ 604 fp = fopen(f_name, "r"); 605 if (fp == NULL) { 606 const char *args[] = { f_name }; 607 err = str_arg_subst(msg_string(MSG_config_open_error), 608 __arraycount(args), args); 609 fprintf(stderr, "%s\n", err); 610 free(err); 611 exit(1); 612 } 613 614 while (fgets(buffer, sizeof buffer, fp) != NULL) { 615 cp = buffer + strspn(buffer, " \t"); 616 if (strchr("#\r\n", *cp) != NULL) 617 continue; 618 for (arg = fflagopts; arg->name != NULL; arg++) { 619 len = strlen(arg->name); 620 if (memcmp(cp, arg->name, len) != 0) 621 continue; 622 if (arg->var == NULL || arg->size == 0) 623 continue; 624 cp1 = cp + len; 625 cp1 += strspn(cp1, " \t"); 626 if (*cp1++ != '=') 627 continue; 628 cp1 += strspn(cp1, " \t"); 629 len = strcspn(cp1, " \n\r\t"); 630 cp1[len] = 0; 631 strlcpy(arg->var, cp1, arg->size); 632 break; 633 } 634 } 635 636 fclose(fp); 637 } 638