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