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)
59 execl(StartName, StartName, NULL);
64 static dialogMenuItem intrmenu[] = {
65 { "Abort", "Abort the installation", NULL, intr_reboot },
66 { "Restart", "Restart the installation program", NULL, intr_restart },
67 { "Continue", "Continue the installation", NULL, intr_continue },
74 WINDOW *save = savescr();
79 ioctl(0, VT_ACTIVATE, 1); /* Switch back */
82 (void)dialog_menu("Installation interrupt",
83 "Do you want to abort the installation?",
84 -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
88 /* Expand a file into a convenient location, nuking it each time */
92 char *gunzip = RunningAsInit ? "/stand/gunzip" : "/usr/bin/gunzip";
94 if (!directory_exists(DOC_TMP_DIR)) {
96 if (chown(DOC_TMP_DIR, 0, 0) < 0)
98 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
102 unlink(DOC_TMP_FILE);
103 if (!file_readable(fname) || vsystem("%s < %s > %s", gunzip, fname, DOC_TMP_FILE))
108 /* Initialize system defaults */
110 systemInitialize(int argc, char **argv)
115 signal(SIGINT, SIG_IGN);
118 i = sizeof(boothowto);
119 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, NULL) &&
120 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
121 variable_set2(VAR_DEBUG, "YES", 0);
123 /* Are we running as init? */
130 fd = open("/dev/ttyv0", O_RDWR);
132 fd = open("/dev/console", O_RDWR); /* fallback */
133 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
137 * To make _sure_ we're on a VTY and don't have /dev/console switched
138 * away to a serial port or something, attempt to set the cursor appearance.
140 type = 0; /* normal */
144 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
145 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
147 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
150 close(fd); close(fd2);
151 open("/dev/console", O_RDWR);
159 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
160 ioctl(0, TIOCSCTTY, (char *)NULL);
162 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
167 sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
173 /* Initalize various things for a multi-user environment */
174 if (!gethostname(hname, sizeof hname))
175 variable_set2(VAR_HOSTNAME, hname, 0);
178 if (set_termcap() == -1) {
179 printf("Can't find terminal entry\n");
183 /* XXX - libdialog has particularly bad return value checking */
186 /* If we haven't crashed I guess dialog is running ! */
189 /* Make sure HOME is set for those utilities that need it */
191 setenv("HOME", "/", 1);
192 signal(SIGINT, handle_intr);
194 * Make sure we can be interrupted even if we were re-executed
197 sigemptyset(&signalset);
198 sigaddset(&signalset, SIGINT);
199 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
201 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
204 /* Close down and prepare to exit */
206 systemShutdown(int status)
208 /* If some media is open, close it down */
212 /* write out any changes to rc.conf .. */
215 /* Shut down the dialog library */
218 DialogActive = FALSE;
221 /* Shut down curses */
224 /* If we have a temporary doc dir lying around, nuke it */
225 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
229 /* Put the console back */
230 ioctl(0, VT_ACTIVATE, 2);
241 /* Run some general command */
243 systemExecute(char *command)
247 WINDOW *w = savescr();
252 DialogActive = FALSE;
253 if (tcgetattr(0, &foo) != -1) {
254 foo.c_cc[VERASE] = '\010';
255 tcsetattr(0, TCSANOW, &foo);
258 status = system(command);
261 msgDebug("systemExecute: Faked execution of `%s'\n", command);
268 /* suspend/resume libdialog/curses screen */
272 systemSuspendDialog(void)
279 DialogActive = FALSE;
283 systemResumeDialog(void)
290 /* Display a help file in a filebox */
292 systemDisplayHelp(char *file)
295 char buf[FILENAME_MAX];
297 WINDOW *w = savescr();
299 fname = systemHelpFile(file, buf);
301 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
304 dialog_mesgbox("Sorry!", buf, -1, -1);
310 dialog_textbox(file, fname, LINES, COLS);
317 systemHelpFile(char *file, char *buf)
323 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
324 if (file_readable(buf))
326 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
327 if (file_readable(buf))
329 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.hlp", file);
330 if (file_readable(buf))
332 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.TXT", file);
333 if (file_readable(buf))
339 systemChangeTerminal(char *color, const u_char c_term[],
340 char *mono, const u_char m_term[])
343 int setupterm(char *color, int, int *);
346 setenv("TERM", color, 1);
347 setenv("TERMCAP", c_term, 1);
353 setenv("TERM", mono, 1);
354 setenv("TERMCAP", m_term, 1);
366 vsystem(char *fmt, ...)
372 sig_t intsave, quitsave;
376 cmd = (char *)alloca(FILENAME_MAX);
379 vsnprintf(cmd, FILENAME_MAX, fmt, args);
382 omask = sigblock(sigmask(SIGCHLD));
384 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
388 msgDebug("Executing command `%s'\n", cmd);
391 (void)sigsetmask(omask);
394 else if (!pid) { /* Junior */
395 (void)sigsetmask(omask);
402 close(1); open("/dev/null", O_WRONLY);
406 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
408 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
412 intsave = signal(SIGINT, SIG_IGN);
413 quitsave = signal(SIGQUIT, SIG_IGN);
414 pid = waitpid(pid, &pstat, 0);
415 (void)sigsetmask(omask);
416 (void)signal(SIGINT, intsave);
417 (void)signal(SIGQUIT, quitsave);
418 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
420 msgDebug("Command `%s' returns status of %d\n", cmd, i);
426 systemCreateHoloshell(void)
430 if ((FixItMode || OnVTY) && RunningAsInit) {
435 if (kill(ehs_pid, 0) == 0) {
437 if (msgNoYes("There seems to be an emergency holographic shell\n"
438 "already running on VTY 4.\n\n"
439 "Kill it and start a new one?"))
442 /* try cleaning up as much as possible */
443 (void) kill(ehs_pid, SIGHUP);
445 (void) kill(ehs_pid, SIGKILL);
448 /* avoid too many zombies */
449 (void) waitpid(ehs_pid, &pstat, WNOHANG);
452 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
453 systemSuspendDialog(); /* must be before the fork() */
454 if ((ehs_pid = fork()) == 0) {
457 extern int login_tty(int);
459 ioctl(0, TIOCNOTTY, NULL);
460 for (i = getdtablesize(); i >= 0; --i)
462 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
463 fd = open("/dev/console", O_RDWR);
465 fd = open("/dev/ttyv3", O_RDWR);
466 ioctl(0, TIOCSCTTY, &fd);
470 if (login_tty(fd) == -1)
471 msgDebug("Doctor: I can't set the controlling terminal.\n");
472 signal(SIGTTOU, SIG_IGN);
473 if (tcgetattr(fd, &foo) != -1) {
474 foo.c_cc[VERASE] = '\010';
475 if (tcsetattr(fd, TCSANOW, &foo) == -1)
476 msgDebug("Doctor: I'm unable to set the erase character.\n");
479 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
480 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
481 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
484 execlp("sh", "-sh", 0);
485 msgDebug("Was unable to execute sh for Holographic shell!\n");
489 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
490 WINDOW *w = savescr();
492 msgNotify("Starting an emergency holographic shell on VTY4");
497 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
502 systemResumeDialog();