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