main.c revision 67862
13969Sjkh/*
298943Sluigi * The new sysinstall program.
33969Sjkh *
498943Sluigi * This is probably the last attempt in the `sysinstall' line, the next
598943Sluigi * generation being slated for what's essentially a complete rewrite.
698943Sluigi *
798943Sluigi * $FreeBSD: head/usr.sbin/sade/main.c 67862 2000-10-29 09:57:50Z eivind $
898943Sluigi *
998943Sluigi * Copyright (c) 1995
1098943Sluigi *	Jordan Hubbard.  All rights reserved.
1198943Sluigi *
123969Sjkh * Redistribution and use in source and binary forms, with or without
1398943Sluigi * modification, are permitted provided that the following conditions
1498943Sluigi * are met:
1598943Sluigi * 1. Redistributions of source code must retain the above copyright
1698943Sluigi *    notice, this list of conditions and the following disclaimer,
1798943Sluigi *    verbatim and that no modifications are made prior to this
1898943Sluigi *    point in the file.
1998943Sluigi * 2. Redistributions in binary form must reproduce the above copyright
2098943Sluigi *    notice, this list of conditions and the following disclaimer in the
2198943Sluigi *    documentation and/or other materials provided with the distribution.
2298943Sluigi *
2398943Sluigi * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
243969Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2550477Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
263969Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
273969Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2898943Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2998943Sluigi * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30100228Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
313969Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3298943Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3398943Sluigi * SUCH DAMAGE.
3498943Sluigi *
35115793Sticso */
3626359Sjulian
3798943Sluigi#include "sysinstall.h"
3898943Sluigi#include <sys/signal.h>
3998943Sluigi#include <sys/fcntl.h>
40115793Sticso
41115793Sticsoconst char *StartName;		/* Initial contents of argv[0] */
42115793Sticso
43115793Sticsostatic void
44115793Sticsoscreech(int sig)
4598943Sluigi{
4698943Sluigi    msgDebug("\007Signal %d caught!  That's bad!\n", sig);
4798943Sluigi    longjmp(BailOut, sig);
4826359Sjulian}
4926359Sjulian
5098943Sluigiint
5198943Sluigimain(int argc, char **argv)
5298943Sluigi{
5398943Sluigi    int choice, scroll, curr, max, status;
5498943Sluigi
5598943Sluigi    /* Record name to be able to restart */
5698943Sluigi    StartName = argv[0];
5798943Sluigi
5898943Sluigi    /* Catch fatal signals and complain about them if running as init */
5998943Sluigi    if (getpid() == 1) {
6098943Sluigi	signal(SIGBUS, screech);
6198943Sluigi	signal(SIGSEGV, screech);
6298943Sluigi    }
6398943Sluigi    signal(SIGPIPE, SIG_IGN);
6498943Sluigi
6598943Sluigi    /* We don't work too well when running as non-root anymore */
6698943Sluigi    if (geteuid() != 0) {
6798943Sluigi	fprintf(stderr, "Error: This utility should only be run as root.\n");
6898943Sluigi	return 1;
6998943Sluigi    }
7098943Sluigi
7198943Sluigi#ifdef PC98
7298943Sluigi    {
7398943Sluigi	/* XXX */
7498943Sluigi	char *p = getenv("TERM");
7598943Sluigi	if (p && strcmp(p, "cons25") == 0)
7698943Sluigi	    putenv("TERM=cons25w");
7798943Sluigi    }
7898943Sluigi#endif
7998943Sluigi
8098943Sluigi    /* Set up whatever things need setting up */
8198943Sluigi    systemInitialize(argc, argv);
8298943Sluigi
8399475Sluigi    /* Set default flag and variable values */
8498943Sluigi    installVarDefaults(NULL);
8598943Sluigi    /* only when multi-user is it reasonable to do this here */
8698943Sluigi    if (!RunningAsInit)
8798943Sluigi	installEnvironment();
8898943Sluigi
8998943Sluigi    if (argc > 1 && !strcmp(argv[1], "-fake")) {
9098943Sluigi	variable_set2(VAR_DEBUG, "YES", 0);
9198943Sluigi	Fake = TRUE;
9298943Sluigi	msgConfirm("I'll be just faking it from here on out, OK?");
9398943Sluigi    }
9498943Sluigi
9598943Sluigi    /* Try to preserve our scroll-back buffer */
9698943Sluigi    if (OnVTY) {
97112250Scjc	for (curr = 0; curr < 25; curr++)
98112250Scjc	    putchar('\n');
9998943Sluigi    }
10098943Sluigi    /* Move stderr aside */
10198943Sluigi    if (DebugFD)
10298943Sluigi	dup2(DebugFD, 2);
10398943Sluigi
10498943Sluigi    /* Initialize PC-card */
10598943Sluigi    pccardInitialize();
10698943Sluigi
10798943Sluigi    /* Initialize USB */
10898943Sluigi    usbInitialize();
10998943Sluigi
11098943Sluigi    /* Probe for all relevant devices on the system */
11198943Sluigi    deviceGetAll();
11298943Sluigi
11398943Sluigi    /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
11498943Sluigi    if (!RunningAsInit) {
11598943Sluigi	int i, start_arg;
11698943Sluigi
11798943Sluigi	if (!strstr(argv[0], "sysinstall"))
11898943Sluigi	    start_arg = 0;
11998943Sluigi	else if (Fake)
12098943Sluigi	    start_arg = 2;
12198943Sluigi	else
12298943Sluigi	    start_arg = 1;
12326359Sjulian	for (i = start_arg; i < argc; i++) {
12426359Sjulian	    if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS)
12526359Sjulian		systemShutdown(1);
12698943Sluigi	}
1273969Sjkh	if (argc > start_arg)
12898943Sluigi	    systemShutdown(0);
12998943Sluigi    }
13098943Sluigi    else
13198943Sluigi	dispatch_load_file_int(TRUE);
13298943Sluigi
13398943Sluigi    status = setjmp(BailOut);
13498943Sluigi    if (status) {
13598943Sluigi	msgConfirm("A signal %d was caught - I'm saving what I can and shutting\n"
13698943Sluigi		   "If you can reproduce the problem, please turn Debug on in\n"
13798943Sluigi		   "the Options menu for the extra information it provides in\n"
13898943Sluigi		   "debugging problems like this.", status);
13998943Sluigi	systemShutdown(status);
14098943Sluigi    }
14198943Sluigi
14298943Sluigi    /* Begin user dialog at outer menu */
14398943Sluigi    dialog_clear();
14498943Sluigi    while (1) {
14598943Sluigi	choice = scroll = curr = max = 0;
14698943Sluigi	dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE);
14798943Sluigi	if (getpid() != 1
14898943Sluigi#ifdef __alpha__
14998943Sluigi	    || !msgYesNo("Are you sure you wish to exit?  The system will halt.")
15098943Sluigi#else
1513969Sjkh	    || !msgYesNo("Are you sure you wish to exit?  The system will reboot\n"
15298943Sluigi		         "(be sure to remove any floppies/CDROMs from the drives).")
15398943Sluigi#endif
15498943Sluigi	    )
15598943Sluigi	    break;
15698943Sluigi    }
15798943Sluigi
15898943Sluigi    /* Say goodnight, Gracie */
1593969Sjkh    systemShutdown(0);
16098943Sluigi
16198943Sluigi    return 0; /* We should never get here */
16298943Sluigi}
16396245Sluigi