main.c revision 60417
158551Skris/*
2230237Sbapt * The new sysinstall program.
3230237Sbapt *
4230237Sbapt * This is probably the last attempt in the `sysinstall' line, the next
558551Skris * generation being slated for what's essentially a complete rewrite.
6230237Sbapt *
758551Skris * $FreeBSD: head/usr.sbin/sade/main.c 60417 2000-05-12 03:01:17Z jhb $
8230237Sbapt *
9230237Sbapt * Copyright (c) 1995
10230237Sbapt *	Jordan Hubbard.  All rights reserved.
11230237Sbapt *
1258551Skris * Redistribution and use in source and binary forms, with or without
13230237Sbapt * modification, are permitted provided that the following conditions
14230237Sbapt * are met:
15230237Sbapt * 1. Redistributions of source code must retain the above copyright
16230237Sbapt *    notice, this list of conditions and the following disclaimer,
1758551Skris *    verbatim and that no modifications are made prior to this
18230237Sbapt *    point in the file.
19230237Sbapt * 2. Redistributions in binary form must reproduce the above copyright
20230237Sbapt *    notice, this list of conditions and the following disclaimer in the
21230237Sbapt *    documentation and/or other materials provided with the distribution.
2258551Skris *
23230237Sbapt * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
2458551Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558551Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658551Skris * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
2758551Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858551Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958551Skris * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
3058551Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31230237Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32230237Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33230237Sbapt * SUCH DAMAGE.
3458551Skris *
35230237Sbapt */
36230237Sbapt
37230237Sbapt#include "sysinstall.h"
38230237Sbapt#include <sys/signal.h>
39230237Sbapt#include <sys/fcntl.h>
40230237Sbapt
41230237Sbaptstatic void
42230237Sbaptscreech(int sig)
43230237Sbapt{
44230237Sbapt    msgDebug("\007Signal %d caught!  That's bad!\n", sig);
45230237Sbapt    longjmp(BailOut, sig);
4658551Skris}
47230237Sbapt
48230237Sbaptint
49230237Sbaptmain(int argc, char **argv)
50230237Sbapt{
51230237Sbapt    int choice, scroll, curr, max, status;
52230237Sbapt
53230237Sbapt    /* Catch fatal signals and complain about them if running as init */
54230237Sbapt    if (getpid() == 1) {
55230237Sbapt	signal(SIGBUS, screech);
56230237Sbapt	signal(SIGSEGV, screech);
57230237Sbapt    }
58230237Sbapt    signal(SIGPIPE, SIG_IGN);
59230237Sbapt
60230237Sbapt    /* We don't work too well when running as non-root anymore */
61230237Sbapt    if (geteuid() != 0) {
62230237Sbapt	fprintf(stderr, "Error: This utility should only be run as root.\n");
63230237Sbapt	return 1;
64230237Sbapt    }
65230237Sbapt
66230237Sbapt    /* Set up whatever things need setting up */
67230237Sbapt    systemInitialize(argc, argv);
68230237Sbapt
69230237Sbapt    /* Set default flag and variable values */
70230237Sbapt    installVarDefaults(NULL);
71230237Sbapt    /* only when multi-user is it reasonable to do this here */
72230237Sbapt    if (!RunningAsInit)
73230237Sbapt	installEnvironment();
74230237Sbapt
75230237Sbapt    if (argc > 1 && !strcmp(argv[1], "-fake")) {
76230237Sbapt	variable_set2(VAR_DEBUG, "YES", 0);
77230237Sbapt	Fake = TRUE;
78230237Sbapt	msgConfirm("I'll be just faking it from here on out, OK?");
79230237Sbapt    }
80230237Sbapt
81230237Sbapt    /* Try to preserve our scroll-back buffer */
82230237Sbapt    if (OnVTY) {
8358551Skris	for (curr = 0; curr < 25; curr++)
8458551Skris	    putchar('\n');
85230237Sbapt    }
86230237Sbapt    /* Move stderr aside */
8758551Skris    if (DebugFD)
88230237Sbapt	dup2(DebugFD, 2);
89230237Sbapt
9058551Skris    /* Initialize PC-card */
91230237Sbapt    pccardInitialize();
92230237Sbapt
93230237Sbapt    /* Initialize USB */
94230237Sbapt    usbInitialize();
95230237Sbapt
9658551Skris    /* Probe for all relevant devices on the system */
97230237Sbapt    deviceGetAll();
98230237Sbapt
99230237Sbapt    /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
100230237Sbapt    if (!RunningAsInit) {
101230237Sbapt	int i, start_arg;
102230237Sbapt
103230237Sbapt	if (!strstr(argv[0], "sysinstall"))
10458551Skris	    start_arg = 0;
105230237Sbapt	else if (Fake)
106230237Sbapt	    start_arg = 2;
107230237Sbapt	else
108230237Sbapt	    start_arg = 1;
109230237Sbapt	for (i = start_arg; i < argc; i++) {
110230237Sbapt	    if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS)
111230237Sbapt		systemShutdown(1);
112230237Sbapt	}
113230237Sbapt	if (argc > start_arg)
114230237Sbapt	    systemShutdown(0);
115230237Sbapt    }
116230237Sbapt    else
117230237Sbapt	dispatch_load_file_int(TRUE);
118230237Sbapt
119230237Sbapt    status = setjmp(BailOut);
120230237Sbapt    if (status) {
121230237Sbapt	msgConfirm("A signal %d was caught - I'm saving what I can and shutting\n"
122230237Sbapt		   "If you can reproduce the problem, please turn Debug on in\n"
123230237Sbapt		   "the Options menu for the extra information it provides in\n"
124230237Sbapt		   "debugging problems like this.", status);
12558551Skris	systemShutdown(status);
12658551Skris    }
127230237Sbapt
128230237Sbapt    /* Begin user dialog at outer menu */
129230237Sbapt    dialog_clear();
130230237Sbapt    while (1) {
131230237Sbapt	choice = scroll = curr = max = 0;
132230237Sbapt	dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE);
133230237Sbapt	if (getpid() != 1
134230237Sbapt#ifdef __alpha__
135230237Sbapt	    || !msgYesNo("Are you sure you wish to exit?  The system will halt.")
136230237Sbapt#else
137230237Sbapt	    || !msgYesNo("Are you sure you wish to exit?  The system will reboot\n"
13858551Skris		         "(be sure to remove any floppies/CDROMs from the drives).")
139230237Sbapt#endif
140230237Sbapt	    )
141230237Sbapt	    break;
142230237Sbapt    }
14358551Skris
144230237Sbapt    /* Say goodnight, Gracie */
145230237Sbapt    systemShutdown(0);
146230237Sbapt
147230237Sbapt    return 0; /* We should never get here */
148230237Sbapt}
149230237Sbapt