1 /* $NetBSD: main.c,v 1.22 2020/02/19 21:51:21 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 static void select_language(void); 56 __dead static void usage(void); 57 __dead static void miscsighandler(int); 58 static void ttysighandler(int); 59 static void cleanup(void); 60 static void process_f_flag(char *); 61 62 static int exit_cleanly = 0; /* Did we finish nicely? */ 63 FILE *logfp; /* log file */ 64 FILE *script; /* script file */ 65 66 const char *multname; 67 const char *err_outofmem; 68 69 #ifdef DEBUG 70 extern int log_flip(void); 71 #endif 72 73 /* Definion for colors */ 74 75 struct { 76 unsigned int bg; 77 unsigned int fg; 78 } clr_arg; 79 80 /* String defaults and stuff for processing the -f file argument. */ 81 82 struct f_arg { 83 const char *name; 84 const char *dflt; 85 char *var; 86 int size; 87 }; 88 89 static const struct f_arg fflagopts[] = { 90 {"release", REL, NULL, 0}, 91 {"machine", MACH, machine, sizeof machine}, 92 {"xfer dir", "/usr/INSTALL", xfer_dir, sizeof xfer_dir}, 93 {"ext dir", "", ext_dir_bin, sizeof ext_dir_bin}, 94 {"ext src dir", "", ext_dir_src, sizeof ext_dir_src}, 95 {"ftp host", SYSINST_FTP_HOST, ftp.xfer_host[XFER_FTP], sizeof ftp.xfer_host[XFER_FTP]}, 96 {"http host", SYSINST_HTTP_HOST, ftp.xfer_host[XFER_HTTP], sizeof ftp.xfer_host[XFER_HTTP]}, 97 {"ftp dir", SYSINST_FTP_DIR, ftp.dir, sizeof ftp.dir}, 98 {"ftp prefix", "/" ARCH_SUBDIR "/binary/sets", set_dir_bin, sizeof set_dir_bin}, 99 {"ftp src prefix", "/source/sets", set_dir_src, sizeof set_dir_src}, 100 {"ftp user", "ftp", ftp.user, sizeof ftp.user}, 101 {"ftp pass", "", ftp.pass, sizeof ftp.pass}, 102 {"ftp proxy", "", ftp.proxy, sizeof ftp.proxy}, 103 {"nfs host", "", nfs_host, sizeof nfs_host}, 104 {"nfs dir", "/bsd/release", nfs_dir, sizeof nfs_dir}, 105 {"cd dev", 0, cdrom_dev, sizeof cdrom_dev}, /* default filled in init */ 106 {"fd dev", "/dev/fd0a", fd_dev, sizeof fd_dev}, 107 {"local dev", "", localfs_dev, sizeof localfs_dev}, 108 {"local fs", "ffs", localfs_fs, sizeof localfs_fs}, 109 {"local dir", "release", localfs_dir, sizeof localfs_dir}, 110 {"targetroot mount", "/targetroot", targetroot_mnt, sizeof targetroot_mnt}, 111 {"dist postfix", "." SETS_TAR_SUFF, dist_postfix, sizeof dist_postfix}, 112 {"dist tgz postfix", ".tgz", dist_tgz_postfix, sizeof dist_tgz_postfix}, 113 {"pkg host", SYSINST_PKG_HOST, pkg.xfer_host[XFER_FTP], sizeof pkg.xfer_host[XFER_FTP]}, 114 {"pkg http host", SYSINST_PKG_HTTP_HOST, pkg.xfer_host[XFER_HTTP], sizeof pkg.xfer_host[XFER_HTTP]}, 115 {"pkg dir", SYSINST_PKG_DIR, pkg.dir, sizeof pkg.dir}, 116 {"pkg prefix", "/" PKG_ARCH_SUBDIR "/" PKG_SUBDIR "/All", pkg_dir, sizeof pkg_dir}, 117 {"pkg user", "ftp", pkg.user, sizeof pkg.user}, 118 {"pkg pass", "", pkg.pass, sizeof pkg.pass}, 119 {"pkg proxy", "", pkg.proxy, sizeof pkg.proxy}, 120 {"pkgsrc host", SYSINST_PKGSRC_HOST, pkgsrc.xfer_host[XFER_FTP], sizeof pkgsrc.xfer_host[XFER_FTP]}, 121 {"pkgsrc http host", SYSINST_PKGSRC_HTTP_HOST, pkgsrc.xfer_host[XFER_HTTP], sizeof pkgsrc.xfer_host[XFER_HTTP]}, 122 {"pkgsrc dir", "", pkgsrc.dir, sizeof pkgsrc.dir}, 123 {"pkgsrc prefix", "pub/pkgsrc/stable", pkgsrc_dir, sizeof pkgsrc_dir}, 124 {"pkgsrc user", "ftp", pkgsrc.user, sizeof pkgsrc.user}, 125 {"pkgsrc pass", "", pkgsrc.pass, sizeof pkgsrc.pass}, 126 {"pkgsrc proxy", "", pkgsrc.proxy, sizeof pkgsrc.proxy}, 127 128 {NULL, NULL, NULL, 0} 129 }; 130 131 static void 132 init(void) 133 { 134 const struct f_arg *arg; 135 136 sizemult = 1; 137 clean_xfer_dir = 0; 138 mnt2_mounted = 0; 139 fd_type = "msdos"; 140 141 pm_head = (struct pm_head_t) SLIST_HEAD_INITIALIZER(pm_head); 142 SLIST_INIT(&pm_head); 143 pm_new = malloc(sizeof (struct pm_devs)); 144 memset(pm_new, 0, sizeof *pm_new); 145 146 for (arg = fflagopts; arg->name != NULL; arg++) { 147 if (arg->var == cdrom_dev) 148 get_default_cdrom(arg->var, arg->size); 149 else 150 strlcpy(arg->var, arg->dflt, arg->size); 151 } 152 pkg.xfer = pkgsrc.xfer = XFER_HTTP; 153 154 clr_arg.bg=COLOR_BLUE; 155 clr_arg.fg=COLOR_WHITE; 156 } 157 158 static void 159 init_lang(void) 160 { 161 sizemult = 1; 162 err_outofmem = msg_string(MSG_out_of_memory); 163 multname = msg_string(MSG_secname); 164 } 165 166 int 167 main(int argc, char **argv) 168 { 169 int ch; 170 171 init(); 172 173 #ifdef DEBUG 174 log_flip(); 175 #endif 176 177 /* Check for TERM ... */ 178 if (!getenv("TERM")) { 179 (void)fprintf(stderr, 180 "sysinst: environment variable TERM not set.\n"); 181 exit(4); 182 } 183 184 /* argv processing */ 185 while ((ch = getopt(argc, argv, "Dr:f:C:" 186 #ifndef NO_PARTMAN 187 "p" 188 #endif 189 )) != -1) 190 switch(ch) { 191 case 'D': /* set to get past certain errors in testing */ 192 debug = 1; 193 break; 194 case 'r': 195 /* Release name - ignore for compatibility with older versions */ 196 break; 197 case 'f': 198 /* Definition file to read. */ 199 process_f_flag(optarg); 200 break; 201 case 'C': 202 /* Define colors */ 203 sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg); 204 break; 205 #ifndef NO_PARTMAN 206 case 'p': 207 /* Partition tool */ 208 partman_go = 1; 209 break; 210 #endif 211 case '?': 212 default: 213 usage(); 214 } 215 216 md_init(); 217 218 /* Initialize the partitioning subsystem */ 219 partitions_init(); 220 221 /* initialize message window */ 222 if (menu_init()) { 223 __menu_initerror(); 224 exit(4); 225 } 226 227 /* 228 * Put 'messages' in a window that has a one-character border 229 * on the real screen. 230 */ 231 mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1); 232 if (mainwin == NULL) { 233 (void)fprintf(stderr, 234 "sysinst: screen too small\n"); 235 exit(1); 236 } 237 if (has_colors()) { 238 start_color(); 239 do_coloring(clr_arg.fg,clr_arg.bg); 240 } else { 241 remove_color_options(); 242 } 243 msg_window(mainwin); 244 245 /* Watch for signals and clean up */ 246 (void)atexit(cleanup); 247 (void)signal(SIGINT, ttysighandler); 248 (void)signal(SIGQUIT, ttysighandler); 249 (void)signal(SIGHUP, miscsighandler); 250 251 /* redraw screen */ 252 touchwin(stdscr); 253 refresh(); 254 255 /* Ensure we have mountpoint for target filesystems */ 256 mkdir(targetroot_mnt, S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH); 257 258 select_language(); 259 get_kb_encoding(); 260 init_lang(); 261 262 /* Menu processing */ 263 if (partman_go) 264 partman(); 265 else 266 process_menu(MENU_netbsd, NULL); 267 268 #ifndef NO_PARTMAN 269 /* clean up internal storage */ 270 pm_destroy_all(); 271 #endif 272 273 partitions_cleanup(); 274 275 exit_cleanly = 1; 276 return 0; 277 } 278 279 static int 280 set_language(menudesc *m, void *arg) 281 { 282 char **fnames = arg; 283 284 msg_file(fnames[m->cursel]); 285 return 1; 286 } 287 288 static void 289 select_language(void) 290 { 291 DIR *dir; 292 struct dirent *dirent; 293 char **lang_msg, **fnames; 294 char prefix[PATH_MAX], fname[PATH_MAX]; 295 int max_lang = 16, num_lang = 0; 296 const char *cp; 297 menu_ent *opt = 0; 298 int lang_menu = -1; 299 int lang; 300 301 #ifdef CATALOG_DIR 302 strcpy(prefix, CATALOG_DIR "/"); 303 dir = opendir(CATALOG_DIR); 304 if (!dir) { 305 strcpy(prefix, "./"); 306 dir = opendir("."); 307 } 308 #else 309 dir = opendir("."); 310 strcpy(prefix, "./"); 311 #endif 312 if (!dir) 313 return; 314 315 lang_msg = malloc(max_lang * sizeof *lang_msg); 316 fnames = malloc(max_lang * sizeof *fnames); 317 if (!lang_msg || !fnames) 318 goto done; 319 320 lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language)); 321 fnames[0] = 0; 322 num_lang = 1; 323 324 while ((dirent = readdir(dir)) != 0) { 325 if (memcmp(dirent->d_name, "sysinstmsgs.", 12)) 326 continue; 327 strcpy(fname, prefix); 328 strcat(fname, dirent->d_name); 329 if (msg_file(fname)) 330 continue; 331 cp = msg_string(MSG_sysinst_message_language); 332 if (!strcmp(cp, lang_msg[0])) 333 continue; 334 if (num_lang == max_lang) { 335 char **new; 336 max_lang *= 2; 337 new = realloc(lang_msg, max_lang * sizeof *lang_msg); 338 if (!new) 339 break; 340 lang_msg = new; 341 new = realloc(fnames, max_lang * sizeof *fnames); 342 if (!new) 343 break; 344 fnames = new; 345 } 346 fnames[num_lang] = strdup(fname); 347 lang_msg[num_lang++] = strdup(cp); 348 } 349 msg_file(0); 350 closedir(dir); 351 dir = 0; 352 353 if (num_lang == 1) 354 goto done; 355 356 opt = calloc(num_lang, sizeof *opt); 357 if (!opt) 358 goto done; 359 360 for (lang = 0; lang < num_lang; lang++) { 361 opt[lang].opt_name = lang_msg[lang]; 362 opt[lang].opt_action = set_language; 363 } 364 365 lang_menu = new_menu(NULL, opt, num_lang, -1, 12, 0, 0, MC_NOEXITOPT, 366 NULL, NULL, NULL, NULL, NULL); 367 368 if (lang_menu != -1) { 369 msg_display(MSG_hello); 370 process_menu(lang_menu, fnames); 371 } 372 373 done: 374 if (dir) 375 closedir(dir); 376 if (lang_menu != -1) 377 free_menu(lang_menu); 378 free(opt); 379 while (num_lang) { 380 free(lang_msg[--num_lang]); 381 free(fnames[num_lang]); 382 } 383 free(lang_msg); 384 free(fnames); 385 386 /* set locale according to selected language */ 387 cp = msg_string(MSG_sysinst_message_locale); 388 if (cp) { 389 setlocale(LC_CTYPE, cp); 390 setenv("LC_CTYPE", cp, 1); 391 } 392 } 393 394 #ifndef md_may_remove_boot_medium 395 #define md_may_remove_boot_medium() (boot_media_still_needed()<=0) 396 #endif 397 398 /* toplevel menu handler ... */ 399 void 400 toplevel(void) 401 { 402 /* 403 * Undo any stateful side-effects of previous menu choices. 404 * XXX must be idempotent, since we get run each time the main 405 * menu is displayed. 406 */ 407 char *home = getenv("HOME"); 408 if (home != NULL) 409 if (chdir(home) != 0) 410 (void)chdir("/"); 411 unwind_mounts(); 412 413 /* Display banner message in (english, francais, deutsch..) */ 414 msg_display(MSG_hello); 415 msg_display_add(MSG_md_hello); 416 if (md_may_remove_boot_medium()) 417 msg_display_add(MSG_md_may_remove_boot_medium); 418 msg_display_add(MSG_thanks); 419 } 420 421 422 /* The usage ... */ 423 424 static void 425 usage(void) 426 { 427 428 (void)fprintf(stderr, "usage: sysinst [-D] [-f definition_file] " 429 "[-r release] [-C bg:fg]" 430 #ifndef NO_PARTMAN 431 " [-p]" 432 #endif 433 "\n" 434 "where:\n" 435 "\t-D\n\t\trun in debug mode\n" 436 "\t-f definition_file\n\t\toverride built-in defaults from file\n" 437 "\t-r release\n\t\toverride release name\n" 438 "\t-C bg:fg\n\t\tuse different color scheme\n" 439 #ifndef NO_PARTMAN 440 "\t-p\n\t\tonly run the partition editor, no installation\n" 441 #endif 442 ); 443 444 exit(1); 445 } 446 447 /* ARGSUSED */ 448 static void 449 miscsighandler(int signo) 450 { 451 452 /* 453 * we need to cleanup(), but it was already scheduled with atexit(), 454 * so it'll be invoked on exit(). 455 */ 456 exit(1); 457 } 458 459 static void 460 ttysighandler(int signo) 461 { 462 463 /* 464 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we 465 * just return. If we want to forward a TTY signal, we forward it 466 * to the specified process group. 467 * 468 * This functionality is used when setting up and displaying child 469 * output so that the child gets the signal and presumably dies, 470 * but sysinst continues. We use this rather than actually ignoring 471 * the signals, because that will be be passed on to a child 472 * through fork/exec, whereas special handlers get reset on exec.. 473 */ 474 if (ttysig_ignore) 475 return; 476 if (ttysig_forward) { 477 killpg(ttysig_forward, signo); 478 return; 479 } 480 481 /* 482 * we need to cleanup(), but it was already scheduled with atexit(), 483 * so it'll be invoked on exit(). 484 */ 485 exit(1); 486 } 487 488 static void 489 cleanup(void) 490 { 491 time_t tloc; 492 493 (void)time(&tloc); 494 495 #if 0 496 restore_etc(); 497 #endif 498 /* Ensure we aren't inside the target tree */ 499 chdir(getenv("HOME")); 500 unwind_mounts(); 501 umount_mnt2(); 502 503 endwin(); 504 505 if (logfp) { 506 fprintf(logfp, "Log ended at: %s\n", safectime(&tloc)); 507 fflush(logfp); 508 fclose(logfp); 509 logfp = NULL; 510 } 511 if (script) { 512 fprintf(script, "# Script ended at: %s\n", safectime(&tloc)); 513 fflush(script); 514 fclose(script); 515 script = NULL; 516 } 517 518 if (!exit_cleanly) 519 fprintf(stderr, "\n\nsysinst terminated.\n"); 520 } 521 522 523 /* process function ... */ 524 525 void 526 process_f_flag(char *f_name) 527 { 528 char buffer[STRSIZE]; 529 int len; 530 const struct f_arg *arg; 531 FILE *fp; 532 char *cp, *cp1, *err; 533 534 /* open the file */ 535 fp = fopen(f_name, "r"); 536 if (fp == NULL) { 537 const char *args[] = { f_name }; 538 err = str_arg_subst(msg_string(MSG_config_open_error), 539 __arraycount(args), args); 540 fprintf(stderr, "%s\n", err); 541 free(err); 542 exit(1); 543 } 544 545 while (fgets(buffer, sizeof buffer, fp) != NULL) { 546 cp = buffer + strspn(buffer, " \t"); 547 if (strchr("#\r\n", *cp) != NULL) 548 continue; 549 for (arg = fflagopts; arg->name != NULL; arg++) { 550 len = strlen(arg->name); 551 if (memcmp(cp, arg->name, len) != 0) 552 continue; 553 if (arg->var == NULL || arg->size == 0) 554 continue; 555 cp1 = cp + len; 556 cp1 += strspn(cp1, " \t"); 557 if (*cp1++ != '=') 558 continue; 559 cp1 += strspn(cp1, " \t"); 560 len = strcspn(cp1, " \n\r\t"); 561 cp1[len] = 0; 562 strlcpy(arg->var, cp1, arg->size); 563 break; 564 } 565 } 566 567 fclose(fp); 568 } 569