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.
7 * $FreeBSD: src/release/sysinstall/system.c,v 1.103.2.9 2002/07/02 22:31:20 jhb Exp $
8 * $DragonFly: src/release/sysinstall/Attic/system.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
12 * My contributions are in the public domain.
14 * Parts of this file are also blatently stolen from Poul-Henning Kamp's
15 * previous version of sysinstall, and as such fall under his "BEERWARE license"
16 * so buy him a beer if you like it! Buy him a beer for me, too!
17 * Heck, get him completely drunk and send me pictures! :-)
20 #include "sysinstall.h"
23 #include <sys/reboot.h>
24 #include <machine/console.h>
25 #include <sys/fcntl.h>
26 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
32 /* Where we stick our temporary expanded doc file */
33 #define DOC_TMP_DIR "/tmp/.doc"
34 #define DOC_TMP_FILE "/tmp/.doc/doc.tmp"
39 * Handle interrupt signals - this probably won't work in all cases
40 * due to our having bogotified the internal state of dialog or curses,
41 * but we'll give it a try.
44 intr_continue(dialogMenuItem *self)
46 return DITEM_LEAVE_MENU;
50 intr_reboot(dialogMenuItem *self)
58 intr_restart(dialogMenuItem *self)
63 ret = execl(StartName, StartName, "-restart", (char *)NULL);
64 msgDebug("execl failed (%s)\n", strerror(errno));
69 static dialogMenuItem intrmenu[] = {
70 { "Abort", "Abort the installation", NULL, intr_reboot },
71 { "Restart", "Restart the installation program", NULL, intr_restart },
72 { "Continue", "Continue the installation", NULL, intr_continue },
79 WINDOW *save = savescr();
84 ioctl(0, VT_ACTIVATE, 1); /* Switch back */
87 (void)dialog_menu("Installation interrupt",
88 "Do you want to abort the installation?",
89 -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
94 * Harvest children if we are init.
97 reap_children(int sig)
101 while (waitpid(-1, NULL, WNOHANG) > 0)
106 /* Expand a file into a convenient location, nuking it each time */
110 char *gunzip = RunningAsInit ? "/stand/gunzip" : "/usr/bin/gunzip";
112 if (!directory_exists(DOC_TMP_DIR)) {
114 if (chown(DOC_TMP_DIR, 0, 0) < 0)
116 if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
120 unlink(DOC_TMP_FILE);
121 if (!file_readable(fname) || vsystem("%s < %s > %s", gunzip, fname, DOC_TMP_FILE))
126 /* Initialize system defaults */
128 systemInitialize(int argc, char **argv)
134 signal(SIGINT, SIG_IGN);
137 i = sizeof(boothowto);
138 if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, NULL) &&
139 (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
140 variable_set2(VAR_DEBUG, "YES", 0);
142 /* Are we running as init? */
149 fd = open("/dev/ttyv0", O_RDWR);
151 fd = open("/dev/console", O_RDWR); /* fallback */
152 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
156 * To make _sure_ we're on a VTY and don't have /dev/console switched
157 * away to a serial port or something, attempt to set the cursor appearance.
162 type = 0; /* normal */
163 if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
164 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
166 variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
169 close(fd); close(fd2);
170 open("/dev/console", O_RDWR);
178 printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
179 ioctl(0, TIOCSCTTY, (char *)NULL);
181 setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
186 sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
189 signal(SIGCHLD, reap_children);
195 /* Initalize various things for a multi-user environment */
196 if (!gethostname(hname, sizeof hname))
197 variable_set2(VAR_HOSTNAME, hname, 0);
200 if (set_termcap() == -1) {
201 printf("Can't find terminal entry\n");
205 /* XXX - libdialog has particularly bad return value checking */
208 /* If we haven't crashed I guess dialog is running ! */
211 /* Make sure HOME is set for those utilities that need it */
213 setenv("HOME", "/", 1);
214 signal(SIGINT, handle_intr);
216 * Make sure we can be interrupted even if we were re-executed
219 sigemptyset(&signalset);
220 sigaddset(&signalset, SIGINT);
221 sigprocmask(SIG_UNBLOCK, &signalset, NULL);
223 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
226 /* Close down and prepare to exit */
228 systemShutdown(int status)
230 /* If some media is open, close it down */
234 /* write out any changes to rc.conf .. */
237 /* Shut down the dialog library */
240 DialogActive = FALSE;
243 /* Shut down curses */
246 /* If we have a temporary doc dir lying around, nuke it */
247 (void)vsystem("rm -rf %s", DOC_TMP_DIR);
251 /* Put the console back */
252 ioctl(0, VT_ACTIVATE, 2);
263 /* Run some general command */
265 systemExecute(char *command)
269 WINDOW *w = savescr();
274 DialogActive = FALSE;
275 if (tcgetattr(0, &foo) != -1) {
276 foo.c_cc[VERASE] = '\010';
277 tcsetattr(0, TCSANOW, &foo);
280 status = system(command);
283 msgDebug("systemExecute: Faked execution of `%s'\n", command);
290 /* suspend/resume libdialog/curses screen */
294 systemSuspendDialog(void)
301 DialogActive = FALSE;
305 systemResumeDialog(void)
312 /* Display a help file in a filebox */
314 systemDisplayHelp(char *file)
317 char buf[FILENAME_MAX];
319 WINDOW *w = savescr();
321 fname = systemHelpFile(file, buf);
323 snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
326 dialog_mesgbox("Sorry!", buf, -1, -1);
332 dialog_textbox(file, fname, LINES, COLS);
339 systemHelpFile(char *file, char *buf)
345 snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
346 if (file_readable(buf))
348 snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
349 if (file_readable(buf))
351 snprintf(buf, FILENAME_MAX, "/usr/src/release/sysinstall/help/%s.hlp", file);
352 if (file_readable(buf))
354 snprintf(buf, FILENAME_MAX, "/usr/src/release/sysinstall/help/%s.TXT", file);
355 if (file_readable(buf))
361 systemChangeTerminal(char *color, const u_char c_term[],
362 char *mono, const u_char m_term[])
365 int setupterm(char *color, int, int *);
368 setenv("TERM", color, 1);
369 setenv("TERMCAP", c_term, 1);
375 setenv("TERM", mono, 1);
376 setenv("TERMCAP", m_term, 1);
388 vsystem(char *fmt, ...)
394 sig_t intsave, quitsave;
398 cmd = (char *)alloca(FILENAME_MAX);
401 vsnprintf(cmd, FILENAME_MAX, fmt, args);
404 omask = sigblock(sigmask(SIGCHLD));
406 msgDebug("vsystem: Faked execution of `%s'\n", cmd);
410 msgDebug("Executing command `%s'\n", cmd);
413 (void)sigsetmask(omask);
416 else if (!pid) { /* Junior */
417 (void)sigsetmask(omask);
424 close(1); open("/dev/null", O_WRONLY);
428 execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
430 execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
434 intsave = signal(SIGINT, SIG_IGN);
435 quitsave = signal(SIGQUIT, SIG_IGN);
436 pid = waitpid(pid, &pstat, 0);
437 (void)sigsetmask(omask);
438 (void)signal(SIGINT, intsave);
439 (void)signal(SIGQUIT, quitsave);
440 i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
442 msgDebug("Command `%s' returns status of %d\n", cmd, i);
448 systemCreateHoloshell(void)
452 if ((FixItMode || OnVTY) && RunningAsInit) {
457 if (kill(ehs_pid, 0) == 0) {
459 if (msgNoYes("There seems to be an emergency holographic shell\n"
460 "already running on VTY 4.\n\n"
461 "Kill it and start a new one?"))
464 /* try cleaning up as much as possible */
465 (void) kill(ehs_pid, SIGHUP);
467 (void) kill(ehs_pid, SIGKILL);
470 /* avoid too many zombies */
471 (void) waitpid(ehs_pid, &pstat, WNOHANG);
474 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
475 systemSuspendDialog(); /* must be before the fork() */
476 if ((ehs_pid = fork()) == 0) {
479 extern int login_tty(int);
481 ioctl(0, TIOCNOTTY, NULL);
482 for (i = getdtablesize(); i >= 0; --i)
484 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
485 fd = open("/dev/console", O_RDWR);
487 fd = open("/dev/ttyv3", O_RDWR);
488 ioctl(0, TIOCSCTTY, &fd);
492 if (login_tty(fd) == -1)
493 msgDebug("Doctor: I can't set the controlling terminal.\n");
494 signal(SIGTTOU, SIG_IGN);
495 if (tcgetattr(fd, &foo) != -1) {
496 foo.c_cc[VERASE] = '\010';
497 if (tcsetattr(fd, TCSANOW, &foo) == -1)
498 msgDebug("Doctor: I'm unable to set the erase character.\n");
501 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
502 if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
503 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
506 execlp("sh", "-sh", 0);
507 msgDebug("Was unable to execute sh for Holographic shell!\n");
511 if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
512 WINDOW *w = savescr();
514 msgNotify("Starting an emergency holographic shell on VTY4");
519 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
524 systemResumeDialog();