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