]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sysinstall/system.c
This commit was generated by cvs2svn to compensate for changes in r175261,
[FreeBSD/FreeBSD.git] / usr.sbin / sysinstall / system.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last program in the `sysinstall' line - the next
5  * generation being essentially a complete rewrite.
6  *
7  * $FreeBSD$
8  *
9  * Jordan Hubbard
10  *
11  * My contributions are in the public domain.
12  *
13  * Parts of this file are also blatantly stolen from Poul-Henning Kamp's
14  * previous version of sysinstall, and as such fall under his "BEERWARE license"
15  * so buy him a beer if you like it!  Buy him a beer for me, too!
16  * Heck, get him completely drunk and send me pictures! :-)
17  */
18
19 #include "sysinstall.h"
20 #include <signal.h>
21 #include <termios.h>
22 #include <sys/param.h>
23 #include <sys/reboot.h>
24 #include <sys/consio.h>
25 #include <sys/fcntl.h>
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
28 #include <sys/stat.h>
29 #include <sys/sysctl.h>
30 #include <ufs/ufs/ufsmount.h>
31
32
33 /* Where we stick our temporary expanded doc file */
34 #define DOC_TMP_DIR     "/tmp/.doc"
35 #define DOC_TMP_FILE    "/tmp/.doc/doc.tmp"
36
37 static pid_t ehs_pid;
38
39 /*
40  * Handle interrupt signals - this probably won't work in all cases
41  * due to our having bogotified the internal state of dialog or curses,
42  * but we'll give it a try.
43  */
44 static int
45 intr_continue(dialogMenuItem *self)
46 {
47     return DITEM_LEAVE_MENU;
48 }
49
50 static int
51 intr_reboot(dialogMenuItem *self)
52 {
53     systemShutdown(-1);
54     /* NOTREACHED */
55     return 0;
56 }
57
58 static int
59 intr_restart(dialogMenuItem *self)
60 {
61     int ret, fd, fdmax;
62
63     mediaClose();
64     free_variables();
65     fdmax = getdtablesize();
66     for (fd = 3; fd < fdmax; fd++)
67         close(fd);
68     ret = execl(StartName, StartName, "-restart", (char *)NULL);
69     msgDebug("execl failed (%s)\n", strerror(errno));
70     /* NOTREACHED */
71     return -1;
72 }
73
74 static dialogMenuItem intrmenu[] = {
75     { "Abort",   "Abort the installation", NULL, intr_reboot },
76     { "Restart", "Restart the installation program", NULL, intr_restart },
77     { "Continue", "Continue the installation", NULL, intr_continue },
78 };
79
80
81 static void
82 handle_intr(int sig)
83 {
84     WINDOW *save = savescr();
85
86     use_helpline(NULL);
87     use_helpfile(NULL);
88     if (OnVTY) {
89         ioctl(0, VT_ACTIVATE, 1);       /* Switch back */
90         msgInfo(NULL);
91     }
92     (void)dialog_menu("Installation interrupt",
93                      "Do you want to abort the installation?",
94                      -1, -1, 3, -3, intrmenu, NULL, NULL, NULL);
95     restorescr(save);
96 }
97
98 #if 0
99 /*
100  * Harvest children if we are init.
101  */
102 static void
103 reap_children(int sig)
104 {
105     int errbak = errno;
106
107     while (waitpid(-1, NULL, WNOHANG) > 0)
108         ;
109     errno = errbak;
110 }
111 #endif
112
113 /* Expand a file into a convenient location, nuking it each time */
114 static char *
115 expand(char *fname)
116 {
117     char *unzipper = RunningAsInit ? "/stand/" UNZIPPER : "/usr/bin/" UNZIPPER;
118
119     if (!directory_exists(DOC_TMP_DIR)) {
120         Mkdir(DOC_TMP_DIR);
121         if (chown(DOC_TMP_DIR, 0, 0) < 0)
122             return NULL;
123         if (chmod(DOC_TMP_DIR, S_IRWXU) < 0)
124             return NULL;
125     }
126     else
127         unlink(DOC_TMP_FILE);
128     if (!file_readable(fname) || vsystem("%s < %s > %s", unzipper, fname,
129         DOC_TMP_FILE))
130         return NULL;
131     return DOC_TMP_FILE;
132 }
133
134 /* Initialize system defaults */
135 void
136 systemInitialize(int argc, char **argv)
137 {
138     size_t i;
139     int boothowto;
140     sigset_t signalset;
141
142     signal(SIGINT, SIG_IGN);
143     globalsInit();
144
145     i = sizeof(boothowto);
146     if (!sysctlbyname("debug.boothowto", &boothowto, &i, NULL, 0) &&
147         (i == sizeof(boothowto)) && (boothowto & RB_VERBOSE))
148         variable_set2(VAR_DEBUG, "YES", 0);
149
150     /* Are we running as init? */
151     if (getpid() == 1) {
152         struct ufs_args ufs_args;
153         int fd;
154
155         RunningAsInit = 1;
156         setsid();
157         close(0);
158         fd = open("/dev/ttyv0", O_RDWR);
159         if (fd == -1) {
160             fd = open("/dev/console", O_RDWR);  /* fallback */
161             variable_set2(VAR_FIXIT_TTY, "serial", 0); /* give fixit a hint */
162         } else
163             OnVTY = TRUE;
164         /*
165          * To make _sure_ we're on a VTY and don't have /dev/console switched
166          * away to a serial port or something, attempt to set the cursor appearance.
167          */
168         if (OnVTY) {
169             int fd2, type;
170
171             type = 0;   /* normal */
172             if ((fd2 = open("/dev/console", O_RDWR)) != -1) {
173                 if (ioctl(fd2, CONS_CURSORTYPE, &type) == -1) {
174                     OnVTY = FALSE;
175                     variable_set2(VAR_FIXIT_TTY, "serial", 0); /* Tell Fixit
176                                                                   the console
177                                                                   type */
178                     close(fd); close(fd2);
179                     open("/dev/console", O_RDWR);
180                 }
181                 else
182                     close(fd2);
183             }
184         }
185         close(1); dup(0);
186         close(2); dup(0);
187         printf("%s running as init on %s\n", argv[0], OnVTY ? "vty0" : "serial console");
188         ioctl(0, TIOCSCTTY, (char *)NULL);
189         setlogin("root");
190         setenv("PATH", "/stand:/bin:/sbin:/usr/sbin:/usr/bin:/mnt/bin:/mnt/sbin:/mnt/usr/sbin:/mnt/usr/bin:/usr/X11R6/bin", 1);
191         setbuf(stdin, 0);
192         setbuf(stderr, 0);
193 #ifdef __alpha__
194         i = 0;
195         sysctlbyname("machdep.unaligned_print", NULL, 0, &i, sizeof(i));
196 #endif
197 #if 0
198         signal(SIGCHLD, reap_children);
199 #endif
200         memset(&ufs_args, 0, sizeof(ufs_args));
201         mount("ufs", "/", MNT_UPDATE, &ufs_args);
202     }
203     else {
204         char hname[256];
205
206         /* Initalize various things for a multi-user environment */
207         if (!gethostname(hname, sizeof hname))
208             variable_set2(VAR_HOSTNAME, hname, 0);
209     }
210
211     if (set_termcap() == -1) {
212         printf("Can't find terminal entry\n");
213         exit(-1);
214     }
215
216     /* XXX - libdialog has particularly bad return value checking */
217     init_dialog();
218
219     /* If we haven't crashed I guess dialog is running ! */
220     DialogActive = TRUE;
221
222     /* Make sure HOME is set for those utilities that need it */
223     if (!getenv("HOME"))
224         setenv("HOME", "/", 1);
225     signal(SIGINT, handle_intr);
226     /*
227      * Make sure we can be interrupted even if we were re-executed
228      * from an interrupt.
229      */
230     sigemptyset(&signalset);
231     sigaddset(&signalset, SIGINT);
232     sigprocmask(SIG_UNBLOCK, &signalset, NULL);
233
234     (void)vsystem("rm -rf %s", DOC_TMP_DIR);
235 }
236
237 /* Close down and prepare to exit */
238 void
239 systemShutdown(int status)
240 {
241     /* If some media is open, close it down */
242     if (status >=0)
243         mediaClose();
244
245     /* write out any changes to rc.conf .. */
246     configRC_conf();
247
248     /* Shut down the dialog library */
249     if (DialogActive) {
250         end_dialog();
251         DialogActive = FALSE;
252     }
253
254     /* Shut down curses */
255     endwin();
256
257     /* If we have a temporary doc dir lying around, nuke it */
258     (void)vsystem("rm -rf %s", DOC_TMP_DIR);
259
260     /* REALLY exit! */
261     if (RunningAsInit) {
262         /* Put the console back */
263         ioctl(0, VT_ACTIVATE, 2);
264 #if defined(__alpha__) || defined(__sparc64__)
265         reboot(RB_HALT);
266 #else
267         reboot(RB_AUTOBOOT);
268 #endif
269     }
270     else
271         exit(status);
272 }
273
274 /* Run some general command */
275 int
276 systemExecute(char *command)
277 {
278     int status;
279     struct termios foo;
280     WINDOW *w = savescr();
281
282     dialog_clear();
283     dialog_update();
284     end_dialog();
285     DialogActive = FALSE;
286     if (tcgetattr(0, &foo) != -1) {
287         foo.c_cc[VERASE] = '\010';
288         tcsetattr(0, TCSANOW, &foo);
289     }
290     if (!Fake)
291         status = system(command);
292     else {
293         status = 0;
294         msgDebug("systemExecute:  Faked execution of `%s'\n", command);
295     }
296     DialogActive = TRUE;
297     restorescr(w);
298     return status;
299 }
300
301 /* suspend/resume libdialog/curses screen */
302 static    WINDOW *oldW;
303
304 void
305 systemSuspendDialog(void)
306 {
307
308     oldW  = savescr();
309     dialog_clear();
310     dialog_update();
311     end_dialog();
312     DialogActive = FALSE;
313 }
314
315 void
316 systemResumeDialog(void)
317 {
318
319     DialogActive = TRUE;
320     restorescr(oldW);
321 }
322
323 /* Display a help file in a filebox */
324 int
325 systemDisplayHelp(char *file)
326 {
327     char *fname = NULL;
328     char buf[FILENAME_MAX];
329     int ret = 0;
330     WINDOW *w = savescr();
331     
332     fname = systemHelpFile(file, buf);
333     if (!fname) {
334         snprintf(buf, FILENAME_MAX, "The %s file is not provided on this particular floppy image.", file);
335         use_helpfile(NULL);
336         use_helpline(NULL);
337         dialog_mesgbox("Sorry!", buf, -1, -1);
338         ret = 1;
339     }
340     else {
341         use_helpfile(NULL);
342         use_helpline(NULL);
343         dialog_textbox(file, fname, LINES, COLS);
344     }
345     restorescr(w);
346     return ret;
347 }
348
349 char *
350 systemHelpFile(char *file, char *buf)
351 {
352     if (!file)
353         return NULL;
354     if (file[0] == '/')
355         return file;
356     snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp.gz", file);
357     if (file_readable(buf)) 
358         return expand(buf);
359     snprintf(buf, FILENAME_MAX, "/stand/help/%s.hlp", file);
360     if (file_readable(buf)) 
361         return expand(buf);
362     snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT.gz", file);
363     if (file_readable(buf)) 
364         return expand(buf);
365     snprintf(buf, FILENAME_MAX, "/stand/help/%s.TXT", file);
366     if (file_readable(buf)) 
367         return expand(buf);
368     snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.hlp", file);
369     if (file_readable(buf))
370         return buf;
371     snprintf(buf, FILENAME_MAX, "/usr/src/usr.sbin/sysinstall/help/%s.TXT", file);
372     if (file_readable(buf))
373         return buf;
374     return NULL;
375 }
376
377 void
378 systemChangeTerminal(char *color, const u_char c_term[],
379                      char *mono, const u_char m_term[])
380 {
381     if (OnVTY) {
382         int setupterm(char *color, int, int *);
383
384         if (ColorDisplay) {
385             setenv("TERM", color, 1);
386             setenv("TERMCAP", c_term, 1);
387             reset_shell_mode();
388             setterm(color);
389             cbreak(); noecho();
390         }
391         else {
392             setenv("TERM", mono, 1);
393             setenv("TERMCAP", m_term, 1);
394             reset_shell_mode();
395             setterm(mono);
396             cbreak(); noecho();
397         }
398     }
399     clear();
400     refresh();
401     dialog_clear();
402 }
403
404 int
405 vsystem(char *fmt, ...)
406 {
407     va_list args;
408     int pstat;
409     pid_t pid;
410     int omask;
411     sig_t intsave, quitsave;
412     char *cmd;
413     int i;
414     struct stat sb;
415
416     cmd = (char *)alloca(FILENAME_MAX);
417     cmd[0] = '\0';
418     va_start(args, fmt);
419     vsnprintf(cmd, FILENAME_MAX, fmt, args);
420     va_end(args);
421
422     omask = sigblock(sigmask(SIGCHLD));
423     if (Fake) {
424         msgDebug("vsystem:  Faked execution of `%s'\n", cmd);
425         return 0;
426     }
427     if (isDebug())
428         msgDebug("Executing command `%s'\n", cmd);
429     pid = fork();
430     if (pid == -1) {
431         (void)sigsetmask(omask);
432         i = 127;
433     }
434     else if (!pid) {    /* Junior */
435         (void)sigsetmask(omask);
436         if (DebugFD != -1) {
437             dup2(DebugFD, 0);
438             dup2(DebugFD, 1);
439             dup2(DebugFD, 2);
440         }
441         else {
442             close(1); open("/dev/null", O_WRONLY);
443             dup2(1, 2);
444         }
445         if (stat("/stand/sh", &sb) == 0)
446             execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL);
447         else
448             execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
449         exit(1);
450     }
451     else {
452         intsave = signal(SIGINT, SIG_IGN);
453         quitsave = signal(SIGQUIT, SIG_IGN);
454         pid = waitpid(pid, &pstat, 0);
455         (void)sigsetmask(omask);
456         (void)signal(SIGINT, intsave);
457         (void)signal(SIGQUIT, quitsave);
458         i = (pid == -1) ? -1 : WEXITSTATUS(pstat);
459         if (isDebug())
460             msgDebug("Command `%s' returns status of %d\n", cmd, i);
461     }
462     return i;
463 }
464
465 void
466 systemCreateHoloshell(void)
467 {
468     int waitstatus;
469
470     if ((FixItMode || OnVTY) && RunningAsInit) {
471
472         if (ehs_pid != 0) {
473             int pstat;
474
475             if (kill(ehs_pid, 0) == 0) {
476
477                 if (msgNoYes("There seems to be an emergency holographic shell\n"
478                              "already running on VTY 4.\n\n"
479                              "Kill it and start a new one?"))
480                     return;
481
482                 /* try cleaning up as much as possible */
483                 (void) kill(ehs_pid, SIGHUP);
484                 sleep(1);
485                 (void) kill(ehs_pid, SIGKILL);
486             }
487
488             /* avoid too many zombies */
489             (void) waitpid(ehs_pid, &pstat, WNOHANG);
490         }
491
492         if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) 
493             systemSuspendDialog();      /* must be before the fork() */
494         if ((ehs_pid = fork()) == 0) {
495             int i, fd;
496             struct termios foo;
497             extern int login_tty(int);
498             
499             ioctl(0, TIOCNOTTY, NULL);
500             for (i = getdtablesize(); i >= 0; --i)
501                 close(i);
502             if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) 
503                 fd = open("/dev/console", O_RDWR);
504             else
505                 fd = open("/dev/ttyv3", O_RDWR);
506             ioctl(0, TIOCSCTTY, &fd);
507             dup2(0, 1);
508             dup2(0, 2);
509             DebugFD = 2;
510             if (login_tty(fd) == -1)
511                 msgDebug("Doctor: I can't set the controlling terminal.\n");
512             signal(SIGTTOU, SIG_IGN);
513             if (tcgetattr(fd, &foo) != -1) {
514                 foo.c_cc[VERASE] = '\010';
515                 if (tcsetattr(fd, TCSANOW, &foo) == -1)
516                     msgDebug("Doctor: I'm unable to set the erase character.\n");
517             }
518             else
519                 msgDebug("Doctor: I'm unable to get the terminal attributes!\n");
520             if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
521                 printf("Type ``exit'' in this fixit shell to resume sysinstall.\n\n");
522                 fflush(stdout);
523             }
524             execlp("sh", "-sh", 0);
525             msgDebug("Was unable to execute sh for Holographic shell!\n");
526             exit(1);
527         }
528         else {
529             if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
530                 WINDOW *w = savescr();
531
532                 msgNotify("Starting an emergency holographic shell on VTY4");
533                 sleep(2);
534                 restorescr(w);
535             }
536             else {
537                 (void)waitpid(ehs_pid, &waitstatus, 0); /* we only wait for
538                                                            shell to finish 
539                                                            in serial mode
540                                                            since there is no
541                                                            virtual console */
542                 systemResumeDialog();
543             }
544         }
545     }
546 }