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