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 blatantly 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/param.h>
23 #include <sys/reboot.h>
24 #include <sys/consio.h>
25 #include <sys/fcntl.h>
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
29 #include <sys/sysctl.h>
30 #include <ufs/ufs/ufsmount.h>
33 /* Where we stick our temporary expanded doc file */
34 #define DOC_TMP_DIR "/tmp/.doc"
35 #define DOC_TMP_FILE "/tmp/.doc/doc.tmp"
40 * Handle interrupt signals - this probably won't work in all cases
41 * due to our having bogotified the internal state of dialog or curses,
42 * but we'll give it a try.
45 intr_continue(dialogMenuItem *self)
47 return DITEM_LEAVE_MENU;
51 intr_reboot(dialogMenuItem *self)
59 intr_restart(dialogMenuItem *self)
65 fdmax = getdtablesize();
66 for (fd = 3; fd < fdmax; fd++)
68 ret = execl(StartName, StartName, "-restart", (char *)NULL);
69 msgDebug("execl failed (%s)\n", strerror(errno));
74 static dialogMenuItem intrmenu[] = {
75 { "Abort", "Abort the installation", NULL, intr_reboot },
76 { "Restart", "Restart the installation program", NULL, intr_restart },
77 { "Continue", "Continue the installation", NULL, intr_continue },
84 WINDOW *save = savescr();
89 ioctl(0, VT_ACTIVATE, 1); /* Switch back */
92 (void)dialog_menu("Installation interrupt",
93 "Do you want to abort the installation?",
94 -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
100 * Harvest children if we are init.
103 reap_children(int sig)
107 while (waitpid(-1, NULL, WNOHANG) > 0)
113 /* Expand a file into a convenient location, nuking it each time */
117 char *unzipper = RunningAsInit ? "/stand/" UNZIPPER : "/usr/bin/" UNZIPPER;
119 if (!directory_exists(DOC_TMP_DIR)) {
121 if (chown(DOC_TMP_DIR, 0, 0) < 0)
123 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
127 unlink(DOC_TMP_FILE);
128 if (!file_readable(fname) || vsystem("%s < %s > %s", unzipper, fname,
134 /* Initialize system defaults */
136 systemInitialize(int argc, char **argv)
142 signal(SIGINT, SIG_IGN);
145 i = sizeof(boothowto);
146 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, 0) &&
147 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
148 variable_set2(VAR_DEBUG, "YES", 0);
150 /* Are we running as init? */
152 struct ufs_args ufs_args;
158 fd = open("/dev/ttyv0", O_RDWR);
160 fd = open("/dev/console", O_RDWR); /* fallback */
161 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
165 * To make _sure_ we're on a VTY and don't have /dev/console switched
166 * away to a serial port or something, attempt to set the cursor appearance.
171 type = 0; /* normal */
172 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
173 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
175 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
178 close(fd); close(fd2);
179 open("/dev/console", O_RDWR);
187 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
188 ioctl(0, TIOCSCTTY, (char *)NULL);
190 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
195 sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
198 signal(SIGCHLD, reap_children);
200 memset(&ufs_args, 0, sizeof(ufs_args));
201 mount("ufs", "/", MNT_UPDATE, &ufs_args);
206 /* Initalize various things for a multi-user environment */
207 if (!gethostname(hname, sizeof hname))
208 variable_set2(VAR_HOSTNAME, hname, 0);
211 if (set_termcap() == -1) {
212 printf("Can't find terminal entry\n");
216 /* XXX - libdialog has particularly bad return value checking */
219 /* If we haven't crashed I guess dialog is running ! */
222 /* Make sure HOME is set for those utilities that need it */
224 setenv("HOME", "/", 1);
225 signal(SIGINT, handle_intr);
227 * Make sure we can be interrupted even if we were re-executed
230 sigemptyset(&signalset);
231 sigaddset(&signalset, SIGINT);
232 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
234 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
237 /* Close down and prepare to exit */
239 systemShutdown(int status)
241 /* If some media is open, close it down */
245 /* write out any changes to rc.conf .. */
248 /* Shut down the dialog library */
251 DialogActive = FALSE;
254 /* Shut down curses */
257 /* If we have a temporary doc dir lying around, nuke it */
258 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
262 /* Put the console back */
263 ioctl(0, VT_ACTIVATE, 2);
264 #if defined(__alpha__) || defined(__sparc64__)
274 /* Run some general command */
276 systemExecute(char *command)
280 WINDOW *w = savescr();
285 DialogActive = FALSE;
286 if (tcgetattr(0, &foo) != -1) {
287 foo.c_cc[VERASE] = '\010';
288 tcsetattr(0, TCSANOW, &foo);
291 status = system(command);
294 msgDebug("systemExecute: Faked execution of `%s'\n", command);
301 /* suspend/resume libdialog/curses screen */
305 systemSuspendDialog(void)
312 DialogActive = FALSE;
316 systemResumeDialog(void)
323 /* Display a help file in a filebox */
325 systemDisplayHelp(char *file)
328 char buf[FILENAME_MAX];
330 WINDOW *w = savescr();
332 fname = systemHelpFile(file, buf);
334 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
337 dialog_mesgbox("Sorry!", buf, -1, -1);
343 dialog_textbox(file, fname, LINES, COLS);
350 systemHelpFile(char *file, char *buf)
356 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
357 if (file_readable(buf))
359 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp", file);
360 if (file_readable(buf))
362 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
363 if (file_readable(buf))
365 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT", file);
366 if (file_readable(buf))
368 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.hlp", ProgName,
370 if (file_readable(buf))
372 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.TXT", ProgName,
374 if (file_readable(buf))
380 systemChangeTerminal(char *color, const u_char c_term[],
381 char *mono, const u_char m_term[])
384 int setupterm(char *color, int, int *);
387 setenv("TERM", color, 1);
388 setenv("TERMCAP", c_term, 1);
394 setenv("TERM", mono, 1);
395 setenv("TERMCAP", m_term, 1);
407 vsystem(char *fmt, ...)
413 sig_t intsave, quitsave;
418 cmd = (char *)alloca(FILENAME_MAX);
421 vsnprintf(cmd, FILENAME_MAX, fmt, args);
424 omask = sigblock(sigmask(SIGCHLD));
426 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
430 msgDebug("Executing command `%s'\n", cmd);
433 (void)sigsetmask(omask);
436 else if (!pid) { /* Junior */
437 (void)sigsetmask(omask);
444 close(1); open("/dev/null", O_WRONLY);
447 if (stat("/stand/sh", &sb) == 0)
448 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
450 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
454 intsave = signal(SIGINT, SIG_IGN);
455 quitsave = signal(SIGQUIT, SIG_IGN);
456 pid = waitpid(pid, &pstat, 0);
457 (void)sigsetmask(omask);
458 (void)signal(SIGINT, intsave);
459 (void)signal(SIGQUIT, quitsave);
460 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
462 msgDebug("Command `%s' returns status of %d\n", cmd, i);
468 systemCreateHoloshell(void)
472 if ((FixItMode || OnVTY) && RunningAsInit) {
477 if (kill(ehs_pid, 0) == 0) {
479 if (msgNoYes("There seems to be an emergency holographic shell\n"
480 "already running on VTY 4.\n\n"
481 "Kill it and start a new one?"))
484 /* try cleaning up as much as possible */
485 (void) kill(ehs_pid, SIGHUP);
487 (void) kill(ehs_pid, SIGKILL);
490 /* avoid too many zombies */
491 (void) waitpid(ehs_pid, &pstat, WNOHANG);
494 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
495 systemSuspendDialog(); /* must be before the fork() */
496 if ((ehs_pid = fork()) == 0) {
499 extern int login_tty(int);
501 ioctl(0, TIOCNOTTY, NULL);
502 for (i = getdtablesize(); i >= 0; --i)
504 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
505 fd = open("/dev/console", O_RDWR);
507 fd = open("/dev/ttyv3", O_RDWR);
508 ioctl(0, TIOCSCTTY, &fd);
512 if (login_tty(fd) == -1)
513 msgDebug("Doctor: I can't set the controlling terminal.\n");
514 signal(SIGTTOU, SIG_IGN);
515 if (tcgetattr(fd, &foo) != -1) {
516 foo.c_cc[VERASE] = '\010';
517 if (tcsetattr(fd, TCSANOW, &foo) == -1)
518 msgDebug("Doctor: I'm unable to set the erase character.\n");
521 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
522 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
523 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
526 execlp("sh", "-sh", 0);
527 msgDebug("Was unable to execute sh for Holographic shell!\n");
531 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
532 WINDOW *w = savescr();
534 msgNotify("Starting an emergency holographic shell on VTY4");
539 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
544 systemResumeDialog();