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