]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/init/init.c
ifconfig(8): wordsmith -G and -g descriptions
[FreeBSD/FreeBSD.git] / sbin / init / init.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Donn Seeley at Berkeley Software Design, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/boottrace.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/mount.h>
40 #include <sys/reboot.h>
41 #include <sys/stat.h>
42 #include <sys/sysctl.h>
43 #include <sys/uio.h>
44 #include <sys/wait.h>
45
46 #include <db.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <kenv.h>
51 #include <libutil.h>
52 #include <paths.h>
53 #include <signal.h>
54 #include <stdarg.h>
55 #include <stdbool.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <syslog.h>
60 #include <time.h>
61 #include <ttyent.h>
62 #include <unistd.h>
63
64 #ifdef SECURE
65 #include <pwd.h>
66 #endif
67
68 #ifdef LOGIN_CAP
69 #include <login_cap.h>
70 #endif
71
72 #include "mntopts.h"
73 #include "pathnames.h"
74
75 /*
76  * Sleep times; used to prevent thrashing.
77  */
78 #define GETTY_SPACING            5      /* N secs minimum getty spacing */
79 #define GETTY_SLEEP             30      /* sleep N secs after spacing problem */
80 #define GETTY_NSPACE             3      /* max. spacing count to bring reaction */
81 #define WINDOW_WAIT              3      /* wait N secs after starting window */
82 #define STALL_TIMEOUT           30      /* wait N secs after warning */
83 #define DEATH_WATCH             10      /* wait N secs for procs to die */
84 #define DEATH_SCRIPT            120     /* wait for 2min for /etc/rc.shutdown */
85 #define RESOURCE_RC             "daemon"
86 #define RESOURCE_WINDOW         "default"
87 #define RESOURCE_GETTY          "default"
88 #define SCRIPT_ARGV_SIZE 3 /* size of argv passed to execute_script, can be increased if needed */
89
90 static void handle(sig_t, ...);
91 static void delset(sigset_t *, ...);
92
93 static void stall(const char *, ...) __printflike(1, 2);
94 static void warning(const char *, ...) __printflike(1, 2);
95 static void emergency(const char *, ...) __printflike(1, 2);
96 static void disaster(int);
97 static void revoke_ttys(void);
98 static int  runshutdown(void);
99 static char *strk(char *);
100 static void runfinal(void);
101
102 /*
103  * We really need a recursive typedef...
104  * The following at least guarantees that the return type of (*state_t)()
105  * is sufficiently wide to hold a function pointer.
106  */
107 typedef long (*state_func_t)(void);
108 typedef state_func_t (*state_t)(void);
109
110 static state_func_t single_user(void);
111 static state_func_t runcom(void);
112 static state_func_t read_ttys(void);
113 static state_func_t multi_user(void);
114 static state_func_t clean_ttys(void);
115 static state_func_t catatonia(void);
116 static state_func_t death(void);
117 static state_func_t death_single(void);
118 static state_func_t reroot(void);
119 static state_func_t reroot_phase_two(void);
120
121 static state_func_t run_script(const char *);
122
123 static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
124
125 static bool Reboot = false;
126 static int howto = RB_AUTOBOOT;
127
128 static bool devfs = false;
129 static char *init_path_argv0;
130
131 static void transition(state_t);
132 static state_t requested_transition;
133 static state_t current_state = death_single;
134
135 static void execute_script(char *argv[]);
136 static void open_console(void);
137 static const char *get_shell(void);
138 static void replace_init(char *path);
139 static void write_stderr(const char *message);
140
141 typedef struct init_session {
142         pid_t   se_process;             /* controlling process */
143         time_t  se_started;             /* used to avoid thrashing */
144         int     se_flags;               /* status of session */
145 #define SE_SHUTDOWN     0x1             /* session won't be restarted */
146 #define SE_PRESENT      0x2             /* session is in /etc/ttys */
147 #define SE_IFEXISTS     0x4             /* session defined as "onifexists" */
148 #define SE_IFCONSOLE    0x8             /* session defined as "onifconsole" */
149         int     se_nspace;              /* spacing count */
150         char    *se_device;             /* filename of port */
151         char    *se_getty;              /* what to run on that port */
152         char    *se_getty_argv_space;   /* pre-parsed argument array space */
153         char    **se_getty_argv;        /* pre-parsed argument array */
154         char    *se_window;             /* window system (started only once) */
155         char    *se_window_argv_space;  /* pre-parsed argument array space */
156         char    **se_window_argv;       /* pre-parsed argument array */
157         char    *se_type;               /* default terminal type */
158         struct  init_session *se_prev;
159         struct  init_session *se_next;
160 } session_t;
161
162 static void free_session(session_t *);
163 static session_t *new_session(session_t *, struct ttyent *);
164 static session_t *sessions;
165
166 static char **construct_argv(char *);
167 static void start_window_system(session_t *);
168 static void collect_child(pid_t);
169 static pid_t start_getty(session_t *);
170 static void transition_handler(int);
171 static void alrm_handler(int);
172 static void setsecuritylevel(int);
173 static int getsecuritylevel(void);
174 static int setupargv(session_t *, struct ttyent *);
175 #ifdef LOGIN_CAP
176 static void setprocresources(const char *);
177 #endif
178 static bool clang;
179
180 static int start_session_db(void);
181 static void add_session(session_t *);
182 static void del_session(session_t *);
183 static session_t *find_session(pid_t);
184 static DB *session_db;
185
186 /*
187  * The mother of all processes.
188  */
189 int
190 main(int argc, char *argv[])
191 {
192         state_t initial_transition = runcom;
193         char kenv_value[PATH_MAX];
194         int c, error;
195         struct sigaction sa;
196         sigset_t mask;
197
198         /* Dispose of random users. */
199         if (getuid() != 0)
200                 errx(1, "%s", strerror(EPERM));
201
202         BOOTTRACE("init(8) starting...");
203
204         /* System V users like to reexec init. */
205         if (getpid() != 1) {
206 #ifdef COMPAT_SYSV_INIT
207                 /* So give them what they want */
208                 if (argc > 1) {
209                         if (strlen(argv[1]) == 1) {
210                                 char runlevel = *argv[1];
211                                 int sig;
212
213                                 switch (runlevel) {
214                                 case '0': /* halt + poweroff */
215                                         sig = SIGUSR2;
216                                         break;
217                                 case '1': /* single-user */
218                                         sig = SIGTERM;
219                                         break;
220                                 case '6': /* reboot */
221                                         sig = SIGINT;
222                                         break;
223                                 case 'c': /* block further logins */
224                                         sig = SIGTSTP;
225                                         break;
226                                 case 'q': /* rescan /etc/ttys */
227                                         sig = SIGHUP;
228                                         break;
229                                 case 'r': /* remount root */
230                                         sig = SIGEMT;
231                                         break;
232                                 default:
233                                         goto invalid;
234                                 }
235                                 kill(1, sig);
236                                 _exit(0);
237                         } else
238 invalid:
239                                 errx(1, "invalid run-level ``%s''", argv[1]);
240                 } else
241 #endif
242                         errx(1, "already running");
243         }
244
245         init_path_argv0 = strdup(argv[0]);
246         if (init_path_argv0 == NULL)
247                 err(1, "strdup");
248
249         /*
250          * Note that this does NOT open a file...
251          * Does 'init' deserve its own facility number?
252          */
253         openlog("init", LOG_CONS, LOG_AUTH);
254
255         /*
256          * Create an initial session.
257          */
258         if (setsid() < 0 && (errno != EPERM || getsid(0) != 1))
259                 warning("initial setsid() failed: %m");
260
261         /*
262          * Establish an initial user so that programs running
263          * single user do not freak out and die (like passwd).
264          */
265         if (setlogin("root") < 0)
266                 warning("setlogin() failed: %m");
267
268         /*
269          * This code assumes that we always get arguments through flags,
270          * never through bits set in some random machine register.
271          */
272         while ((c = getopt(argc, argv, "dsfr")) != -1)
273                 switch (c) {
274                 case 'd':
275                         devfs = true;
276                         break;
277                 case 's':
278                         initial_transition = single_user;
279                         break;
280                 case 'f':
281                         runcom_mode = FASTBOOT;
282                         break;
283                 case 'r':
284                         initial_transition = reroot_phase_two;
285                         break;
286                 default:
287                         warning("unrecognized flag '-%c'", c);
288                         break;
289                 }
290
291         if (optind != argc)
292                 warning("ignoring excess arguments");
293
294         /*
295          * We catch or block signals rather than ignore them,
296          * so that they get reset on exec.
297          */
298         handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
299             SIGXCPU, SIGXFSZ, 0);
300         handle(transition_handler, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
301             SIGUSR1, SIGUSR2, SIGWINCH, 0);
302         handle(alrm_handler, SIGALRM, 0);
303         sigfillset(&mask);
304         delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
305             SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
306             SIGALRM, SIGUSR1, SIGUSR2, SIGWINCH, 0);
307         sigprocmask(SIG_SETMASK, &mask, NULL);
308         sigemptyset(&sa.sa_mask);
309         sa.sa_flags = 0;
310         sa.sa_handler = SIG_IGN;
311         sigaction(SIGTTIN, &sa, NULL);
312         sigaction(SIGTTOU, &sa, NULL);
313
314         /*
315          * Paranoia.
316          */
317         close(0);
318         close(1);
319         close(2);
320
321         if (kenv(KENV_GET, "init_exec", kenv_value, sizeof(kenv_value)) > 0) {
322                 replace_init(kenv_value);
323                 _exit(0); /* reboot */
324         }
325
326         if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) {
327                 state_func_t next_transition;
328
329                 if ((next_transition = run_script(kenv_value)) != NULL)
330                         initial_transition = (state_t) next_transition;
331         }
332
333         if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) {
334                 if (chdir(kenv_value) != 0 || chroot(".") != 0)
335                         warning("Can't chroot to %s: %m", kenv_value);
336         }
337
338         /*
339          * Additional check if devfs needs to be mounted:
340          * If "/" and "/dev" have the same device number,
341          * then it hasn't been mounted yet.
342          */
343         if (!devfs) {
344                 struct stat stst;
345                 dev_t root_devno;
346
347                 stat("/", &stst);
348                 root_devno = stst.st_dev;
349                 if (stat("/dev", &stst) != 0)
350                         warning("Can't stat /dev: %m");
351                 else if (stst.st_dev == root_devno)
352                         devfs = true;
353         }
354
355         if (devfs) {
356                 struct iovec iov[4];
357                 char *s;
358                 int i;
359
360                 char _fstype[]  = "fstype";
361                 char _devfs[]   = "devfs";
362                 char _fspath[]  = "fspath";
363                 char _path_dev[]= _PATH_DEV;
364
365                 iov[0].iov_base = _fstype;
366                 iov[0].iov_len = sizeof(_fstype);
367                 iov[1].iov_base = _devfs;
368                 iov[1].iov_len = sizeof(_devfs);
369                 iov[2].iov_base = _fspath;
370                 iov[2].iov_len = sizeof(_fspath);
371                 /*
372                  * Try to avoid the trailing slash in _PATH_DEV.
373                  * Be *very* defensive.
374                  */
375                 s = strdup(_PATH_DEV);
376                 if (s != NULL) {
377                         i = strlen(s);
378                         if (i > 0 && s[i - 1] == '/')
379                                 s[i - 1] = '\0';
380                         iov[3].iov_base = s;
381                         iov[3].iov_len = strlen(s) + 1;
382                 } else {
383                         iov[3].iov_base = _path_dev;
384                         iov[3].iov_len = sizeof(_path_dev);
385                 }
386                 nmount(iov, 4, 0);
387                 if (s != NULL)
388                         free(s);
389         }
390
391         if (initial_transition != reroot_phase_two) {
392                 /*
393                  * Unmount reroot leftovers.  This runs after init(8)
394                  * gets reexecuted after reroot_phase_two() is done.
395                  */
396                 error = unmount(_PATH_REROOT, MNT_FORCE);
397                 if (error != 0 && errno != EINVAL)
398                         warning("Cannot unmount %s: %m", _PATH_REROOT);
399         }
400
401         /*
402          * Start the state machine.
403          */
404         transition(initial_transition);
405
406         /*
407          * Should never reach here.
408          */
409         return 1;
410 }
411
412 /*
413  * Associate a function with a signal handler.
414  */
415 static void
416 handle(sig_t handler, ...)
417 {
418         int sig;
419         struct sigaction sa;
420         sigset_t mask_everything;
421         va_list ap;
422         va_start(ap, handler);
423
424         sa.sa_handler = handler;
425         sigfillset(&mask_everything);
426
427         while ((sig = va_arg(ap, int)) != 0) {
428                 sa.sa_mask = mask_everything;
429                 /* XXX SA_RESTART? */
430                 sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
431                 sigaction(sig, &sa, NULL);
432         }
433         va_end(ap);
434 }
435
436 /*
437  * Delete a set of signals from a mask.
438  */
439 static void
440 delset(sigset_t *maskp, ...)
441 {
442         int sig;
443         va_list ap;
444         va_start(ap, maskp);
445
446         while ((sig = va_arg(ap, int)) != 0)
447                 sigdelset(maskp, sig);
448         va_end(ap);
449 }
450
451 /*
452  * Log a message and sleep for a while (to give someone an opportunity
453  * to read it and to save log or hardcopy output if the problem is chronic).
454  * NB: should send a message to the session logger to avoid blocking.
455  */
456 static void
457 stall(const char *message, ...)
458 {
459         va_list ap;
460         va_start(ap, message);
461
462         vsyslog(LOG_ALERT, message, ap);
463         va_end(ap);
464         sleep(STALL_TIMEOUT);
465 }
466
467 /*
468  * Like stall(), but doesn't sleep.
469  * If cpp had variadic macros, the two functions could be #defines for another.
470  * NB: should send a message to the session logger to avoid blocking.
471  */
472 static void
473 warning(const char *message, ...)
474 {
475         va_list ap;
476         va_start(ap, message);
477
478         vsyslog(LOG_ALERT, message, ap);
479         va_end(ap);
480 }
481
482 /*
483  * Log an emergency message.
484  * NB: should send a message to the session logger to avoid blocking.
485  */
486 static void
487 emergency(const char *message, ...)
488 {
489         va_list ap;
490         va_start(ap, message);
491
492         vsyslog(LOG_EMERG, message, ap);
493         va_end(ap);
494 }
495
496 /*
497  * Catch an unexpected signal.
498  */
499 static void
500 disaster(int sig)
501 {
502
503         emergency("fatal signal: %s",
504             (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
505
506         sleep(STALL_TIMEOUT);
507         _exit(sig);             /* reboot */
508 }
509
510 /*
511  * Get the security level of the kernel.
512  */
513 static int
514 getsecuritylevel(void)
515 {
516 #ifdef KERN_SECURELVL
517         int name[2], curlevel;
518         size_t len;
519
520         name[0] = CTL_KERN;
521         name[1] = KERN_SECURELVL;
522         len = sizeof curlevel;
523         if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
524                 emergency("cannot get kernel security level: %m");
525                 return (-1);
526         }
527         return (curlevel);
528 #else
529         return (-1);
530 #endif
531 }
532
533 /*
534  * Set the security level of the kernel.
535  */
536 static void
537 setsecuritylevel(int newlevel)
538 {
539 #ifdef KERN_SECURELVL
540         int name[2], curlevel;
541
542         curlevel = getsecuritylevel();
543         if (newlevel == curlevel)
544                 return;
545         name[0] = CTL_KERN;
546         name[1] = KERN_SECURELVL;
547         if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
548                 emergency(
549                     "cannot change kernel security level from %d to %d: %m",
550                     curlevel, newlevel);
551                 return;
552         }
553 #ifdef SECURE
554         warning("kernel security level changed from %d to %d",
555             curlevel, newlevel);
556 #endif
557 #endif
558 }
559
560 /*
561  * Change states in the finite state machine.
562  * The initial state is passed as an argument.
563  */
564 static void
565 transition(state_t s)
566 {
567
568         current_state = s;
569         for (;;)
570                 current_state = (state_t) (*current_state)();
571 }
572
573 /*
574  * Start a session and allocate a controlling terminal.
575  * Only called by children of init after forking.
576  */
577 static void
578 open_console(void)
579 {
580         int fd;
581
582         /*
583          * Try to open /dev/console.  Open the device with O_NONBLOCK to
584          * prevent potential blocking on a carrier.
585          */
586         revoke(_PATH_CONSOLE);
587         if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
588                 (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
589                 if (login_tty(fd) == 0)
590                         return;
591                 close(fd);
592         }
593
594         /* No luck.  Log output to file if possible. */
595         if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
596                 stall("cannot open null device.");
597                 _exit(1);
598         }
599         if (fd != STDIN_FILENO) {
600                 dup2(fd, STDIN_FILENO);
601                 close(fd);
602         }
603         fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
604         if (fd == -1)
605                 dup2(STDIN_FILENO, STDOUT_FILENO);
606         else if (fd != STDOUT_FILENO) {
607                 dup2(fd, STDOUT_FILENO);
608                 close(fd);
609         }
610         dup2(STDOUT_FILENO, STDERR_FILENO);
611 }
612
613 static const char *
614 get_shell(void)
615 {
616         static char kenv_value[PATH_MAX];
617
618         if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0)
619                 return kenv_value;
620         else
621                 return _PATH_BSHELL;
622 }
623
624 static void
625 write_stderr(const char *message)
626 {
627
628         write(STDERR_FILENO, message, strlen(message));
629 }
630
631 static int
632 read_file(const char *path, void **bufp, size_t *bufsizep)
633 {
634         struct stat sb;
635         size_t bufsize;
636         void *buf;
637         ssize_t nbytes;
638         int error, fd;
639
640         fd = open(path, O_RDONLY);
641         if (fd < 0) {
642                 emergency("%s: %m", path);
643                 return (-1);
644         }
645
646         error = fstat(fd, &sb);
647         if (error != 0) {
648                 emergency("fstat: %m");
649                 close(fd);
650                 return (error);
651         }
652
653         bufsize = sb.st_size;
654         buf = malloc(bufsize);
655         if (buf == NULL) {
656                 emergency("malloc: %m");
657                 close(fd);
658                 return (error);
659         }
660
661         nbytes = read(fd, buf, bufsize);
662         if (nbytes != (ssize_t)bufsize) {
663                 emergency("read: %m");
664                 close(fd);
665                 free(buf);
666                 return (error);
667         }
668
669         error = close(fd);
670         if (error != 0) {
671                 emergency("close: %m");
672                 free(buf);
673                 return (error);
674         }
675
676         *bufp = buf;
677         *bufsizep = bufsize;
678
679         return (0);
680 }
681
682 static int
683 create_file(const char *path, const void *buf, size_t bufsize)
684 {
685         ssize_t nbytes;
686         int error, fd;
687
688         fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700);
689         if (fd < 0) {
690                 emergency("%s: %m", path);
691                 return (-1);
692         }
693
694         nbytes = write(fd, buf, bufsize);
695         if (nbytes != (ssize_t)bufsize) {
696                 emergency("write: %m");
697                 close(fd);
698                 return (-1);
699         }
700
701         error = close(fd);
702         if (error != 0) {
703                 emergency("close: %m");
704                 return (-1);
705         }
706
707         return (0);
708 }
709
710 static int
711 mount_tmpfs(const char *fspath)
712 {
713         struct iovec *iov;
714         char errmsg[255];
715         int error, iovlen;
716
717         iov = NULL;
718         iovlen = 0;
719         memset(errmsg, 0, sizeof(errmsg));
720         build_iovec(&iov, &iovlen, "fstype",
721             __DECONST(void *, "tmpfs"), (size_t)-1);
722         build_iovec(&iov, &iovlen, "fspath",
723             __DECONST(void *, fspath), (size_t)-1);
724         build_iovec(&iov, &iovlen, "errmsg",
725             errmsg, sizeof(errmsg));
726
727         error = nmount(iov, iovlen, 0);
728         if (error != 0) {
729                 if (*errmsg != '\0') {
730                         emergency("cannot mount tmpfs on %s: %s: %m",
731                             fspath, errmsg);
732                 } else {
733                         emergency("cannot mount tmpfs on %s: %m",
734                             fspath);
735                 }
736                 return (error);
737         }
738         return (0);
739 }
740
741 static state_func_t
742 reroot(void)
743 {
744         void *buf;
745         size_t bufsize;
746         int error;
747
748         buf = NULL;
749         bufsize = 0;
750
751         revoke_ttys();
752         runshutdown();
753
754         /*
755          * Make sure nobody can interfere with our scheme.
756          * Ignore ESRCH, which can apparently happen when
757          * there are no processes to kill.
758          */
759         error = kill(-1, SIGKILL);
760         if (error != 0 && errno != ESRCH) {
761                 emergency("kill(2) failed: %m");
762                 goto out;
763         }
764
765         /*
766          * Copy the init binary into tmpfs, so that we can unmount
767          * the old rootfs without committing suicide.
768          */
769         error = read_file(init_path_argv0, &buf, &bufsize);
770         if (error != 0)
771                 goto out;
772         error = mount_tmpfs(_PATH_REROOT);
773         if (error != 0)
774                 goto out;
775         error = create_file(_PATH_REROOT_INIT, buf, bufsize);
776         if (error != 0)
777                 goto out;
778
779         /*
780          * Execute the temporary init.
781          */
782         execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL);
783         emergency("cannot exec %s: %m", _PATH_REROOT_INIT);
784
785 out:
786         emergency("reroot failed; going to single user mode");
787         free(buf);
788         return (state_func_t) single_user;
789 }
790
791 static state_func_t
792 reroot_phase_two(void)
793 {
794         char init_path[PATH_MAX], *path, *path_component;
795         size_t init_path_len;
796         int nbytes, error;
797
798         /*
799          * Ask the kernel to mount the new rootfs.
800          */
801         error = reboot(RB_REROOT);
802         if (error != 0) {
803                 emergency("RB_REBOOT failed: %m");
804                 goto out;
805         }
806
807         /*
808          * Figure out where the destination init(8) binary is.  Note that
809          * the path could be different than what we've started with.  Use
810          * the value from kenv, if set, or the one from sysctl otherwise.
811          * The latter defaults to a hardcoded value, but can be overridden
812          * by a build time option.
813          */
814         nbytes = kenv(KENV_GET, "init_path", init_path, sizeof(init_path));
815         if (nbytes <= 0) {
816                 init_path_len = sizeof(init_path);
817                 error = sysctlbyname("kern.init_path",
818                     init_path, &init_path_len, NULL, 0);
819                 if (error != 0) {
820                         emergency("failed to retrieve kern.init_path: %m");
821                         goto out;
822                 }
823         }
824
825         /*
826          * Repeat the init search logic from sys/kern/init_path.c
827          */
828         path_component = init_path;
829         while ((path = strsep(&path_component, ":")) != NULL) {
830                 /*
831                  * Execute init(8) from the new rootfs.
832                  */
833                 execl(path, path, NULL);
834         }
835         emergency("cannot exec init from %s: %m", init_path);
836
837 out:
838         emergency("reroot failed; going to single user mode");
839         return (state_func_t) single_user;
840 }
841
842 /*
843  * Bring the system up single user.
844  */
845 static state_func_t
846 single_user(void)
847 {
848         pid_t pid, wpid;
849         int status;
850         sigset_t mask;
851         const char *shell;
852         char *argv[2];
853         struct timeval tv, tn;
854 #ifdef SECURE
855         struct ttyent *typ;
856         struct passwd *pp;
857         static const char banner[] =
858                 "Enter root password, or ^D to go multi-user\n";
859         char *clear, *password;
860 #endif
861 #ifdef DEBUGSHELL
862         char altshell[128];
863 #endif
864
865         if (Reboot) {
866                 /* Instead of going single user, let's reboot the machine */
867                 BOOTTRACE("shutting down the system");
868                 sync();
869                 /* Run scripts after all processes have been terminated. */
870                 runfinal();
871                 if (reboot(howto) == -1) {
872                         emergency("reboot(%#x) failed, %m", howto);
873                         _exit(1); /* panic and reboot */
874                 }
875                 warning("reboot(%#x) returned", howto);
876                 _exit(0); /* panic as well */
877         }
878
879         BOOTTRACE("going to single user mode");
880         shell = get_shell();
881
882         if ((pid = fork()) == 0) {
883                 /*
884                  * Start the single user session.
885                  */
886                 open_console();
887
888 #ifdef SECURE
889                 /*
890                  * Check the root password.
891                  * We don't care if the console is 'on' by default;
892                  * it's the only tty that can be 'off' and 'secure'.
893                  */
894                 typ = getttynam("console");
895                 pp = getpwnam("root");
896                 if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
897                     pp && *pp->pw_passwd) {
898                         write_stderr(banner);
899                         for (;;) {
900                                 clear = getpass("Password:");
901                                 if (clear == NULL || *clear == '\0')
902                                         _exit(0);
903                                 password = crypt(clear, pp->pw_passwd);
904                                 explicit_bzero(clear, _PASSWORD_LEN);
905                                 if (password != NULL &&
906                                     strcmp(password, pp->pw_passwd) == 0)
907                                         break;
908                                 warning("single-user login failed\n");
909                         }
910                 }
911                 endttyent();
912                 endpwent();
913 #endif /* SECURE */
914
915 #ifdef DEBUGSHELL
916                 {
917                         char *cp = altshell;
918                         int num;
919
920 #define SHREQUEST "Enter full pathname of shell or RETURN for "
921                         write_stderr(SHREQUEST);
922                         write_stderr(shell);
923                         write_stderr(": ");
924                         while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
925                             num != 0 && *cp != '\n' && cp < &altshell[127])
926                                 cp++;
927                         *cp = '\0';
928                         if (altshell[0] != '\0')
929                                 shell = altshell;
930                 }
931 #endif /* DEBUGSHELL */
932
933                 /*
934                  * Unblock signals.
935                  * We catch all the interesting ones,
936                  * and those are reset to SIG_DFL on exec.
937                  */
938                 sigemptyset(&mask);
939                 sigprocmask(SIG_SETMASK, &mask, NULL);
940
941                 /*
942                  * Fire off a shell.
943                  * If the default one doesn't work, try the Bourne shell.
944                  */
945
946                 char name[] = "-sh";
947
948                 argv[0] = name;
949                 argv[1] = NULL;
950                 execv(shell, argv);
951                 emergency("can't exec %s for single user: %m", shell);
952                 execv(_PATH_BSHELL, argv);
953                 emergency("can't exec %s for single user: %m", _PATH_BSHELL);
954                 sleep(STALL_TIMEOUT);
955                 _exit(1);
956         }
957
958         if (pid == -1) {
959                 /*
960                  * We are seriously hosed.  Do our best.
961                  */
962                 emergency("can't fork single-user shell, trying again");
963                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
964                         continue;
965                 return (state_func_t) single_user;
966         }
967
968         requested_transition = 0;
969         do {
970                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
971                         collect_child(wpid);
972                 if (wpid == -1) {
973                         if (errno == EINTR)
974                                 continue;
975                         warning("wait for single-user shell failed: %m; restarting");
976                         return (state_func_t) single_user;
977                 }
978                 if (wpid == pid && WIFSTOPPED(status)) {
979                         warning("init: shell stopped, restarting\n");
980                         kill(pid, SIGCONT);
981                         wpid = -1;
982                 }
983         } while (wpid != pid && !requested_transition);
984
985         if (requested_transition)
986                 return (state_func_t) requested_transition;
987
988         if (!WIFEXITED(status)) {
989                 if (WTERMSIG(status) == SIGKILL) {
990                         /*
991                          *  reboot(8) killed shell?
992                          */
993                         warning("single user shell terminated.");
994                         gettimeofday(&tv, NULL);
995                         tn = tv;
996                         tv.tv_sec += STALL_TIMEOUT;
997                         while (tv.tv_sec > tn.tv_sec || (tv.tv_sec ==
998                             tn.tv_sec && tv.tv_usec > tn.tv_usec)) {
999                                 sleep(1);
1000                                 gettimeofday(&tn, NULL);
1001                         }
1002                         _exit(0);
1003                 } else {
1004                         warning("single user shell terminated, restarting");
1005                         return (state_func_t) single_user;
1006                 }
1007         }
1008
1009         runcom_mode = FASTBOOT;
1010         return (state_func_t) runcom;
1011 }
1012
1013 /*
1014  * Run the system startup script.
1015  */
1016 static state_func_t
1017 runcom(void)
1018 {
1019         state_func_t next_transition;
1020
1021         BOOTTRACE("/etc/rc starting...");
1022         if ((next_transition = run_script(_PATH_RUNCOM)) != NULL)
1023                 return next_transition;
1024         BOOTTRACE("/etc/rc finished");
1025
1026         runcom_mode = AUTOBOOT;         /* the default */
1027         return (state_func_t) read_ttys;
1028 }
1029
1030 static void
1031 execute_script(char *argv[])
1032 {
1033         struct sigaction sa;
1034         char* sh_argv[3 + SCRIPT_ARGV_SIZE];
1035         const char *shell, *script;
1036         int error, sh_argv_len, i;
1037
1038         bzero(&sa, sizeof(sa));
1039         sigemptyset(&sa.sa_mask);
1040         sa.sa_handler = SIG_IGN;
1041         sigaction(SIGTSTP, &sa, NULL);
1042         sigaction(SIGHUP, &sa, NULL);
1043
1044         open_console();
1045
1046         sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
1047 #ifdef LOGIN_CAP
1048         setprocresources(RESOURCE_RC);
1049 #endif
1050
1051         /*
1052          * Try to directly execute the script first.  If it
1053          * fails, try the old method of passing the script path
1054          * to sh(1).  Don't complain if it fails because of
1055          * the missing execute bit.
1056          */
1057         script = argv[0];
1058         error = access(script, X_OK);
1059         if (error == 0) {
1060                 execv(script, argv);
1061                 warning("can't directly exec %s: %m", script);
1062         } else if (errno != EACCES) {
1063                 warning("can't access %s: %m", script);
1064         }
1065
1066         shell = get_shell();
1067         sh_argv[0] = __DECONST(char*, shell);
1068         sh_argv_len = 1;
1069 #ifdef SECURE
1070         if (strcmp(shell, _PATH_BSHELL) == 0) {
1071                 sh_argv[1] = __DECONST(char*, "-o");
1072                 sh_argv[2] = __DECONST(char*, "verify");
1073                 sh_argv_len = 3;
1074         }
1075 #endif
1076         for (i = 0; i != SCRIPT_ARGV_SIZE; ++i)
1077                 sh_argv[i + sh_argv_len] = argv[i];
1078         execv(shell, sh_argv);
1079         stall("can't exec %s for %s: %m", shell, script);
1080 }
1081
1082 /*
1083  * Execute binary, replacing init(8) as PID 1.
1084  */
1085 static void
1086 replace_init(char *path)
1087 {
1088         char *argv[SCRIPT_ARGV_SIZE];
1089
1090         argv[0] = path;
1091         argv[1] = NULL;
1092
1093         execute_script(argv);
1094 }
1095
1096 /*
1097  * Run a shell script.
1098  * Returns 0 on success, otherwise the next transition to enter:
1099  *  - single_user if fork/execv/waitpid failed, or if the script
1100  *    terminated with a signal or exit code != 0.
1101  *  - death_single if a SIGTERM was delivered to init(8).
1102  */
1103 static state_func_t
1104 run_script(const char *script)
1105 {
1106         pid_t pid, wpid;
1107         int status;
1108         char *argv[SCRIPT_ARGV_SIZE];
1109         const char *shell;
1110
1111         shell = get_shell();
1112
1113         if ((pid = fork()) == 0) {
1114
1115                 char _autoboot[] = "autoboot";
1116
1117                 argv[0] = __DECONST(char *, script);
1118                 argv[1] = runcom_mode == AUTOBOOT ? _autoboot : NULL;
1119                 argv[2] = NULL;
1120
1121                 execute_script(argv);
1122                 sleep(STALL_TIMEOUT);
1123                 _exit(1);       /* force single user mode */
1124         }
1125
1126         if (pid == -1) {
1127                 emergency("can't fork for %s on %s: %m", shell, script);
1128                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
1129                         continue;
1130                 sleep(STALL_TIMEOUT);
1131                 return (state_func_t) single_user;
1132         }
1133
1134         /*
1135          * Copied from single_user().  This is a bit paranoid.
1136          */
1137         requested_transition = 0;
1138         do {
1139                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
1140                         collect_child(wpid);
1141                 if (requested_transition == death_single ||
1142                     requested_transition == reroot)
1143                         return (state_func_t) requested_transition;
1144                 if (wpid == -1) {
1145                         if (errno == EINTR)
1146                                 continue;
1147                         warning("wait for %s on %s failed: %m; going to "
1148                             "single user mode", shell, script);
1149                         return (state_func_t) single_user;
1150                 }
1151                 if (wpid == pid && WIFSTOPPED(status)) {
1152                         warning("init: %s on %s stopped, restarting\n",
1153                             shell, script);
1154                         kill(pid, SIGCONT);
1155                         wpid = -1;
1156                 }
1157         } while (wpid != pid);
1158
1159         if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
1160             requested_transition == catatonia) {
1161                 /* /etc/rc executed /sbin/reboot; wait for the end quietly */
1162                 sigset_t s;
1163
1164                 sigfillset(&s);
1165                 for (;;)
1166                         sigsuspend(&s);
1167         }
1168
1169         if (!WIFEXITED(status)) {
1170                 warning("%s on %s terminated abnormally, going to single "
1171                     "user mode", shell, script);
1172                 return (state_func_t) single_user;
1173         }
1174
1175         if (WEXITSTATUS(status))
1176                 return (state_func_t) single_user;
1177
1178         return (state_func_t) 0;
1179 }
1180
1181 /*
1182  * Open the session database.
1183  *
1184  * NB: We could pass in the size here; is it necessary?
1185  */
1186 static int
1187 start_session_db(void)
1188 {
1189         if (session_db && (*session_db->close)(session_db))
1190                 emergency("session database close: %m");
1191         if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL) {
1192                 emergency("session database open: %m");
1193                 return (1);
1194         }
1195         return (0);
1196
1197 }
1198
1199 /*
1200  * Add a new login session.
1201  */
1202 static void
1203 add_session(session_t *sp)
1204 {
1205         DBT key;
1206         DBT data;
1207
1208         key.data = &sp->se_process;
1209         key.size = sizeof sp->se_process;
1210         data.data = &sp;
1211         data.size = sizeof sp;
1212
1213         if ((*session_db->put)(session_db, &key, &data, 0))
1214                 emergency("insert %d: %m", sp->se_process);
1215 }
1216
1217 /*
1218  * Delete an old login session.
1219  */
1220 static void
1221 del_session(session_t *sp)
1222 {
1223         DBT key;
1224
1225         key.data = &sp->se_process;
1226         key.size = sizeof sp->se_process;
1227
1228         if ((*session_db->del)(session_db, &key, 0))
1229                 emergency("delete %d: %m", sp->se_process);
1230 }
1231
1232 /*
1233  * Look up a login session by pid.
1234  */
1235 static session_t *
1236 find_session(pid_t pid)
1237 {
1238         DBT key;
1239         DBT data;
1240         session_t *ret;
1241
1242         key.data = &pid;
1243         key.size = sizeof pid;
1244         if ((*session_db->get)(session_db, &key, &data, 0) != 0)
1245                 return 0;
1246         bcopy(data.data, (char *)&ret, sizeof(ret));
1247         return ret;
1248 }
1249
1250 /*
1251  * Construct an argument vector from a command line.
1252  */
1253 static char **
1254 construct_argv(char *command)
1255 {
1256         int argc = 0;
1257         char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
1258                                                 * sizeof (char *));
1259
1260         if ((argv[argc++] = strk(command)) == NULL) {
1261                 free(argv);
1262                 return (NULL);
1263         }
1264         while ((argv[argc++] = strk((char *) 0)) != NULL)
1265                 continue;
1266         return argv;
1267 }
1268
1269 /*
1270  * Deallocate a session descriptor.
1271  */
1272 static void
1273 free_session(session_t *sp)
1274 {
1275         free(sp->se_device);
1276         if (sp->se_getty) {
1277                 free(sp->se_getty);
1278                 free(sp->se_getty_argv_space);
1279                 free(sp->se_getty_argv);
1280         }
1281         if (sp->se_window) {
1282                 free(sp->se_window);
1283                 free(sp->se_window_argv_space);
1284                 free(sp->se_window_argv);
1285         }
1286         if (sp->se_type)
1287                 free(sp->se_type);
1288         free(sp);
1289 }
1290
1291 /*
1292  * Allocate a new session descriptor.
1293  * Mark it SE_PRESENT.
1294  */
1295 static session_t *
1296 new_session(session_t *sprev, struct ttyent *typ)
1297 {
1298         session_t *sp;
1299
1300         if ((typ->ty_status & TTY_ON) == 0 ||
1301             typ->ty_name == 0 ||
1302             typ->ty_getty == 0)
1303                 return 0;
1304
1305         sp = (session_t *) calloc(1, sizeof (session_t));
1306
1307         sp->se_flags |= SE_PRESENT;
1308
1309         if ((typ->ty_status & TTY_IFEXISTS) != 0)
1310                 sp->se_flags |= SE_IFEXISTS;
1311
1312         if ((typ->ty_status & TTY_IFCONSOLE) != 0)
1313                 sp->se_flags |= SE_IFCONSOLE;
1314
1315         if (asprintf(&sp->se_device, "%s%s", _PATH_DEV, typ->ty_name) < 0)
1316                 err(1, "asprintf");
1317
1318         if (setupargv(sp, typ) == 0) {
1319                 free_session(sp);
1320                 return (0);
1321         }
1322
1323         sp->se_next = 0;
1324         if (sprev == NULL) {
1325                 sessions = sp;
1326                 sp->se_prev = 0;
1327         } else {
1328                 sprev->se_next = sp;
1329                 sp->se_prev = sprev;
1330         }
1331
1332         return sp;
1333 }
1334
1335 /*
1336  * Calculate getty and if useful window argv vectors.
1337  */
1338 static int
1339 setupargv(session_t *sp, struct ttyent *typ)
1340 {
1341
1342         if (sp->se_getty) {
1343                 free(sp->se_getty);
1344                 free(sp->se_getty_argv_space);
1345                 free(sp->se_getty_argv);
1346         }
1347         if (asprintf(&sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name) < 0)
1348                 err(1, "asprintf");
1349         sp->se_getty_argv_space = strdup(sp->se_getty);
1350         sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
1351         if (sp->se_getty_argv == NULL) {
1352                 warning("can't parse getty for port %s", sp->se_device);
1353                 free(sp->se_getty);
1354                 free(sp->se_getty_argv_space);
1355                 sp->se_getty = sp->se_getty_argv_space = 0;
1356                 return (0);
1357         }
1358         if (sp->se_window) {
1359                 free(sp->se_window);
1360                 free(sp->se_window_argv_space);
1361                 free(sp->se_window_argv);
1362         }
1363         sp->se_window = sp->se_window_argv_space = 0;
1364         sp->se_window_argv = 0;
1365         if (typ->ty_window) {
1366                 sp->se_window = strdup(typ->ty_window);
1367                 sp->se_window_argv_space = strdup(sp->se_window);
1368                 sp->se_window_argv = construct_argv(sp->se_window_argv_space);
1369                 if (sp->se_window_argv == NULL) {
1370                         warning("can't parse window for port %s",
1371                             sp->se_device);
1372                         free(sp->se_window_argv_space);
1373                         free(sp->se_window);
1374                         sp->se_window = sp->se_window_argv_space = 0;
1375                         return (0);
1376                 }
1377         }
1378         if (sp->se_type)
1379                 free(sp->se_type);
1380         sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
1381         return (1);
1382 }
1383
1384 /*
1385  * Walk the list of ttys and create sessions for each active line.
1386  */
1387 static state_func_t
1388 read_ttys(void)
1389 {
1390         session_t *sp, *snext;
1391         struct ttyent *typ;
1392
1393         /*
1394          * Destroy any previous session state.
1395          * There shouldn't be any, but just in case...
1396          */
1397         for (sp = sessions; sp; sp = snext) {
1398                 snext = sp->se_next;
1399                 free_session(sp);
1400         }
1401         sessions = 0;
1402         if (start_session_db())
1403                 return (state_func_t) single_user;
1404
1405         /*
1406          * Allocate a session entry for each active port.
1407          * Note that sp starts at 0.
1408          */
1409         while ((typ = getttyent()) != NULL)
1410                 if ((snext = new_session(sp, typ)) != NULL)
1411                         sp = snext;
1412
1413         endttyent();
1414
1415         return (state_func_t) multi_user;
1416 }
1417
1418 /*
1419  * Start a window system running.
1420  */
1421 static void
1422 start_window_system(session_t *sp)
1423 {
1424         pid_t pid;
1425         sigset_t mask;
1426         char term[64], *env[2];
1427         int status;
1428
1429         if ((pid = fork()) == -1) {
1430                 emergency("can't fork for window system on port %s: %m",
1431                     sp->se_device);
1432                 /* hope that getty fails and we can try again */
1433                 return;
1434         }
1435         if (pid) {
1436                 waitpid(-1, &status, 0);
1437                 return;
1438         }
1439
1440         /* reparent window process to the init to not make a zombie on exit */
1441         if ((pid = fork()) == -1) {
1442                 emergency("can't fork for window system on port %s: %m",
1443                     sp->se_device);
1444                 _exit(1);
1445         }
1446         if (pid)
1447                 _exit(0);
1448
1449         sigemptyset(&mask);
1450         sigprocmask(SIG_SETMASK, &mask, NULL);
1451
1452         if (setsid() < 0)
1453                 emergency("setsid failed (window) %m");
1454
1455 #ifdef LOGIN_CAP
1456         setprocresources(RESOURCE_WINDOW);
1457 #endif
1458         if (sp->se_type) {
1459                 /* Don't use malloc after fork */
1460                 strcpy(term, "TERM=");
1461                 strlcat(term, sp->se_type, sizeof(term));
1462                 env[0] = term;
1463                 env[1] = NULL;
1464         }
1465         else
1466                 env[0] = NULL;
1467         execve(sp->se_window_argv[0], sp->se_window_argv, env);
1468         stall("can't exec window system '%s' for port %s: %m",
1469                 sp->se_window_argv[0], sp->se_device);
1470         _exit(1);
1471 }
1472
1473 /*
1474  * Start a login session running.
1475  */
1476 static pid_t
1477 start_getty(session_t *sp)
1478 {
1479         pid_t pid;
1480         sigset_t mask;
1481         time_t current_time = time((time_t *) 0);
1482         int too_quick = 0;
1483         char term[64], *env[2];
1484
1485         if (current_time >= sp->se_started &&
1486             current_time - sp->se_started < GETTY_SPACING) {
1487                 if (++sp->se_nspace > GETTY_NSPACE) {
1488                         sp->se_nspace = 0;
1489                         too_quick = 1;
1490                 }
1491         } else
1492                 sp->se_nspace = 0;
1493
1494         /*
1495          * fork(), not vfork() -- we can't afford to block.
1496          */
1497         if ((pid = fork()) == -1) {
1498                 emergency("can't fork for getty on port %s: %m", sp->se_device);
1499                 return -1;
1500         }
1501
1502         if (pid)
1503                 return pid;
1504
1505         if (too_quick) {
1506                 warning("getty repeating too quickly on port %s, sleeping %d secs",
1507                     sp->se_device, GETTY_SLEEP);
1508                 sleep((unsigned) GETTY_SLEEP);
1509         }
1510
1511         if (sp->se_window) {
1512                 start_window_system(sp);
1513                 sleep(WINDOW_WAIT);
1514         }
1515
1516         sigemptyset(&mask);
1517         sigprocmask(SIG_SETMASK, &mask, NULL);
1518
1519 #ifdef LOGIN_CAP
1520         setprocresources(RESOURCE_GETTY);
1521 #endif
1522         if (sp->se_type) {
1523                 /* Don't use malloc after fork */
1524                 strcpy(term, "TERM=");
1525                 strlcat(term, sp->se_type, sizeof(term));
1526                 env[0] = term;
1527                 env[1] = NULL;
1528         } else
1529                 env[0] = NULL;
1530         execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
1531         stall("can't exec getty '%s' for port %s: %m",
1532                 sp->se_getty_argv[0], sp->se_device);
1533         _exit(1);
1534 }
1535
1536 /*
1537  * Return 1 if the session is defined as "onifexists"
1538  * or "onifconsole" and the device node does not exist.
1539  */
1540 static int
1541 session_has_no_tty(session_t *sp)
1542 {
1543         int fd;
1544
1545         if ((sp->se_flags & SE_IFEXISTS) == 0 &&
1546             (sp->se_flags & SE_IFCONSOLE) == 0)
1547                 return (0);
1548
1549         fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0);
1550         if (fd < 0) {
1551                 if (errno == ENOENT)
1552                         return (1);
1553                 return (0);
1554         }
1555
1556         close(fd);
1557         return (0);
1558 }
1559
1560 /*
1561  * Collect exit status for a child.
1562  * If an exiting login, start a new login running.
1563  */
1564 static void
1565 collect_child(pid_t pid)
1566 {
1567         session_t *sp, *sprev, *snext;
1568
1569         if (! sessions)
1570                 return;
1571
1572         if (! (sp = find_session(pid)))
1573                 return;
1574
1575         del_session(sp);
1576         sp->se_process = 0;
1577
1578         if (sp->se_flags & SE_SHUTDOWN ||
1579             session_has_no_tty(sp)) {
1580                 if ((sprev = sp->se_prev) != NULL)
1581                         sprev->se_next = sp->se_next;
1582                 else
1583                         sessions = sp->se_next;
1584                 if ((snext = sp->se_next) != NULL)
1585                         snext->se_prev = sp->se_prev;
1586                 free_session(sp);
1587                 return;
1588         }
1589
1590         if ((pid = start_getty(sp)) == -1) {
1591                 /* serious trouble */
1592                 requested_transition = clean_ttys;
1593                 return;
1594         }
1595
1596         sp->se_process = pid;
1597         sp->se_started = time((time_t *) 0);
1598         add_session(sp);
1599 }
1600
1601 static const char *
1602 get_current_state(void)
1603 {
1604
1605         if (current_state == single_user)
1606                 return ("single-user");
1607         if (current_state == runcom)
1608                 return ("runcom");
1609         if (current_state == read_ttys)
1610                 return ("read-ttys");
1611         if (current_state == multi_user)
1612                 return ("multi-user");
1613         if (current_state == clean_ttys)
1614                 return ("clean-ttys");
1615         if (current_state == catatonia)
1616                 return ("catatonia");
1617         if (current_state == death)
1618                 return ("death");
1619         if (current_state == death_single)
1620                 return ("death-single");
1621         return ("unknown");
1622 }
1623
1624 static void
1625 boottrace_transition(int sig)
1626 {
1627         const char *action;
1628
1629         switch (sig) {
1630         case SIGUSR2:
1631                 action = "halt & poweroff";
1632                 break;
1633         case SIGUSR1:
1634                 action = "halt";
1635                 break;
1636         case SIGINT:
1637                 action = "reboot";
1638                 break;
1639         case SIGWINCH:
1640                 action = "powercycle";
1641                 break;
1642         case SIGTERM:
1643                 action = Reboot ? "reboot" : "single-user";
1644                 break;
1645         default:
1646                 BOOTTRACE("signal %d from %s", sig, get_current_state());
1647                 return;
1648         }
1649
1650         /* Trace the shutdown reason. */
1651         SHUTTRACE("%s from %s", action, get_current_state());
1652 }
1653
1654 /*
1655  * Catch a signal and request a state transition.
1656  */
1657 static void
1658 transition_handler(int sig)
1659 {
1660
1661         boottrace_transition(sig);
1662         switch (sig) {
1663         case SIGHUP:
1664                 if (current_state == read_ttys || current_state == multi_user ||
1665                     current_state == clean_ttys || current_state == catatonia)
1666                         requested_transition = clean_ttys;
1667                 break;
1668         case SIGUSR2:
1669                 howto = RB_POWEROFF;
1670         case SIGUSR1:
1671                 howto |= RB_HALT;
1672         case SIGWINCH:
1673         case SIGINT:
1674                 if (sig == SIGWINCH)
1675                         howto |= RB_POWERCYCLE;
1676                 Reboot = true;
1677         case SIGTERM:
1678                 if (current_state == read_ttys || current_state == multi_user ||
1679                     current_state == clean_ttys || current_state == catatonia)
1680                         requested_transition = death;
1681                 else
1682                         requested_transition = death_single;
1683                 break;
1684         case SIGTSTP:
1685                 if (current_state == runcom || current_state == read_ttys ||
1686                     current_state == clean_ttys ||
1687                     current_state == multi_user || current_state == catatonia)
1688                         requested_transition = catatonia;
1689                 break;
1690         case SIGEMT:
1691                 requested_transition = reroot;
1692                 break;
1693         default:
1694                 requested_transition = 0;
1695                 break;
1696         }
1697 }
1698
1699 /*
1700  * Take the system multiuser.
1701  */
1702 static state_func_t
1703 multi_user(void)
1704 {
1705         static bool inmultiuser = false;
1706         pid_t pid;
1707         session_t *sp;
1708
1709         requested_transition = 0;
1710
1711         /*
1712          * If the administrator has not set the security level to -1
1713          * to indicate that the kernel should not run multiuser in secure
1714          * mode, and the run script has not set a higher level of security
1715          * than level 1, then put the kernel into secure mode.
1716          */
1717         if (getsecuritylevel() == 0)
1718                 setsecuritylevel(1);
1719
1720         for (sp = sessions; sp; sp = sp->se_next) {
1721                 if (sp->se_process)
1722                         continue;
1723                 if (session_has_no_tty(sp))
1724                         continue;
1725                 if ((pid = start_getty(sp)) == -1) {
1726                         /* serious trouble */
1727                         requested_transition = clean_ttys;
1728                         break;
1729                 }
1730                 sp->se_process = pid;
1731                 sp->se_started = time((time_t *) 0);
1732                 add_session(sp);
1733         }
1734
1735         if (requested_transition == 0 && !inmultiuser) {
1736                 inmultiuser = true;
1737                 /* This marks the change from boot-time tracing to run-time. */
1738                 RUNTRACE("multi-user start");
1739         }
1740         while (!requested_transition)
1741                 if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
1742                         collect_child(pid);
1743
1744         return (state_func_t) requested_transition;
1745 }
1746
1747 /*
1748  * This is an (n*2)+(n^2) algorithm.  We hope it isn't run often...
1749  */
1750 static state_func_t
1751 clean_ttys(void)
1752 {
1753         session_t *sp, *sprev;
1754         struct ttyent *typ;
1755         int devlen;
1756         char *old_getty, *old_window, *old_type;
1757
1758         /*
1759          * mark all sessions for death, (!SE_PRESENT)
1760          * as we find or create new ones they'll be marked as keepers,
1761          * we'll later nuke all the ones not found in /etc/ttys
1762          */
1763         for (sp = sessions; sp != NULL; sp = sp->se_next)
1764                 sp->se_flags &= ~SE_PRESENT;
1765
1766         devlen = sizeof(_PATH_DEV) - 1;
1767         while ((typ = getttyent()) != NULL) {
1768                 for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
1769                         if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
1770                                 break;
1771
1772                 if (sp) {
1773                         /* we want this one to live */
1774                         sp->se_flags |= SE_PRESENT;
1775                         if ((typ->ty_status & TTY_ON) == 0 ||
1776                             typ->ty_getty == 0) {
1777                                 sp->se_flags |= SE_SHUTDOWN;
1778                                 kill(sp->se_process, SIGHUP);
1779                                 continue;
1780                         }
1781                         sp->se_flags &= ~SE_SHUTDOWN;
1782                         old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1783                         old_window = sp->se_window ? strdup(sp->se_window) : 0;
1784                         old_type = sp->se_type ? strdup(sp->se_type) : 0;
1785                         if (setupargv(sp, typ) == 0) {
1786                                 warning("can't parse getty for port %s",
1787                                         sp->se_device);
1788                                 sp->se_flags |= SE_SHUTDOWN;
1789                                 kill(sp->se_process, SIGHUP);
1790                         }
1791                         else if (   !old_getty
1792                                  || (!old_type && sp->se_type)
1793                                  || (old_type && !sp->se_type)
1794                                  || (!old_window && sp->se_window)
1795                                  || (old_window && !sp->se_window)
1796                                  || (strcmp(old_getty, sp->se_getty) != 0)
1797                                  || (old_window && strcmp(old_window, sp->se_window) != 0)
1798                                  || (old_type && strcmp(old_type, sp->se_type) != 0)
1799                                 ) {
1800                                 /* Don't set SE_SHUTDOWN here */
1801                                 sp->se_nspace = 0;
1802                                 sp->se_started = 0;
1803                                 kill(sp->se_process, SIGHUP);
1804                         }
1805                         if (old_getty)
1806                                 free(old_getty);
1807                         if (old_window)
1808                                 free(old_window);
1809                         if (old_type)
1810                                 free(old_type);
1811                         continue;
1812                 }
1813
1814                 new_session(sprev, typ);
1815         }
1816
1817         endttyent();
1818
1819         /*
1820          * sweep through and kill all deleted sessions
1821          * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1822          */
1823         for (sp = sessions; sp != NULL; sp = sp->se_next) {
1824                 if ((sp->se_flags & SE_PRESENT) == 0) {
1825                         sp->se_flags |= SE_SHUTDOWN;
1826                         kill(sp->se_process, SIGHUP);
1827                 }
1828         }
1829
1830         return (state_func_t) multi_user;
1831 }
1832
1833 /*
1834  * Block further logins.
1835  */
1836 static state_func_t
1837 catatonia(void)
1838 {
1839         session_t *sp;
1840
1841         for (sp = sessions; sp; sp = sp->se_next)
1842                 sp->se_flags |= SE_SHUTDOWN;
1843
1844         return (state_func_t) multi_user;
1845 }
1846
1847 /*
1848  * Note SIGALRM.
1849  */
1850 static void
1851 alrm_handler(int sig)
1852 {
1853
1854         (void)sig;
1855         clang = true;
1856 }
1857
1858 /*
1859  * Bring the system down to single user.
1860  */
1861 static state_func_t
1862 death(void)
1863 {
1864         int block, blocked;
1865         size_t len;
1866
1867         /* Temporarily block suspend. */
1868         len = sizeof(blocked);
1869         block = 1;
1870         if (sysctlbyname("kern.suspend_blocked", &blocked, &len,
1871             &block, sizeof(block)) == -1)
1872                 blocked = 0;
1873
1874         /*
1875          * Also revoke the TTY here.  Because runshutdown() may reopen
1876          * the TTY whose getty we're killing here, there is no guarantee
1877          * runshutdown() will perform the initial open() call, causing
1878          * the terminal attributes to be misconfigured.
1879          */
1880         revoke_ttys();
1881
1882         /* Try to run the rc.shutdown script within a period of time */
1883         runshutdown();
1884
1885         /* Unblock suspend if we blocked it. */
1886         if (!blocked)
1887                 sysctlbyname("kern.suspend_blocked", NULL, NULL,
1888                     &blocked, sizeof(blocked));
1889
1890         return (state_func_t) death_single;
1891 }
1892
1893 /*
1894  * Do what is necessary to reinitialize single user mode or reboot
1895  * from an incomplete state.
1896  */
1897 static state_func_t
1898 death_single(void)
1899 {
1900         int i;
1901         pid_t pid;
1902         static const int death_sigs[2] = { SIGTERM, SIGKILL };
1903
1904         revoke(_PATH_CONSOLE);
1905
1906         BOOTTRACE("start killing user processes");
1907         for (i = 0; i < 2; ++i) {
1908                 if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
1909                         return (state_func_t) single_user;
1910
1911                 clang = false;
1912                 alarm(DEATH_WATCH);
1913                 do
1914                         if ((pid = waitpid(-1, (int *)0, 0)) != -1)
1915                                 collect_child(pid);
1916                 while (!clang && errno != ECHILD);
1917
1918                 if (errno == ECHILD)
1919                         return (state_func_t) single_user;
1920         }
1921
1922         warning("some processes would not die; ps axl advised");
1923
1924         return (state_func_t) single_user;
1925 }
1926
1927 static void
1928 revoke_ttys(void)
1929 {
1930         session_t *sp;
1931
1932         for (sp = sessions; sp; sp = sp->se_next) {
1933                 sp->se_flags |= SE_SHUTDOWN;
1934                 kill(sp->se_process, SIGHUP);
1935                 revoke(sp->se_device);
1936         }
1937 }
1938
1939 /*
1940  * Run the system shutdown script.
1941  *
1942  * Exit codes:      XXX I should document more
1943  * -2       shutdown script terminated abnormally
1944  * -1       fatal error - can't run script
1945  * 0        good.
1946  * >0       some error (exit code)
1947  */
1948 static int
1949 runshutdown(void)
1950 {
1951         pid_t pid, wpid;
1952         int status;
1953         int shutdowntimeout;
1954         size_t len;
1955         char *argv[SCRIPT_ARGV_SIZE];
1956         struct stat sb;
1957
1958         BOOTTRACE("init(8): start rc.shutdown");
1959
1960         /*
1961          * rc.shutdown is optional, so to prevent any unnecessary
1962          * complaints from the shell we simply don't run it if the
1963          * file does not exist. If the stat() here fails for other
1964          * reasons, we'll let the shell complain.
1965          */
1966         if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
1967                 return 0;
1968
1969         if ((pid = fork()) == 0) {
1970                 char _reboot[]  = "reboot";
1971                 char _single[]  = "single";
1972                 char _path_rundown[] = _PATH_RUNDOWN;
1973
1974                 argv[0] = _path_rundown;
1975                 argv[1] = Reboot ? _reboot : _single;
1976                 argv[2] = NULL;
1977
1978                 execute_script(argv);
1979                 _exit(1);       /* force single user mode */
1980         }
1981
1982         if (pid == -1) {
1983                 emergency("can't fork for %s: %m", _PATH_RUNDOWN);
1984                 while (waitpid(-1, (int *) 0, WNOHANG) > 0)
1985                         continue;
1986                 sleep(STALL_TIMEOUT);
1987                 return -1;
1988         }
1989
1990         len = sizeof(shutdowntimeout);
1991         if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len,
1992             NULL, 0) == -1 || shutdowntimeout < 2)
1993                 shutdowntimeout = DEATH_SCRIPT;
1994         alarm(shutdowntimeout);
1995         clang = false;
1996         /*
1997          * Copied from single_user().  This is a bit paranoid.
1998          * Use the same ALRM handler.
1999          */
2000         do {
2001                 if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
2002                         collect_child(wpid);
2003                 if (clang) {
2004                         /* we were waiting for the sub-shell */
2005                         kill(wpid, SIGTERM);
2006                         warning("timeout expired for %s: %m; going to "
2007                             "single user mode", _PATH_RUNDOWN);
2008                         BOOTTRACE("rc.shutdown's %d sec timeout expired",
2009                                   shutdowntimeout);
2010                         return -1;
2011                 }
2012                 if (wpid == -1) {
2013                         if (errno == EINTR)
2014                                 continue;
2015                         warning("wait for %s failed: %m; going to "
2016                             "single user mode", _PATH_RUNDOWN);
2017                         return -1;
2018                 }
2019                 if (wpid == pid && WIFSTOPPED(status)) {
2020                         warning("init: %s stopped, restarting\n",
2021                             _PATH_RUNDOWN);
2022                         kill(pid, SIGCONT);
2023                         wpid = -1;
2024                 }
2025         } while (wpid != pid && !clang);
2026
2027         /* Turn off the alarm */
2028         alarm(0);
2029
2030         if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
2031             requested_transition == catatonia) {
2032                 /*
2033                  * /etc/rc.shutdown executed /sbin/reboot;
2034                  * wait for the end quietly
2035                  */
2036                 sigset_t s;
2037
2038                 sigfillset(&s);
2039                 for (;;)
2040                         sigsuspend(&s);
2041         }
2042
2043         if (!WIFEXITED(status)) {
2044                 warning("%s terminated abnormally, going to "
2045                     "single user mode", _PATH_RUNDOWN);
2046                 return -2;
2047         }
2048
2049         if ((status = WEXITSTATUS(status)) != 0)
2050                 warning("%s returned status %d", _PATH_RUNDOWN, status);
2051
2052         return status;
2053 }
2054
2055 static char *
2056 strk(char *p)
2057 {
2058         static char *t;
2059         char *q;
2060         int c;
2061
2062         if (p)
2063                 t = p;
2064         if (!t)
2065                 return 0;
2066
2067         c = *t;
2068         while (c == ' ' || c == '\t' )
2069                 c = *++t;
2070         if (!c) {
2071                 t = 0;
2072                 return 0;
2073         }
2074         q = t;
2075         if (c == '\'') {
2076                 c = *++t;
2077                 q = t;
2078                 while (c && c != '\'')
2079                         c = *++t;
2080                 if (!c)  /* unterminated string */
2081                         q = t = 0;
2082                 else
2083                         *t++ = 0;
2084         } else {
2085                 while (c && c != ' ' && c != '\t' )
2086                         c = *++t;
2087                 *t++ = 0;
2088                 if (!c)
2089                         t = 0;
2090         }
2091         return q;
2092 }
2093
2094 #ifdef LOGIN_CAP
2095 static void
2096 setprocresources(const char *cname)
2097 {
2098         login_cap_t *lc;
2099         if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
2100                 setusercontext(lc, (struct passwd*)NULL, 0,
2101                     LOGIN_SETENV |
2102                     LOGIN_SETPRIORITY | LOGIN_SETRESOURCES |
2103                     LOGIN_SETLOGINCLASS | LOGIN_SETCPUMASK);
2104                 login_close(lc);
2105         }
2106 }
2107 #endif
2108
2109 /*
2110  * Run /etc/rc.final to execute scripts after all user processes have been
2111  * terminated.
2112  */
2113 static void
2114 runfinal(void)
2115 {
2116         struct stat sb;
2117         pid_t other_pid, pid;
2118         sigset_t mask;
2119
2120         /* Avoid any surprises. */
2121         alarm(0);
2122
2123         /* rc.final is optional. */
2124         if (stat(_PATH_RUNFINAL, &sb) == -1 && errno == ENOENT)
2125                 return;
2126         if (access(_PATH_RUNFINAL, X_OK) != 0) {
2127                 warning("%s exists, but not executable", _PATH_RUNFINAL);
2128                 return;
2129         }
2130
2131         pid = fork();
2132         if (pid == 0) {
2133                 /*
2134                  * Reopen stdin/stdout/stderr so that scripts can write to
2135                  * console.
2136                  */
2137                 close(0);
2138                 open(_PATH_DEVNULL, O_RDONLY);
2139                 close(1);
2140                 close(2);
2141                 open_console();
2142                 dup2(1, 2);
2143                 sigemptyset(&mask);
2144                 sigprocmask(SIG_SETMASK, &mask, NULL);
2145                 signal(SIGCHLD, SIG_DFL);
2146                 execl(_PATH_RUNFINAL, _PATH_RUNFINAL, NULL);
2147                 perror("execl(" _PATH_RUNFINAL ") failed");
2148                 exit(1);
2149         }
2150
2151         /* Wait for rc.final script to exit */
2152         while ((other_pid = waitpid(-1, NULL, 0)) != pid && other_pid > 0) {
2153                 continue;
2154         }
2155 }