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)
66 fdmax = getdtablesize();
67 for (fd = 3; fd < fdmax; fd++)
71 arg = "-restart -fakeInit";
75 ret = execl(StartName, StartName, arg, NULL);
76 msgDebug("execl failed (%s)\n", strerror(errno));
81 static dialogMenuItem intrmenu[] = {
82 { "Abort", "Abort the installation", NULL, intr_reboot },
83 { "Restart", "Restart the installation program", NULL, intr_restart },
84 { "Continue", "Continue the installation", NULL, intr_continue },
91 WINDOW *save = savescr();
96 ioctl(0, VT_ACTIVATE, 1); /* Switch back */
99 (void)dialog_menu("Installation interrupt",
100 "Do you want to abort the installation?",
101 -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
107 * Harvest children if we are init.
110 reap_children(int sig)
114 while (waitpid(-1, NULL, WNOHANG) > 0)
120 /* Expand a file into a convenient location, nuking it each time */
124 char *unzipper = RunningAsInit ? "/stand/" UNZIPPER : "/usr/bin/" UNZIPPER;
126 if (!directory_exists(DOC_TMP_DIR)) {
128 if (chown(DOC_TMP_DIR, 0, 0) < 0)
130 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
134 unlink(DOC_TMP_FILE);
135 if (!file_readable(fname) || vsystem("%s < %s > %s", unzipper, fname,
141 /* Initialize system defaults */
143 systemInitialize(int argc, char **argv)
149 signal(SIGINT, SIG_IGN);
152 i = sizeof(boothowto);
153 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, 0) &&
154 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
155 variable_set2(VAR_DEBUG, "YES", 0);
157 /* Are we running as init? */
159 struct ufs_args ufs_args;
164 fd = open("/dev/ttyv0", O_RDWR);
166 fd = open("/dev/console", O_RDWR); /* fallback */
167 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
171 * To make _sure_ we're on a VTY and don't have /dev/console switched
172 * away to a serial port or something, attempt to set the cursor appearance.
177 type = 0; /* normal */
178 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
179 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
181 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
184 close(fd); close(fd2);
185 open("/dev/console", O_RDWR);
193 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
194 ioctl(0, TIOCSCTTY, (char *)NULL);
196 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin", 1);
200 signal(SIGCHLD, reap_children);
202 memset(&ufs_args, 0, sizeof(ufs_args));
203 mount("ufs", "/", MNT_UPDATE, &ufs_args);
208 /* Initalize various things for a multi-user environment */
209 if (!gethostname(hname, sizeof hname))
210 variable_set2(VAR_HOSTNAME, hname, 0);
213 if (set_termcap() == -1) {
214 printf("Can't find terminal entry\n");
218 /* XXX - libdialog has particularly bad return value checking */
221 /* If we haven't crashed I guess dialog is running ! */
224 /* Make sure HOME is set for those utilities that need it */
226 setenv("HOME", "/", 1);
227 signal(SIGINT, handle_intr);
229 * Make sure we can be interrupted even if we were re-executed
232 sigemptyset(&signalset);
233 sigaddset(&signalset, SIGINT);
234 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
236 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
239 /* Close down and prepare to exit */
241 systemShutdown(int status)
243 /* If some media is open, close it down */
245 if (mediaDevice != NULL && mediaDevice->type == DEVICE_TYPE_CDROM) {
247 msgConfirm("Be sure to remove the media from the drive.");
252 /* write out any changes to rc.conf .. */
255 /* Shut down the dialog library */
258 DialogActive = FALSE;
261 /* Shut down curses */
264 /* If we have a temporary doc dir lying around, nuke it */
265 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
269 /* Put the console back */
270 ioctl(0, VT_ACTIVATE, 2);
271 #if defined(__sparc64__)
281 /* Run some general command */
283 systemExecute(char *command)
287 WINDOW *w = savescr();
292 DialogActive = FALSE;
293 if (tcgetattr(0, &foo) != -1) {
294 foo.c_cc[VERASE] = '\010';
295 tcsetattr(0, TCSANOW, &foo);
298 status = system(command);
301 msgDebug("systemExecute: Faked execution of `%s'\n", command);
308 /* suspend/resume libdialog/curses screen */
312 systemSuspendDialog(void)
319 DialogActive = FALSE;
323 systemResumeDialog(void)
330 /* Display a help file in a filebox */
332 systemDisplayHelp(char *file)
335 char buf[FILENAME_MAX];
337 WINDOW *w = savescr();
339 fname = systemHelpFile(file, buf);
341 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
344 dialog_mesgbox("Sorry!", buf, -1, -1);
350 dialog_textbox(file, fname, LINES, COLS);
357 systemHelpFile(char *file, char *buf)
363 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
364 if (file_readable(buf))
366 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp", file);
367 if (file_readable(buf))
369 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
370 if (file_readable(buf))
372 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT", file);
373 if (file_readable(buf))
375 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.hlp", ProgName,
377 if (file_readable(buf))
379 snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/%s/help/%s.TXT", ProgName,
381 if (file_readable(buf))
387 systemChangeTerminal(char *color, const u_char c_term[],
388 char *mono, const u_char m_term[])
391 int setupterm(char *color, int, int *);
394 setenv("TERM", color, 1);
395 setenv("TERMCAP", c_term, 1);
401 setenv("TERM", mono, 1);
402 setenv("TERMCAP", m_term, 1);
414 vsystem(char *fmt, ...)
420 sig_t intsave, quitsave;
425 cmd = (char *)alloca(FILENAME_MAX);
428 vsnprintf(cmd, FILENAME_MAX, fmt, args);
431 omask = sigblock(sigmask(SIGCHLD));
433 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
437 msgDebug("Executing command `%s'\n", cmd);
440 (void)sigsetmask(omask);
443 else if (!pid) { /* Junior */
444 (void)sigsetmask(omask);
451 close(1); open("/dev/null", O_WRONLY);
454 if (stat("/stand/sh", &sb) == 0)
455 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
457 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
461 intsave = signal(SIGINT, SIG_IGN);
462 quitsave = signal(SIGQUIT, SIG_IGN);
463 pid = waitpid(pid, &pstat, 0);
464 (void)sigsetmask(omask);
465 (void)signal(SIGINT, intsave);
466 (void)signal(SIGQUIT, quitsave);
467 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
469 msgDebug("Command `%s' returns status of %d\n", cmd, i);
475 systemCreateHoloshell(void)
479 if ((FixItMode || OnVTY) && RunningAsInit) {
484 if (kill(ehs_pid, 0) == 0) {
486 if (msgNoYes("There seems to be an emergency holographic shell\n"
487 "already running on VTY 4.\n\n"
488 "Kill it and start a new one?"))
491 /* try cleaning up as much as possible */
492 (void) kill(ehs_pid, SIGHUP);
494 (void) kill(ehs_pid, SIGKILL);
497 /* avoid too many zombies */
498 (void) waitpid(ehs_pid, &pstat, WNOHANG);
501 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
502 systemSuspendDialog(); /* must be before the fork() */
503 if ((ehs_pid = fork()) == 0) {
506 extern int login_tty(int);
508 ioctl(0, TIOCNOTTY, NULL);
509 for (i = getdtablesize(); i >= 0; --i)
511 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
512 fd = open("/dev/console", O_RDWR);
514 fd = open("/dev/ttyv3", O_RDWR);
515 ioctl(0, TIOCSCTTY, &fd);
519 if (login_tty(fd) == -1)
520 msgDebug("Doctor: I can't set the controlling terminal.\n");
521 signal(SIGTTOU, SIG_IGN);
522 if (tcgetattr(fd, &foo) != -1) {
523 foo.c_cc[VERASE] = '\010';
524 if (tcsetattr(fd, TCSANOW, &foo) == -1)
525 msgDebug("Doctor: I'm unable to set the erase character.\n");
528 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
529 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
530 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
533 execlp("sh", "-sh", NULL);
534 msgDebug("Was unable to execute sh for Holographic shell!\n");
538 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
539 WINDOW *w = savescr();
541 msgNotify("Starting an emergency holographic shell on VTY4");
546 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
551 systemResumeDialog();