2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
11 * My contributions are in the public domain.
13 * Parts of this file are also blatently stolen from Poul-Henning Kamp's
14 * previous version of sysinstall, and as such fall under his "BEERWARE license"
15 * so buy him a beer if you like it! Buy him a beer for me, too!
16 * Heck, get him completely drunk and send me pictures! :-)
19 #include "sysinstall.h"
22 #include <sys/reboot.h>
23 #include <sys/consio.h>
24 #include <sys/fcntl.h>
25 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
31 /* Where we stick our temporary expanded doc file */
32 #define DOC_TMP_DIR "/tmp/.doc"
33 #define DOC_TMP_FILE "/tmp/.doc/doc.tmp"
38 * Handle interrupt signals - this probably won't work in all cases
39 * due to our having bogotified the internal state of dialog or curses,
40 * but we'll give it a try.
43 intr_continue(dialogMenuItem *self)
45 return DITEM_LEAVE_MENU;
49 intr_reboot(dialogMenuItem *self)
57 intr_restart(dialogMenuItem *self)
63 fdmax = getdtablesize();
64 for (fd = 3; fd < fdmax; fd++)
66 ret = execl(StartName, StartName, "-restart", (char *)NULL);
67 msgDebug("execl failed (%s)\n", strerror(errno));
72 static dialogMenuItem intrmenu[] = {
73 { "Abort", "Abort the installation", NULL, intr_reboot },
74 { "Restart", "Restart the installation program", NULL, intr_restart },
75 { "Continue", "Continue the installation", NULL, intr_continue },
82 WINDOW *save = savescr();
87 ioctl(0, VT_ACTIVATE, 1); /* Switch back */
90 (void)dialog_menu("Installation interrupt",
91 "Do you want to abort the installation?",
92 -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
97 * Harvest children if we are init.
100 reap_children(int sig)
104 while (waitpid(-1, NULL, WNOHANG) > 0)
109 /* Expand a file into a convenient location, nuking it each time */
113 char *gunzip = RunningAsInit ? "/stand/gunzip" : "/usr/bin/gunzip";
115 if (!directory_exists(DOC_TMP_DIR)) {
117 if (chown(DOC_TMP_DIR, 0, 0) < 0)
119 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
123 unlink(DOC_TMP_FILE);
124 if (!file_readable(fname) || vsystem("%s < %s > %s", gunzip, fname, DOC_TMP_FILE))
129 /* Initialize system defaults */
131 systemInitialize(int argc, char **argv)
137 signal(SIGINT, SIG_IGN);
140 i = sizeof(boothowto);
141 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, NULL) &&
142 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
143 variable_set2(VAR_DEBUG, "YES", 0);
145 /* Are we running as init? */
152 fd = open("/dev/ttyv0", O_RDWR);
154 fd = open("/dev/console", O_RDWR); /* fallback */
155 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
159 * To make _sure_ we're on a VTY and don't have /dev/console switched
160 * away to a serial port or something, attempt to set the cursor appearance.
165 type = 0; /* normal */
166 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
167 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
169 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
172 close(fd); close(fd2);
173 open("/dev/console", O_RDWR);
181 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
182 ioctl(0, TIOCSCTTY, (char *)NULL);
184 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
189 sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
192 signal(SIGCHLD, reap_children);
198 /* Initalize various things for a multi-user environment */
199 if (!gethostname(hname, sizeof hname))
200 variable_set2(VAR_HOSTNAME, hname, 0);
203 if (set_termcap() == -1) {
204 printf("Can't find terminal entry\n");
208 /* XXX - libdialog has particularly bad return value checking */
211 /* If we haven't crashed I guess dialog is running ! */
214 /* Make sure HOME is set for those utilities that need it */
216 setenv("HOME", "/", 1);
217 signal(SIGINT, handle_intr);
219 * Make sure we can be interrupted even if we were re-executed
222 sigemptyset(&signalset);
223 sigaddset(&signalset, SIGINT);
224 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
226 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
229 /* Close down and prepare to exit */
231 systemShutdown(int status)
233 /* If some media is open, close it down */
237 /* write out any changes to rc.conf .. */
240 /* Shut down the dialog library */
243 DialogActive = FALSE;
246 /* Shut down curses */
249 /* If we have a temporary doc dir lying around, nuke it */
250 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
254 /* Put the console back */
255 ioctl(0, VT_ACTIVATE, 2);
266 /* Run some general command */
268 systemExecute(char *command)
272 WINDOW *w = savescr();
277 DialogActive = FALSE;
278 if (tcgetattr(0, &foo) != -1) {
279 foo.c_cc[VERASE] = '\010';
280 tcsetattr(0, TCSANOW, &foo);
283 status = system(command);
286 msgDebug("systemExecute: Faked execution of `%s'\n", command);
293 /* suspend/resume libdialog/curses screen */
297 systemSuspendDialog(void)
304 DialogActive = FALSE;
308 systemResumeDialog(void)
315 /* Display a help file in a filebox */
317 systemDisplayHelp(char *file)
320 char buf[FILENAME_MAX];
322 WINDOW *w = savescr();
324 fname = systemHelpFile(file, buf);
326 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
329 dialog_mesgbox("Sorry!", buf, -1, -1);
335 dialog_textbox(file, fname, LINES, COLS);
342 systemHelpFile(char *file, char *buf)
348 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
349 if (file_readable(buf))
351 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
352 if (file_readable(buf))
354 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.hlp", file);
355 if (file_readable(buf))
357 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.TXT", file);
358 if (file_readable(buf))
364 systemChangeTerminal(char *color, const u_char c_term[],
365 char *mono, const u_char m_term[])
368 int setupterm(char *color, int, int *);
371 setenv("TERM", color, 1);
372 setenv("TERMCAP", c_term, 1);
378 setenv("TERM", mono, 1);
379 setenv("TERMCAP", m_term, 1);
391 vsystem(char *fmt, ...)
397 sig_t intsave, quitsave;
401 cmd = (char *)alloca(FILENAME_MAX);
404 vsnprintf(cmd, FILENAME_MAX, fmt, args);
407 omask = sigblock(sigmask(SIGCHLD));
409 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
413 msgDebug("Executing command `%s'\n", cmd);
416 (void)sigsetmask(omask);
419 else if (!pid) { /* Junior */
420 (void)sigsetmask(omask);
427 close(1); open("/dev/null", O_WRONLY);
431 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
433 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
437 intsave = signal(SIGINT, SIG_IGN);
438 quitsave = signal(SIGQUIT, SIG_IGN);
439 pid = waitpid(pid, &pstat, 0);
440 (void)sigsetmask(omask);
441 (void)signal(SIGINT, intsave);
442 (void)signal(SIGQUIT, quitsave);
443 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
445 msgDebug("Command `%s' returns status of %d\n", cmd, i);
451 systemCreateHoloshell(void)
455 if ((FixItMode || OnVTY) && RunningAsInit) {
460 if (kill(ehs_pid, 0) == 0) {
462 if (msgNoYes("There seems to be an emergency holographic shell\n"
463 "already running on VTY 4.\n\n"
464 "Kill it and start a new one?"))
467 /* try cleaning up as much as possible */
468 (void) kill(ehs_pid, SIGHUP);
470 (void) kill(ehs_pid, SIGKILL);
473 /* avoid too many zombies */
474 (void) waitpid(ehs_pid, &pstat, WNOHANG);
477 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
478 systemSuspendDialog(); /* must be before the fork() */
479 if ((ehs_pid = fork()) == 0) {
482 extern int login_tty(int);
484 ioctl(0, TIOCNOTTY, NULL);
485 for (i = getdtablesize(); i >= 0; --i)
487 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
488 fd = open("/dev/console", O_RDWR);
490 fd = open("/dev/ttyv3", O_RDWR);
491 ioctl(0, TIOCSCTTY, &fd);
495 if (login_tty(fd) == -1)
496 msgDebug("Doctor: I can't set the controlling terminal.\n");
497 signal(SIGTTOU, SIG_IGN);
498 if (tcgetattr(fd, &foo) != -1) {
499 foo.c_cc[VERASE] = '\010';
500 if (tcsetattr(fd, TCSANOW, &foo) == -1)
501 msgDebug("Doctor: I'm unable to set the erase character.\n");
504 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
505 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
506 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
509 execlp("sh", "-sh", 0);
510 msgDebug("Was unable to execute sh for Holographic shell!\n");
514 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
515 WINDOW *w = savescr();
517 msgNotify("Starting an emergency holographic shell on VTY4");
522 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
527 systemResumeDialog();