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