1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* The new sysinstall program.
*
* This is probably the last program in the `sysinstall' line - the next
* generation being essentially a complete rewrite.
*
* $Id: system.c,v 1.35 1995/05/26 08:41:49 jkh Exp $
*
* Jordan Hubbard
*
* My contributions are in the public domain.
*
* Parts of this file are also blatently stolen from Poul-Henning Kamp's
* previous version of sysinstall, and as such fall under his "BEERWARE license"
* so buy him a beer if you like it! Buy him a beer for me, too!
* Heck, get him completely drunk and send me pictures! :-)
*/
#include "sysinstall.h"
#include <signal.h>
#include <sys/reboot.h>
#include <machine/console.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
/*
* Handle interrupt signals - this probably won't work in all cases
* due to our having bogotified the internal state of dialog or curses,
* but we'll give it a try.
*/
static void
handle_intr(int sig)
{
if (!msgYesNo("Are you sure you want to abort the installation?"))
systemShutdown();
}
/* Welcome the user to the system */
void
systemWelcome(void)
{
printf("Installation system initializing..\n");
}
/* Initialize system defaults */
void
systemInitialize(int argc, char **argv)
{
int i;
signal(SIGINT, SIG_IGN);
globalsInit();
/* Are we running as init? */
if (getpid() == 1) {
setsid();
if (argc > 1 && strchr(argv[1],'C')) {
/* Kernel told us that we are on a CDROM root */
close(0); open("/bootcd/dev/console", O_RDWR);
close(1); dup(0);
close(2); dup(0);
OnCDROM = TRUE;
chroot("/bootcd");
} else {
close(0); open("/dev/console", O_RDWR);
close(1); dup(0);
close(2); dup(0);
}
printf("%s running as init\n", argv[0]);
i = ioctl(0, TIOCSCTTY, (char *)NULL);
setlogin("root");
setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin", 1);
setbuf(stdin, 0);
setbuf(stderr, 0);
}
for(i = 0; i < 256; i++)
default_scrnmap[i] = i;
if (set_termcap() == -1) {
printf("Can't find terminal entry\n");
exit(-1);
}
/* XXX - libdialog has particularly bad return value checking */
init_dialog();
/* If we haven't crashed I guess dialog is running ! */
DialogActive = TRUE;
signal(SIGINT, handle_intr);
}
/* Close down and prepare to exit */
void
systemShutdown(void)
{
if (DialogActive) {
end_dialog();
DialogActive = FALSE;
}
/* REALLY exit! */
if (getpid() == 1)
reboot(RB_HALT);
else
exit(1);
}
/* Run some general command */
int
systemExecute(char *command)
{
int status;
dialog_clear();
dialog_update();
end_dialog();
DialogActive = FALSE;
status = system(command);
DialogActive = TRUE;
dialog_clear();
dialog_update();
return status;
}
/* Find and execute a shell */
int
systemShellEscape(void)
{
char *sh = NULL;
if (file_executable("/bin/sh"))
sh = "/bin/sh";
else if (file_executable("/stand/sh"))
sh = "/stand/sh";
else {
msgWarn("No shell available, sorry!");
return 1;
}
setenv("PS1", "freebsd% ", 1);
dialog_clear();
dialog_update();
move(0, 0);
standout();
addstr("Type `exit' to leave this shell and continue installation");
standend();
refresh();
end_dialog();
DialogActive = FALSE;
if (fork() == 0)
execlp(sh, "-sh", 0);
else
wait(NULL);
dialog_clear();
DialogActive = TRUE;
return 0;
}
/* Display a file in a filebox */
int
systemDisplayFile(char *file)
{
char *fname = NULL;
char buf[FILENAME_MAX];
WINDOW *w;
fname = systemHelpFile(file, buf);
if (!fname) {
snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
use_helpfile(NULL);
use_helpline(NULL);
w = dupwin(newscr);
dialog_mesgbox("Sorry!", buf, -1, -1);
touchwin(w);
wrefresh(w);
delwin(w);
return 1;
}
else {
use_helpfile(NULL);
use_helpline(NULL);
w = dupwin(newscr);
dialog_textbox(file, fname, LINES, COLS);
touchwin(w);
wrefresh(w);
delwin(w);
}
return 0;
}
char *
systemHelpFile(char *file, char *buf)
{
char *cp, *fname = NULL;
if (!file)
return NULL;
if ((cp = getenv("LANG")) != NULL) {
snprintf(buf, FILENAME_MAX, "help/%s/%s", cp, file);
if (file_readable(buf))
fname = buf;
else {
snprintf(buf, FILENAME_MAX, "/stand/help/%s/%s", cp, file);
if (file_readable(buf))
fname = buf;
}
}
if (!fname) {
snprintf(buf, FILENAME_MAX, "help/en_US.ISO8859-1/%s", file);
if (file_readable(buf))
fname = buf;
else {
snprintf(buf, FILENAME_MAX, "/stand/help/en_US.ISO8859-1/%s",
file);
if (file_readable(buf))
fname = buf;
}
}
return fname;
}
void
systemChangeFont(const u_char font[])
{
if (OnVTY && ColorDisplay) {
if (ioctl(0, PIO_FONT8x16, font) < 0)
msgConfirm("Sorry! Unable to load font for %s", getenv("LANG"));
}
dialog_clear();
}
void
systemChangeLang(char *lang)
{
variable_set2("LANG", lang);
}
void
systemChangeTerminal(char *color, const u_char c_term[],
char *mono, const u_char m_term[])
{
extern void init_acs(void);
if (OnVTY) {
if (ColorDisplay) {
setenv("TERM", color, 1);
setenv("TERMCAP", c_term, 1);
reset_shell_mode();
setterm(color);
init_acs();
cbreak(); noecho();
}
else {
setenv("TERM", mono, 1);
setenv("TERMCAP", m_term, 1);
reset_shell_mode();
setterm(mono);
init_acs();
cbreak(); noecho();
}
dialog_clear();
}
}
void
systemChangeScreenmap(const u_char newmap[])
{
if (OnVTY) {
if (ioctl(0, PIO_SCRNMAP, newmap) < 0)
msgConfirm("Sorry! Unable to load the screenmap for %s",
getenv("LANG"));
}
dialog_clear();
}
int
vsystem(char *fmt, ...)
{
va_list args;
int pstat;
pid_t pid;
int omask;
sig_t intsave, quitsave;
char *cmd,*p;
int i,magic=0;
cmd = (char *)malloc(FILENAME_MAX);
cmd[0] = '\0';
va_start(args, fmt);
vsnprintf(cmd, FILENAME_MAX, fmt, args);
va_end(args);
/* Find out if this command needs the wizardry of the shell */
for (p="<>|'`=\"()" ; *p; p++)
if (strchr(cmd,*p)) magic++;
omask = sigblock(sigmask(SIGCHLD));
msgDebug("Executing command `%s' (Magic=%d)\n", cmd, magic);
switch(pid = fork()) {
case -1: /* error */
(void)sigsetmask(omask);
i = 127;
case 0: /* child */
(void)sigsetmask(omask);
if (DebugFD != -1) {
if (OnVTY)
msgInfo("Command output is on debugging screen - type ALT-F2 to see it");
dup2(DebugFD, 0);
dup2(DebugFD, 1);
dup2(DebugFD, 2);
}
#ifdef CRUNCHED_BINARY
if (magic) {
char *argv[100];
i = 0;
argv[i++] = "crunch";
argv[i++] = "sh";
argv[i++] = "-c";
argv[i++] = cmd;
argv[i] = 0;
exit(crunched_main(i,argv));
} else {
char *argv[100];
i = 0;
argv[i++] = "crunch";
while (cmd && *cmd) {
argv[i] = strsep(&cmd," \t");
if (*argv[i])
i++;
}
argv[i] = 0;
if (crunched_here(argv[1]))
exit(crunched_main(i,argv));
else
execvp(argv[1],argv+1);
kill(getpid(),9);
}
#else /* !CRUNCHED_BINARY */
execl("/stand/sh", "sh", "-c", cmd, (char *)NULL);
kill(getpid(),9);
#endif /* CRUNCHED_BINARY */
}
intsave = signal(SIGINT, SIG_IGN);
quitsave = signal(SIGQUIT, SIG_IGN);
pid = waitpid(pid, &pstat, 0);
(void)sigsetmask(omask);
(void)signal(SIGINT, intsave);
(void)signal(SIGQUIT, quitsave);
i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
msgDebug("Command `%s' returns status of %d\n", cmd, i);
free(cmd);
return i;
}
|