]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/sysinstall/main.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / sysinstall / main.c
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$
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 #include <sys/time.h>
41 #include <sys/resource.h>
42
43 const char *StartName;          /* Initial contents of argv[0] */
44 const char *ProgName = "sysinstall";
45
46 static void
47 screech(int sig)
48 {
49     msgDebug("\007Signal %d caught!  That's bad!\n", sig);
50     longjmp(BailOut, sig);
51 }
52
53 int
54 main(int argc, char **argv)
55 {
56     int choice, scroll, curr, max, status;
57     char titlestr[80], *arch, *osrel, *ostype;
58     struct rlimit rlim;
59     char *arg;
60     int i;
61     int optionArgs = 0;
62
63     /* Record name to be able to restart */
64     StartName = argv[0];
65
66     Restarting = FALSE;
67     RunningAsInit = FALSE;
68     Fake = FALSE;
69
70     for (i = 1; i < argc; i++) {
71         arg = argv[i];
72
73         if (arg[0] != '-')
74                 break;
75
76         optionArgs++;
77
78         if (!strcmp(arg, "-fake")) {
79                 variable_set2(VAR_DEBUG, "YES", 0);
80                 Fake = TRUE;
81         } else if (!strcmp(arg, "-restart")) {
82                 Restarting = TRUE;
83         } else if (!strcmp(arg, "-fakeInit")) {
84                 RunningAsInit = TRUE;
85         }
86         
87         arg = argv[optionArgs+1];
88     }
89
90     if (getpid() == 1)
91             RunningAsInit = TRUE;
92    
93     /* Catch fatal signals and complain about them if running as init */
94     if (RunningAsInit) {
95         signal(SIGBUS, screech);
96         signal(SIGSEGV, screech);
97     }
98     signal(SIGPIPE, SIG_IGN);
99
100     /* We don't work too well when running as non-root anymore */
101     if (geteuid() != 0) {
102         fprintf(stderr, "Error: This utility should only be run as root.\n");
103         return 1;
104     }
105
106     /*
107      * Given what it does sysinstall (and stuff sysinstall runs like
108      * pkg_add) shouldn't be subject to process limits.  Better to just
109      * let them have what they think they need than have them blow
110      * their brains out during an install (in sometimes strange and
111      * mysterious ways).
112      */
113
114     rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
115     if (setrlimit(RLIMIT_DATA, &rlim) != 0)
116         fprintf(stderr, "Warning: setrlimit() of datasize failed.\n");
117     if (setrlimit(RLIMIT_STACK, &rlim) != 0)
118         fprintf(stderr, "Warning: setrlimit() of stacksize failed.\n");
119
120 #ifdef PC98
121     {
122         /* XXX */
123         char *p = getenv("TERM");
124         if (p && strcmp(p, "cons25") == 0)
125             setenv("TERM", "cons25w", 1);
126     }
127 #endif
128
129     /* Set up whatever things need setting up */
130     systemInitialize(argc, argv);
131
132     /* Set default flag and variable values */
133     installVarDefaults(NULL);
134     /* only when multi-user is it reasonable to do this here */
135     if (!RunningAsInit)
136         installEnvironment();
137
138     if (Fake)
139         msgConfirm("I'll be just faking it from here on out, OK?");
140
141     /* Try to preserve our scroll-back buffer */
142     if (OnVTY) {
143         for (curr = 0; curr < 25; curr++)
144             putchar('\n');
145     }
146     /* Move stderr aside */
147     if (DebugFD)
148         dup2(DebugFD, 2);
149
150     /* Initialize driver modules, if we haven't already done so (ie,
151        the user hit Ctrl-C -> Restart. */
152     if (!pvariable_get("modulesInitialize")) {
153         moduleInitialize();
154         pvariable_set("modulesInitialize=1");
155     }
156
157     /* Probe for all relevant devices on the system */
158     deviceGetAll();
159
160     /* Prompt for the driver floppy if appropriate. */
161     if (!pvariable_get("driverFloppyCheck")) {
162         driverFloppyCheck();
163         pvariable_set("driverFloppyCheck=1");
164     }
165
166     /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
167     if (!RunningAsInit) {
168         int start_arg;
169
170         if (!strstr(argv[0], "sysinstall"))
171                 start_arg = 0;
172         else
173                 start_arg = optionArgs + 1;
174
175         for (i = start_arg; i < argc; i++) {
176             if (DITEM_STATUS(dispatchCommand(argv[i])) != DITEM_SUCCESS)
177                 systemShutdown(1);
178         }
179
180         /* If we were given commands to process on the command line, just exit
181          * now */
182         if (argc > start_arg)
183             systemShutdown(0);
184     }
185     else
186         dispatch_load_file_int(TRUE);
187
188     status = setjmp(BailOut);
189     if (status) {
190         msgConfirm("A signal %d was caught - I'm saving what I can and shutting\n"
191                    "down.  If you can reproduce the problem, please turn Debug on\n"
192                    "in the Options menu for the extra information it provides\n"
193                    "in debugging problems like this.", status);
194         systemShutdown(status);
195     }
196
197     /* Get user's country and keymap */
198     if (RunningAsInit)
199         configCountry(NULL);
200
201     /* Add FreeBSD version info to the menu title */
202     arch = getsysctlbyname("hw.machine_arch");
203     osrel = getsysctlbyname("kern.osrelease");
204     ostype = getsysctlbyname("kern.ostype");
205     snprintf(titlestr, sizeof(titlestr), "%s/%s %s - %s", ostype, arch,
206              osrel, MenuInitial.title);
207     free(arch);
208     free(osrel);
209     free(ostype);
210     MenuInitial.title = titlestr;
211
212     /* Begin user dialog at outer menu */
213     dialog_clear();
214     while (1) {
215         choice = scroll = curr = max = 0;
216         dmenuOpen(&MenuInitial, &choice, &scroll, &curr, &max, TRUE);
217         if (!RunningAsInit
218 #if defined(__sparc64__)
219             || !msgNoYes("Are you sure you wish to exit?  The system will halt.")
220 #else
221             || !msgNoYes("Are you sure you wish to exit?  The system will reboot.")
222 #endif
223             )
224             break;
225     }
226
227     /* Say goodnight, Gracie */
228     systemShutdown(0);
229
230     return 0; /* We should never get here */
231 }