main.c revision 20355
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 * $Id: main.c,v 1.31 1996/12/12 08:23:49 jkh Exp $
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 <stdio.h>
39#include <sys/signal.h>
40#include <sys/fcntl.h>
41
42static void
43screech(int sig)
44{
45    printf("\007Fatal signal %d caught!  I'm dead..\n", sig);
46    if (RunningAsInit)
47	pause();
48    else
49	exit(1);
50}
51
52int
53main(int argc, char **argv)
54{
55    int choice, scroll, curr, max;
56
57    /* Catch fatal signals and complain about them if running as init */
58    if (getpid() == 1) {
59	signal(SIGBUS, screech);
60	signal(SIGSEGV, screech);
61    }
62
63    /* We don't work too well when running as non-root anymore */
64    if (geteuid() != 0) {
65	fprintf(stderr, "Error: This utility should only be run as root.\n");
66	return 1;
67    }
68
69    /* Set up whatever things need setting up */
70    systemInitialize(argc, argv);
71
72    /* Set default flag and variable values */
73    installVarDefaults(NULL);
74
75    if (argc > 1 && !strcmp(argv[1], "-fake")) {
76	variable_set2(VAR_DEBUG, "YES");
77	Fake = TRUE;
78	msgConfirm("I'll be just faking it from here on out, OK?");
79    }
80
81    /* Try to preserve our scroll-back buffer */
82    if (OnVTY) {
83	for (curr = 0; curr < 25; curr++)
84	    putchar('\n');
85	/* Move stderr aside */
86	if (DebugFD)
87	    dup2(DebugFD, 2);
88    }
89
90    /* Probe for all relevant devices on the system */
91    deviceGetAll();
92
93    /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
94    if (!RunningAsInit) {
95	int i, start_arg;
96
97	/* Try to set ourselves up as a CDROM if we can do that first */
98	if (DITEM_STATUS(mediaSetCDROM(NULL)) == DITEM_SUCCESS) {
99	    /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
100	    if (!mediaDevice->init(mediaDevice))
101		mediaDevice = NULL;
102	}
103
104	if (!strstr(argv[0], "sysinstall"))
105	    start_arg = 0;
106	else if (Fake)
107	    start_arg = 2;
108	else
109	    start_arg = 1;
110	for (i = start_arg; i < argc; i++) {
111	    if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS)
112		systemShutdown(1);
113	}
114	if (argc > start_arg)
115	    systemShutdown(0);
116    }
117
118    {
119	FILE *fp;
120	Attribs *attrs;
121
122	attrs = alloca(sizeof(Attribs) * MAX_ATTRIBS);
123	fp = fopen("install.cfg", "r");
124	if (fp) {
125	    msgNotify("Loading pre-configuration file");
126	    if (DITEM_STATUS(attr_parse(attrs, fp)) == DITEM_SUCCESS) {
127		int i;
128
129		for (i = 0; attrs[i].name; i++)
130		    variable_set2(attrs[i].name, attrs[i].value);
131	    }
132	    fclose(fp);
133	}
134
135#if defined(LOAD_CONFIG_FILE)
136	else {
137	    /* If we have a compiled-in startup config file name on
138	       the floppy, look for it and try to load it on startup */
139	    extern char *distWanted;
140
141	    /* Tell mediaSetFloppy() to try floppy now */
142	    distWanted = (char *)1;
143
144	    /* Try to open the floppy drive if we can do that first */
145	    if (DITEM_STATUS(mediaSetFloppy(NULL)) != DITEM_FAILURE &&
146		mediaDevice->init(mediaDevice)) {
147		int fd;
148
149		fp = mediaDevice->get(mediaDevice, LOAD_CONFIG_FILE, TRUE);
150		if (fp) {
151		    msgNotify("Loading %s pre-configuration file",
152			      LOAD_CONFIG_FILE);
153		    if (DITEM_STATUS(attr_parse(attrs, fp)) == DITEM_SUCCESS) {
154			int i;
155
156			for (i = 0; attrs[i].name; i++)
157			    variable_set2(attrs[i].name, attrs[i].value);
158		    }
159		    fclose(fp);
160		}
161		mediaDevice->shutdown(mediaDevice);
162	    }
163	}
164#endif
165    }
166
167    /* Begin user dialog at outer menu */
168    dialog_clear();
169    while (1) {
170	choice = scroll = curr = max = 0;
171	dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE);
172	if (getpid() != 1 || !msgYesNo("Are you sure you wish to exit?  The system will reboot\n"
173				       "(be sure to remove any floppies from the drives)."))
174	    break;
175    }
176
177    /* Say goodnight, Gracie */
178    systemShutdown(0);
179
180    return 0; /* We should never get here */
181}
182