xref: /freebsd/sbin/init/init.c (revision 3f5ac575ea1288891300312b2fb9590c4c1483c4)
18fae3551SRodney W. Grimes /*-
28fae3551SRodney W. Grimes  * Copyright (c) 1991, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Donn Seeley at Berkeley Software Design, Inc.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
178fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
188fae3551SRodney W. Grimes  *    without specific prior written permission.
198fae3551SRodney W. Grimes  *
208fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
218fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
248fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308fae3551SRodney W. Grimes  * SUCH DAMAGE.
318fae3551SRodney W. Grimes  */
328fae3551SRodney W. Grimes 
338fae3551SRodney W. Grimes #ifndef lint
345df42cf4SPhilippe Charnier static const char copyright[] =
358fae3551SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\
368fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
378fae3551SRodney W. Grimes #endif /* not lint */
388fae3551SRodney W. Grimes 
398fae3551SRodney W. Grimes #ifndef lint
405df42cf4SPhilippe Charnier #if 0
418fae3551SRodney W. Grimes static char sccsid[] = "@(#)init.c	8.1 (Berkeley) 7/15/93";
425df42cf4SPhilippe Charnier #endif
435df42cf4SPhilippe Charnier static const char rcsid[] =
447f3dea24SPeter Wemm   "$FreeBSD$";
458fae3551SRodney W. Grimes #endif /* not lint */
468fae3551SRodney W. Grimes 
478fae3551SRodney W. Grimes #include <sys/param.h>
488889c700SDavid Nugent #include <sys/ioctl.h>
49*3f5ac575SEdward Tomasz Napierala #include <sys/mman.h>
5057622f22SPoul-Henning Kamp #include <sys/mount.h>
518fae3551SRodney W. Grimes #include <sys/sysctl.h>
528fae3551SRodney W. Grimes #include <sys/wait.h>
5386bf62dcSDavid Nugent #include <sys/stat.h>
541f083b1eSMaxime Henrion #include <sys/uio.h>
558fae3551SRodney W. Grimes 
568fae3551SRodney W. Grimes #include <db.h>
578fae3551SRodney W. Grimes #include <errno.h>
588fae3551SRodney W. Grimes #include <fcntl.h>
591a7bec91SWarner Losh #include <kenv.h>
60423b6a39SAndrey A. Chernov #include <libutil.h>
611a37aa56SDavid E. O'Brien #include <paths.h>
628fae3551SRodney W. Grimes #include <signal.h>
638fae3551SRodney W. Grimes #include <stdio.h>
648fae3551SRodney W. Grimes #include <stdlib.h>
658fae3551SRodney W. Grimes #include <string.h>
668fae3551SRodney W. Grimes #include <syslog.h>
678fae3551SRodney W. Grimes #include <time.h>
688fae3551SRodney W. Grimes #include <ttyent.h>
698fae3551SRodney W. Grimes #include <unistd.h>
70e460cfd3SNate Williams #include <sys/reboot.h>
71c5842835SPhilippe Charnier #include <err.h>
728fae3551SRodney W. Grimes 
738fae3551SRodney W. Grimes #include <stdarg.h>
748fae3551SRodney W. Grimes 
758fae3551SRodney W. Grimes #ifdef SECURE
768fae3551SRodney W. Grimes #include <pwd.h>
778fae3551SRodney W. Grimes #endif
788fae3551SRodney W. Grimes 
791ef60eb1SDavid Nugent #ifdef LOGIN_CAP
801ef60eb1SDavid Nugent #include <login_cap.h>
811ef60eb1SDavid Nugent #endif
821ef60eb1SDavid Nugent 
83*3f5ac575SEdward Tomasz Napierala #include "mntopts.h"
848fae3551SRodney W. Grimes #include "pathnames.h"
858fae3551SRodney W. Grimes 
868fae3551SRodney W. Grimes /*
878fae3551SRodney W. Grimes  * Sleep times; used to prevent thrashing.
888fae3551SRodney W. Grimes  */
898fae3551SRodney W. Grimes #define	GETTY_SPACING		 5	/* N secs minimum getty spacing */
908fae3551SRodney W. Grimes #define	GETTY_SLEEP		30	/* sleep N secs after spacing problem */
91b5df27e2SAndrey A. Chernov #define	GETTY_NSPACE		 3	/* max. spacing count to bring reaction */
928fae3551SRodney W. Grimes #define	WINDOW_WAIT		 3	/* wait N secs after starting window */
938fae3551SRodney W. Grimes #define	STALL_TIMEOUT		30	/* wait N secs after warning */
948fae3551SRodney W. Grimes #define	DEATH_WATCH		10	/* wait N secs for procs to die */
955df42cf4SPhilippe Charnier #define	DEATH_SCRIPT		120	/* wait for 2min for /etc/rc.shutdown */
96e82d5545SDavid Nugent #define	RESOURCE_RC		"daemon"
97e82d5545SDavid Nugent #define	RESOURCE_WINDOW		"default"
98e82d5545SDavid Nugent #define	RESOURCE_GETTY		"default"
998fae3551SRodney W. Grimes 
10045cfb1dcSXin LI static void handle(sig_t, ...);
10145cfb1dcSXin LI static void delset(sigset_t *, ...);
1028fae3551SRodney W. Grimes 
10345cfb1dcSXin LI static void stall(const char *, ...) __printflike(1, 2);
10445cfb1dcSXin LI static void warning(const char *, ...) __printflike(1, 2);
10545cfb1dcSXin LI static void emergency(const char *, ...) __printflike(1, 2);
10645cfb1dcSXin LI static void disaster(int);
10745cfb1dcSXin LI static void badsys(int);
108*3f5ac575SEdward Tomasz Napierala static void revoke_ttys(void);
10945cfb1dcSXin LI static int  runshutdown(void);
110ab03e6d5SXin LI static char *strk(char *);
1118fae3551SRodney W. Grimes 
1128fae3551SRodney W. Grimes /*
1138fae3551SRodney W. Grimes  * We really need a recursive typedef...
1148fae3551SRodney W. Grimes  * The following at least guarantees that the return type of (*state_t)()
1158fae3551SRodney W. Grimes  * is sufficiently wide to hold a function pointer.
1168fae3551SRodney W. Grimes  */
11773bf18edSWarner Losh typedef long (*state_func_t)(void);
11873bf18edSWarner Losh typedef state_func_t (*state_t)(void);
1198fae3551SRodney W. Grimes 
12045cfb1dcSXin LI static state_func_t single_user(void);
12145cfb1dcSXin LI static state_func_t runcom(void);
12245cfb1dcSXin LI static state_func_t read_ttys(void);
12345cfb1dcSXin LI static state_func_t multi_user(void);
12445cfb1dcSXin LI static state_func_t clean_ttys(void);
12545cfb1dcSXin LI static state_func_t catatonia(void);
12645cfb1dcSXin LI static state_func_t death(void);
127acf0ab06SJilles Tjoelker static state_func_t death_single(void);
128*3f5ac575SEdward Tomasz Napierala static state_func_t reroot(void);
129*3f5ac575SEdward Tomasz Napierala static state_func_t reroot_phase_two(void);
1308fae3551SRodney W. Grimes 
13145cfb1dcSXin LI static state_func_t run_script(const char *);
1321a7bec91SWarner Losh 
1331efe3c6bSEd Schouten static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
13477103ea3SPoul-Henning Kamp #define FALSE	0
13577103ea3SPoul-Henning Kamp #define TRUE	1
13677103ea3SPoul-Henning Kamp 
1371efe3c6bSEd Schouten static int Reboot = FALSE;
1381efe3c6bSEd Schouten static int howto = RB_AUTOBOOT;
1398fae3551SRodney W. Grimes 
1401efe3c6bSEd Schouten static int devfs;
14157622f22SPoul-Henning Kamp 
14245cfb1dcSXin LI static void transition(state_t);
14345cfb1dcSXin LI static state_t requested_transition;
144acf0ab06SJilles Tjoelker static state_t current_state = death_single;
1458fae3551SRodney W. Grimes 
1464c2c7b2cSEd Schouten static void open_console(void);
14745cfb1dcSXin LI static const char *get_shell(void);
14845cfb1dcSXin LI static void write_stderr(const char *message);
1498fae3551SRodney W. Grimes 
1508fae3551SRodney W. Grimes typedef struct init_session {
1518fae3551SRodney W. Grimes 	pid_t	se_process;		/* controlling process */
1528fae3551SRodney W. Grimes 	time_t	se_started;		/* used to avoid thrashing */
1538fae3551SRodney W. Grimes 	int	se_flags;		/* status of session */
1548fae3551SRodney W. Grimes #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
155b0b670eeSAlfred Perlstein #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
156b5df27e2SAndrey A. Chernov 	int	se_nspace;		/* spacing count */
1578fae3551SRodney W. Grimes 	char	*se_device;		/* filename of port */
1588fae3551SRodney W. Grimes 	char	*se_getty;		/* what to run on that port */
159b5df27e2SAndrey A. Chernov 	char	*se_getty_argv_space;   /* pre-parsed argument array space */
1608fae3551SRodney W. Grimes 	char	**se_getty_argv;	/* pre-parsed argument array */
1618fae3551SRodney W. Grimes 	char	*se_window;		/* window system (started only once) */
162b5df27e2SAndrey A. Chernov 	char	*se_window_argv_space;  /* pre-parsed argument array space */
1638fae3551SRodney W. Grimes 	char	**se_window_argv;	/* pre-parsed argument array */
164b5df27e2SAndrey A. Chernov 	char	*se_type;		/* default terminal type */
1658fae3551SRodney W. Grimes 	struct	init_session *se_prev;
1668fae3551SRodney W. Grimes 	struct	init_session *se_next;
1678fae3551SRodney W. Grimes } session_t;
1688fae3551SRodney W. Grimes 
16945cfb1dcSXin LI static void free_session(session_t *);
1700b57dd6bSJilles Tjoelker static session_t *new_session(session_t *, struct ttyent *);
17145cfb1dcSXin LI static session_t *sessions;
1728fae3551SRodney W. Grimes 
17345cfb1dcSXin LI static char **construct_argv(char *);
17445cfb1dcSXin LI static void start_window_system(session_t *);
17545cfb1dcSXin LI static void collect_child(pid_t);
17645cfb1dcSXin LI static pid_t start_getty(session_t *);
17745cfb1dcSXin LI static void transition_handler(int);
17845cfb1dcSXin LI static void alrm_handler(int);
17945cfb1dcSXin LI static void setsecuritylevel(int);
18045cfb1dcSXin LI static int getsecuritylevel(void);
18145cfb1dcSXin LI static int setupargv(session_t *, struct ttyent *);
182e82d5545SDavid Nugent #ifdef LOGIN_CAP
18345cfb1dcSXin LI static void setprocresources(const char *);
184e82d5545SDavid Nugent #endif
18545cfb1dcSXin LI static int clang;
1868fae3551SRodney W. Grimes 
18745cfb1dcSXin LI static int start_session_db(void);
18845cfb1dcSXin LI static void add_session(session_t *);
18945cfb1dcSXin LI static void del_session(session_t *);
19045cfb1dcSXin LI static session_t *find_session(pid_t);
19145cfb1dcSXin LI static DB *session_db;
1928fae3551SRodney W. Grimes 
1938fae3551SRodney W. Grimes /*
1948fae3551SRodney W. Grimes  * The mother of all processes.
1958fae3551SRodney W. Grimes  */
1968fae3551SRodney W. Grimes int
19773bf18edSWarner Losh main(int argc, char *argv[])
1988fae3551SRodney W. Grimes {
1991a7bec91SWarner Losh 	state_t initial_transition = runcom;
2001a7bec91SWarner Losh 	char kenv_value[PATH_MAX];
201*3f5ac575SEdward Tomasz Napierala 	int c, error;
2028fae3551SRodney W. Grimes 	struct sigaction sa;
2038fae3551SRodney W. Grimes 	sigset_t mask;
2048fae3551SRodney W. Grimes 
2058fae3551SRodney W. Grimes 	/* Dispose of random users. */
206c5842835SPhilippe Charnier 	if (getuid() != 0)
207c5842835SPhilippe Charnier 		errx(1, "%s", strerror(EPERM));
2088fae3551SRodney W. Grimes 
2098fae3551SRodney W. Grimes 	/* System V users like to reexec init. */
2101681d659SRuslan Ermilov 	if (getpid() != 1) {
2111681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT
2121681d659SRuslan Ermilov 		/* So give them what they want */
2131681d659SRuslan Ermilov 		if (argc > 1) {
2141681d659SRuslan Ermilov 			if (strlen(argv[1]) == 1) {
2153d438ad6SDavid E. O'Brien 				char runlevel = *argv[1];
2163d438ad6SDavid E. O'Brien 				int sig;
2178fae3551SRodney W. Grimes 
2181681d659SRuslan Ermilov 				switch (runlevel) {
2191681d659SRuslan Ermilov 				case '0': /* halt + poweroff */
2201681d659SRuslan Ermilov 					sig = SIGUSR2;
2211681d659SRuslan Ermilov 					break;
2221681d659SRuslan Ermilov 				case '1': /* single-user */
2231681d659SRuslan Ermilov 					sig = SIGTERM;
2241681d659SRuslan Ermilov 					break;
2251681d659SRuslan Ermilov 				case '6': /* reboot */
2261681d659SRuslan Ermilov 					sig = SIGINT;
2271681d659SRuslan Ermilov 					break;
2281681d659SRuslan Ermilov 				case 'c': /* block further logins */
2291681d659SRuslan Ermilov 					sig = SIGTSTP;
2301681d659SRuslan Ermilov 					break;
2311681d659SRuslan Ermilov 				case 'q': /* rescan /etc/ttys */
2321681d659SRuslan Ermilov 					sig = SIGHUP;
2331681d659SRuslan Ermilov 					break;
234*3f5ac575SEdward Tomasz Napierala 				case 'r': /* remount root */
235*3f5ac575SEdward Tomasz Napierala 					sig = SIGEMT;
236*3f5ac575SEdward Tomasz Napierala 					break;
2371681d659SRuslan Ermilov 				default:
2381681d659SRuslan Ermilov 					goto invalid;
2391681d659SRuslan Ermilov 				}
2401681d659SRuslan Ermilov 				kill(1, sig);
2411681d659SRuslan Ermilov 				_exit(0);
2421681d659SRuslan Ermilov 			} else
2431681d659SRuslan Ermilov invalid:
2441681d659SRuslan Ermilov 				errx(1, "invalid run-level ``%s''", argv[1]);
2451681d659SRuslan Ermilov 		} else
2461681d659SRuslan Ermilov #endif
2471681d659SRuslan Ermilov 			errx(1, "already running");
2481681d659SRuslan Ermilov 	}
2498fae3551SRodney W. Grimes 	/*
2508fae3551SRodney W. Grimes 	 * Note that this does NOT open a file...
2518fae3551SRodney W. Grimes 	 * Does 'init' deserve its own facility number?
2528fae3551SRodney W. Grimes 	 */
25306224a94SNeel Natu 	openlog("init", LOG_CONS, LOG_AUTH);
2548fae3551SRodney W. Grimes 
2558fae3551SRodney W. Grimes 	/*
2568fae3551SRodney W. Grimes 	 * Create an initial session.
2578fae3551SRodney W. Grimes 	 */
258*3f5ac575SEdward Tomasz Napierala 	if (setsid() < 0 && (errno != EPERM || getsid(0) != 1))
2598fae3551SRodney W. Grimes 		warning("initial setsid() failed: %m");
2608fae3551SRodney W. Grimes 
2618fae3551SRodney W. Grimes 	/*
2628fae3551SRodney W. Grimes 	 * Establish an initial user so that programs running
2638fae3551SRodney W. Grimes 	 * single user do not freak out and die (like passwd).
2648fae3551SRodney W. Grimes 	 */
2658fae3551SRodney W. Grimes 	if (setlogin("root") < 0)
2668fae3551SRodney W. Grimes 		warning("setlogin() failed: %m");
2678fae3551SRodney W. Grimes 
2688fae3551SRodney W. Grimes 	/*
2698fae3551SRodney W. Grimes 	 * This code assumes that we always get arguments through flags,
2708fae3551SRodney W. Grimes 	 * never through bits set in some random machine register.
2718fae3551SRodney W. Grimes 	 */
272*3f5ac575SEdward Tomasz Napierala 	while ((c = getopt(argc, argv, "dsfr")) != -1)
2738fae3551SRodney W. Grimes 		switch (c) {
27457622f22SPoul-Henning Kamp 		case 'd':
27557622f22SPoul-Henning Kamp 			devfs = 1;
27657622f22SPoul-Henning Kamp 			break;
2778fae3551SRodney W. Grimes 		case 's':
2781a7bec91SWarner Losh 			initial_transition = single_user;
2798fae3551SRodney W. Grimes 			break;
2808fae3551SRodney W. Grimes 		case 'f':
2818fae3551SRodney W. Grimes 			runcom_mode = FASTBOOT;
2828fae3551SRodney W. Grimes 			break;
283*3f5ac575SEdward Tomasz Napierala 		case 'r':
284*3f5ac575SEdward Tomasz Napierala 			initial_transition = reroot_phase_two;
285*3f5ac575SEdward Tomasz Napierala 			break;
2868fae3551SRodney W. Grimes 		default:
2878fae3551SRodney W. Grimes 			warning("unrecognized flag '-%c'", c);
2888fae3551SRodney W. Grimes 			break;
2898fae3551SRodney W. Grimes 		}
2908fae3551SRodney W. Grimes 
2918fae3551SRodney W. Grimes 	if (optind != argc)
2928fae3551SRodney W. Grimes 		warning("ignoring excess arguments");
2938fae3551SRodney W. Grimes 
2941a7bec91SWarner Losh 	/*
2951a7bec91SWarner Losh 	 * We catch or block signals rather than ignore them,
2961a7bec91SWarner Losh 	 * so that they get reset on exec.
2971a7bec91SWarner Losh 	 */
2981a7bec91SWarner Losh 	handle(badsys, SIGSYS, 0);
299091abe40SDavid E. O'Brien 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGXCPU,
300091abe40SDavid E. O'Brien 	    SIGXFSZ, 0);
301*3f5ac575SEdward Tomasz Napierala 	handle(transition_handler, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
302*3f5ac575SEdward Tomasz Napierala 	    SIGUSR1, SIGUSR2, 0);
3031a7bec91SWarner Losh 	handle(alrm_handler, SIGALRM, 0);
3041a7bec91SWarner Losh 	sigfillset(&mask);
3051a7bec91SWarner Losh 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
306*3f5ac575SEdward Tomasz Napierala 	    SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
307*3f5ac575SEdward Tomasz Napierala 	    SIGALRM, SIGUSR1, SIGUSR2, 0);
3081a7bec91SWarner Losh 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
3091a7bec91SWarner Losh 	sigemptyset(&sa.sa_mask);
3101a7bec91SWarner Losh 	sa.sa_flags = 0;
3111a7bec91SWarner Losh 	sa.sa_handler = SIG_IGN;
312091abe40SDavid E. O'Brien 	sigaction(SIGTTIN, &sa, (struct sigaction *)0);
313091abe40SDavid E. O'Brien 	sigaction(SIGTTOU, &sa, (struct sigaction *)0);
3141a7bec91SWarner Losh 
3151a7bec91SWarner Losh 	/*
3161a7bec91SWarner Losh 	 * Paranoia.
3171a7bec91SWarner Losh 	 */
3181a7bec91SWarner Losh 	close(0);
3191a7bec91SWarner Losh 	close(1);
3201a7bec91SWarner Losh 	close(2);
3211a7bec91SWarner Losh 
3221a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) {
3231a7bec91SWarner Losh 		state_func_t next_transition;
3241a7bec91SWarner Losh 
3251a7bec91SWarner Losh 		if ((next_transition = run_script(kenv_value)) != 0)
3261a7bec91SWarner Losh 			initial_transition = (state_t) next_transition;
3271a7bec91SWarner Losh 	}
3281a7bec91SWarner Losh 
3291a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) {
3301a7bec91SWarner Losh 		if (chdir(kenv_value) != 0 || chroot(".") != 0)
3311a7bec91SWarner Losh 			warning("Can't chroot to %s: %m", kenv_value);
3321a7bec91SWarner Losh 	}
3331a7bec91SWarner Losh 
3341a7bec91SWarner Losh 	/*
3351a7bec91SWarner Losh 	 * Additional check if devfs needs to be mounted:
3361a7bec91SWarner Losh 	 * If "/" and "/dev" have the same device number,
3371a7bec91SWarner Losh 	 * then it hasn't been mounted yet.
3381a7bec91SWarner Losh 	 */
3391a7bec91SWarner Losh 	if (!devfs) {
3401a7bec91SWarner Losh 		struct stat stst;
3411a7bec91SWarner Losh 		dev_t root_devno;
3421a7bec91SWarner Losh 
3431a7bec91SWarner Losh 		stat("/", &stst);
3441a7bec91SWarner Losh 		root_devno = stst.st_dev;
3451a7bec91SWarner Losh 		if (stat("/dev", &stst) != 0)
3461a7bec91SWarner Losh 			warning("Can't stat /dev: %m");
3471a7bec91SWarner Losh 		else if (stst.st_dev == root_devno)
3481a7bec91SWarner Losh 			devfs++;
3491a7bec91SWarner Losh 	}
3501a7bec91SWarner Losh 
35157622f22SPoul-Henning Kamp 	if (devfs) {
3521f083b1eSMaxime Henrion 		struct iovec iov[4];
353421b0201SPoul-Henning Kamp 		char *s;
354421b0201SPoul-Henning Kamp 		int i;
355421b0201SPoul-Henning Kamp 
356ab03e6d5SXin LI 		char _fstype[]	= "fstype";
357ab03e6d5SXin LI 		char _devfs[]	= "devfs";
358ab03e6d5SXin LI 		char _fspath[]	= "fspath";
359ab03e6d5SXin LI 		char _path_dev[]= _PATH_DEV;
360ab03e6d5SXin LI 
361ab03e6d5SXin LI 		iov[0].iov_base = _fstype;
362ab03e6d5SXin LI 		iov[0].iov_len = sizeof(_fstype);
363ab03e6d5SXin LI 		iov[1].iov_base = _devfs;
364ab03e6d5SXin LI 		iov[1].iov_len = sizeof(_devfs);
365ab03e6d5SXin LI 		iov[2].iov_base = _fspath;
366ab03e6d5SXin LI 		iov[2].iov_len = sizeof(_fspath);
367421b0201SPoul-Henning Kamp 		/*
368421b0201SPoul-Henning Kamp 		 * Try to avoid the trailing slash in _PATH_DEV.
369421b0201SPoul-Henning Kamp 		 * Be *very* defensive.
370421b0201SPoul-Henning Kamp 		 */
371421b0201SPoul-Henning Kamp 		s = strdup(_PATH_DEV);
372421b0201SPoul-Henning Kamp 		if (s != NULL) {
373421b0201SPoul-Henning Kamp 			i = strlen(s);
374421b0201SPoul-Henning Kamp 			if (i > 0 && s[i - 1] == '/')
375421b0201SPoul-Henning Kamp 				s[i - 1] = '\0';
3761f083b1eSMaxime Henrion 			iov[3].iov_base = s;
3771f083b1eSMaxime Henrion 			iov[3].iov_len = strlen(s) + 1;
378421b0201SPoul-Henning Kamp 		} else {
379ab03e6d5SXin LI 			iov[3].iov_base = _path_dev;
380ab03e6d5SXin LI 			iov[3].iov_len = sizeof(_path_dev);
38157622f22SPoul-Henning Kamp 		}
3821f083b1eSMaxime Henrion 		nmount(iov, 4, 0);
3831f083b1eSMaxime Henrion 		if (s != NULL)
3841f083b1eSMaxime Henrion 			free(s);
385421b0201SPoul-Henning Kamp 	}
38657622f22SPoul-Henning Kamp 
387*3f5ac575SEdward Tomasz Napierala 	if (initial_transition != reroot_phase_two) {
388*3f5ac575SEdward Tomasz Napierala 		/*
389*3f5ac575SEdward Tomasz Napierala 		 * Unmount reroot leftovers.  This runs after init(8)
390*3f5ac575SEdward Tomasz Napierala 		 * gets reexecuted after reroot_phase_two() is done.
391*3f5ac575SEdward Tomasz Napierala 		 */
392*3f5ac575SEdward Tomasz Napierala 		error = unmount(_PATH_REROOT, MNT_FORCE);
393*3f5ac575SEdward Tomasz Napierala 		if (error != 0 && errno != EINVAL)
394*3f5ac575SEdward Tomasz Napierala 			warning("Cannot unmount %s: %m", _PATH_REROOT);
395*3f5ac575SEdward Tomasz Napierala 	}
396*3f5ac575SEdward Tomasz Napierala 
3978fae3551SRodney W. Grimes 	/*
3988fae3551SRodney W. Grimes 	 * Start the state machine.
3998fae3551SRodney W. Grimes 	 */
4001a7bec91SWarner Losh 	transition(initial_transition);
4018fae3551SRodney W. Grimes 
4028fae3551SRodney W. Grimes 	/*
4038fae3551SRodney W. Grimes 	 * Should never reach here.
4048fae3551SRodney W. Grimes 	 */
4058fae3551SRodney W. Grimes 	return 1;
4068fae3551SRodney W. Grimes }
4078fae3551SRodney W. Grimes 
4088fae3551SRodney W. Grimes /*
4098fae3551SRodney W. Grimes  * Associate a function with a signal handler.
4108fae3551SRodney W. Grimes  */
41145cfb1dcSXin LI static void
4128fae3551SRodney W. Grimes handle(sig_t handler, ...)
4138fae3551SRodney W. Grimes {
4148fae3551SRodney W. Grimes 	int sig;
4158fae3551SRodney W. Grimes 	struct sigaction sa;
41639034633SJames Raynard 	sigset_t mask_everything;
4178fae3551SRodney W. Grimes 	va_list ap;
4188fae3551SRodney W. Grimes 	va_start(ap, handler);
4198fae3551SRodney W. Grimes 
4208fae3551SRodney W. Grimes 	sa.sa_handler = handler;
4218fae3551SRodney W. Grimes 	sigfillset(&mask_everything);
4228fae3551SRodney W. Grimes 
42330e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0) {
4248fae3551SRodney W. Grimes 		sa.sa_mask = mask_everything;
4258fae3551SRodney W. Grimes 		/* XXX SA_RESTART? */
4268fae3551SRodney W. Grimes 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
4278fae3551SRodney W. Grimes 		sigaction(sig, &sa, (struct sigaction *) 0);
4288fae3551SRodney W. Grimes 	}
4298fae3551SRodney W. Grimes 	va_end(ap);
4308fae3551SRodney W. Grimes }
4318fae3551SRodney W. Grimes 
4328fae3551SRodney W. Grimes /*
4338fae3551SRodney W. Grimes  * Delete a set of signals from a mask.
4348fae3551SRodney W. Grimes  */
43545cfb1dcSXin LI static void
4368fae3551SRodney W. Grimes delset(sigset_t *maskp, ...)
4378fae3551SRodney W. Grimes {
4388fae3551SRodney W. Grimes 	int sig;
4398fae3551SRodney W. Grimes 	va_list ap;
4408fae3551SRodney W. Grimes 	va_start(ap, maskp);
4418fae3551SRodney W. Grimes 
44230e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0)
4438fae3551SRodney W. Grimes 		sigdelset(maskp, sig);
4448fae3551SRodney W. Grimes 	va_end(ap);
4458fae3551SRodney W. Grimes }
4468fae3551SRodney W. Grimes 
4478fae3551SRodney W. Grimes /*
4488fae3551SRodney W. Grimes  * Log a message and sleep for a while (to give someone an opportunity
4498fae3551SRodney W. Grimes  * to read it and to save log or hardcopy output if the problem is chronic).
4508fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4518fae3551SRodney W. Grimes  */
45245cfb1dcSXin LI static void
4535979df34SKris Kennaway stall(const char *message, ...)
4548fae3551SRodney W. Grimes {
4558fae3551SRodney W. Grimes 	va_list ap;
4568fae3551SRodney W. Grimes 	va_start(ap, message);
4578fae3551SRodney W. Grimes 
4588fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4598fae3551SRodney W. Grimes 	va_end(ap);
4608fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
4618fae3551SRodney W. Grimes }
4628fae3551SRodney W. Grimes 
4638fae3551SRodney W. Grimes /*
4648fae3551SRodney W. Grimes  * Like stall(), but doesn't sleep.
4658fae3551SRodney W. Grimes  * If cpp had variadic macros, the two functions could be #defines for another.
4668fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4678fae3551SRodney W. Grimes  */
46845cfb1dcSXin LI static void
4695979df34SKris Kennaway warning(const char *message, ...)
4708fae3551SRodney W. Grimes {
4718fae3551SRodney W. Grimes 	va_list ap;
4728fae3551SRodney W. Grimes 	va_start(ap, message);
4738fae3551SRodney W. Grimes 
4748fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4758fae3551SRodney W. Grimes 	va_end(ap);
4768fae3551SRodney W. Grimes }
4778fae3551SRodney W. Grimes 
4788fae3551SRodney W. Grimes /*
4798fae3551SRodney W. Grimes  * Log an emergency message.
4808fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4818fae3551SRodney W. Grimes  */
48245cfb1dcSXin LI static void
4835979df34SKris Kennaway emergency(const char *message, ...)
4848fae3551SRodney W. Grimes {
4858fae3551SRodney W. Grimes 	va_list ap;
4868fae3551SRodney W. Grimes 	va_start(ap, message);
4878fae3551SRodney W. Grimes 
4888fae3551SRodney W. Grimes 	vsyslog(LOG_EMERG, message, ap);
4898fae3551SRodney W. Grimes 	va_end(ap);
4908fae3551SRodney W. Grimes }
4918fae3551SRodney W. Grimes 
4928fae3551SRodney W. Grimes /*
4938fae3551SRodney W. Grimes  * Catch a SIGSYS signal.
4948fae3551SRodney W. Grimes  *
4958fae3551SRodney W. Grimes  * These may arise if a system does not support sysctl.
4968fae3551SRodney W. Grimes  * We tolerate up to 25 of these, then throw in the towel.
4978fae3551SRodney W. Grimes  */
49845cfb1dcSXin LI static void
49973bf18edSWarner Losh badsys(int sig)
5008fae3551SRodney W. Grimes {
5018fae3551SRodney W. Grimes 	static int badcount = 0;
5028fae3551SRodney W. Grimes 
5038fae3551SRodney W. Grimes 	if (badcount++ < 25)
5048fae3551SRodney W. Grimes 		return;
5058fae3551SRodney W. Grimes 	disaster(sig);
5068fae3551SRodney W. Grimes }
5078fae3551SRodney W. Grimes 
5088fae3551SRodney W. Grimes /*
5098fae3551SRodney W. Grimes  * Catch an unexpected signal.
5108fae3551SRodney W. Grimes  */
51145cfb1dcSXin LI static void
51273bf18edSWarner Losh disaster(int sig)
5138fae3551SRodney W. Grimes {
514091abe40SDavid E. O'Brien 
5158fae3551SRodney W. Grimes 	emergency("fatal signal: %s",
5168889c700SDavid Nugent 	    (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
5178fae3551SRodney W. Grimes 
5188fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
5198fae3551SRodney W. Grimes 	_exit(sig);		/* reboot */
5208fae3551SRodney W. Grimes }
5218fae3551SRodney W. Grimes 
5228fae3551SRodney W. Grimes /*
5238fae3551SRodney W. Grimes  * Get the security level of the kernel.
5248fae3551SRodney W. Grimes  */
52545cfb1dcSXin LI static int
52673bf18edSWarner Losh getsecuritylevel(void)
5278fae3551SRodney W. Grimes {
5288fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5298fae3551SRodney W. Grimes 	int name[2], curlevel;
5308fae3551SRodney W. Grimes 	size_t len;
5318fae3551SRodney W. Grimes 
5328fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
5338fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
5348fae3551SRodney W. Grimes 	len = sizeof curlevel;
5358fae3551SRodney W. Grimes 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
5368fae3551SRodney W. Grimes 		emergency("cannot get kernel security level: %s",
5378fae3551SRodney W. Grimes 		    strerror(errno));
5388fae3551SRodney W. Grimes 		return (-1);
5398fae3551SRodney W. Grimes 	}
5408fae3551SRodney W. Grimes 	return (curlevel);
5418fae3551SRodney W. Grimes #else
5428fae3551SRodney W. Grimes 	return (-1);
5438fae3551SRodney W. Grimes #endif
5448fae3551SRodney W. Grimes }
5458fae3551SRodney W. Grimes 
5468fae3551SRodney W. Grimes /*
5478fae3551SRodney W. Grimes  * Set the security level of the kernel.
5488fae3551SRodney W. Grimes  */
54945cfb1dcSXin LI static void
55073bf18edSWarner Losh setsecuritylevel(int newlevel)
5518fae3551SRodney W. Grimes {
5528fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5538fae3551SRodney W. Grimes 	int name[2], curlevel;
5548fae3551SRodney W. Grimes 
5558fae3551SRodney W. Grimes 	curlevel = getsecuritylevel();
5568fae3551SRodney W. Grimes 	if (newlevel == curlevel)
5578fae3551SRodney W. Grimes 		return;
5588fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
5598fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
5608fae3551SRodney W. Grimes 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
5618fae3551SRodney W. Grimes 		emergency(
5628fae3551SRodney W. Grimes 		    "cannot change kernel security level from %d to %d: %s",
5638fae3551SRodney W. Grimes 		    curlevel, newlevel, strerror(errno));
5648fae3551SRodney W. Grimes 		return;
5658fae3551SRodney W. Grimes 	}
5668fae3551SRodney W. Grimes #ifdef SECURE
5678fae3551SRodney W. Grimes 	warning("kernel security level changed from %d to %d",
5688fae3551SRodney W. Grimes 	    curlevel, newlevel);
5698fae3551SRodney W. Grimes #endif
5708fae3551SRodney W. Grimes #endif
5718fae3551SRodney W. Grimes }
5728fae3551SRodney W. Grimes 
5738fae3551SRodney W. Grimes /*
5748fae3551SRodney W. Grimes  * Change states in the finite state machine.
5758fae3551SRodney W. Grimes  * The initial state is passed as an argument.
5768fae3551SRodney W. Grimes  */
57745cfb1dcSXin LI static void
57873bf18edSWarner Losh transition(state_t s)
5798fae3551SRodney W. Grimes {
580091abe40SDavid E. O'Brien 
581acf0ab06SJilles Tjoelker 	current_state = s;
5828fae3551SRodney W. Grimes 	for (;;)
583acf0ab06SJilles Tjoelker 		current_state = (state_t) (*current_state)();
5848fae3551SRodney W. Grimes }
5858fae3551SRodney W. Grimes 
5868fae3551SRodney W. Grimes /*
5878fae3551SRodney W. Grimes  * Start a session and allocate a controlling terminal.
5888fae3551SRodney W. Grimes  * Only called by children of init after forking.
5898fae3551SRodney W. Grimes  */
59045cfb1dcSXin LI static void
5914c2c7b2cSEd Schouten open_console(void)
5928fae3551SRodney W. Grimes {
5938fae3551SRodney W. Grimes 	int fd;
5948fae3551SRodney W. Grimes 
5956ee5808bSEd Schouten 	/*
5966ee5808bSEd Schouten 	 * Try to open /dev/console.  Open the device with O_NONBLOCK to
5976ee5808bSEd Schouten 	 * prevent potential blocking on a carrier.
5986ee5808bSEd Schouten 	 */
5994c2c7b2cSEd Schouten 	revoke(_PATH_CONSOLE);
6004c2c7b2cSEd Schouten 	if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
6016ee5808bSEd Schouten 		(void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
6024c2c7b2cSEd Schouten 		if (login_tty(fd) == 0)
6034c2c7b2cSEd Schouten 			return;
6044c2c7b2cSEd Schouten 		close(fd);
6054c2c7b2cSEd Schouten 	}
6064c2c7b2cSEd Schouten 
6074c2c7b2cSEd Schouten 	/* No luck.  Log output to file if possible. */
6084c2c7b2cSEd Schouten 	if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
6094c2c7b2cSEd Schouten 		stall("cannot open null device.");
6108fae3551SRodney W. Grimes 		_exit(1);
6118fae3551SRodney W. Grimes 	}
6124c2c7b2cSEd Schouten 	if (fd != STDIN_FILENO) {
6134c2c7b2cSEd Schouten 		dup2(fd, STDIN_FILENO);
6144c2c7b2cSEd Schouten 		close(fd);
6158fae3551SRodney W. Grimes 	}
6164c2c7b2cSEd Schouten 	fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
6174c2c7b2cSEd Schouten 	if (fd == -1)
6184c2c7b2cSEd Schouten 		dup2(STDIN_FILENO, STDOUT_FILENO);
6194c2c7b2cSEd Schouten 	else if (fd != STDOUT_FILENO) {
6204c2c7b2cSEd Schouten 		dup2(fd, STDOUT_FILENO);
6214c2c7b2cSEd Schouten 		close(fd);
6224c2c7b2cSEd Schouten 	}
6234c2c7b2cSEd Schouten 	dup2(STDOUT_FILENO, STDERR_FILENO);
6248fae3551SRodney W. Grimes }
6258fae3551SRodney W. Grimes 
62645cfb1dcSXin LI static const char *
6271a7bec91SWarner Losh get_shell(void)
6281a7bec91SWarner Losh {
6291a7bec91SWarner Losh 	static char kenv_value[PATH_MAX];
6301a7bec91SWarner Losh 
6311a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0)
6321a7bec91SWarner Losh 		return kenv_value;
6331a7bec91SWarner Losh 	else
6341a7bec91SWarner Losh 		return _PATH_BSHELL;
6351a7bec91SWarner Losh }
6361a7bec91SWarner Losh 
63745cfb1dcSXin LI static void
6381a7bec91SWarner Losh write_stderr(const char *message)
6391a7bec91SWarner Losh {
640091abe40SDavid E. O'Brien 
6411a7bec91SWarner Losh 	write(STDERR_FILENO, message, strlen(message));
6421a7bec91SWarner Losh }
6431a7bec91SWarner Losh 
644*3f5ac575SEdward Tomasz Napierala static int
645*3f5ac575SEdward Tomasz Napierala read_file(const char *path, void **bufp, size_t *bufsizep)
646*3f5ac575SEdward Tomasz Napierala {
647*3f5ac575SEdward Tomasz Napierala 	struct stat sb;
648*3f5ac575SEdward Tomasz Napierala 	size_t bufsize;
649*3f5ac575SEdward Tomasz Napierala 	void *buf;
650*3f5ac575SEdward Tomasz Napierala 	ssize_t nbytes;
651*3f5ac575SEdward Tomasz Napierala 	int error, fd;
652*3f5ac575SEdward Tomasz Napierala 
653*3f5ac575SEdward Tomasz Napierala 	fd = open(path, O_RDONLY);
654*3f5ac575SEdward Tomasz Napierala 	if (fd < 0) {
655*3f5ac575SEdward Tomasz Napierala 		emergency("%s: %s", path, strerror(errno));
656*3f5ac575SEdward Tomasz Napierala 		return (-1);
657*3f5ac575SEdward Tomasz Napierala 	}
658*3f5ac575SEdward Tomasz Napierala 
659*3f5ac575SEdward Tomasz Napierala 	error = fstat(fd, &sb);
660*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
661*3f5ac575SEdward Tomasz Napierala 		emergency("fstat: %s", strerror(errno));
662*3f5ac575SEdward Tomasz Napierala 		return (error);
663*3f5ac575SEdward Tomasz Napierala 	}
664*3f5ac575SEdward Tomasz Napierala 
665*3f5ac575SEdward Tomasz Napierala 	bufsize = sb.st_size;
666*3f5ac575SEdward Tomasz Napierala 	buf = malloc(bufsize);
667*3f5ac575SEdward Tomasz Napierala 	if (buf == NULL) {
668*3f5ac575SEdward Tomasz Napierala 		emergency("malloc: %s", strerror(errno));
669*3f5ac575SEdward Tomasz Napierala 		return (error);
670*3f5ac575SEdward Tomasz Napierala 	}
671*3f5ac575SEdward Tomasz Napierala 
672*3f5ac575SEdward Tomasz Napierala 	nbytes = read(fd, buf, bufsize);
673*3f5ac575SEdward Tomasz Napierala 	if (nbytes != (ssize_t)bufsize) {
674*3f5ac575SEdward Tomasz Napierala 		emergency("read: %s", strerror(errno));
675*3f5ac575SEdward Tomasz Napierala 		free(buf);
676*3f5ac575SEdward Tomasz Napierala 		return (error);
677*3f5ac575SEdward Tomasz Napierala 	}
678*3f5ac575SEdward Tomasz Napierala 
679*3f5ac575SEdward Tomasz Napierala 	error = close(fd);
680*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
681*3f5ac575SEdward Tomasz Napierala 		emergency("close: %s", strerror(errno));
682*3f5ac575SEdward Tomasz Napierala 		free(buf);
683*3f5ac575SEdward Tomasz Napierala 		return (error);
684*3f5ac575SEdward Tomasz Napierala 	}
685*3f5ac575SEdward Tomasz Napierala 
686*3f5ac575SEdward Tomasz Napierala 	*bufp = buf;
687*3f5ac575SEdward Tomasz Napierala 	*bufsizep = bufsize;
688*3f5ac575SEdward Tomasz Napierala 
689*3f5ac575SEdward Tomasz Napierala 	return (0);
690*3f5ac575SEdward Tomasz Napierala }
691*3f5ac575SEdward Tomasz Napierala 
692*3f5ac575SEdward Tomasz Napierala static int
693*3f5ac575SEdward Tomasz Napierala create_file(const char *path, void *buf, size_t bufsize)
694*3f5ac575SEdward Tomasz Napierala {
695*3f5ac575SEdward Tomasz Napierala 	ssize_t nbytes;
696*3f5ac575SEdward Tomasz Napierala 	int error, fd;
697*3f5ac575SEdward Tomasz Napierala 
698*3f5ac575SEdward Tomasz Napierala 	fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700);
699*3f5ac575SEdward Tomasz Napierala 	if (fd < 0) {
700*3f5ac575SEdward Tomasz Napierala 		emergency("%s: %s", path, strerror(errno));
701*3f5ac575SEdward Tomasz Napierala 		return (-1);
702*3f5ac575SEdward Tomasz Napierala 	}
703*3f5ac575SEdward Tomasz Napierala 
704*3f5ac575SEdward Tomasz Napierala 	nbytes = write(fd, buf, bufsize);
705*3f5ac575SEdward Tomasz Napierala 	if (nbytes != (ssize_t)bufsize) {
706*3f5ac575SEdward Tomasz Napierala 		emergency("write: %s", strerror(errno));
707*3f5ac575SEdward Tomasz Napierala 		return (-1);
708*3f5ac575SEdward Tomasz Napierala 	}
709*3f5ac575SEdward Tomasz Napierala 
710*3f5ac575SEdward Tomasz Napierala 	error = close(fd);
711*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
712*3f5ac575SEdward Tomasz Napierala 		emergency("close: %s", strerror(errno));
713*3f5ac575SEdward Tomasz Napierala 		free(buf);
714*3f5ac575SEdward Tomasz Napierala 		return (-1);
715*3f5ac575SEdward Tomasz Napierala 	}
716*3f5ac575SEdward Tomasz Napierala 
717*3f5ac575SEdward Tomasz Napierala 	return (0);
718*3f5ac575SEdward Tomasz Napierala }
719*3f5ac575SEdward Tomasz Napierala 
720*3f5ac575SEdward Tomasz Napierala static int
721*3f5ac575SEdward Tomasz Napierala mount_tmpfs(const char *fspath)
722*3f5ac575SEdward Tomasz Napierala {
723*3f5ac575SEdward Tomasz Napierala 	struct iovec *iov;
724*3f5ac575SEdward Tomasz Napierala 	char errmsg[255];
725*3f5ac575SEdward Tomasz Napierala 	int error, iovlen;
726*3f5ac575SEdward Tomasz Napierala 
727*3f5ac575SEdward Tomasz Napierala 	iov = NULL;
728*3f5ac575SEdward Tomasz Napierala 	iovlen = 0;
729*3f5ac575SEdward Tomasz Napierala 	memset(errmsg, 0, sizeof(errmsg));
730*3f5ac575SEdward Tomasz Napierala 	build_iovec(&iov, &iovlen, "fstype",
731*3f5ac575SEdward Tomasz Napierala 	    __DECONST(void *, "tmpfs"), (size_t)-1);
732*3f5ac575SEdward Tomasz Napierala 	build_iovec(&iov, &iovlen, "fspath",
733*3f5ac575SEdward Tomasz Napierala 	    __DECONST(void *, fspath), (size_t)-1);
734*3f5ac575SEdward Tomasz Napierala 	build_iovec(&iov, &iovlen, "errmsg",
735*3f5ac575SEdward Tomasz Napierala 	    errmsg, sizeof(errmsg));
736*3f5ac575SEdward Tomasz Napierala 
737*3f5ac575SEdward Tomasz Napierala 	error = nmount(iov, iovlen, 0);
738*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
739*3f5ac575SEdward Tomasz Napierala 		if (*errmsg != '\0') {
740*3f5ac575SEdward Tomasz Napierala 			emergency("cannot mount tmpfs on %s: %s: %s",
741*3f5ac575SEdward Tomasz Napierala 			    fspath, errmsg, strerror(errno));
742*3f5ac575SEdward Tomasz Napierala 		} else {
743*3f5ac575SEdward Tomasz Napierala 			emergency("cannot mount tmpfs on %s: %s",
744*3f5ac575SEdward Tomasz Napierala 			    fspath, strerror(errno));
745*3f5ac575SEdward Tomasz Napierala 		}
746*3f5ac575SEdward Tomasz Napierala 		return (error);
747*3f5ac575SEdward Tomasz Napierala 	}
748*3f5ac575SEdward Tomasz Napierala 	return (0);
749*3f5ac575SEdward Tomasz Napierala }
750*3f5ac575SEdward Tomasz Napierala 
751*3f5ac575SEdward Tomasz Napierala static state_func_t
752*3f5ac575SEdward Tomasz Napierala reroot(void)
753*3f5ac575SEdward Tomasz Napierala {
754*3f5ac575SEdward Tomasz Napierala 	void *buf;
755*3f5ac575SEdward Tomasz Napierala 	char init_path[PATH_MAX];
756*3f5ac575SEdward Tomasz Napierala 	size_t bufsize, init_path_len;
757*3f5ac575SEdward Tomasz Napierala 	int error, name[4];
758*3f5ac575SEdward Tomasz Napierala 
759*3f5ac575SEdward Tomasz Napierala 	name[0] = CTL_KERN;
760*3f5ac575SEdward Tomasz Napierala 	name[1] = KERN_PROC;
761*3f5ac575SEdward Tomasz Napierala 	name[2] = KERN_PROC_PATHNAME;
762*3f5ac575SEdward Tomasz Napierala 	name[3] = -1;
763*3f5ac575SEdward Tomasz Napierala 	init_path_len = sizeof(init_path);
764*3f5ac575SEdward Tomasz Napierala 	error = sysctl(name, 4, init_path, &init_path_len, NULL, 0);
765*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
766*3f5ac575SEdward Tomasz Napierala 		emergency("failed to get kern.proc.pathname: %s",
767*3f5ac575SEdward Tomasz Napierala 		    strerror(errno));
768*3f5ac575SEdward Tomasz Napierala 		goto out;
769*3f5ac575SEdward Tomasz Napierala 	}
770*3f5ac575SEdward Tomasz Napierala 
771*3f5ac575SEdward Tomasz Napierala 	revoke_ttys();
772*3f5ac575SEdward Tomasz Napierala 	runshutdown();
773*3f5ac575SEdward Tomasz Napierala 
774*3f5ac575SEdward Tomasz Napierala 	/*
775*3f5ac575SEdward Tomasz Napierala 	 * Make sure nobody can interfere with our scheme.
776*3f5ac575SEdward Tomasz Napierala 	 */
777*3f5ac575SEdward Tomasz Napierala 	error = kill(-1, SIGKILL);
778*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
779*3f5ac575SEdward Tomasz Napierala 		emergency("kill(2) failed: %s", strerror(errno));
780*3f5ac575SEdward Tomasz Napierala 		goto out;
781*3f5ac575SEdward Tomasz Napierala 	}
782*3f5ac575SEdward Tomasz Napierala 
783*3f5ac575SEdward Tomasz Napierala 	/*
784*3f5ac575SEdward Tomasz Napierala 	 * Pacify GCC.
785*3f5ac575SEdward Tomasz Napierala 	 */
786*3f5ac575SEdward Tomasz Napierala 	buf = NULL;
787*3f5ac575SEdward Tomasz Napierala 	bufsize = 0;
788*3f5ac575SEdward Tomasz Napierala 
789*3f5ac575SEdward Tomasz Napierala 	/*
790*3f5ac575SEdward Tomasz Napierala 	 * Copy the init binary into tmpfs, so that we can unmount
791*3f5ac575SEdward Tomasz Napierala 	 * the old rootfs without committing suicide.
792*3f5ac575SEdward Tomasz Napierala 	 */
793*3f5ac575SEdward Tomasz Napierala 	error = read_file(init_path, &buf, &bufsize);
794*3f5ac575SEdward Tomasz Napierala 	if (error != 0)
795*3f5ac575SEdward Tomasz Napierala 		goto out;
796*3f5ac575SEdward Tomasz Napierala 	error = mount_tmpfs(_PATH_REROOT);
797*3f5ac575SEdward Tomasz Napierala 	if (error != 0)
798*3f5ac575SEdward Tomasz Napierala 		goto out;
799*3f5ac575SEdward Tomasz Napierala 	error = create_file(_PATH_REROOT_INIT, buf, bufsize);
800*3f5ac575SEdward Tomasz Napierala 	if (error != 0)
801*3f5ac575SEdward Tomasz Napierala 		goto out;
802*3f5ac575SEdward Tomasz Napierala 
803*3f5ac575SEdward Tomasz Napierala 	/*
804*3f5ac575SEdward Tomasz Napierala 	 * Execute the temporary init.
805*3f5ac575SEdward Tomasz Napierala 	 */
806*3f5ac575SEdward Tomasz Napierala 	execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL);
807*3f5ac575SEdward Tomasz Napierala 	emergency("cannot exec %s: %s", _PATH_REROOT_INIT, strerror(errno));
808*3f5ac575SEdward Tomasz Napierala 
809*3f5ac575SEdward Tomasz Napierala out:
810*3f5ac575SEdward Tomasz Napierala 	emergency("reroot failed; going to single user mode");
811*3f5ac575SEdward Tomasz Napierala 	return (state_func_t) single_user;
812*3f5ac575SEdward Tomasz Napierala }
813*3f5ac575SEdward Tomasz Napierala 
814*3f5ac575SEdward Tomasz Napierala static state_func_t
815*3f5ac575SEdward Tomasz Napierala reroot_phase_two(void)
816*3f5ac575SEdward Tomasz Napierala {
817*3f5ac575SEdward Tomasz Napierala 	char init_path[PATH_MAX], *path, *path_component;
818*3f5ac575SEdward Tomasz Napierala 	size_t init_path_len;
819*3f5ac575SEdward Tomasz Napierala 	int nbytes, error;
820*3f5ac575SEdward Tomasz Napierala 
821*3f5ac575SEdward Tomasz Napierala 	/*
822*3f5ac575SEdward Tomasz Napierala 	 * Ask the kernel to mount the new rootfs.
823*3f5ac575SEdward Tomasz Napierala 	 */
824*3f5ac575SEdward Tomasz Napierala 	error = reboot(RB_REROOT);
825*3f5ac575SEdward Tomasz Napierala 	if (error != 0) {
826*3f5ac575SEdward Tomasz Napierala 		emergency("RB_REBOOT failed: %s", strerror(errno));
827*3f5ac575SEdward Tomasz Napierala 		goto out;
828*3f5ac575SEdward Tomasz Napierala 	}
829*3f5ac575SEdward Tomasz Napierala 
830*3f5ac575SEdward Tomasz Napierala 	/*
831*3f5ac575SEdward Tomasz Napierala 	 * Figure out where the destination init(8) binary is.  Note that
832*3f5ac575SEdward Tomasz Napierala 	 * the path could be different than what we've started with.  Use
833*3f5ac575SEdward Tomasz Napierala 	 * the value from kenv, if set, or the one from sysctl otherwise.
834*3f5ac575SEdward Tomasz Napierala 	 * The latter defaults to a hardcoded value, but can be overridden
835*3f5ac575SEdward Tomasz Napierala 	 * by a build time option.
836*3f5ac575SEdward Tomasz Napierala 	 */
837*3f5ac575SEdward Tomasz Napierala 	nbytes = kenv(KENV_GET, "init_path", init_path, sizeof(init_path));
838*3f5ac575SEdward Tomasz Napierala 	if (nbytes <= 0) {
839*3f5ac575SEdward Tomasz Napierala 		init_path_len = sizeof(init_path);
840*3f5ac575SEdward Tomasz Napierala 		error = sysctlbyname("kern.init_path",
841*3f5ac575SEdward Tomasz Napierala 		    init_path, &init_path_len, NULL, 0);
842*3f5ac575SEdward Tomasz Napierala 		if (error != 0) {
843*3f5ac575SEdward Tomasz Napierala 			emergency("failed to retrieve kern.init_path: %s",
844*3f5ac575SEdward Tomasz Napierala 			    strerror(errno));
845*3f5ac575SEdward Tomasz Napierala 			goto out;
846*3f5ac575SEdward Tomasz Napierala 		}
847*3f5ac575SEdward Tomasz Napierala 	}
848*3f5ac575SEdward Tomasz Napierala 
849*3f5ac575SEdward Tomasz Napierala 	/*
850*3f5ac575SEdward Tomasz Napierala 	 * Repeat the init search logic from sys/kern/init_path.c
851*3f5ac575SEdward Tomasz Napierala 	 */
852*3f5ac575SEdward Tomasz Napierala 	path_component = init_path;
853*3f5ac575SEdward Tomasz Napierala 	while ((path = strsep(&path_component, ":")) != NULL) {
854*3f5ac575SEdward Tomasz Napierala 		/*
855*3f5ac575SEdward Tomasz Napierala 		 * Execute init(8) from the new rootfs.
856*3f5ac575SEdward Tomasz Napierala 		 */
857*3f5ac575SEdward Tomasz Napierala 		execl(path, path, NULL);
858*3f5ac575SEdward Tomasz Napierala 	}
859*3f5ac575SEdward Tomasz Napierala 	emergency("cannot exec init from %s: %s", init_path, strerror(errno));
860*3f5ac575SEdward Tomasz Napierala 
861*3f5ac575SEdward Tomasz Napierala out:
862*3f5ac575SEdward Tomasz Napierala 	emergency("reroot failed; going to single user mode");
863*3f5ac575SEdward Tomasz Napierala 	return (state_func_t) single_user;
864*3f5ac575SEdward Tomasz Napierala }
865*3f5ac575SEdward Tomasz Napierala 
8668fae3551SRodney W. Grimes /*
8678fae3551SRodney W. Grimes  * Bring the system up single user.
8688fae3551SRodney W. Grimes  */
86945cfb1dcSXin LI static state_func_t
87073bf18edSWarner Losh single_user(void)
8718fae3551SRodney W. Grimes {
8728fae3551SRodney W. Grimes 	pid_t pid, wpid;
8738fae3551SRodney W. Grimes 	int status;
8748fae3551SRodney W. Grimes 	sigset_t mask;
8751a7bec91SWarner Losh 	const char *shell;
8768fae3551SRodney W. Grimes 	char *argv[2];
8778fae3551SRodney W. Grimes #ifdef SECURE
8788fae3551SRodney W. Grimes 	struct ttyent *typ;
8798fae3551SRodney W. Grimes 	struct passwd *pp;
8808fae3551SRodney W. Grimes 	static const char banner[] =
8818fae3551SRodney W. Grimes 		"Enter root password, or ^D to go multi-user\n";
8828fae3551SRodney W. Grimes 	char *clear, *password;
8838fae3551SRodney W. Grimes #endif
88463322c28SPoul-Henning Kamp #ifdef DEBUGSHELL
88563322c28SPoul-Henning Kamp 	char altshell[128];
88663322c28SPoul-Henning Kamp #endif
8878fae3551SRodney W. Grimes 
888db8ad19dSJordan K. Hubbard 	if (Reboot) {
889a0a549c7SRuslan Ermilov 		/* Instead of going single user, let's reboot the machine */
890e460cfd3SNate Williams 		sync();
891a0a549c7SRuslan Ermilov 		reboot(howto);
892e460cfd3SNate Williams 		_exit(0);
893e460cfd3SNate Williams 	}
894e460cfd3SNate Williams 
8951a7bec91SWarner Losh 	shell = get_shell();
8961a7bec91SWarner Losh 
8978fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
8988fae3551SRodney W. Grimes 		/*
8998fae3551SRodney W. Grimes 		 * Start the single user session.
9008fae3551SRodney W. Grimes 		 */
9014c2c7b2cSEd Schouten 		open_console();
9028fae3551SRodney W. Grimes 
9038fae3551SRodney W. Grimes #ifdef SECURE
9048fae3551SRodney W. Grimes 		/*
9058fae3551SRodney W. Grimes 		 * Check the root password.
9068fae3551SRodney W. Grimes 		 * We don't care if the console is 'on' by default;
9078fae3551SRodney W. Grimes 		 * it's the only tty that can be 'off' and 'secure'.
9088fae3551SRodney W. Grimes 		 */
9098fae3551SRodney W. Grimes 		typ = getttynam("console");
9108fae3551SRodney W. Grimes 		pp = getpwnam("root");
911a69497d7SMatthew Dillon 		if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
912a69497d7SMatthew Dillon 		    pp && *pp->pw_passwd) {
9131a7bec91SWarner Losh 			write_stderr(banner);
9148fae3551SRodney W. Grimes 			for (;;) {
9158fae3551SRodney W. Grimes 				clear = getpass("Password:");
9168fae3551SRodney W. Grimes 				if (clear == 0 || *clear == '\0')
9178fae3551SRodney W. Grimes 					_exit(0);
9188fae3551SRodney W. Grimes 				password = crypt(clear, pp->pw_passwd);
9198fae3551SRodney W. Grimes 				bzero(clear, _PASSWORD_LEN);
92029dcf726SKevin Lo 				if (password == NULL ||
92129dcf726SKevin Lo 				    strcmp(password, pp->pw_passwd) == 0)
9228fae3551SRodney W. Grimes 					break;
9238fae3551SRodney W. Grimes 				warning("single-user login failed\n");
9248fae3551SRodney W. Grimes 			}
9258fae3551SRodney W. Grimes 		}
9268fae3551SRodney W. Grimes 		endttyent();
9278fae3551SRodney W. Grimes 		endpwent();
9288fae3551SRodney W. Grimes #endif /* SECURE */
9298fae3551SRodney W. Grimes 
9308fae3551SRodney W. Grimes #ifdef DEBUGSHELL
9318fae3551SRodney W. Grimes 		{
93263322c28SPoul-Henning Kamp 			char *cp = altshell;
9338fae3551SRodney W. Grimes 			int num;
9348fae3551SRodney W. Grimes 
9351a7bec91SWarner Losh #define	SHREQUEST "Enter full pathname of shell or RETURN for "
9361a7bec91SWarner Losh 			write_stderr(SHREQUEST);
9371a7bec91SWarner Losh 			write_stderr(shell);
9381a7bec91SWarner Losh 			write_stderr(": ");
9398fae3551SRodney W. Grimes 			while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
9408fae3551SRodney W. Grimes 			    num != 0 && *cp != '\n' && cp < &altshell[127])
9418fae3551SRodney W. Grimes 				cp++;
9428fae3551SRodney W. Grimes 			*cp = '\0';
9438fae3551SRodney W. Grimes 			if (altshell[0] != '\0')
9448fae3551SRodney W. Grimes 				shell = altshell;
9458fae3551SRodney W. Grimes 		}
9468fae3551SRodney W. Grimes #endif /* DEBUGSHELL */
9478fae3551SRodney W. Grimes 
9488fae3551SRodney W. Grimes 		/*
9498fae3551SRodney W. Grimes 		 * Unblock signals.
9508fae3551SRodney W. Grimes 		 * We catch all the interesting ones,
9518fae3551SRodney W. Grimes 		 * and those are reset to SIG_DFL on exec.
9528fae3551SRodney W. Grimes 		 */
9538fae3551SRodney W. Grimes 		sigemptyset(&mask);
9548fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
9558fae3551SRodney W. Grimes 
9568fae3551SRodney W. Grimes 		/*
9578fae3551SRodney W. Grimes 		 * Fire off a shell.
9588fae3551SRodney W. Grimes 		 * If the default one doesn't work, try the Bourne shell.
9598fae3551SRodney W. Grimes 		 */
960ab03e6d5SXin LI 
961ab03e6d5SXin LI 		char name[] = "-sh";
962ab03e6d5SXin LI 
963ab03e6d5SXin LI 		argv[0] = name;
9648fae3551SRodney W. Grimes 		argv[1] = 0;
9658fae3551SRodney W. Grimes 		execv(shell, argv);
9668fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", shell);
9678fae3551SRodney W. Grimes 		execv(_PATH_BSHELL, argv);
9688fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
9698fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
9708fae3551SRodney W. Grimes 		_exit(1);
9718fae3551SRodney W. Grimes 	}
9728fae3551SRodney W. Grimes 
9738fae3551SRodney W. Grimes 	if (pid == -1) {
9748fae3551SRodney W. Grimes 		/*
9758fae3551SRodney W. Grimes 		 * We are seriously hosed.  Do our best.
9768fae3551SRodney W. Grimes 		 */
9778fae3551SRodney W. Grimes 		emergency("can't fork single-user shell, trying again");
9788fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
9798fae3551SRodney W. Grimes 			continue;
9808fae3551SRodney W. Grimes 		return (state_func_t) single_user;
9818fae3551SRodney W. Grimes 	}
9828fae3551SRodney W. Grimes 
9838fae3551SRodney W. Grimes 	requested_transition = 0;
9848fae3551SRodney W. Grimes 	do {
9858fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
9868fae3551SRodney W. Grimes 			collect_child(wpid);
9878fae3551SRodney W. Grimes 		if (wpid == -1) {
9888fae3551SRodney W. Grimes 			if (errno == EINTR)
9898fae3551SRodney W. Grimes 				continue;
9908fae3551SRodney W. Grimes 			warning("wait for single-user shell failed: %m; restarting");
9918fae3551SRodney W. Grimes 			return (state_func_t) single_user;
9928fae3551SRodney W. Grimes 		}
9938fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
9948fae3551SRodney W. Grimes 			warning("init: shell stopped, restarting\n");
9958fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
9968fae3551SRodney W. Grimes 			wpid = -1;
9978fae3551SRodney W. Grimes 		}
9988fae3551SRodney W. Grimes 	} while (wpid != pid && !requested_transition);
9998fae3551SRodney W. Grimes 
10008fae3551SRodney W. Grimes 	if (requested_transition)
10018fae3551SRodney W. Grimes 		return (state_func_t) requested_transition;
10028fae3551SRodney W. Grimes 
10038fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
10048fae3551SRodney W. Grimes 		if (WTERMSIG(status) == SIGKILL) {
10058fae3551SRodney W. Grimes 			/*
10068fae3551SRodney W. Grimes 			 *  reboot(8) killed shell?
10078fae3551SRodney W. Grimes 			 */
10088fae3551SRodney W. Grimes 			warning("single user shell terminated.");
10098fae3551SRodney W. Grimes 			sleep(STALL_TIMEOUT);
10108fae3551SRodney W. Grimes 			_exit(0);
10118fae3551SRodney W. Grimes 		} else {
10128fae3551SRodney W. Grimes 			warning("single user shell terminated, restarting");
10138fae3551SRodney W. Grimes 			return (state_func_t) single_user;
10148fae3551SRodney W. Grimes 		}
10158fae3551SRodney W. Grimes 	}
10168fae3551SRodney W. Grimes 
10178fae3551SRodney W. Grimes 	runcom_mode = FASTBOOT;
10188fae3551SRodney W. Grimes 	return (state_func_t) runcom;
10198fae3551SRodney W. Grimes }
10208fae3551SRodney W. Grimes 
10218fae3551SRodney W. Grimes /*
10228fae3551SRodney W. Grimes  * Run the system startup script.
10238fae3551SRodney W. Grimes  */
102445cfb1dcSXin LI static state_func_t
102573bf18edSWarner Losh runcom(void)
10268fae3551SRodney W. Grimes {
10271a7bec91SWarner Losh 	state_func_t next_transition;
10281a7bec91SWarner Losh 
10291a7bec91SWarner Losh 	if ((next_transition = run_script(_PATH_RUNCOM)) != 0)
10301a7bec91SWarner Losh 		return next_transition;
10311a7bec91SWarner Losh 
10321a7bec91SWarner Losh 	runcom_mode = AUTOBOOT;		/* the default */
10331a7bec91SWarner Losh 	return (state_func_t) read_ttys;
10341a7bec91SWarner Losh }
10351a7bec91SWarner Losh 
10361a7bec91SWarner Losh /*
10371a7bec91SWarner Losh  * Run a shell script.
10381a7bec91SWarner Losh  * Returns 0 on success, otherwise the next transition to enter:
10391a7bec91SWarner Losh  *  - single_user if fork/execv/waitpid failed, or if the script
10401a7bec91SWarner Losh  *    terminated with a signal or exit code != 0.
1041acf0ab06SJilles Tjoelker  *  - death_single if a SIGTERM was delivered to init(8).
10421a7bec91SWarner Losh  */
104345cfb1dcSXin LI static state_func_t
10441a7bec91SWarner Losh run_script(const char *script)
10451a7bec91SWarner Losh {
10468fae3551SRodney W. Grimes 	pid_t pid, wpid;
10478fae3551SRodney W. Grimes 	int status;
10488fae3551SRodney W. Grimes 	char *argv[4];
10491a7bec91SWarner Losh 	const char *shell;
10508fae3551SRodney W. Grimes 	struct sigaction sa;
10518fae3551SRodney W. Grimes 
10521a7bec91SWarner Losh 	shell = get_shell();
10531a7bec91SWarner Losh 
10548fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
10558fae3551SRodney W. Grimes 		sigemptyset(&sa.sa_mask);
10568fae3551SRodney W. Grimes 		sa.sa_flags = 0;
10578fae3551SRodney W. Grimes 		sa.sa_handler = SIG_IGN;
1058091abe40SDavid E. O'Brien 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
1059091abe40SDavid E. O'Brien 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
10608fae3551SRodney W. Grimes 
10614c2c7b2cSEd Schouten 		open_console();
10628fae3551SRodney W. Grimes 
1063ab03e6d5SXin LI 		char _sh[]		= "sh";
1064ab03e6d5SXin LI 		char _autoboot[]	= "autoboot";
1065ab03e6d5SXin LI 
1066ab03e6d5SXin LI 		argv[0] = _sh;
10671a7bec91SWarner Losh 		argv[1] = __DECONST(char *, script);
1068ab03e6d5SXin LI 		argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0;
10698fae3551SRodney W. Grimes 		argv[3] = 0;
10708fae3551SRodney W. Grimes 
10718fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
10728fae3551SRodney W. Grimes 
10731ef60eb1SDavid Nugent #ifdef LOGIN_CAP
10741ef60eb1SDavid Nugent 		setprocresources(RESOURCE_RC);
10751ef60eb1SDavid Nugent #endif
10761a7bec91SWarner Losh 		execv(shell, argv);
10771a7bec91SWarner Losh 		stall("can't exec %s for %s: %m", shell, script);
10788fae3551SRodney W. Grimes 		_exit(1);	/* force single user mode */
10798fae3551SRodney W. Grimes 	}
10808fae3551SRodney W. Grimes 
10818fae3551SRodney W. Grimes 	if (pid == -1) {
10821a7bec91SWarner Losh 		emergency("can't fork for %s on %s: %m", shell, script);
10838fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
10848fae3551SRodney W. Grimes 			continue;
10858fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
10868fae3551SRodney W. Grimes 		return (state_func_t) single_user;
10878fae3551SRodney W. Grimes 	}
10888fae3551SRodney W. Grimes 
10898fae3551SRodney W. Grimes 	/*
10908fae3551SRodney W. Grimes 	 * Copied from single_user().  This is a bit paranoid.
10918fae3551SRodney W. Grimes 	 */
10926e8ff8b7SDag-Erling Smørgrav 	requested_transition = 0;
10938fae3551SRodney W. Grimes 	do {
10948fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
10958fae3551SRodney W. Grimes 			collect_child(wpid);
10968fae3551SRodney W. Grimes 		if (wpid == -1) {
1097*3f5ac575SEdward Tomasz Napierala 			if (requested_transition == death_single ||
1098*3f5ac575SEdward Tomasz Napierala 			    requested_transition == reroot)
1099*3f5ac575SEdward Tomasz Napierala 				return (state_func_t) requested_transition;
11008fae3551SRodney W. Grimes 			if (errno == EINTR)
11018fae3551SRodney W. Grimes 				continue;
11021a7bec91SWarner Losh 			warning("wait for %s on %s failed: %m; going to "
11031a7bec91SWarner Losh 			    "single user mode", shell, script);
11048fae3551SRodney W. Grimes 			return (state_func_t) single_user;
11058fae3551SRodney W. Grimes 		}
11068fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
11078fae3551SRodney W. Grimes 			warning("init: %s on %s stopped, restarting\n",
11081a7bec91SWarner Losh 			    shell, script);
11098fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
11108fae3551SRodney W. Grimes 			wpid = -1;
11118fae3551SRodney W. Grimes 		}
11128fae3551SRodney W. Grimes 	} while (wpid != pid);
11138fae3551SRodney W. Grimes 
11148fae3551SRodney W. Grimes 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
11158fae3551SRodney W. Grimes 	    requested_transition == catatonia) {
11168fae3551SRodney W. Grimes 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
11178fae3551SRodney W. Grimes 		sigset_t s;
11188fae3551SRodney W. Grimes 
11198fae3551SRodney W. Grimes 		sigfillset(&s);
11208fae3551SRodney W. Grimes 		for (;;)
11218fae3551SRodney W. Grimes 			sigsuspend(&s);
11228fae3551SRodney W. Grimes 	}
11238fae3551SRodney W. Grimes 
11248fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
11251a7bec91SWarner Losh 		warning("%s on %s terminated abnormally, going to single "
11261a7bec91SWarner Losh 		    "user mode", shell, script);
11278fae3551SRodney W. Grimes 		return (state_func_t) single_user;
11288fae3551SRodney W. Grimes 	}
11298fae3551SRodney W. Grimes 
11308fae3551SRodney W. Grimes 	if (WEXITSTATUS(status))
11318fae3551SRodney W. Grimes 		return (state_func_t) single_user;
11328fae3551SRodney W. Grimes 
11331a7bec91SWarner Losh 	return (state_func_t) 0;
11348fae3551SRodney W. Grimes }
11358fae3551SRodney W. Grimes 
11368fae3551SRodney W. Grimes /*
11378fae3551SRodney W. Grimes  * Open the session database.
11388fae3551SRodney W. Grimes  *
11398fae3551SRodney W. Grimes  * NB: We could pass in the size here; is it necessary?
11408fae3551SRodney W. Grimes  */
114145cfb1dcSXin LI static int
114273bf18edSWarner Losh start_session_db(void)
11438fae3551SRodney W. Grimes {
11448fae3551SRodney W. Grimes 	if (session_db && (*session_db->close)(session_db))
11458fae3551SRodney W. Grimes 		emergency("session database close: %s", strerror(errno));
11468fae3551SRodney W. Grimes 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
11478fae3551SRodney W. Grimes 		emergency("session database open: %s", strerror(errno));
11488fae3551SRodney W. Grimes 		return (1);
11498fae3551SRodney W. Grimes 	}
11508fae3551SRodney W. Grimes 	return (0);
11518fae3551SRodney W. Grimes 
11528fae3551SRodney W. Grimes }
11538fae3551SRodney W. Grimes 
11548fae3551SRodney W. Grimes /*
11558fae3551SRodney W. Grimes  * Add a new login session.
11568fae3551SRodney W. Grimes  */
115745cfb1dcSXin LI static void
115873bf18edSWarner Losh add_session(session_t *sp)
11598fae3551SRodney W. Grimes {
11608fae3551SRodney W. Grimes 	DBT key;
11618fae3551SRodney W. Grimes 	DBT data;
11628fae3551SRodney W. Grimes 
11638fae3551SRodney W. Grimes 	key.data = &sp->se_process;
11648fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
11658fae3551SRodney W. Grimes 	data.data = &sp;
11668fae3551SRodney W. Grimes 	data.size = sizeof sp;
11678fae3551SRodney W. Grimes 
11688fae3551SRodney W. Grimes 	if ((*session_db->put)(session_db, &key, &data, 0))
11698fae3551SRodney W. Grimes 		emergency("insert %d: %s", sp->se_process, strerror(errno));
11708fae3551SRodney W. Grimes }
11718fae3551SRodney W. Grimes 
11728fae3551SRodney W. Grimes /*
11738fae3551SRodney W. Grimes  * Delete an old login session.
11748fae3551SRodney W. Grimes  */
117545cfb1dcSXin LI static void
117673bf18edSWarner Losh del_session(session_t *sp)
11778fae3551SRodney W. Grimes {
11788fae3551SRodney W. Grimes 	DBT key;
11798fae3551SRodney W. Grimes 
11808fae3551SRodney W. Grimes 	key.data = &sp->se_process;
11818fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
11828fae3551SRodney W. Grimes 
11838fae3551SRodney W. Grimes 	if ((*session_db->del)(session_db, &key, 0))
11848fae3551SRodney W. Grimes 		emergency("delete %d: %s", sp->se_process, strerror(errno));
11858fae3551SRodney W. Grimes }
11868fae3551SRodney W. Grimes 
11878fae3551SRodney W. Grimes /*
11888fae3551SRodney W. Grimes  * Look up a login session by pid.
11898fae3551SRodney W. Grimes  */
119045cfb1dcSXin LI static session_t *
11918fae3551SRodney W. Grimes find_session(pid_t pid)
11928fae3551SRodney W. Grimes {
11938fae3551SRodney W. Grimes 	DBT key;
11948fae3551SRodney W. Grimes 	DBT data;
11958fae3551SRodney W. Grimes 	session_t *ret;
11968fae3551SRodney W. Grimes 
11978fae3551SRodney W. Grimes 	key.data = &pid;
11988fae3551SRodney W. Grimes 	key.size = sizeof pid;
11998fae3551SRodney W. Grimes 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
12008fae3551SRodney W. Grimes 		return 0;
12018fae3551SRodney W. Grimes 	bcopy(data.data, (char *)&ret, sizeof(ret));
12028fae3551SRodney W. Grimes 	return ret;
12038fae3551SRodney W. Grimes }
12048fae3551SRodney W. Grimes 
12058fae3551SRodney W. Grimes /*
12068fae3551SRodney W. Grimes  * Construct an argument vector from a command line.
12078fae3551SRodney W. Grimes  */
120845cfb1dcSXin LI static char **
120973bf18edSWarner Losh construct_argv(char *command)
12108fae3551SRodney W. Grimes {
12113d438ad6SDavid E. O'Brien 	int argc = 0;
12123d438ad6SDavid E. O'Brien 	char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
12138fae3551SRodney W. Grimes 						* sizeof (char *));
12148fae3551SRodney W. Grimes 
12156be40c95SRuslan Ermilov 	if ((argv[argc++] = strk(command)) == 0) {
12166be40c95SRuslan Ermilov 		free(argv);
12176be40c95SRuslan Ermilov 		return (NULL);
12186be40c95SRuslan Ermilov 	}
12198889c700SDavid Nugent 	while ((argv[argc++] = strk((char *) 0)) != NULL)
12208fae3551SRodney W. Grimes 		continue;
12218fae3551SRodney W. Grimes 	return argv;
12228fae3551SRodney W. Grimes }
12238fae3551SRodney W. Grimes 
12248fae3551SRodney W. Grimes /*
12258fae3551SRodney W. Grimes  * Deallocate a session descriptor.
12268fae3551SRodney W. Grimes  */
122745cfb1dcSXin LI static void
122873bf18edSWarner Losh free_session(session_t *sp)
12298fae3551SRodney W. Grimes {
12308fae3551SRodney W. Grimes 	free(sp->se_device);
12318fae3551SRodney W. Grimes 	if (sp->se_getty) {
12328fae3551SRodney W. Grimes 		free(sp->se_getty);
1233b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
12348fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
12358fae3551SRodney W. Grimes 	}
12368fae3551SRodney W. Grimes 	if (sp->se_window) {
12378fae3551SRodney W. Grimes 		free(sp->se_window);
1238b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
12398fae3551SRodney W. Grimes 		free(sp->se_window_argv);
12408fae3551SRodney W. Grimes 	}
1241b5df27e2SAndrey A. Chernov 	if (sp->se_type)
1242b5df27e2SAndrey A. Chernov 		free(sp->se_type);
12438fae3551SRodney W. Grimes 	free(sp);
12448fae3551SRodney W. Grimes }
12458fae3551SRodney W. Grimes 
12468fae3551SRodney W. Grimes /*
12478fae3551SRodney W. Grimes  * Allocate a new session descriptor.
1248b0b670eeSAlfred Perlstein  * Mark it SE_PRESENT.
12498fae3551SRodney W. Grimes  */
125045cfb1dcSXin LI static session_t *
12510b57dd6bSJilles Tjoelker new_session(session_t *sprev, struct ttyent *typ)
12528fae3551SRodney W. Grimes {
12533d438ad6SDavid E. O'Brien 	session_t *sp;
12544cbf8903SPaul Traina 	int fd;
12558fae3551SRodney W. Grimes 
12568fae3551SRodney W. Grimes 	if ((typ->ty_status & TTY_ON) == 0 ||
12578fae3551SRodney W. Grimes 	    typ->ty_name == 0 ||
12588fae3551SRodney W. Grimes 	    typ->ty_getty == 0)
12598fae3551SRodney W. Grimes 		return 0;
12608fae3551SRodney W. Grimes 
12611054bb1eSAndrey A. Chernov 	sp = (session_t *) calloc(1, sizeof (session_t));
12628fae3551SRodney W. Grimes 
1263b0b670eeSAlfred Perlstein 	sp->se_flags |= SE_PRESENT;
12648fae3551SRodney W. Grimes 
12658fae3551SRodney W. Grimes 	sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
1266091abe40SDavid E. O'Brien 	sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
12678fae3551SRodney W. Grimes 
12684cbf8903SPaul Traina 	/*
12694cbf8903SPaul Traina 	 * Attempt to open the device, if we get "device not configured"
12704cbf8903SPaul Traina 	 * then don't add the device to the session list.
12714cbf8903SPaul Traina 	 */
12724cbf8903SPaul Traina 	if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) {
127389e3b380SWarner Losh 		if (errno == ENXIO) {
12744cbf8903SPaul Traina 			free_session(sp);
12754cbf8903SPaul Traina 			return (0);
12764cbf8903SPaul Traina 		}
12774cbf8903SPaul Traina 	} else
12784cbf8903SPaul Traina 		close(fd);
12794cbf8903SPaul Traina 
12808fae3551SRodney W. Grimes 	if (setupargv(sp, typ) == 0) {
12818fae3551SRodney W. Grimes 		free_session(sp);
12828fae3551SRodney W. Grimes 		return (0);
12838fae3551SRodney W. Grimes 	}
12848fae3551SRodney W. Grimes 
12858fae3551SRodney W. Grimes 	sp->se_next = 0;
12868fae3551SRodney W. Grimes 	if (sprev == 0) {
12878fae3551SRodney W. Grimes 		sessions = sp;
12888fae3551SRodney W. Grimes 		sp->se_prev = 0;
12898fae3551SRodney W. Grimes 	} else {
12908fae3551SRodney W. Grimes 		sprev->se_next = sp;
12918fae3551SRodney W. Grimes 		sp->se_prev = sprev;
12928fae3551SRodney W. Grimes 	}
12938fae3551SRodney W. Grimes 
12948fae3551SRodney W. Grimes 	return sp;
12958fae3551SRodney W. Grimes }
12968fae3551SRodney W. Grimes 
12978fae3551SRodney W. Grimes /*
12988fae3551SRodney W. Grimes  * Calculate getty and if useful window argv vectors.
12998fae3551SRodney W. Grimes  */
130045cfb1dcSXin LI static int
130173bf18edSWarner Losh setupargv(session_t *sp, struct ttyent *typ)
13028fae3551SRodney W. Grimes {
13038fae3551SRodney W. Grimes 
13048fae3551SRodney W. Grimes 	if (sp->se_getty) {
13058fae3551SRodney W. Grimes 		free(sp->se_getty);
1306b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
13078fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
13088fae3551SRodney W. Grimes 	}
13098fae3551SRodney W. Grimes 	sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
1310091abe40SDavid E. O'Brien 	sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
1311b5df27e2SAndrey A. Chernov 	sp->se_getty_argv_space = strdup(sp->se_getty);
1312b5df27e2SAndrey A. Chernov 	sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
13138fae3551SRodney W. Grimes 	if (sp->se_getty_argv == 0) {
13148fae3551SRodney W. Grimes 		warning("can't parse getty for port %s", sp->se_device);
13158fae3551SRodney W. Grimes 		free(sp->se_getty);
1316b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
1317b5df27e2SAndrey A. Chernov 		sp->se_getty = sp->se_getty_argv_space = 0;
13188fae3551SRodney W. Grimes 		return (0);
13198fae3551SRodney W. Grimes 	}
1320b5df27e2SAndrey A. Chernov 	if (sp->se_window) {
13218fae3551SRodney W. Grimes 		free(sp->se_window);
1322b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
1323b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv);
1324b5df27e2SAndrey A. Chernov 	}
1325b5df27e2SAndrey A. Chernov 	sp->se_window = sp->se_window_argv_space = 0;
1326b5df27e2SAndrey A. Chernov 	sp->se_window_argv = 0;
1327b5df27e2SAndrey A. Chernov 	if (typ->ty_window) {
13288fae3551SRodney W. Grimes 		sp->se_window = strdup(typ->ty_window);
1329b5df27e2SAndrey A. Chernov 		sp->se_window_argv_space = strdup(sp->se_window);
1330b5df27e2SAndrey A. Chernov 		sp->se_window_argv = construct_argv(sp->se_window_argv_space);
13318fae3551SRodney W. Grimes 		if (sp->se_window_argv == 0) {
13328fae3551SRodney W. Grimes 			warning("can't parse window for port %s",
13338fae3551SRodney W. Grimes 			    sp->se_device);
1334b5df27e2SAndrey A. Chernov 			free(sp->se_window_argv_space);
13358fae3551SRodney W. Grimes 			free(sp->se_window);
1336b5df27e2SAndrey A. Chernov 			sp->se_window = sp->se_window_argv_space = 0;
13378fae3551SRodney W. Grimes 			return (0);
13388fae3551SRodney W. Grimes 		}
13398fae3551SRodney W. Grimes 	}
1340b5df27e2SAndrey A. Chernov 	if (sp->se_type)
1341b5df27e2SAndrey A. Chernov 		free(sp->se_type);
1342b5df27e2SAndrey A. Chernov 	sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
13438fae3551SRodney W. Grimes 	return (1);
13448fae3551SRodney W. Grimes }
13458fae3551SRodney W. Grimes 
13468fae3551SRodney W. Grimes /*
13478fae3551SRodney W. Grimes  * Walk the list of ttys and create sessions for each active line.
13488fae3551SRodney W. Grimes  */
134945cfb1dcSXin LI static state_func_t
135073bf18edSWarner Losh read_ttys(void)
13518fae3551SRodney W. Grimes {
13523d438ad6SDavid E. O'Brien 	session_t *sp, *snext;
13533d438ad6SDavid E. O'Brien 	struct ttyent *typ;
13548fae3551SRodney W. Grimes 
13558fae3551SRodney W. Grimes 	/*
13568fae3551SRodney W. Grimes 	 * Destroy any previous session state.
13578fae3551SRodney W. Grimes 	 * There shouldn't be any, but just in case...
13588fae3551SRodney W. Grimes 	 */
13598fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = snext) {
13608fae3551SRodney W. Grimes 		snext = sp->se_next;
13618fae3551SRodney W. Grimes 		free_session(sp);
13628fae3551SRodney W. Grimes 	}
13638fae3551SRodney W. Grimes 	sessions = 0;
13648fae3551SRodney W. Grimes 	if (start_session_db())
13658fae3551SRodney W. Grimes 		return (state_func_t) single_user;
13668fae3551SRodney W. Grimes 
13678fae3551SRodney W. Grimes 	/*
13688fae3551SRodney W. Grimes 	 * Allocate a session entry for each active port.
13698fae3551SRodney W. Grimes 	 * Note that sp starts at 0.
13708fae3551SRodney W. Grimes 	 */
13718889c700SDavid Nugent 	while ((typ = getttyent()) != NULL)
13720b57dd6bSJilles Tjoelker 		if ((snext = new_session(sp, typ)) != NULL)
13738fae3551SRodney W. Grimes 			sp = snext;
13748fae3551SRodney W. Grimes 
13758fae3551SRodney W. Grimes 	endttyent();
13768fae3551SRodney W. Grimes 
13778fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
13788fae3551SRodney W. Grimes }
13798fae3551SRodney W. Grimes 
13808fae3551SRodney W. Grimes /*
13818fae3551SRodney W. Grimes  * Start a window system running.
13828fae3551SRodney W. Grimes  */
138345cfb1dcSXin LI static void
138473bf18edSWarner Losh start_window_system(session_t *sp)
13858fae3551SRodney W. Grimes {
13868fae3551SRodney W. Grimes 	pid_t pid;
13878fae3551SRodney W. Grimes 	sigset_t mask;
1388b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
13895010c3b6SKonstantin Belousov 	int status;
13908fae3551SRodney W. Grimes 
13918fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
13928fae3551SRodney W. Grimes 		emergency("can't fork for window system on port %s: %m",
13938fae3551SRodney W. Grimes 		    sp->se_device);
13948fae3551SRodney W. Grimes 		/* hope that getty fails and we can try again */
13958fae3551SRodney W. Grimes 		return;
13968fae3551SRodney W. Grimes 	}
1397091abe40SDavid E. O'Brien 	if (pid) {
13985010c3b6SKonstantin Belousov 		waitpid(-1, &status, 0);
13998fae3551SRodney W. Grimes 		return;
14005010c3b6SKonstantin Belousov 	}
14015010c3b6SKonstantin Belousov 
14025010c3b6SKonstantin Belousov 	/* reparent window process to the init to not make a zombie on exit */
14035010c3b6SKonstantin Belousov 	if ((pid = fork()) == -1) {
14045010c3b6SKonstantin Belousov 		emergency("can't fork for window system on port %s: %m",
14055010c3b6SKonstantin Belousov 		    sp->se_device);
14065010c3b6SKonstantin Belousov 		_exit(1);
14075010c3b6SKonstantin Belousov 	}
14085010c3b6SKonstantin Belousov 	if (pid)
14095010c3b6SKonstantin Belousov 		_exit(0);
14108fae3551SRodney W. Grimes 
14118fae3551SRodney W. Grimes 	sigemptyset(&mask);
14128fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
14138fae3551SRodney W. Grimes 
14148fae3551SRodney W. Grimes 	if (setsid() < 0)
14158fae3551SRodney W. Grimes 		emergency("setsid failed (window) %m");
14168fae3551SRodney W. Grimes 
14171ef60eb1SDavid Nugent #ifdef LOGIN_CAP
14181ef60eb1SDavid Nugent 	setprocresources(RESOURCE_WINDOW);
14191ef60eb1SDavid Nugent #endif
1420b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1421b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1422b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
142333a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1424b5df27e2SAndrey A. Chernov 		env[0] = term;
1425b5df27e2SAndrey A. Chernov 		env[1] = 0;
1426b5df27e2SAndrey A. Chernov 	}
1427b5df27e2SAndrey A. Chernov 	else
1428b5df27e2SAndrey A. Chernov 		env[0] = 0;
1429b5df27e2SAndrey A. Chernov 	execve(sp->se_window_argv[0], sp->se_window_argv, env);
14308fae3551SRodney W. Grimes 	stall("can't exec window system '%s' for port %s: %m",
14318fae3551SRodney W. Grimes 		sp->se_window_argv[0], sp->se_device);
14328fae3551SRodney W. Grimes 	_exit(1);
14338fae3551SRodney W. Grimes }
14348fae3551SRodney W. Grimes 
14358fae3551SRodney W. Grimes /*
14368fae3551SRodney W. Grimes  * Start a login session running.
14378fae3551SRodney W. Grimes  */
143845cfb1dcSXin LI static pid_t
143973bf18edSWarner Losh start_getty(session_t *sp)
14408fae3551SRodney W. Grimes {
14418fae3551SRodney W. Grimes 	pid_t pid;
14428fae3551SRodney W. Grimes 	sigset_t mask;
14438fae3551SRodney W. Grimes 	time_t current_time = time((time_t *) 0);
1444228d7ef2SAndrey A. Chernov 	int too_quick = 0;
1445b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
14468fae3551SRodney W. Grimes 
1447bb2e87c4SMike Pritchard 	if (current_time >= sp->se_started &&
1448228d7ef2SAndrey A. Chernov 	    current_time - sp->se_started < GETTY_SPACING) {
1449228d7ef2SAndrey A. Chernov 		if (++sp->se_nspace > GETTY_NSPACE) {
1450228d7ef2SAndrey A. Chernov 			sp->se_nspace = 0;
1451228d7ef2SAndrey A. Chernov 			too_quick = 1;
1452228d7ef2SAndrey A. Chernov 		}
1453228d7ef2SAndrey A. Chernov 	} else
1454228d7ef2SAndrey A. Chernov 		sp->se_nspace = 0;
1455228d7ef2SAndrey A. Chernov 
14568fae3551SRodney W. Grimes 	/*
14578fae3551SRodney W. Grimes 	 * fork(), not vfork() -- we can't afford to block.
14588fae3551SRodney W. Grimes 	 */
14598fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
14608fae3551SRodney W. Grimes 		emergency("can't fork for getty on port %s: %m", sp->se_device);
14618fae3551SRodney W. Grimes 		return -1;
14628fae3551SRodney W. Grimes 	}
14638fae3551SRodney W. Grimes 
14648fae3551SRodney W. Grimes 	if (pid)
14658fae3551SRodney W. Grimes 		return pid;
14668fae3551SRodney W. Grimes 
1467228d7ef2SAndrey A. Chernov 	if (too_quick) {
1468b5df27e2SAndrey A. Chernov 		warning("getty repeating too quickly on port %s, sleeping %d secs",
1469b5df27e2SAndrey A. Chernov 		    sp->se_device, GETTY_SLEEP);
14708fae3551SRodney W. Grimes 		sleep((unsigned) GETTY_SLEEP);
14718fae3551SRodney W. Grimes 	}
14728fae3551SRodney W. Grimes 
14738fae3551SRodney W. Grimes 	if (sp->se_window) {
14748fae3551SRodney W. Grimes 		start_window_system(sp);
14758fae3551SRodney W. Grimes 		sleep(WINDOW_WAIT);
14768fae3551SRodney W. Grimes 	}
14778fae3551SRodney W. Grimes 
14788fae3551SRodney W. Grimes 	sigemptyset(&mask);
14798fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
14808fae3551SRodney W. Grimes 
14811ef60eb1SDavid Nugent #ifdef LOGIN_CAP
14821ef60eb1SDavid Nugent 	setprocresources(RESOURCE_GETTY);
14831ef60eb1SDavid Nugent #endif
1484b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1485b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1486b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
148733a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1488b5df27e2SAndrey A. Chernov 		env[0] = term;
1489b5df27e2SAndrey A. Chernov 		env[1] = 0;
1490091abe40SDavid E. O'Brien 	} else
1491b5df27e2SAndrey A. Chernov 		env[0] = 0;
1492b5df27e2SAndrey A. Chernov 	execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
14938fae3551SRodney W. Grimes 	stall("can't exec getty '%s' for port %s: %m",
14948fae3551SRodney W. Grimes 		sp->se_getty_argv[0], sp->se_device);
14958fae3551SRodney W. Grimes 	_exit(1);
14968fae3551SRodney W. Grimes }
14978fae3551SRodney W. Grimes 
14988fae3551SRodney W. Grimes /*
14998fae3551SRodney W. Grimes  * Collect exit status for a child.
15008fae3551SRodney W. Grimes  * If an exiting login, start a new login running.
15018fae3551SRodney W. Grimes  */
150245cfb1dcSXin LI static void
15038fae3551SRodney W. Grimes collect_child(pid_t pid)
15048fae3551SRodney W. Grimes {
15053d438ad6SDavid E. O'Brien 	session_t *sp, *sprev, *snext;
15068fae3551SRodney W. Grimes 
15078fae3551SRodney W. Grimes 	if (! sessions)
15088fae3551SRodney W. Grimes 		return;
15098fae3551SRodney W. Grimes 
15108fae3551SRodney W. Grimes 	if (! (sp = find_session(pid)))
15118fae3551SRodney W. Grimes 		return;
15128fae3551SRodney W. Grimes 
15138fae3551SRodney W. Grimes 	del_session(sp);
15148fae3551SRodney W. Grimes 	sp->se_process = 0;
15158fae3551SRodney W. Grimes 
15168fae3551SRodney W. Grimes 	if (sp->se_flags & SE_SHUTDOWN) {
15178889c700SDavid Nugent 		if ((sprev = sp->se_prev) != NULL)
15188fae3551SRodney W. Grimes 			sprev->se_next = sp->se_next;
15198fae3551SRodney W. Grimes 		else
15208fae3551SRodney W. Grimes 			sessions = sp->se_next;
15218889c700SDavid Nugent 		if ((snext = sp->se_next) != NULL)
15228fae3551SRodney W. Grimes 			snext->se_prev = sp->se_prev;
15238fae3551SRodney W. Grimes 		free_session(sp);
15248fae3551SRodney W. Grimes 		return;
15258fae3551SRodney W. Grimes 	}
15268fae3551SRodney W. Grimes 
15278fae3551SRodney W. Grimes 	if ((pid = start_getty(sp)) == -1) {
15288fae3551SRodney W. Grimes 		/* serious trouble */
15298fae3551SRodney W. Grimes 		requested_transition = clean_ttys;
15308fae3551SRodney W. Grimes 		return;
15318fae3551SRodney W. Grimes 	}
15328fae3551SRodney W. Grimes 
15338fae3551SRodney W. Grimes 	sp->se_process = pid;
15348fae3551SRodney W. Grimes 	sp->se_started = time((time_t *) 0);
15358fae3551SRodney W. Grimes 	add_session(sp);
15368fae3551SRodney W. Grimes }
15378fae3551SRodney W. Grimes 
15388fae3551SRodney W. Grimes /*
15398fae3551SRodney W. Grimes  * Catch a signal and request a state transition.
15408fae3551SRodney W. Grimes  */
154145cfb1dcSXin LI static void
154273bf18edSWarner Losh transition_handler(int sig)
15438fae3551SRodney W. Grimes {
15448fae3551SRodney W. Grimes 
15458fae3551SRodney W. Grimes 	switch (sig) {
15468fae3551SRodney W. Grimes 	case SIGHUP:
1547acf0ab06SJilles Tjoelker 		if (current_state == read_ttys || current_state == multi_user ||
1548acf0ab06SJilles Tjoelker 		    current_state == clean_ttys || current_state == catatonia)
15498fae3551SRodney W. Grimes 			requested_transition = clean_ttys;
15508fae3551SRodney W. Grimes 		break;
1551a0a549c7SRuslan Ermilov 	case SIGUSR2:
1552a0a549c7SRuslan Ermilov 		howto = RB_POWEROFF;
1553a0a549c7SRuslan Ermilov 	case SIGUSR1:
1554a0a549c7SRuslan Ermilov 		howto |= RB_HALT;
1555e460cfd3SNate Williams 	case SIGINT:
1556db8ad19dSJordan K. Hubbard 		Reboot = TRUE;
15578fae3551SRodney W. Grimes 	case SIGTERM:
1558acf0ab06SJilles Tjoelker 		if (current_state == read_ttys || current_state == multi_user ||
1559acf0ab06SJilles Tjoelker 		    current_state == clean_ttys || current_state == catatonia)
15608fae3551SRodney W. Grimes 			requested_transition = death;
1561acf0ab06SJilles Tjoelker 		else
1562acf0ab06SJilles Tjoelker 			requested_transition = death_single;
15638fae3551SRodney W. Grimes 		break;
15648fae3551SRodney W. Grimes 	case SIGTSTP:
1565acf0ab06SJilles Tjoelker 		if (current_state == runcom || current_state == read_ttys ||
1566acf0ab06SJilles Tjoelker 		    current_state == clean_ttys ||
1567acf0ab06SJilles Tjoelker 		    current_state == multi_user || current_state == catatonia)
15688fae3551SRodney W. Grimes 			requested_transition = catatonia;
15698fae3551SRodney W. Grimes 		break;
1570*3f5ac575SEdward Tomasz Napierala 	case SIGEMT:
1571*3f5ac575SEdward Tomasz Napierala 		requested_transition = reroot;
1572*3f5ac575SEdward Tomasz Napierala 		break;
15738fae3551SRodney W. Grimes 	default:
15748fae3551SRodney W. Grimes 		requested_transition = 0;
15758fae3551SRodney W. Grimes 		break;
15768fae3551SRodney W. Grimes 	}
15778fae3551SRodney W. Grimes }
15788fae3551SRodney W. Grimes 
15798fae3551SRodney W. Grimes /*
15808fae3551SRodney W. Grimes  * Take the system multiuser.
15818fae3551SRodney W. Grimes  */
158245cfb1dcSXin LI static state_func_t
158373bf18edSWarner Losh multi_user(void)
15848fae3551SRodney W. Grimes {
15858fae3551SRodney W. Grimes 	pid_t pid;
15863d438ad6SDavid E. O'Brien 	session_t *sp;
15878fae3551SRodney W. Grimes 
15888fae3551SRodney W. Grimes 	requested_transition = 0;
15898fae3551SRodney W. Grimes 
15908fae3551SRodney W. Grimes 	/*
15918fae3551SRodney W. Grimes 	 * If the administrator has not set the security level to -1
15928fae3551SRodney W. Grimes 	 * to indicate that the kernel should not run multiuser in secure
15938fae3551SRodney W. Grimes 	 * mode, and the run script has not set a higher level of security
15948fae3551SRodney W. Grimes 	 * than level 1, then put the kernel into secure mode.
15958fae3551SRodney W. Grimes 	 */
15968fae3551SRodney W. Grimes 	if (getsecuritylevel() == 0)
15978fae3551SRodney W. Grimes 		setsecuritylevel(1);
15988fae3551SRodney W. Grimes 
15998fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next) {
16008fae3551SRodney W. Grimes 		if (sp->se_process)
16018fae3551SRodney W. Grimes 			continue;
16028fae3551SRodney W. Grimes 		if ((pid = start_getty(sp)) == -1) {
16038fae3551SRodney W. Grimes 			/* serious trouble */
16048fae3551SRodney W. Grimes 			requested_transition = clean_ttys;
16058fae3551SRodney W. Grimes 			break;
16068fae3551SRodney W. Grimes 		}
16078fae3551SRodney W. Grimes 		sp->se_process = pid;
16088fae3551SRodney W. Grimes 		sp->se_started = time((time_t *) 0);
16098fae3551SRodney W. Grimes 		add_session(sp);
16108fae3551SRodney W. Grimes 	}
16118fae3551SRodney W. Grimes 
16128fae3551SRodney W. Grimes 	while (!requested_transition)
16138fae3551SRodney W. Grimes 		if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
16148fae3551SRodney W. Grimes 			collect_child(pid);
16158fae3551SRodney W. Grimes 
16168fae3551SRodney W. Grimes 	return (state_func_t) requested_transition;
16178fae3551SRodney W. Grimes }
16188fae3551SRodney W. Grimes 
16198fae3551SRodney W. Grimes /*
1620b0b670eeSAlfred Perlstein  * This is an (n*2)+(n^2) algorithm.  We hope it isn't run often...
16218fae3551SRodney W. Grimes  */
162245cfb1dcSXin LI static state_func_t
162373bf18edSWarner Losh clean_ttys(void)
16248fae3551SRodney W. Grimes {
16253d438ad6SDavid E. O'Brien 	session_t *sp, *sprev;
16263d438ad6SDavid E. O'Brien 	struct ttyent *typ;
16273d438ad6SDavid E. O'Brien 	int devlen;
1628b5df27e2SAndrey A. Chernov 	char *old_getty, *old_window, *old_type;
16298fae3551SRodney W. Grimes 
1630b0b670eeSAlfred Perlstein 	/*
1631b0b670eeSAlfred Perlstein 	 * mark all sessions for death, (!SE_PRESENT)
1632b0b670eeSAlfred Perlstein 	 * as we find or create new ones they'll be marked as keepers,
1633b0b670eeSAlfred Perlstein 	 * we'll later nuke all the ones not found in /etc/ttys
1634b0b670eeSAlfred Perlstein 	 */
1635b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next)
1636b0b670eeSAlfred Perlstein 		sp->se_flags &= ~SE_PRESENT;
1637b0b670eeSAlfred Perlstein 
16388fae3551SRodney W. Grimes 	devlen = sizeof(_PATH_DEV) - 1;
16398889c700SDavid Nugent 	while ((typ = getttyent()) != NULL) {
16408fae3551SRodney W. Grimes 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
16418fae3551SRodney W. Grimes 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
16428fae3551SRodney W. Grimes 				break;
16438fae3551SRodney W. Grimes 
16448fae3551SRodney W. Grimes 		if (sp) {
1645b0b670eeSAlfred Perlstein 			/* we want this one to live */
1646b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_PRESENT;
16478fae3551SRodney W. Grimes 			if ((typ->ty_status & TTY_ON) == 0 ||
16488fae3551SRodney W. Grimes 			    typ->ty_getty == 0) {
16498fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
16508fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
16518fae3551SRodney W. Grimes 				continue;
16528fae3551SRodney W. Grimes 			}
16538fae3551SRodney W. Grimes 			sp->se_flags &= ~SE_SHUTDOWN;
1654b5df27e2SAndrey A. Chernov 			old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1655b5df27e2SAndrey A. Chernov 			old_window = sp->se_window ? strdup(sp->se_window) : 0;
1656b5df27e2SAndrey A. Chernov 			old_type = sp->se_type ? strdup(sp->se_type) : 0;
16578fae3551SRodney W. Grimes 			if (setupargv(sp, typ) == 0) {
16588fae3551SRodney W. Grimes 				warning("can't parse getty for port %s",
16598fae3551SRodney W. Grimes 					sp->se_device);
16608fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
16618fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
16628fae3551SRodney W. Grimes 			}
1663b5df27e2SAndrey A. Chernov 			else if (   !old_getty
16648889c700SDavid Nugent 				 || (!old_type && sp->se_type)
16658889c700SDavid Nugent 				 || (old_type && !sp->se_type)
16668889c700SDavid Nugent 				 || (!old_window && sp->se_window)
16678889c700SDavid Nugent 				 || (old_window && !sp->se_window)
16688889c700SDavid Nugent 				 || (strcmp(old_getty, sp->se_getty) != 0)
16698889c700SDavid Nugent 				 || (old_window && strcmp(old_window, sp->se_window) != 0)
16708889c700SDavid Nugent 				 || (old_type && strcmp(old_type, sp->se_type) != 0)
1671b5df27e2SAndrey A. Chernov 				) {
1672b5df27e2SAndrey A. Chernov 				/* Don't set SE_SHUTDOWN here */
1673b5df27e2SAndrey A. Chernov 				sp->se_nspace = 0;
1674b5df27e2SAndrey A. Chernov 				sp->se_started = 0;
1675b5df27e2SAndrey A. Chernov 				kill(sp->se_process, SIGHUP);
1676b5df27e2SAndrey A. Chernov 			}
1677b5df27e2SAndrey A. Chernov 			if (old_getty)
1678b5df27e2SAndrey A. Chernov 				free(old_getty);
16792d887af5SMike Heffner 			if (old_window)
1680b5df27e2SAndrey A. Chernov 				free(old_window);
1681b5df27e2SAndrey A. Chernov 			if (old_type)
1682b5df27e2SAndrey A. Chernov 				free(old_type);
16838fae3551SRodney W. Grimes 			continue;
16848fae3551SRodney W. Grimes 		}
16858fae3551SRodney W. Grimes 
16860b57dd6bSJilles Tjoelker 		new_session(sprev, typ);
16878fae3551SRodney W. Grimes 	}
16888fae3551SRodney W. Grimes 
16898fae3551SRodney W. Grimes 	endttyent();
16908fae3551SRodney W. Grimes 
1691b0b670eeSAlfred Perlstein 	/*
1692b0b670eeSAlfred Perlstein 	 * sweep through and kill all deleted sessions
1693b0b670eeSAlfred Perlstein 	 * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1694b0b670eeSAlfred Perlstein 	 */
1695b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next) {
1696b0b670eeSAlfred Perlstein 		if ((sp->se_flags & SE_PRESENT) == 0) {
1697b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_SHUTDOWN;
1698b0b670eeSAlfred Perlstein 			kill(sp->se_process, SIGHUP);
1699b0b670eeSAlfred Perlstein 		}
1700b0b670eeSAlfred Perlstein 	}
1701b0b670eeSAlfred Perlstein 
17028fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
17038fae3551SRodney W. Grimes }
17048fae3551SRodney W. Grimes 
17058fae3551SRodney W. Grimes /*
17068fae3551SRodney W. Grimes  * Block further logins.
17078fae3551SRodney W. Grimes  */
170845cfb1dcSXin LI static state_func_t
170973bf18edSWarner Losh catatonia(void)
17108fae3551SRodney W. Grimes {
17113d438ad6SDavid E. O'Brien 	session_t *sp;
17128fae3551SRodney W. Grimes 
17138fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next)
17148fae3551SRodney W. Grimes 		sp->se_flags |= SE_SHUTDOWN;
17158fae3551SRodney W. Grimes 
17168fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
17178fae3551SRodney W. Grimes }
17188fae3551SRodney W. Grimes 
17198fae3551SRodney W. Grimes /*
17208fae3551SRodney W. Grimes  * Note SIGALRM.
17218fae3551SRodney W. Grimes  */
172245cfb1dcSXin LI static void
172373bf18edSWarner Losh alrm_handler(int sig)
17248fae3551SRodney W. Grimes {
1725091abe40SDavid E. O'Brien 
17268889c700SDavid Nugent 	(void)sig;
17278fae3551SRodney W. Grimes 	clang = 1;
17288fae3551SRodney W. Grimes }
17298fae3551SRodney W. Grimes 
17308fae3551SRodney W. Grimes /*
17318fae3551SRodney W. Grimes  * Bring the system down to single user.
17328fae3551SRodney W. Grimes  */
173345cfb1dcSXin LI static state_func_t
173473bf18edSWarner Losh death(void)
17358fae3551SRodney W. Grimes {
17362eb0015aSColin Percival 	int block, blocked;
17372eb0015aSColin Percival 	size_t len;
17382eb0015aSColin Percival 
17392eb0015aSColin Percival 	/* Temporarily block suspend. */
17402eb0015aSColin Percival 	len = sizeof(blocked);
17412eb0015aSColin Percival 	block = 1;
17422eb0015aSColin Percival 	if (sysctlbyname("kern.suspend_blocked", &blocked, &len,
17432eb0015aSColin Percival 	    &block, sizeof(block)) == -1)
17442eb0015aSColin Percival 		blocked = 0;
17458fae3551SRodney W. Grimes 
17464ae35b5dSEd Schouten 	/*
17474ae35b5dSEd Schouten 	 * Also revoke the TTY here.  Because runshutdown() may reopen
17484ae35b5dSEd Schouten 	 * the TTY whose getty we're killing here, there is no guarantee
17494ae35b5dSEd Schouten 	 * runshutdown() will perform the initial open() call, causing
17504ae35b5dSEd Schouten 	 * the terminal attributes to be misconfigured.
17514ae35b5dSEd Schouten 	 */
1752*3f5ac575SEdward Tomasz Napierala 	revoke_ttys();
17538fae3551SRodney W. Grimes 
17548889c700SDavid Nugent 	/* Try to run the rc.shutdown script within a period of time */
1755091abe40SDavid E. O'Brien 	runshutdown();
17568889c700SDavid Nugent 
17572eb0015aSColin Percival 	/* Unblock suspend if we blocked it. */
17582eb0015aSColin Percival 	if (!blocked)
17592eb0015aSColin Percival 		sysctlbyname("kern.suspend_blocked", NULL, NULL,
17602eb0015aSColin Percival 		    &blocked, sizeof(blocked));
17612eb0015aSColin Percival 
1762acf0ab06SJilles Tjoelker 	return (state_func_t) death_single;
1763acf0ab06SJilles Tjoelker }
1764acf0ab06SJilles Tjoelker 
1765acf0ab06SJilles Tjoelker /*
1766acf0ab06SJilles Tjoelker  * Do what is necessary to reinitialize single user mode or reboot
1767acf0ab06SJilles Tjoelker  * from an incomplete state.
1768acf0ab06SJilles Tjoelker  */
1769acf0ab06SJilles Tjoelker static state_func_t
1770acf0ab06SJilles Tjoelker death_single(void)
1771acf0ab06SJilles Tjoelker {
1772acf0ab06SJilles Tjoelker 	int i;
1773acf0ab06SJilles Tjoelker 	pid_t pid;
1774acf0ab06SJilles Tjoelker 	static const int death_sigs[2] = { SIGTERM, SIGKILL };
1775acf0ab06SJilles Tjoelker 
1776acf0ab06SJilles Tjoelker 	revoke(_PATH_CONSOLE);
1777acf0ab06SJilles Tjoelker 
1778c3d7c52eSAndrey A. Chernov 	for (i = 0; i < 2; ++i) {
17798fae3551SRodney W. Grimes 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
17808fae3551SRodney W. Grimes 			return (state_func_t) single_user;
17818fae3551SRodney W. Grimes 
17828fae3551SRodney W. Grimes 		clang = 0;
17838fae3551SRodney W. Grimes 		alarm(DEATH_WATCH);
17848fae3551SRodney W. Grimes 		do
17858fae3551SRodney W. Grimes 			if ((pid = waitpid(-1, (int *)0, 0)) != -1)
17868fae3551SRodney W. Grimes 				collect_child(pid);
17878fae3551SRodney W. Grimes 		while (clang == 0 && errno != ECHILD);
17888fae3551SRodney W. Grimes 
17898fae3551SRodney W. Grimes 		if (errno == ECHILD)
17908fae3551SRodney W. Grimes 			return (state_func_t) single_user;
17918fae3551SRodney W. Grimes 	}
17928fae3551SRodney W. Grimes 
17938fae3551SRodney W. Grimes 	warning("some processes would not die; ps axl advised");
17948fae3551SRodney W. Grimes 
17958fae3551SRodney W. Grimes 	return (state_func_t) single_user;
17968fae3551SRodney W. Grimes }
17978889c700SDavid Nugent 
1798*3f5ac575SEdward Tomasz Napierala static void
1799*3f5ac575SEdward Tomasz Napierala revoke_ttys(void)
1800*3f5ac575SEdward Tomasz Napierala {
1801*3f5ac575SEdward Tomasz Napierala 	session_t *sp;
1802*3f5ac575SEdward Tomasz Napierala 
1803*3f5ac575SEdward Tomasz Napierala 	for (sp = sessions; sp; sp = sp->se_next) {
1804*3f5ac575SEdward Tomasz Napierala 		sp->se_flags |= SE_SHUTDOWN;
1805*3f5ac575SEdward Tomasz Napierala 		kill(sp->se_process, SIGHUP);
1806*3f5ac575SEdward Tomasz Napierala 		revoke(sp->se_device);
1807*3f5ac575SEdward Tomasz Napierala 	}
1808*3f5ac575SEdward Tomasz Napierala }
1809*3f5ac575SEdward Tomasz Napierala 
18108889c700SDavid Nugent /*
18118889c700SDavid Nugent  * Run the system shutdown script.
18128889c700SDavid Nugent  *
18138889c700SDavid Nugent  * Exit codes:      XXX I should document more
18148889c700SDavid Nugent  * -2       shutdown script terminated abnormally
18158889c700SDavid Nugent  * -1       fatal error - can't run script
18168889c700SDavid Nugent  * 0        good.
18178889c700SDavid Nugent  * >0       some error (exit code)
18188889c700SDavid Nugent  */
181945cfb1dcSXin LI static int
182073bf18edSWarner Losh runshutdown(void)
18218889c700SDavid Nugent {
18228889c700SDavid Nugent 	pid_t pid, wpid;
18238889c700SDavid Nugent 	int status;
18248889c700SDavid Nugent 	int shutdowntimeout;
18258889c700SDavid Nugent 	size_t len;
1826a69497d7SMatthew Dillon 	char *argv[4];
18271a7bec91SWarner Losh 	const char *shell;
18288889c700SDavid Nugent 	struct sigaction sa;
182986bf62dcSDavid Nugent 	struct stat sb;
183086bf62dcSDavid Nugent 
183186bf62dcSDavid Nugent 	/*
183286bf62dcSDavid Nugent 	 * rc.shutdown is optional, so to prevent any unnecessary
183386bf62dcSDavid Nugent 	 * complaints from the shell we simply don't run it if the
183486bf62dcSDavid Nugent 	 * file does not exist. If the stat() here fails for other
183586bf62dcSDavid Nugent 	 * reasons, we'll let the shell complain.
183686bf62dcSDavid Nugent 	 */
183786bf62dcSDavid Nugent 	if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
183886bf62dcSDavid Nugent 		return 0;
18398889c700SDavid Nugent 
18401a7bec91SWarner Losh 	shell = get_shell();
18411a7bec91SWarner Losh 
18428889c700SDavid Nugent 	if ((pid = fork()) == 0) {
18438889c700SDavid Nugent 		sigemptyset(&sa.sa_mask);
18448889c700SDavid Nugent 		sa.sa_flags = 0;
18458889c700SDavid Nugent 		sa.sa_handler = SIG_IGN;
1846091abe40SDavid E. O'Brien 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
1847091abe40SDavid E. O'Brien 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
18488889c700SDavid Nugent 
18494c2c7b2cSEd Schouten 		open_console();
1850ab03e6d5SXin LI 
1851ab03e6d5SXin LI 		char _sh[]	= "sh";
1852ab03e6d5SXin LI 		char _reboot[]	= "reboot";
1853ab03e6d5SXin LI 		char _single[]	= "single";
1854ab03e6d5SXin LI 		char _path_rundown[] = _PATH_RUNDOWN;
1855ab03e6d5SXin LI 
1856ab03e6d5SXin LI 		argv[0] = _sh;
1857ab03e6d5SXin LI 		argv[1] = _path_rundown;
1858ab03e6d5SXin LI 		argv[2] = Reboot ? _reboot : _single;
1859a69497d7SMatthew Dillon 		argv[3] = 0;
18608889c700SDavid Nugent 
18618889c700SDavid Nugent 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
18628889c700SDavid Nugent 
186325cf4a54SAndrey A. Chernov #ifdef LOGIN_CAP
186425cf4a54SAndrey A. Chernov 		setprocresources(RESOURCE_RC);
186525cf4a54SAndrey A. Chernov #endif
18661a7bec91SWarner Losh 		execv(shell, argv);
18671a7bec91SWarner Losh 		warning("can't exec %s for %s: %m", shell, _PATH_RUNDOWN);
18688889c700SDavid Nugent 		_exit(1);	/* force single user mode */
18698889c700SDavid Nugent 	}
18708889c700SDavid Nugent 
18718889c700SDavid Nugent 	if (pid == -1) {
18721a7bec91SWarner Losh 		emergency("can't fork for %s on %s: %m", shell, _PATH_RUNDOWN);
18738889c700SDavid Nugent 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
18748889c700SDavid Nugent 			continue;
18758889c700SDavid Nugent 		sleep(STALL_TIMEOUT);
18768889c700SDavid Nugent 		return -1;
18778889c700SDavid Nugent 	}
18788889c700SDavid Nugent 
18798889c700SDavid Nugent 	len = sizeof(shutdowntimeout);
1880091abe40SDavid E. O'Brien 	if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len,
1881091abe40SDavid E. O'Brien 	    NULL, 0) == -1 || shutdowntimeout < 2)
18828889c700SDavid Nugent 		shutdowntimeout = DEATH_SCRIPT;
18838889c700SDavid Nugent 	alarm(shutdowntimeout);
18848889c700SDavid Nugent 	clang = 0;
18858889c700SDavid Nugent 	/*
18868889c700SDavid Nugent 	 * Copied from single_user().  This is a bit paranoid.
18878889c700SDavid Nugent 	 * Use the same ALRM handler.
18888889c700SDavid Nugent 	 */
18898889c700SDavid Nugent 	do {
18908889c700SDavid Nugent 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
18918889c700SDavid Nugent 			collect_child(wpid);
18928889c700SDavid Nugent 		if (clang == 1) {
18938889c700SDavid Nugent 			/* we were waiting for the sub-shell */
18948889c700SDavid Nugent 			kill(wpid, SIGTERM);
18951a7bec91SWarner Losh 			warning("timeout expired for %s on %s: %m; going to "
18961a7bec91SWarner Losh 			    "single user mode", shell, _PATH_RUNDOWN);
18978889c700SDavid Nugent 			return -1;
18988889c700SDavid Nugent 		}
18998889c700SDavid Nugent 		if (wpid == -1) {
19008889c700SDavid Nugent 			if (errno == EINTR)
19018889c700SDavid Nugent 				continue;
19021a7bec91SWarner Losh 			warning("wait for %s on %s failed: %m; going to "
19031a7bec91SWarner Losh 			    "single user mode", shell, _PATH_RUNDOWN);
19048889c700SDavid Nugent 			return -1;
19058889c700SDavid Nugent 		}
19068889c700SDavid Nugent 		if (wpid == pid && WIFSTOPPED(status)) {
19078889c700SDavid Nugent 			warning("init: %s on %s stopped, restarting\n",
19081a7bec91SWarner Losh 				shell, _PATH_RUNDOWN);
19098889c700SDavid Nugent 			kill(pid, SIGCONT);
19108889c700SDavid Nugent 			wpid = -1;
19118889c700SDavid Nugent 		}
19128889c700SDavid Nugent 	} while (wpid != pid && !clang);
19138889c700SDavid Nugent 
19148889c700SDavid Nugent 	/* Turn off the alarm */
19158889c700SDavid Nugent 	alarm(0);
19168889c700SDavid Nugent 
19178889c700SDavid Nugent 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
19188889c700SDavid Nugent 	    requested_transition == catatonia) {
19198889c700SDavid Nugent 		/*
19208889c700SDavid Nugent 		 * /etc/rc.shutdown executed /sbin/reboot;
19218889c700SDavid Nugent 		 * wait for the end quietly
19228889c700SDavid Nugent 		 */
19238889c700SDavid Nugent 		sigset_t s;
19248889c700SDavid Nugent 
19258889c700SDavid Nugent 		sigfillset(&s);
19268889c700SDavid Nugent 		for (;;)
19278889c700SDavid Nugent 			sigsuspend(&s);
19288889c700SDavid Nugent 	}
19298889c700SDavid Nugent 
19308889c700SDavid Nugent 	if (!WIFEXITED(status)) {
19311a7bec91SWarner Losh 		warning("%s on %s terminated abnormally, going to "
19321a7bec91SWarner Losh 		    "single user mode", shell, _PATH_RUNDOWN);
19338889c700SDavid Nugent 		return -2;
19348889c700SDavid Nugent 	}
19358889c700SDavid Nugent 
19368889c700SDavid Nugent 	if ((status = WEXITSTATUS(status)) != 0)
19378889c700SDavid Nugent 		warning("%s returned status %d", _PATH_RUNDOWN, status);
19388889c700SDavid Nugent 
19398889c700SDavid Nugent 	return status;
19408889c700SDavid Nugent }
19418889c700SDavid Nugent 
1942ab03e6d5SXin LI static char *
194381ab7fb2SAndrey A. Chernov strk(char *p)
194481ab7fb2SAndrey A. Chernov {
194581ab7fb2SAndrey A. Chernov 	static char *t;
194681ab7fb2SAndrey A. Chernov 	char *q;
194781ab7fb2SAndrey A. Chernov 	int c;
194881ab7fb2SAndrey A. Chernov 
194981ab7fb2SAndrey A. Chernov 	if (p)
195081ab7fb2SAndrey A. Chernov 		t = p;
195181ab7fb2SAndrey A. Chernov 	if (!t)
195281ab7fb2SAndrey A. Chernov 		return 0;
195381ab7fb2SAndrey A. Chernov 
195481ab7fb2SAndrey A. Chernov 	c = *t;
195581ab7fb2SAndrey A. Chernov 	while (c == ' ' || c == '\t' )
195681ab7fb2SAndrey A. Chernov 		c = *++t;
195781ab7fb2SAndrey A. Chernov 	if (!c) {
195881ab7fb2SAndrey A. Chernov 		t = 0;
195981ab7fb2SAndrey A. Chernov 		return 0;
196081ab7fb2SAndrey A. Chernov 	}
196181ab7fb2SAndrey A. Chernov 	q = t;
196281ab7fb2SAndrey A. Chernov 	if (c == '\'') {
196381ab7fb2SAndrey A. Chernov 		c = *++t;
196481ab7fb2SAndrey A. Chernov 		q = t;
196581ab7fb2SAndrey A. Chernov 		while (c && c != '\'')
196681ab7fb2SAndrey A. Chernov 			c = *++t;
196781ab7fb2SAndrey A. Chernov 		if (!c)  /* unterminated string */
196881ab7fb2SAndrey A. Chernov 			q = t = 0;
196981ab7fb2SAndrey A. Chernov 		else
197081ab7fb2SAndrey A. Chernov 			*t++ = 0;
197181ab7fb2SAndrey A. Chernov 	} else {
197281ab7fb2SAndrey A. Chernov 		while (c && c != ' ' && c != '\t' )
197381ab7fb2SAndrey A. Chernov 			c = *++t;
197481ab7fb2SAndrey A. Chernov 		*t++ = 0;
197581ab7fb2SAndrey A. Chernov 		if (!c)
197681ab7fb2SAndrey A. Chernov 			t = 0;
197781ab7fb2SAndrey A. Chernov 	}
197881ab7fb2SAndrey A. Chernov 	return q;
197981ab7fb2SAndrey A. Chernov }
19801ef60eb1SDavid Nugent 
19811ef60eb1SDavid Nugent #ifdef LOGIN_CAP
198245cfb1dcSXin LI static void
198373bf18edSWarner Losh setprocresources(const char *cname)
19841ef60eb1SDavid Nugent {
1985e82d5545SDavid Nugent 	login_cap_t *lc;
1986a2ee73bcSAndrey A. Chernov 	if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
1987091abe40SDavid E. O'Brien 		setusercontext(lc, (struct passwd*)NULL, 0,
1988595ab563SJilles Tjoelker 		    LOGIN_SETPRIORITY | LOGIN_SETRESOURCES |
1989595ab563SJilles Tjoelker 		    LOGIN_SETLOGINCLASS | LOGIN_SETCPUMASK);
19901ef60eb1SDavid Nugent 		login_close(lc);
19911ef60eb1SDavid Nugent 	}
19921ef60eb1SDavid Nugent }
19931ef60eb1SDavid Nugent #endif
1994