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