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/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);
99 * Harvest children if we are init.
102 reap_children(int sig)
106 while (waitpid(-1, NULL, WNOHANG) > 0)
111 /* Expand a file into a convenient location, nuking it each time */
115 char *gunzip = RunningAsInit ? "/stand/gunzip" : "/usr/bin/gunzip";
117 if (!directory_exists(DOC_TMP_DIR)) {
119 if (chown(DOC_TMP_DIR, 0, 0) < 0)
121 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
125 unlink(DOC_TMP_FILE);
126 if (!file_readable(fname) || vsystem("%s < %s > %s", gunzip, fname, DOC_TMP_FILE))
131 /* Initialize system defaults */
133 systemInitialize(int argc, char **argv)
139 signal(SIGINT, SIG_IGN);
142 i = sizeof(boothowto);
143 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, 0) &&
144 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
145 variable_set2(VAR_DEBUG, "YES", 0);
147 /* Are we running as init? */
149 struct ufs_args ufs_args;
155 fd = open("/dev/ttyv0", O_RDWR);
157 fd = open("/dev/console", O_RDWR); /* fallback */
158 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
162 * To make _sure_ we're on a VTY and don't have /dev/console switched
163 * away to a serial port or something, attempt to set the cursor appearance.
168 type = 0; /* normal */
169 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
170 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
172 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
175 close(fd); close(fd2);
176 open("/dev/console", O_RDWR);
184 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
185 ioctl(0, TIOCSCTTY, (char *)NULL);
187 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
192 sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
195 signal(SIGCHLD, reap_children);
197 memset(&ufs_args, 0, sizeof(ufs_args));
198 mount("ufs", "/", MNT_UPDATE, &ufs_args);
203 /* Initalize various things for a multi-user environment */
204 if (!gethostname(hname, sizeof hname))
205 variable_set2(VAR_HOSTNAME, hname, 0);
208 if (set_termcap() == -1) {
209 printf("Can't find terminal entry\n");
213 /* XXX - libdialog has particularly bad return value checking */
216 /* If we haven't crashed I guess dialog is running ! */
219 /* Make sure HOME is set for those utilities that need it */
221 setenv("HOME", "/", 1);
222 signal(SIGINT, handle_intr);
224 * Make sure we can be interrupted even if we were re-executed
227 sigemptyset(&signalset);
228 sigaddset(&signalset, SIGINT);
229 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
231 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
234 /* Close down and prepare to exit */
236 systemShutdown(int status)
238 /* If some media is open, close it down */
242 /* write out any changes to rc.conf .. */
245 /* Shut down the dialog library */
248 DialogActive = FALSE;
251 /* Shut down curses */
254 /* If we have a temporary doc dir lying around, nuke it */
255 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
259 /* Put the console back */
260 ioctl(0, VT_ACTIVATE, 2);
261 #if defined(__alpha__) || defined(__sparc64__)
271 /* Run some general command */
273 systemExecute(char *command)
277 WINDOW *w = savescr();
282 DialogActive = FALSE;
283 if (tcgetattr(0, &foo) != -1) {
284 foo.c_cc[VERASE] = '\010';
285 tcsetattr(0, TCSANOW, &foo);
288 status = system(command);
291 msgDebug("systemExecute: Faked execution of `%s'\n", command);
298 /* suspend/resume libdialog/curses screen */
302 systemSuspendDialog(void)
309 DialogActive = FALSE;
313 systemResumeDialog(void)
320 /* Display a help file in a filebox */
322 systemDisplayHelp(char *file)
325 char buf[FILENAME_MAX];
327 WINDOW *w = savescr();
329 fname = systemHelpFile(file, buf);
331 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
334 dialog_mesgbox("Sorry!", buf, -1, -1);
340 dialog_textbox(file, fname, LINES, COLS);
347 systemHelpFile(char *file, char *buf)
353 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
354 if (file_readable(buf))
356 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp", file);
357 if (file_readable(buf))
359 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
360 if (file_readable(buf))
362 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT", file);
363 if (file_readable(buf))
365 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.hlp", file);
366 if (file_readable(buf))
368 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.TXT", file);
369 if (file_readable(buf))
375 systemChangeTerminal(char *color, const u_char c_term[],
376 char *mono, const u_char m_term[])
379 int setupterm(char *color, int, int *);
382 setenv("TERM", color, 1);
383 setenv("TERMCAP", c_term, 1);
389 setenv("TERM", mono, 1);
390 setenv("TERMCAP", m_term, 1);
402 vsystem(char *fmt, ...)
408 sig_t intsave, quitsave;
412 cmd = (char *)alloca(FILENAME_MAX);
415 vsnprintf(cmd, FILENAME_MAX, fmt, args);
418 omask = sigblock(sigmask(SIGCHLD));
420 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
424 msgDebug("Executing command `%s'\n", cmd);
427 (void)sigsetmask(omask);
430 else if (!pid) { /* Junior */
431 (void)sigsetmask(omask);
438 close(1); open("/dev/null", O_WRONLY);
442 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
444 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
448 intsave = signal(SIGINT, SIG_IGN);
449 quitsave = signal(SIGQUIT, SIG_IGN);
450 pid = waitpid(pid, &pstat, 0);
451 (void)sigsetmask(omask);
452 (void)signal(SIGINT, intsave);
453 (void)signal(SIGQUIT, quitsave);
454 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
456 msgDebug("Command `%s' returns status of %d\n", cmd, i);
462 systemCreateHoloshell(void)
466 if ((FixItMode || OnVTY) && RunningAsInit) {
471 if (kill(ehs_pid, 0) == 0) {
473 if (msgNoYes("There seems to be an emergency holographic shell\n"
474 "already running on VTY 4.\n\n"
475 "Kill it and start a new one?"))
478 /* try cleaning up as much as possible */
479 (void) kill(ehs_pid, SIGHUP);
481 (void) kill(ehs_pid, SIGKILL);
484 /* avoid too many zombies */
485 (void) waitpid(ehs_pid, &pstat, WNOHANG);
488 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
489 systemSuspendDialog(); /* must be before the fork() */
490 if ((ehs_pid = fork()) == 0) {
493 extern int login_tty(int);
495 ioctl(0, TIOCNOTTY, NULL);
496 for (i = getdtablesize(); i >= 0; --i)
498 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
499 fd = open("/dev/console", O_RDWR);
501 fd = open("/dev/ttyv3", O_RDWR);
502 ioctl(0, TIOCSCTTY, &fd);
506 if (login_tty(fd) == -1)
507 msgDebug("Doctor: I can't set the controlling terminal.\n");
508 signal(SIGTTOU, SIG_IGN);
509 if (tcgetattr(fd, &foo) != -1) {
510 foo.c_cc[VERASE] = '\010';
511 if (tcsetattr(fd, TCSANOW, &foo) == -1)
512 msgDebug("Doctor: I'm unable to set the erase character.\n");
515 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
516 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
517 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
520 execlp("sh", "-sh", 0);
521 msgDebug("Was unable to execute sh for Holographic shell!\n");
525 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
526 WINDOW *w = savescr();
528 msgNotify("Starting an emergency holographic shell on VTY4");
533 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
538 systemResumeDialog();