main.c revision 21673
1292915Sdim/*
2292915Sdim * The new sysinstall program.
3353358Sdim *
4353358Sdim * This is probably the last attempt in the `sysinstall' line, the next
5353358Sdim * generation being slated for what's essentially a complete rewrite.
6292915Sdim *
7292915Sdim * $FreeBSD: head/usr.sbin/sade/main.c 21673 1997-01-14 07:20:47Z jkh $
8292915Sdim *
9292915Sdim * Copyright (c) 1995
10341825Sdim *	Jordan Hubbard.  All rights reserved.
11292915Sdim *
12292915Sdim * Redistribution and use in source and binary forms, with or without
13292915Sdim * modification, are permitted provided that the following conditions
14292915Sdim * are met:
15292915Sdim * 1. Redistributions of source code must retain the above copyright
16321369Sdim *    notice, this list of conditions and the following disclaimer,
17292915Sdim *    verbatim and that no modifications are made prior to this
18321369Sdim *    point in the file.
19353358Sdim * 2. Redistributions in binary form must reproduce the above copyright
20292915Sdim *    notice, this list of conditions and the following disclaimer in the
21292915Sdim *    documentation and/or other materials provided with the distribution.
22292915Sdim *
23292915Sdim * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24292915Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25292915Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26292915Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27321369Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28292915Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29292915Sdim * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30292915Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31292915Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32344779Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33344779Sdim * SUCH DAMAGE.
34344779Sdim *
35344779Sdim */
36344779Sdim
37344779Sdim#include "sysinstall.h"
38344779Sdim#include <sys/signal.h>
39353358Sdim#include <sys/fcntl.h>
40344779Sdim
41344779Sdimstatic void
42344779Sdimscreech(int sig)
43344779Sdim{
44344779Sdim    printf("\007Fatal signal %d caught!  I'm dead..\n", sig);
45344779Sdim    if (RunningAsInit)
46344779Sdim	pause();
47292915Sdim    else
48292915Sdim	exit(1);
49321369Sdim}
50353358Sdim
51321369Sdimint
52321369Sdimmain(int argc, char **argv)
53321369Sdim{
54321369Sdim    int choice, scroll, curr, max;
55327952Sdim
56321369Sdim    /* Catch fatal signals and complain about them if running as init */
57344779Sdim    if (getpid() == 1) {
58344779Sdim	signal(SIGBUS, screech);
59353358Sdim	signal(SIGSEGV, screech);
60321369Sdim    }
61353358Sdim
62344779Sdim    /* We don't work too well when running as non-root anymore */
63344779Sdim    if (geteuid() != 0) {
64341825Sdim	fprintf(stderr, "Error: This utility should only be run as root.\n");
65321369Sdim	return 1;
66321369Sdim    }
67321369Sdim
68292915Sdim    /* Set up whatever things need setting up */
69292915Sdim    systemInitialize(argc, argv);
70292915Sdim
71292915Sdim    /* Set default flag and variable values */
72321369Sdim    installVarDefaults(NULL);
73353358Sdim
74321369Sdim    if (argc > 1 && !strcmp(argv[1], "-fake")) {
75321369Sdim	variable_set2(VAR_DEBUG, "YES");
76353358Sdim	Fake = TRUE;
77353358Sdim	msgConfirm("I'll be just faking it from here on out, OK?");
78353358Sdim    }
79353358Sdim
80353358Sdim    /* Try to preserve our scroll-back buffer */
81353358Sdim    if (OnVTY) {
82360784Sdim	for (curr = 0; curr < 25; curr++)
83353358Sdim	    putchar('\n');
84353358Sdim	/* Move stderr aside */
85341825Sdim	if (DebugFD)
86341825Sdim	    dup2(DebugFD, 2);
87341825Sdim    }
88341825Sdim
89353358Sdim    /* Probe for all relevant devices on the system */
90321369Sdim    deviceGetAll();
91341825Sdim
92321369Sdim    /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
93321369Sdim    if (!RunningAsInit) {
94321369Sdim	int i, start_arg;
95344779Sdim
96344779Sdim	/* Try to set ourselves up as a CDROM if we can do that first */
97344779Sdim	if (DITEM_STATUS(mediaSetCDROM(NULL)) == DITEM_SUCCESS) {
98344779Sdim	    /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
99344779Sdim	    if (!mediaDevice->init(mediaDevice))
100344779Sdim		mediaDevice = NULL;
101344779Sdim	}
102344779Sdim
103344779Sdim	if (!strstr(argv[0], "sysinstall"))
104344779Sdim	    start_arg = 0;
105344779Sdim	else if (Fake)
106321369Sdim	    start_arg = 2;
107344779Sdim	else
108344779Sdim	    start_arg = 1;
109344779Sdim	for (i = start_arg; i < argc; i++) {
110344779Sdim	    if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS)
111344779Sdim		systemShutdown(1);
112344779Sdim	}
113344779Sdim	if (argc > start_arg)
114344779Sdim	    systemShutdown(0);
115353358Sdim    }
116344779Sdim
117344779Sdim    {
118360784Sdim	FILE *fp;
119344779Sdim	Attribs *attrs;
120344779Sdim
121321369Sdim	attrs = alloca(sizeof(Attribs) * MAX_ATTRIBS);
122321369Sdim	fp = fopen("install.cfg", "r");
123292915Sdim	if (fp) {
124292915Sdim	    msgNotify("Loading pre-configuration file");
125353358Sdim	    if (DITEM_STATUS(attr_parse(attrs, fp)) == DITEM_SUCCESS) {
126353358Sdim		int i;
127353358Sdim
128353358Sdim		for (i = 0; attrs[i].name; i++)
129321369Sdim		    variable_set2(attrs[i].name, attrs[i].value);
130353358Sdim	    }
131353358Sdim	    fclose(fp);
132353358Sdim	}
133353358Sdim
134353358Sdim#if defined(LOAD_CONFIG_FILE)
135353358Sdim	else {
136353358Sdim	    /* If we have a compiled-in startup config file name on
137353358Sdim	       the floppy, look for it and try to load it on startup */
138353358Sdim	    extern char *distWanted;
139353358Sdim
140353358Sdim	    /* Tell mediaSetFloppy() to try floppy now */
141353358Sdim	    distWanted = (char *)1;
142353358Sdim
143353358Sdim	    /* Try to open the floppy drive if we can do that first */
144353358Sdim	    if (DITEM_STATUS(mediaSetFloppy(NULL)) != DITEM_FAILURE &&
145292915Sdim		mediaDevice->init(mediaDevice)) {
146353358Sdim		int fd;
147353358Sdim
148353358Sdim		fp = mediaDevice->get(mediaDevice, LOAD_CONFIG_FILE, TRUE);
149353358Sdim		if (fp) {
150353358Sdim		    msgNotify("Loading %s pre-configuration file",
151353358Sdim			      LOAD_CONFIG_FILE);
152353358Sdim		    if (DITEM_STATUS(attr_parse(attrs, fp)) == DITEM_SUCCESS) {
153294024Sdim			int i;
154353358Sdim
155344779Sdim			for (i = 0; attrs[i].name; i++)
156353358Sdim			    variable_set2(attrs[i].name, attrs[i].value);
157344779Sdim		    }
158353358Sdim		    fclose(fp);
159353358Sdim		}
160353358Sdim		mediaDevice->shutdown(mediaDevice);
161292915Sdim	    }
162292915Sdim	}
163292915Sdim#endif
164292915Sdim    }
165292915Sdim
166360784Sdim    /* Begin user dialog at outer menu */
167360784Sdim    dialog_clear();
168360784Sdim    while (1) {
169360784Sdim	choice = scroll = curr = max = 0;
170360784Sdim	dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE);
171360784Sdim	if (getpid() != 1 || !msgYesNo("Are you sure you wish to exit?  The system will reboot\n"
172360784Sdim				       "(be sure to remove any floppies from the drives)."))
173360784Sdim	    break;
174360784Sdim    }
175360784Sdim
176360784Sdim    /* Say goodnight, Gracie */
177360784Sdim    systemShutdown(0);
178360784Sdim
179360784Sdim    return 0; /* We should never get here */
180360784Sdim}
181321369Sdim