main.c revision 21853
1/* 2 * The new sysinstall program. 3 * 4 * This is probably the last attempt in the `sysinstall' line, the next 5 * generation being slated for what's essentially a complete rewrite. 6 * 7 * $FreeBSD: head/usr.sbin/sade/main.c 21853 1997-01-18 16:37:15Z jkh $ 8 * 9 * Copyright (c) 1995 10 * Jordan Hubbard. All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer, 17 * verbatim and that no modifications are made prior to this 18 * point in the file. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37#include "sysinstall.h" 38#include <sys/signal.h> 39#include <sys/fcntl.h> 40 41static void 42screech(int sig) 43{ 44 printf("\007Fatal signal %d caught! I'm dead..\n", sig); 45 if (RunningAsInit) 46 pause(); 47 else 48 exit(1); 49} 50 51int 52main(int argc, char **argv) 53{ 54 int choice, scroll, curr, max; 55 56 /* Catch fatal signals and complain about them if running as init */ 57 if (getpid() == 1) { 58 signal(SIGBUS, screech); 59 signal(SIGSEGV, screech); 60 } 61 62 /* We don't work too well when running as non-root anymore */ 63 if (geteuid() != 0) { 64 fprintf(stderr, "Error: This utility should only be run as root.\n"); 65 return 1; 66 } 67 68 /* Set up whatever things need setting up */ 69 systemInitialize(argc, argv); 70 71 /* Set default flag and variable values */ 72 installVarDefaults(NULL); 73 74 if (argc > 1 && !strcmp(argv[1], "-fake")) { 75 variable_set2(VAR_DEBUG, "YES"); 76 Fake = TRUE; 77 msgConfirm("I'll be just faking it from here on out, OK?"); 78 } 79 80 /* Try to preserve our scroll-back buffer */ 81 if (OnVTY) { 82 for (curr = 0; curr < 25; curr++) 83 putchar('\n'); 84 /* Move stderr aside */ 85 if (DebugFD) 86 dup2(DebugFD, 2); 87 } 88 89 /* Probe for all relevant devices on the system */ 90 deviceGetAll(); 91 92 /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */ 93 if (!RunningAsInit) { 94 int i, start_arg; 95 96 /* Try to set ourselves up as a CDROM if we can do that first */ 97 if (DITEM_STATUS(mediaSetCDROM(NULL)) == DITEM_SUCCESS) { 98 /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */ 99 if (!mediaDevice->init(mediaDevice)) 100 mediaDevice = NULL; 101 } 102 103 if (!strstr(argv[0], "sysinstall")) 104 start_arg = 0; 105 else if (Fake) 106 start_arg = 2; 107 else 108 start_arg = 1; 109 for (i = start_arg; i < argc; i++) { 110 if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS) 111 systemShutdown(1); 112 } 113 if (argc > start_arg) 114 systemShutdown(0); 115 } 116 else { 117 FILE *fp; 118 char buf[BUFSIZ]; 119 120 fp = fopen("install.cfg", "r"); 121 if (fp) { 122 msgNotify("Loading pre-configuration file"); 123 while (fgets(buf, sizeof buf, fp)) { 124 if (DITEM_STATUS(dispatchCommand(buf)) != DITEM_SUCCESS) { 125 msgDebug("Command `%s' failed - rest of script aborted.\n", buf); 126 break; 127 } 128 } 129 fclose(fp); 130 } 131#if defined(LOAD_CONFIG_FILE) 132 else { 133 /* If we have a compiled-in startup config file name on 134 the floppy, look for it and try to load it on startup */ 135 extern char *distWanted; 136 137 /* Tell mediaSetFloppy() to try floppy now */ 138 distWanted = LOAD_CONFIG_FILE; 139 140 /* Try to open the floppy drive if we can do that first */ 141 if (DITEM_STATUS(mediaSetFloppy(NULL)) != DITEM_FAILURE && mediaDevice->init(mediaDevice)) { 142 fp = mediaDevice->get(mediaDevice, LOAD_CONFIG_FILE, TRUE); 143 if (fp) { 144 msgNotify("Loading %s pre-configuration file", LOAD_CONFIG_FILE); 145 while (fgets(buf, sizeof buf, fp)) { 146 if (DITEM_STATUS(dispatchCommand(buf)) != DITEM_SUCCESS) { 147 msgDebug("Command `%s' failed - rest of script aborted.\n", buf); 148 break; 149 } 150 } 151 fclose(fp); 152 } 153 mediaDevice->shutdown(mediaDevice); 154 } 155 } 156#endif 157 } 158 159 /* Begin user dialog at outer menu */ 160 dialog_clear(); 161 while (1) { 162 choice = scroll = curr = max = 0; 163 dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE); 164 if (getpid() != 1 || !msgYesNo("Are you sure you wish to exit? The system will reboot\n" 165 "(be sure to remove any floppies from the drives).")) 166 break; 167 } 168 169 /* Say goodnight, Gracie */ 170 systemShutdown(0); 171 172 return 0; /* We should never get here */ 173} 174