Home | History | Annotate | Line # | Download | only in sysinst
main.c revision 1.20
      1 /*	$NetBSD: main.c,v 1.20 2020/01/16 16:47:19 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, rel, sizeof rel},
     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 other than compiled in release. */
    196 			strncpy(rel, optarg, sizeof rel);
    197 			break;
    198 		case 'f':
    199 			/* Definition file to read. */
    200 			process_f_flag(optarg);
    201 			break;
    202 		case 'C':
    203 			/* Define colors */
    204 			sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg);
    205 			break;
    206 #ifndef NO_PARTMAN
    207 		case 'p':
    208 			/* Partition tool */
    209 			partman_go = 1;
    210 			break;
    211 #endif
    212 		case '?':
    213 		default:
    214 			usage();
    215 		}
    216 
    217 	md_init();
    218 
    219 	/* Initialize the partitioning subsystem */
    220 	partitions_init();
    221 
    222 	/* initialize message window */
    223 	if (menu_init()) {
    224 		__menu_initerror();
    225 		exit(4);
    226 	}
    227 
    228 	/*
    229 	 * Put 'messages' in a window that has a one-character border
    230 	 * on the real screen.
    231 	 */
    232 	mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1);
    233 	if (mainwin == NULL) {
    234 		(void)fprintf(stderr,
    235 			 "sysinst: screen too small\n");
    236 		exit(1);
    237 	}
    238 	if (has_colors()) {
    239 		start_color();
    240 		do_coloring(clr_arg.fg,clr_arg.bg);
    241 	} else {
    242 		remove_color_options();
    243 	}
    244 	msg_window(mainwin);
    245 
    246 	/* Watch for signals and clean up */
    247 	(void)atexit(cleanup);
    248 	(void)signal(SIGINT, ttysighandler);
    249 	(void)signal(SIGQUIT, ttysighandler);
    250 	(void)signal(SIGHUP, miscsighandler);
    251 
    252 	/* redraw screen */
    253 	touchwin(stdscr);
    254 	refresh();
    255 
    256 	/* Ensure we have mountpoint for target filesystems */
    257 	mkdir(targetroot_mnt, S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
    258 
    259 	select_language();
    260 	get_kb_encoding();
    261 	init_lang();
    262 
    263 	/* Menu processing */
    264 	if (partman_go)
    265 		partman();
    266 	else
    267 		process_menu(MENU_netbsd, NULL);
    268 
    269 #ifndef NO_PARTMAN
    270 	/* clean up internal storage */
    271 	pm_destroy_all();
    272 #endif
    273 
    274 	partitions_cleanup();
    275 
    276 	exit_cleanly = 1;
    277 	return 0;
    278 }
    279 
    280 static int
    281 set_language(menudesc *m, void *arg)
    282 {
    283 	char **fnames = arg;
    284 
    285 	msg_file(fnames[m->cursel]);
    286 	return 1;
    287 }
    288 
    289 static void
    290 select_language(void)
    291 {
    292 	DIR *dir;
    293 	struct dirent *dirent;
    294 	char **lang_msg, **fnames;
    295 	char prefix[PATH_MAX], fname[PATH_MAX];
    296 	int max_lang = 16, num_lang = 0;
    297 	const char *cp;
    298 	menu_ent *opt = 0;
    299 	int lang_menu = -1;
    300 	int lang;
    301 
    302 #ifdef CATALOG_DIR
    303 	strcpy(prefix, CATALOG_DIR "/");
    304 	dir = opendir(CATALOG_DIR);
    305 	if (!dir) {
    306 		strcpy(prefix, "./");
    307 		dir = opendir(".");
    308 	}
    309 #else
    310 	dir = opendir(".");
    311 	strcpy(prefix, "./");
    312 #endif
    313 	if (!dir)
    314 		return;
    315 
    316 	lang_msg = malloc(max_lang * sizeof *lang_msg);
    317 	fnames = malloc(max_lang * sizeof *fnames);
    318 	if (!lang_msg || !fnames)
    319 		goto done;
    320 
    321 	lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language));
    322 	fnames[0] = 0;
    323 	num_lang = 1;
    324 
    325 	while ((dirent = readdir(dir)) != 0) {
    326 		if (memcmp(dirent->d_name, "sysinstmsgs.", 12))
    327 			continue;
    328 		strcpy(fname, prefix);
    329 		strcat(fname, dirent->d_name);
    330 		if (msg_file(fname))
    331 			continue;
    332 		cp = msg_string(MSG_sysinst_message_language);
    333 		if (!strcmp(cp, lang_msg[0]))
    334 			continue;
    335 		if (num_lang == max_lang) {
    336 			char **new;
    337 			max_lang *= 2;
    338 			new = realloc(lang_msg, max_lang * sizeof *lang_msg);
    339 			if (!new)
    340 				break;
    341 			lang_msg = new;
    342 			new = realloc(fnames, max_lang * sizeof *fnames);
    343 			if (!new)
    344 				break;
    345 			fnames = new;
    346 		}
    347 		fnames[num_lang] = strdup(fname);
    348 		lang_msg[num_lang++] = strdup(cp);
    349 	}
    350 	msg_file(0);
    351 	closedir(dir);
    352 	dir = 0;
    353 
    354 	if (num_lang == 1)
    355 		goto done;
    356 
    357 	opt = calloc(num_lang, sizeof *opt);
    358 	if (!opt)
    359 		goto done;
    360 
    361 	for (lang = 0; lang < num_lang; lang++) {
    362 		opt[lang].opt_name = lang_msg[lang];
    363 		opt[lang].opt_action = set_language;
    364 	}
    365 
    366 	lang_menu = new_menu(NULL, opt, num_lang, -1, 12, 0, 0, MC_NOEXITOPT,
    367 		NULL, NULL, NULL, NULL, NULL);
    368 
    369 	if (lang_menu != -1) {
    370 		msg_display(MSG_hello);
    371 		process_menu(lang_menu, fnames);
    372 	}
    373 
    374     done:
    375 	if (dir)
    376 		closedir(dir);
    377 	if (lang_menu != -1)
    378 		free_menu(lang_menu);
    379 	free(opt);
    380 	while (num_lang) {
    381 		free(lang_msg[--num_lang]);
    382 		free(fnames[num_lang]);
    383 	}
    384 	free(lang_msg);
    385 	free(fnames);
    386 
    387 	/* set locale according to selected language */
    388 	cp = msg_string(MSG_sysinst_message_locale);
    389 	if (cp) {
    390 		setlocale(LC_CTYPE, cp);
    391 		setenv("LC_CTYPE", cp, 1);
    392 	}
    393 }
    394 
    395 #ifndef md_may_remove_boot_medium
    396 #define md_may_remove_boot_medium()	(boot_media_still_needed()<=0)
    397 #endif
    398 
    399 /* toplevel menu handler ... */
    400 void
    401 toplevel(void)
    402 {
    403 	/*
    404 	 * Undo any stateful side-effects of previous menu choices.
    405 	 * XXX must be idempotent, since we get run each time the main
    406 	 *     menu is displayed.
    407 	 */
    408 	char *home = getenv("HOME");
    409 	if (home != NULL)
    410 		if (chdir(home) != 0)
    411 			(void)chdir("/");
    412 	unwind_mounts();
    413 
    414 	/* Display banner message in (english, francais, deutsch..) */
    415 	msg_display(MSG_hello);
    416 	msg_display_add(MSG_md_hello);
    417 	if (md_may_remove_boot_medium())
    418 		msg_display_add(MSG_md_may_remove_boot_medium);
    419 	msg_display_add(MSG_thanks);
    420 }
    421 
    422 
    423 /* The usage ... */
    424 
    425 static void
    426 usage(void)
    427 {
    428 
    429 	(void)fprintf(stderr, "usage: sysinst [-D] [-f definition_file] "
    430 	    "[-r release] [-C bg:fg]"
    431 #ifndef NO_PARTMAN
    432 	    " [-p]"
    433 #endif
    434 	    "\n"
    435 	    "where:\n"
    436 	    "\t-D\n\t\trun in debug mode\n"
    437 	    "\t-f definition_file\n\t\toverride built-in defaults from file\n"
    438 	    "\t-r release\n\t\toverride release name\n"
    439 	    "\t-C bg:fg\n\t\tuse different color scheme\n"
    440 #ifndef NO_PARTMAN
    441 	    "\t-p\n\t\tonly run the partition editor, no installation\n"
    442 #endif
    443 	    );
    444 
    445 	exit(1);
    446 }
    447 
    448 /* ARGSUSED */
    449 static void
    450 miscsighandler(int signo)
    451 {
    452 
    453 	/*
    454 	 * we need to cleanup(), but it was already scheduled with atexit(),
    455 	 * so it'll be invoked on exit().
    456 	 */
    457 	exit(1);
    458 }
    459 
    460 static void
    461 ttysighandler(int signo)
    462 {
    463 
    464 	/*
    465 	 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we
    466 	 * just return.  If we want to forward a TTY signal, we forward it
    467 	 * to the specified process group.
    468 	 *
    469 	 * This functionality is used when setting up and displaying child
    470 	 * output so that the child gets the signal and presumably dies,
    471 	 * but sysinst continues.  We use this rather than actually ignoring
    472 	 * the signals, because that will be be passed on to a child
    473 	 * through fork/exec, whereas special handlers get reset on exec..
    474 	 */
    475 	if (ttysig_ignore)
    476 		return;
    477 	if (ttysig_forward) {
    478 		killpg(ttysig_forward, signo);
    479 		return;
    480 	}
    481 
    482 	/*
    483 	 * we need to cleanup(), but it was already scheduled with atexit(),
    484 	 * so it'll be invoked on exit().
    485 	 */
    486 	exit(1);
    487 }
    488 
    489 static void
    490 cleanup(void)
    491 {
    492 	time_t tloc;
    493 
    494 	(void)time(&tloc);
    495 
    496 #if 0
    497 	restore_etc();
    498 #endif
    499 	/* Ensure we aren't inside the target tree */
    500 	chdir(getenv("HOME"));
    501 	unwind_mounts();
    502 	umount_mnt2();
    503 
    504 	endwin();
    505 
    506 	if (logfp) {
    507 		fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
    508 		fflush(logfp);
    509 		fclose(logfp);
    510 		logfp = NULL;
    511 	}
    512 	if (script) {
    513 		fprintf(script, "# Script ended at: %s\n", safectime(&tloc));
    514 		fflush(script);
    515 		fclose(script);
    516 		script = NULL;
    517 	}
    518 
    519 	if (!exit_cleanly)
    520 		fprintf(stderr, "\n\nsysinst terminated.\n");
    521 }
    522 
    523 
    524 /* process function ... */
    525 
    526 void
    527 process_f_flag(char *f_name)
    528 {
    529 	char buffer[STRSIZE];
    530 	int len;
    531 	const struct f_arg *arg;
    532 	FILE *fp;
    533 	char *cp, *cp1, *err;
    534 
    535 	/* open the file */
    536 	fp = fopen(f_name, "r");
    537 	if (fp == NULL) {
    538 		const char *args[] = { f_name };
    539 		err = str_arg_subst(msg_string(MSG_config_open_error),
    540 		    __arraycount(args), args);
    541 		fprintf(stderr, "%s\n", err);
    542 		free(err);
    543 		exit(1);
    544 	}
    545 
    546 	while (fgets(buffer, sizeof buffer, fp) != NULL) {
    547 		cp = buffer + strspn(buffer, " \t");
    548 		if (strchr("#\r\n", *cp) != NULL)
    549 			continue;
    550 		for (arg = fflagopts; arg->name != NULL; arg++) {
    551 			len = strlen(arg->name);
    552 			if (memcmp(cp, arg->name, len) != 0)
    553 				continue;
    554 			cp1 = cp + len;
    555 			cp1 += strspn(cp1, " \t");
    556 			if (*cp1++ != '=')
    557 				continue;
    558 			cp1 += strspn(cp1, " \t");
    559 			len = strcspn(cp1, " \n\r\t");
    560 			cp1[len] = 0;
    561 			strlcpy(arg->var, cp1, arg->size);
    562 			break;
    563 		}
    564 	}
    565 
    566 	fclose(fp);
    567 }
    568