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