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