xref: /freebsd/sbin/init/init.c (revision 1efe3c6b58ea5d0e5a0d49039d84d00f2370c7de)
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>
4957622f22SPoul-Henning Kamp #include <sys/mount.h>
508fae3551SRodney W. Grimes #include <sys/sysctl.h>
518fae3551SRodney W. Grimes #include <sys/wait.h>
5286bf62dcSDavid Nugent #include <sys/stat.h>
531f083b1eSMaxime Henrion #include <sys/uio.h>
548fae3551SRodney W. Grimes 
558fae3551SRodney W. Grimes #include <db.h>
568fae3551SRodney W. Grimes #include <errno.h>
578fae3551SRodney W. Grimes #include <fcntl.h>
581a7bec91SWarner Losh #include <kenv.h>
59423b6a39SAndrey A. Chernov #include <libutil.h>
601a37aa56SDavid E. O'Brien #include <paths.h>
618fae3551SRodney W. Grimes #include <signal.h>
628fae3551SRodney W. Grimes #include <stdio.h>
638fae3551SRodney W. Grimes #include <stdlib.h>
648fae3551SRodney W. Grimes #include <string.h>
658fae3551SRodney W. Grimes #include <syslog.h>
668fae3551SRodney W. Grimes #include <time.h>
678fae3551SRodney W. Grimes #include <ttyent.h>
688fae3551SRodney W. Grimes #include <unistd.h>
69bb6c7612SEd Schouten #include <utmpx.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 
838fae3551SRodney W. Grimes #include "pathnames.h"
848fae3551SRodney W. Grimes 
858fae3551SRodney W. Grimes /*
868fae3551SRodney W. Grimes  * Sleep times; used to prevent thrashing.
878fae3551SRodney W. Grimes  */
888fae3551SRodney W. Grimes #define	GETTY_SPACING		 5	/* N secs minimum getty spacing */
898fae3551SRodney W. Grimes #define	GETTY_SLEEP		30	/* sleep N secs after spacing problem */
90b5df27e2SAndrey A. Chernov #define	GETTY_NSPACE             3      /* max. spacing count to bring reaction */
918fae3551SRodney W. Grimes #define	WINDOW_WAIT		 3	/* wait N secs after starting window */
928fae3551SRodney W. Grimes #define	STALL_TIMEOUT		30	/* wait N secs after warning */
938fae3551SRodney W. Grimes #define	DEATH_WATCH		10	/* wait N secs for procs to die */
945df42cf4SPhilippe Charnier #define	DEATH_SCRIPT		120	/* wait for 2min for /etc/rc.shutdown */
95e82d5545SDavid Nugent #define	RESOURCE_RC		"daemon"
96e82d5545SDavid Nugent #define	RESOURCE_WINDOW 	"default"
97e82d5545SDavid Nugent #define	RESOURCE_GETTY		"default"
988fae3551SRodney W. Grimes 
9945cfb1dcSXin LI static void handle(sig_t, ...);
10045cfb1dcSXin LI static void delset(sigset_t *, ...);
1018fae3551SRodney W. Grimes 
10245cfb1dcSXin LI static void stall(const char *, ...) __printflike(1, 2);
10345cfb1dcSXin LI static void warning(const char *, ...) __printflike(1, 2);
10445cfb1dcSXin LI static void emergency(const char *, ...) __printflike(1, 2);
10545cfb1dcSXin LI static void disaster(int);
10645cfb1dcSXin LI static void badsys(int);
10745cfb1dcSXin LI static int  runshutdown(void);
108ab03e6d5SXin LI static char *strk(char *);
1098fae3551SRodney W. Grimes 
1108fae3551SRodney W. Grimes /*
1118fae3551SRodney W. Grimes  * We really need a recursive typedef...
1128fae3551SRodney W. Grimes  * The following at least guarantees that the return type of (*state_t)()
1138fae3551SRodney W. Grimes  * is sufficiently wide to hold a function pointer.
1148fae3551SRodney W. Grimes  */
11573bf18edSWarner Losh typedef long (*state_func_t)(void);
11673bf18edSWarner Losh typedef state_func_t (*state_t)(void);
1178fae3551SRodney W. Grimes 
11845cfb1dcSXin LI static state_func_t single_user(void);
11945cfb1dcSXin LI static state_func_t runcom(void);
12045cfb1dcSXin LI static state_func_t read_ttys(void);
12145cfb1dcSXin LI static state_func_t multi_user(void);
12245cfb1dcSXin LI static state_func_t clean_ttys(void);
12345cfb1dcSXin LI static state_func_t catatonia(void);
12445cfb1dcSXin LI static state_func_t death(void);
125acf0ab06SJilles Tjoelker static state_func_t death_single(void);
1268fae3551SRodney W. Grimes 
12745cfb1dcSXin LI static state_func_t run_script(const char *);
1281a7bec91SWarner Losh 
129*1efe3c6bSEd Schouten static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
13077103ea3SPoul-Henning Kamp #define FALSE	0
13177103ea3SPoul-Henning Kamp #define TRUE	1
13277103ea3SPoul-Henning Kamp 
133*1efe3c6bSEd Schouten static int Reboot = FALSE;
134*1efe3c6bSEd Schouten static int howto = RB_AUTOBOOT;
1358fae3551SRodney W. Grimes 
136*1efe3c6bSEd Schouten static int devfs;
13757622f22SPoul-Henning Kamp 
13845cfb1dcSXin LI static void transition(state_t);
13945cfb1dcSXin LI static state_t requested_transition;
140acf0ab06SJilles Tjoelker static state_t current_state = death_single;
1418fae3551SRodney W. Grimes 
14245cfb1dcSXin LI static void setctty(const char *);
14345cfb1dcSXin LI static const char *get_shell(void);
14445cfb1dcSXin LI static void write_stderr(const char *message);
1458fae3551SRodney W. Grimes 
1468fae3551SRodney W. Grimes typedef struct init_session {
1478fae3551SRodney W. Grimes 	int	se_index;		/* index of entry in ttys file */
1488fae3551SRodney W. Grimes 	pid_t	se_process;		/* controlling process */
1498fae3551SRodney W. Grimes 	time_t	se_started;		/* used to avoid thrashing */
1508fae3551SRodney W. Grimes 	int	se_flags;		/* status of session */
1518fae3551SRodney W. Grimes #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
152b0b670eeSAlfred Perlstein #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
153b5df27e2SAndrey A. Chernov 	int     se_nspace;              /* spacing count */
1548fae3551SRodney W. Grimes 	char	*se_device;		/* filename of port */
1558fae3551SRodney W. Grimes 	char	*se_getty;		/* what to run on that port */
156b5df27e2SAndrey A. Chernov 	char    *se_getty_argv_space;   /* pre-parsed argument array space */
1578fae3551SRodney W. Grimes 	char	**se_getty_argv;	/* pre-parsed argument array */
1588fae3551SRodney W. Grimes 	char	*se_window;		/* window system (started only once) */
159b5df27e2SAndrey A. Chernov 	char    *se_window_argv_space;  /* pre-parsed argument array space */
1608fae3551SRodney W. Grimes 	char	**se_window_argv;	/* pre-parsed argument array */
161b5df27e2SAndrey A. Chernov 	char    *se_type;               /* default terminal type */
1628fae3551SRodney W. Grimes 	struct	init_session *se_prev;
1638fae3551SRodney W. Grimes 	struct	init_session *se_next;
1648fae3551SRodney W. Grimes } session_t;
1658fae3551SRodney W. Grimes 
16645cfb1dcSXin LI static void free_session(session_t *);
16745cfb1dcSXin LI static session_t *new_session(session_t *, int, struct ttyent *);
16845cfb1dcSXin LI static session_t *sessions;
1698fae3551SRodney W. Grimes 
17045cfb1dcSXin LI static char **construct_argv(char *);
17145cfb1dcSXin LI static void start_window_system(session_t *);
17245cfb1dcSXin LI static void collect_child(pid_t);
17345cfb1dcSXin LI static pid_t start_getty(session_t *);
17445cfb1dcSXin LI static void transition_handler(int);
17545cfb1dcSXin LI static void alrm_handler(int);
17645cfb1dcSXin LI static void setsecuritylevel(int);
17745cfb1dcSXin LI static int getsecuritylevel(void);
17845cfb1dcSXin LI static int setupargv(session_t *, struct ttyent *);
179e82d5545SDavid Nugent #ifdef LOGIN_CAP
18045cfb1dcSXin LI static void setprocresources(const char *);
181e82d5545SDavid Nugent #endif
18245cfb1dcSXin LI static int clang;
1838fae3551SRodney W. Grimes 
18445cfb1dcSXin LI static void clear_session_logs(session_t *);
1858fae3551SRodney W. Grimes 
18645cfb1dcSXin LI static int start_session_db(void);
18745cfb1dcSXin LI static void add_session(session_t *);
18845cfb1dcSXin LI static void del_session(session_t *);
18945cfb1dcSXin LI static session_t *find_session(pid_t);
19045cfb1dcSXin LI static DB *session_db;
1918fae3551SRodney W. Grimes 
1928fae3551SRodney W. Grimes /*
1938fae3551SRodney W. Grimes  * The mother of all processes.
1948fae3551SRodney W. Grimes  */
1958fae3551SRodney W. Grimes int
19673bf18edSWarner Losh main(int argc, char *argv[])
1978fae3551SRodney W. Grimes {
1981a7bec91SWarner Losh 	state_t initial_transition = runcom;
1991a7bec91SWarner Losh 	char kenv_value[PATH_MAX];
2008fae3551SRodney W. Grimes 	int c;
2018fae3551SRodney W. Grimes 	struct sigaction sa;
2028fae3551SRodney W. Grimes 	sigset_t mask;
2038fae3551SRodney W. Grimes 
2048fae3551SRodney W. Grimes 	/* Dispose of random users. */
205c5842835SPhilippe Charnier 	if (getuid() != 0)
206c5842835SPhilippe Charnier 		errx(1, "%s", strerror(EPERM));
2078fae3551SRodney W. Grimes 
2088fae3551SRodney W. Grimes 	/* System V users like to reexec init. */
2091681d659SRuslan Ermilov 	if (getpid() != 1) {
2101681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT
2111681d659SRuslan Ermilov 		/* So give them what they want */
2121681d659SRuslan Ermilov 		if (argc > 1) {
2131681d659SRuslan Ermilov 			if (strlen(argv[1]) == 1) {
2143d438ad6SDavid E. O'Brien 				char runlevel = *argv[1];
2153d438ad6SDavid E. O'Brien 				int sig;
2168fae3551SRodney W. Grimes 
2171681d659SRuslan Ermilov 				switch (runlevel) {
2181681d659SRuslan Ermilov 				case '0': /* halt + poweroff */
2191681d659SRuslan Ermilov 					sig = SIGUSR2;
2201681d659SRuslan Ermilov 					break;
2211681d659SRuslan Ermilov 				case '1': /* single-user */
2221681d659SRuslan Ermilov 					sig = SIGTERM;
2231681d659SRuslan Ermilov 					break;
2241681d659SRuslan Ermilov 				case '6': /* reboot */
2251681d659SRuslan Ermilov 					sig = SIGINT;
2261681d659SRuslan Ermilov 					break;
2271681d659SRuslan Ermilov 				case 'c': /* block further logins */
2281681d659SRuslan Ermilov 					sig = SIGTSTP;
2291681d659SRuslan Ermilov 					break;
2301681d659SRuslan Ermilov 				case 'q': /* rescan /etc/ttys */
2311681d659SRuslan Ermilov 					sig = SIGHUP;
2321681d659SRuslan Ermilov 					break;
2331681d659SRuslan Ermilov 				default:
2341681d659SRuslan Ermilov 					goto invalid;
2351681d659SRuslan Ermilov 				}
2361681d659SRuslan Ermilov 				kill(1, sig);
2371681d659SRuslan Ermilov 				_exit(0);
2381681d659SRuslan Ermilov 			} else
2391681d659SRuslan Ermilov invalid:
2401681d659SRuslan Ermilov 				errx(1, "invalid run-level ``%s''", argv[1]);
2411681d659SRuslan Ermilov 		} else
2421681d659SRuslan Ermilov #endif
2431681d659SRuslan Ermilov 			errx(1, "already running");
2441681d659SRuslan Ermilov 	}
2458fae3551SRodney W. Grimes 	/*
2468fae3551SRodney W. Grimes 	 * Note that this does NOT open a file...
2478fae3551SRodney W. Grimes 	 * Does 'init' deserve its own facility number?
2488fae3551SRodney W. Grimes 	 */
2498fae3551SRodney W. Grimes 	openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH);
2508fae3551SRodney W. Grimes 
2518fae3551SRodney W. Grimes 	/*
2528fae3551SRodney W. Grimes 	 * Create an initial session.
2538fae3551SRodney W. Grimes 	 */
2548fae3551SRodney W. Grimes 	if (setsid() < 0)
2558fae3551SRodney W. Grimes 		warning("initial setsid() failed: %m");
2568fae3551SRodney W. Grimes 
2578fae3551SRodney W. Grimes 	/*
2588fae3551SRodney W. Grimes 	 * Establish an initial user so that programs running
2598fae3551SRodney W. Grimes 	 * single user do not freak out and die (like passwd).
2608fae3551SRodney W. Grimes 	 */
2618fae3551SRodney W. Grimes 	if (setlogin("root") < 0)
2628fae3551SRodney W. Grimes 		warning("setlogin() failed: %m");
2638fae3551SRodney W. Grimes 
2648fae3551SRodney W. Grimes 	/*
2658fae3551SRodney W. Grimes 	 * This code assumes that we always get arguments through flags,
2668fae3551SRodney W. Grimes 	 * never through bits set in some random machine register.
2678fae3551SRodney W. Grimes 	 */
26857622f22SPoul-Henning Kamp 	while ((c = getopt(argc, argv, "dsf")) != -1)
2698fae3551SRodney W. Grimes 		switch (c) {
27057622f22SPoul-Henning Kamp 		case 'd':
27157622f22SPoul-Henning Kamp 			devfs = 1;
27257622f22SPoul-Henning Kamp 			break;
2738fae3551SRodney W. Grimes 		case 's':
2741a7bec91SWarner Losh 			initial_transition = single_user;
2758fae3551SRodney W. Grimes 			break;
2768fae3551SRodney W. Grimes 		case 'f':
2778fae3551SRodney W. Grimes 			runcom_mode = FASTBOOT;
2788fae3551SRodney W. Grimes 			break;
2798fae3551SRodney W. Grimes 		default:
2808fae3551SRodney W. Grimes 			warning("unrecognized flag '-%c'", c);
2818fae3551SRodney W. Grimes 			break;
2828fae3551SRodney W. Grimes 		}
2838fae3551SRodney W. Grimes 
2848fae3551SRodney W. Grimes 	if (optind != argc)
2858fae3551SRodney W. Grimes 		warning("ignoring excess arguments");
2868fae3551SRodney W. Grimes 
2871a7bec91SWarner Losh 	/*
2881a7bec91SWarner Losh 	 * We catch or block signals rather than ignore them,
2891a7bec91SWarner Losh 	 * so that they get reset on exec.
2901a7bec91SWarner Losh 	 */
2911a7bec91SWarner Losh 	handle(badsys, SIGSYS, 0);
292091abe40SDavid E. O'Brien 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGXCPU,
293091abe40SDavid E. O'Brien 	    SIGXFSZ, 0);
294091abe40SDavid E. O'Brien 	handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGUSR1,
295091abe40SDavid E. O'Brien 	    SIGUSR2, 0);
2961a7bec91SWarner Losh 	handle(alrm_handler, SIGALRM, 0);
2971a7bec91SWarner Losh 	sigfillset(&mask);
2981a7bec91SWarner Losh 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
2991a7bec91SWarner Losh 	    SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM,
3001a7bec91SWarner Losh 	    SIGUSR1, SIGUSR2, 0);
3011a7bec91SWarner Losh 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
3021a7bec91SWarner Losh 	sigemptyset(&sa.sa_mask);
3031a7bec91SWarner Losh 	sa.sa_flags = 0;
3041a7bec91SWarner Losh 	sa.sa_handler = SIG_IGN;
305091abe40SDavid E. O'Brien 	sigaction(SIGTTIN, &sa, (struct sigaction *)0);
306091abe40SDavid E. O'Brien 	sigaction(SIGTTOU, &sa, (struct sigaction *)0);
3071a7bec91SWarner Losh 
3081a7bec91SWarner Losh 	/*
3091a7bec91SWarner Losh 	 * Paranoia.
3101a7bec91SWarner Losh 	 */
3111a7bec91SWarner Losh 	close(0);
3121a7bec91SWarner Losh 	close(1);
3131a7bec91SWarner Losh 	close(2);
3141a7bec91SWarner Losh 
3151a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) {
3161a7bec91SWarner Losh 		state_func_t next_transition;
3171a7bec91SWarner Losh 
3181a7bec91SWarner Losh 		if ((next_transition = run_script(kenv_value)) != 0)
3191a7bec91SWarner Losh 			initial_transition = (state_t) next_transition;
3201a7bec91SWarner Losh 	}
3211a7bec91SWarner Losh 
3221a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) {
3231a7bec91SWarner Losh 		if (chdir(kenv_value) != 0 || chroot(".") != 0)
3241a7bec91SWarner Losh 			warning("Can't chroot to %s: %m", kenv_value);
3251a7bec91SWarner Losh 	}
3261a7bec91SWarner Losh 
3271a7bec91SWarner Losh 	/*
3281a7bec91SWarner Losh 	 * Additional check if devfs needs to be mounted:
3291a7bec91SWarner Losh 	 * If "/" and "/dev" have the same device number,
3301a7bec91SWarner Losh 	 * then it hasn't been mounted yet.
3311a7bec91SWarner Losh 	 */
3321a7bec91SWarner Losh 	if (!devfs) {
3331a7bec91SWarner Losh 		struct stat stst;
3341a7bec91SWarner Losh 		dev_t root_devno;
3351a7bec91SWarner Losh 
3361a7bec91SWarner Losh 		stat("/", &stst);
3371a7bec91SWarner Losh 		root_devno = stst.st_dev;
3381a7bec91SWarner Losh 		if (stat("/dev", &stst) != 0)
3391a7bec91SWarner Losh 			warning("Can't stat /dev: %m");
3401a7bec91SWarner Losh 		else if (stst.st_dev == root_devno)
3411a7bec91SWarner Losh 			devfs++;
3421a7bec91SWarner Losh 	}
3431a7bec91SWarner Losh 
34457622f22SPoul-Henning Kamp 	if (devfs) {
3451f083b1eSMaxime Henrion 		struct iovec iov[4];
346421b0201SPoul-Henning Kamp 		char *s;
347421b0201SPoul-Henning Kamp 		int i;
348421b0201SPoul-Henning Kamp 
349ab03e6d5SXin LI 		char _fstype[]	= "fstype";
350ab03e6d5SXin LI 		char _devfs[]	= "devfs";
351ab03e6d5SXin LI 		char _fspath[]	= "fspath";
352ab03e6d5SXin LI 		char _path_dev[]= _PATH_DEV;
353ab03e6d5SXin LI 
354ab03e6d5SXin LI 		iov[0].iov_base = _fstype;
355ab03e6d5SXin LI 		iov[0].iov_len = sizeof(_fstype);
356ab03e6d5SXin LI 		iov[1].iov_base = _devfs;
357ab03e6d5SXin LI 		iov[1].iov_len = sizeof(_devfs);
358ab03e6d5SXin LI 		iov[2].iov_base = _fspath;
359ab03e6d5SXin LI 		iov[2].iov_len = sizeof(_fspath);
360421b0201SPoul-Henning Kamp 		/*
361421b0201SPoul-Henning Kamp 		 * Try to avoid the trailing slash in _PATH_DEV.
362421b0201SPoul-Henning Kamp 		 * Be *very* defensive.
363421b0201SPoul-Henning Kamp 		 */
364421b0201SPoul-Henning Kamp 		s = strdup(_PATH_DEV);
365421b0201SPoul-Henning Kamp 		if (s != NULL) {
366421b0201SPoul-Henning Kamp 			i = strlen(s);
367421b0201SPoul-Henning Kamp 			if (i > 0 && s[i - 1] == '/')
368421b0201SPoul-Henning Kamp 				s[i - 1] = '\0';
3691f083b1eSMaxime Henrion 			iov[3].iov_base = s;
3701f083b1eSMaxime Henrion 			iov[3].iov_len = strlen(s) + 1;
371421b0201SPoul-Henning Kamp 		} else {
372ab03e6d5SXin LI 			iov[3].iov_base = _path_dev;
373ab03e6d5SXin LI 			iov[3].iov_len = sizeof(_path_dev);
37457622f22SPoul-Henning Kamp 		}
3751f083b1eSMaxime Henrion 		nmount(iov, 4, 0);
3761f083b1eSMaxime Henrion 		if (s != NULL)
3771f083b1eSMaxime Henrion 			free(s);
378421b0201SPoul-Henning Kamp 	}
37957622f22SPoul-Henning Kamp 
3808fae3551SRodney W. Grimes 	/*
3818fae3551SRodney W. Grimes 	 * Start the state machine.
3828fae3551SRodney W. Grimes 	 */
3831a7bec91SWarner Losh 	transition(initial_transition);
3848fae3551SRodney W. Grimes 
3858fae3551SRodney W. Grimes 	/*
3868fae3551SRodney W. Grimes 	 * Should never reach here.
3878fae3551SRodney W. Grimes 	 */
3888fae3551SRodney W. Grimes 	return 1;
3898fae3551SRodney W. Grimes }
3908fae3551SRodney W. Grimes 
3918fae3551SRodney W. Grimes /*
3928fae3551SRodney W. Grimes  * Associate a function with a signal handler.
3938fae3551SRodney W. Grimes  */
39445cfb1dcSXin LI static void
3958fae3551SRodney W. Grimes handle(sig_t handler, ...)
3968fae3551SRodney W. Grimes {
3978fae3551SRodney W. Grimes 	int sig;
3988fae3551SRodney W. Grimes 	struct sigaction sa;
39939034633SJames Raynard 	sigset_t mask_everything;
4008fae3551SRodney W. Grimes 	va_list ap;
4018fae3551SRodney W. Grimes 	va_start(ap, handler);
4028fae3551SRodney W. Grimes 
4038fae3551SRodney W. Grimes 	sa.sa_handler = handler;
4048fae3551SRodney W. Grimes 	sigfillset(&mask_everything);
4058fae3551SRodney W. Grimes 
40630e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0) {
4078fae3551SRodney W. Grimes 		sa.sa_mask = mask_everything;
4088fae3551SRodney W. Grimes 		/* XXX SA_RESTART? */
4098fae3551SRodney W. Grimes 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
4108fae3551SRodney W. Grimes 		sigaction(sig, &sa, (struct sigaction *) 0);
4118fae3551SRodney W. Grimes 	}
4128fae3551SRodney W. Grimes 	va_end(ap);
4138fae3551SRodney W. Grimes }
4148fae3551SRodney W. Grimes 
4158fae3551SRodney W. Grimes /*
4168fae3551SRodney W. Grimes  * Delete a set of signals from a mask.
4178fae3551SRodney W. Grimes  */
41845cfb1dcSXin LI static void
4198fae3551SRodney W. Grimes delset(sigset_t *maskp, ...)
4208fae3551SRodney W. Grimes {
4218fae3551SRodney W. Grimes 	int sig;
4228fae3551SRodney W. Grimes 	va_list ap;
4238fae3551SRodney W. Grimes 	va_start(ap, maskp);
4248fae3551SRodney W. Grimes 
42530e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0)
4268fae3551SRodney W. Grimes 		sigdelset(maskp, sig);
4278fae3551SRodney W. Grimes 	va_end(ap);
4288fae3551SRodney W. Grimes }
4298fae3551SRodney W. Grimes 
4308fae3551SRodney W. Grimes /*
4318fae3551SRodney W. Grimes  * Log a message and sleep for a while (to give someone an opportunity
4328fae3551SRodney W. Grimes  * to read it and to save log or hardcopy output if the problem is chronic).
4338fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4348fae3551SRodney W. Grimes  */
43545cfb1dcSXin LI static void
4365979df34SKris Kennaway stall(const char *message, ...)
4378fae3551SRodney W. Grimes {
4388fae3551SRodney W. Grimes 	va_list ap;
4398fae3551SRodney W. Grimes 	va_start(ap, message);
4408fae3551SRodney W. Grimes 
4418fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4428fae3551SRodney W. Grimes 	va_end(ap);
4438fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
4448fae3551SRodney W. Grimes }
4458fae3551SRodney W. Grimes 
4468fae3551SRodney W. Grimes /*
4478fae3551SRodney W. Grimes  * Like stall(), but doesn't sleep.
4488fae3551SRodney W. Grimes  * If cpp had variadic macros, the two functions could be #defines for another.
4498fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4508fae3551SRodney W. Grimes  */
45145cfb1dcSXin LI static void
4525979df34SKris Kennaway warning(const char *message, ...)
4538fae3551SRodney W. Grimes {
4548fae3551SRodney W. Grimes 	va_list ap;
4558fae3551SRodney W. Grimes 	va_start(ap, message);
4568fae3551SRodney W. Grimes 
4578fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4588fae3551SRodney W. Grimes 	va_end(ap);
4598fae3551SRodney W. Grimes }
4608fae3551SRodney W. Grimes 
4618fae3551SRodney W. Grimes /*
4628fae3551SRodney W. Grimes  * Log an emergency message.
4638fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4648fae3551SRodney W. Grimes  */
46545cfb1dcSXin LI static void
4665979df34SKris Kennaway emergency(const char *message, ...)
4678fae3551SRodney W. Grimes {
4688fae3551SRodney W. Grimes 	va_list ap;
4698fae3551SRodney W. Grimes 	va_start(ap, message);
4708fae3551SRodney W. Grimes 
4718fae3551SRodney W. Grimes 	vsyslog(LOG_EMERG, message, ap);
4728fae3551SRodney W. Grimes 	va_end(ap);
4738fae3551SRodney W. Grimes }
4748fae3551SRodney W. Grimes 
4758fae3551SRodney W. Grimes /*
4768fae3551SRodney W. Grimes  * Catch a SIGSYS signal.
4778fae3551SRodney W. Grimes  *
4788fae3551SRodney W. Grimes  * These may arise if a system does not support sysctl.
4798fae3551SRodney W. Grimes  * We tolerate up to 25 of these, then throw in the towel.
4808fae3551SRodney W. Grimes  */
48145cfb1dcSXin LI static void
48273bf18edSWarner Losh badsys(int sig)
4838fae3551SRodney W. Grimes {
4848fae3551SRodney W. Grimes 	static int badcount = 0;
4858fae3551SRodney W. Grimes 
4868fae3551SRodney W. Grimes 	if (badcount++ < 25)
4878fae3551SRodney W. Grimes 		return;
4888fae3551SRodney W. Grimes 	disaster(sig);
4898fae3551SRodney W. Grimes }
4908fae3551SRodney W. Grimes 
4918fae3551SRodney W. Grimes /*
4928fae3551SRodney W. Grimes  * Catch an unexpected signal.
4938fae3551SRodney W. Grimes  */
49445cfb1dcSXin LI static void
49573bf18edSWarner Losh disaster(int sig)
4968fae3551SRodney W. Grimes {
497091abe40SDavid E. O'Brien 
4988fae3551SRodney W. Grimes 	emergency("fatal signal: %s",
4998889c700SDavid Nugent 	    (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
5008fae3551SRodney W. Grimes 
5018fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
5028fae3551SRodney W. Grimes 	_exit(sig);		/* reboot */
5038fae3551SRodney W. Grimes }
5048fae3551SRodney W. Grimes 
5058fae3551SRodney W. Grimes /*
5068fae3551SRodney W. Grimes  * Get the security level of the kernel.
5078fae3551SRodney W. Grimes  */
50845cfb1dcSXin LI static int
50973bf18edSWarner Losh getsecuritylevel(void)
5108fae3551SRodney W. Grimes {
5118fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5128fae3551SRodney W. Grimes 	int name[2], curlevel;
5138fae3551SRodney W. Grimes 	size_t len;
5148fae3551SRodney W. Grimes 
5158fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
5168fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
5178fae3551SRodney W. Grimes 	len = sizeof curlevel;
5188fae3551SRodney W. Grimes 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
5198fae3551SRodney W. Grimes 		emergency("cannot get kernel security level: %s",
5208fae3551SRodney W. Grimes 		    strerror(errno));
5218fae3551SRodney W. Grimes 		return (-1);
5228fae3551SRodney W. Grimes 	}
5238fae3551SRodney W. Grimes 	return (curlevel);
5248fae3551SRodney W. Grimes #else
5258fae3551SRodney W. Grimes 	return (-1);
5268fae3551SRodney W. Grimes #endif
5278fae3551SRodney W. Grimes }
5288fae3551SRodney W. Grimes 
5298fae3551SRodney W. Grimes /*
5308fae3551SRodney W. Grimes  * Set the security level of the kernel.
5318fae3551SRodney W. Grimes  */
53245cfb1dcSXin LI static void
53373bf18edSWarner Losh setsecuritylevel(int newlevel)
5348fae3551SRodney W. Grimes {
5358fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5368fae3551SRodney W. Grimes 	int name[2], curlevel;
5378fae3551SRodney W. Grimes 
5388fae3551SRodney W. Grimes 	curlevel = getsecuritylevel();
5398fae3551SRodney W. Grimes 	if (newlevel == curlevel)
5408fae3551SRodney W. Grimes 		return;
5418fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
5428fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
5438fae3551SRodney W. Grimes 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
5448fae3551SRodney W. Grimes 		emergency(
5458fae3551SRodney W. Grimes 		    "cannot change kernel security level from %d to %d: %s",
5468fae3551SRodney W. Grimes 		    curlevel, newlevel, strerror(errno));
5478fae3551SRodney W. Grimes 		return;
5488fae3551SRodney W. Grimes 	}
5498fae3551SRodney W. Grimes #ifdef SECURE
5508fae3551SRodney W. Grimes 	warning("kernel security level changed from %d to %d",
5518fae3551SRodney W. Grimes 	    curlevel, newlevel);
5528fae3551SRodney W. Grimes #endif
5538fae3551SRodney W. Grimes #endif
5548fae3551SRodney W. Grimes }
5558fae3551SRodney W. Grimes 
5568fae3551SRodney W. Grimes /*
5578fae3551SRodney W. Grimes  * Change states in the finite state machine.
5588fae3551SRodney W. Grimes  * The initial state is passed as an argument.
5598fae3551SRodney W. Grimes  */
56045cfb1dcSXin LI static void
56173bf18edSWarner Losh transition(state_t s)
5628fae3551SRodney W. Grimes {
563091abe40SDavid E. O'Brien 
564acf0ab06SJilles Tjoelker 	current_state = s;
5658fae3551SRodney W. Grimes 	for (;;)
566acf0ab06SJilles Tjoelker 		current_state = (state_t) (*current_state)();
5678fae3551SRodney W. Grimes }
5688fae3551SRodney W. Grimes 
5698fae3551SRodney W. Grimes /*
5708fae3551SRodney W. Grimes  * Close out the accounting files for a login session.
5718fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
5728fae3551SRodney W. Grimes  */
57345cfb1dcSXin LI static void
574bb6c7612SEd Schouten clear_session_logs(session_t *sp __unused)
5758fae3551SRodney W. Grimes {
5768fae3551SRodney W. Grimes 
577bb6c7612SEd Schouten 	/*
578bb6c7612SEd Schouten 	 * XXX: Use getutxline() and call pututxline() for each entry.
579bb6c7612SEd Schouten 	 * Is this safe to do this here?  Is it really required anyway?
580bb6c7612SEd Schouten 	 */
5818fae3551SRodney W. Grimes }
5828fae3551SRodney W. Grimes 
5838fae3551SRodney W. Grimes /*
5848fae3551SRodney W. Grimes  * Start a session and allocate a controlling terminal.
5858fae3551SRodney W. Grimes  * Only called by children of init after forking.
5868fae3551SRodney W. Grimes  */
58745cfb1dcSXin LI static void
588ab03e6d5SXin LI setctty(const char *name)
5898fae3551SRodney W. Grimes {
5908fae3551SRodney W. Grimes 	int fd;
5918fae3551SRodney W. Grimes 
592091abe40SDavid E. O'Brien 	revoke(name);
5938fae3551SRodney W. Grimes 	if ((fd = open(name, O_RDWR)) == -1) {
5948fae3551SRodney W. Grimes 		stall("can't open %s: %m", name);
5958fae3551SRodney W. Grimes 		_exit(1);
5968fae3551SRodney W. Grimes 	}
5978fae3551SRodney W. Grimes 	if (login_tty(fd) == -1) {
5988fae3551SRodney W. Grimes 		stall("can't get %s for controlling terminal: %m", name);
5998fae3551SRodney W. Grimes 		_exit(1);
6008fae3551SRodney W. Grimes 	}
6018fae3551SRodney W. Grimes }
6028fae3551SRodney W. Grimes 
60345cfb1dcSXin LI static const char *
6041a7bec91SWarner Losh get_shell(void)
6051a7bec91SWarner Losh {
6061a7bec91SWarner Losh 	static char kenv_value[PATH_MAX];
6071a7bec91SWarner Losh 
6081a7bec91SWarner Losh 	if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0)
6091a7bec91SWarner Losh 		return kenv_value;
6101a7bec91SWarner Losh 	else
6111a7bec91SWarner Losh 		return _PATH_BSHELL;
6121a7bec91SWarner Losh }
6131a7bec91SWarner Losh 
61445cfb1dcSXin LI static void
6151a7bec91SWarner Losh write_stderr(const char *message)
6161a7bec91SWarner Losh {
617091abe40SDavid E. O'Brien 
6181a7bec91SWarner Losh 	write(STDERR_FILENO, message, strlen(message));
6191a7bec91SWarner Losh }
6201a7bec91SWarner Losh 
6218fae3551SRodney W. Grimes /*
6228fae3551SRodney W. Grimes  * Bring the system up single user.
6238fae3551SRodney W. Grimes  */
62445cfb1dcSXin LI static state_func_t
62573bf18edSWarner Losh single_user(void)
6268fae3551SRodney W. Grimes {
6278fae3551SRodney W. Grimes 	pid_t pid, wpid;
6288fae3551SRodney W. Grimes 	int status;
6298fae3551SRodney W. Grimes 	sigset_t mask;
6301a7bec91SWarner Losh 	const char *shell;
6318fae3551SRodney W. Grimes 	char *argv[2];
6328fae3551SRodney W. Grimes #ifdef SECURE
6338fae3551SRodney W. Grimes 	struct ttyent *typ;
6348fae3551SRodney W. Grimes 	struct passwd *pp;
6358fae3551SRodney W. Grimes 	static const char banner[] =
6368fae3551SRodney W. Grimes 		"Enter root password, or ^D to go multi-user\n";
6378fae3551SRodney W. Grimes 	char *clear, *password;
6388fae3551SRodney W. Grimes #endif
63963322c28SPoul-Henning Kamp #ifdef DEBUGSHELL
64063322c28SPoul-Henning Kamp 	char altshell[128];
64163322c28SPoul-Henning Kamp #endif
6428fae3551SRodney W. Grimes 
643db8ad19dSJordan K. Hubbard 	if (Reboot) {
644a0a549c7SRuslan Ermilov 		/* Instead of going single user, let's reboot the machine */
645e460cfd3SNate Williams 		sync();
646e460cfd3SNate Williams 		alarm(2);
647e460cfd3SNate Williams 		pause();
648a0a549c7SRuslan Ermilov 		reboot(howto);
649e460cfd3SNate Williams 		_exit(0);
650e460cfd3SNate Williams 	}
651e460cfd3SNate Williams 
6521a7bec91SWarner Losh 	shell = get_shell();
6531a7bec91SWarner Losh 
6548fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
6558fae3551SRodney W. Grimes 		/*
6568fae3551SRodney W. Grimes 		 * Start the single user session.
6578fae3551SRodney W. Grimes 		 */
6588fae3551SRodney W. Grimes 		setctty(_PATH_CONSOLE);
6598fae3551SRodney W. Grimes 
6608fae3551SRodney W. Grimes #ifdef SECURE
6618fae3551SRodney W. Grimes 		/*
6628fae3551SRodney W. Grimes 		 * Check the root password.
6638fae3551SRodney W. Grimes 		 * We don't care if the console is 'on' by default;
6648fae3551SRodney W. Grimes 		 * it's the only tty that can be 'off' and 'secure'.
6658fae3551SRodney W. Grimes 		 */
6668fae3551SRodney W. Grimes 		typ = getttynam("console");
6678fae3551SRodney W. Grimes 		pp = getpwnam("root");
668a69497d7SMatthew Dillon 		if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
669a69497d7SMatthew Dillon 		    pp && *pp->pw_passwd) {
6701a7bec91SWarner Losh 			write_stderr(banner);
6718fae3551SRodney W. Grimes 			for (;;) {
6728fae3551SRodney W. Grimes 				clear = getpass("Password:");
6738fae3551SRodney W. Grimes 				if (clear == 0 || *clear == '\0')
6748fae3551SRodney W. Grimes 					_exit(0);
6758fae3551SRodney W. Grimes 				password = crypt(clear, pp->pw_passwd);
6768fae3551SRodney W. Grimes 				bzero(clear, _PASSWORD_LEN);
6778fae3551SRodney W. Grimes 				if (strcmp(password, pp->pw_passwd) == 0)
6788fae3551SRodney W. Grimes 					break;
6798fae3551SRodney W. Grimes 				warning("single-user login failed\n");
6808fae3551SRodney W. Grimes 			}
6818fae3551SRodney W. Grimes 		}
6828fae3551SRodney W. Grimes 		endttyent();
6838fae3551SRodney W. Grimes 		endpwent();
6848fae3551SRodney W. Grimes #endif /* SECURE */
6858fae3551SRodney W. Grimes 
6868fae3551SRodney W. Grimes #ifdef DEBUGSHELL
6878fae3551SRodney W. Grimes 		{
68863322c28SPoul-Henning Kamp 			char *cp = altshell;
6898fae3551SRodney W. Grimes 			int num;
6908fae3551SRodney W. Grimes 
6911a7bec91SWarner Losh #define	SHREQUEST "Enter full pathname of shell or RETURN for "
6921a7bec91SWarner Losh 			write_stderr(SHREQUEST);
6931a7bec91SWarner Losh 			write_stderr(shell);
6941a7bec91SWarner Losh 			write_stderr(": ");
6958fae3551SRodney W. Grimes 			while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
6968fae3551SRodney W. Grimes 			    num != 0 && *cp != '\n' && cp < &altshell[127])
6978fae3551SRodney W. Grimes 				cp++;
6988fae3551SRodney W. Grimes 			*cp = '\0';
6998fae3551SRodney W. Grimes 			if (altshell[0] != '\0')
7008fae3551SRodney W. Grimes 				shell = altshell;
7018fae3551SRodney W. Grimes 		}
7028fae3551SRodney W. Grimes #endif /* DEBUGSHELL */
7038fae3551SRodney W. Grimes 
7048fae3551SRodney W. Grimes 		/*
7058fae3551SRodney W. Grimes 		 * Unblock signals.
7068fae3551SRodney W. Grimes 		 * We catch all the interesting ones,
7078fae3551SRodney W. Grimes 		 * and those are reset to SIG_DFL on exec.
7088fae3551SRodney W. Grimes 		 */
7098fae3551SRodney W. Grimes 		sigemptyset(&mask);
7108fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
7118fae3551SRodney W. Grimes 
7128fae3551SRodney W. Grimes 		/*
7138fae3551SRodney W. Grimes 		 * Fire off a shell.
7148fae3551SRodney W. Grimes 		 * If the default one doesn't work, try the Bourne shell.
7158fae3551SRodney W. Grimes 		 */
716ab03e6d5SXin LI 
717ab03e6d5SXin LI 		char name[] = "-sh";
718ab03e6d5SXin LI 
719ab03e6d5SXin LI 		argv[0] = name;
7208fae3551SRodney W. Grimes 		argv[1] = 0;
7218fae3551SRodney W. Grimes 		execv(shell, argv);
7228fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", shell);
7238fae3551SRodney W. Grimes 		execv(_PATH_BSHELL, argv);
7248fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
7258fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
7268fae3551SRodney W. Grimes 		_exit(1);
7278fae3551SRodney W. Grimes 	}
7288fae3551SRodney W. Grimes 
7298fae3551SRodney W. Grimes 	if (pid == -1) {
7308fae3551SRodney W. Grimes 		/*
7318fae3551SRodney W. Grimes 		 * We are seriously hosed.  Do our best.
7328fae3551SRodney W. Grimes 		 */
7338fae3551SRodney W. Grimes 		emergency("can't fork single-user shell, trying again");
7348fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
7358fae3551SRodney W. Grimes 			continue;
7368fae3551SRodney W. Grimes 		return (state_func_t) single_user;
7378fae3551SRodney W. Grimes 	}
7388fae3551SRodney W. Grimes 
7398fae3551SRodney W. Grimes 	requested_transition = 0;
7408fae3551SRodney W. Grimes 	do {
7418fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
7428fae3551SRodney W. Grimes 			collect_child(wpid);
7438fae3551SRodney W. Grimes 		if (wpid == -1) {
7448fae3551SRodney W. Grimes 			if (errno == EINTR)
7458fae3551SRodney W. Grimes 				continue;
7468fae3551SRodney W. Grimes 			warning("wait for single-user shell failed: %m; restarting");
7478fae3551SRodney W. Grimes 			return (state_func_t) single_user;
7488fae3551SRodney W. Grimes 		}
7498fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
7508fae3551SRodney W. Grimes 			warning("init: shell stopped, restarting\n");
7518fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
7528fae3551SRodney W. Grimes 			wpid = -1;
7538fae3551SRodney W. Grimes 		}
7548fae3551SRodney W. Grimes 	} while (wpid != pid && !requested_transition);
7558fae3551SRodney W. Grimes 
7568fae3551SRodney W. Grimes 	if (requested_transition)
7578fae3551SRodney W. Grimes 		return (state_func_t) requested_transition;
7588fae3551SRodney W. Grimes 
7598fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
7608fae3551SRodney W. Grimes 		if (WTERMSIG(status) == SIGKILL) {
7618fae3551SRodney W. Grimes 			/*
7628fae3551SRodney W. Grimes 			 *  reboot(8) killed shell?
7638fae3551SRodney W. Grimes 			 */
7648fae3551SRodney W. Grimes 			warning("single user shell terminated.");
7658fae3551SRodney W. Grimes 			sleep(STALL_TIMEOUT);
7668fae3551SRodney W. Grimes 			_exit(0);
7678fae3551SRodney W. Grimes 		} else {
7688fae3551SRodney W. Grimes 			warning("single user shell terminated, restarting");
7698fae3551SRodney W. Grimes 			return (state_func_t) single_user;
7708fae3551SRodney W. Grimes 		}
7718fae3551SRodney W. Grimes 	}
7728fae3551SRodney W. Grimes 
7738fae3551SRodney W. Grimes 	runcom_mode = FASTBOOT;
7748fae3551SRodney W. Grimes 	return (state_func_t) runcom;
7758fae3551SRodney W. Grimes }
7768fae3551SRodney W. Grimes 
7778fae3551SRodney W. Grimes /*
7788fae3551SRodney W. Grimes  * Run the system startup script.
7798fae3551SRodney W. Grimes  */
78045cfb1dcSXin LI static state_func_t
78173bf18edSWarner Losh runcom(void)
7828fae3551SRodney W. Grimes {
78314c69f21SEd Schouten 	struct utmpx utx;
7841a7bec91SWarner Losh 	state_func_t next_transition;
7851a7bec91SWarner Losh 
7861a7bec91SWarner Losh 	if ((next_transition = run_script(_PATH_RUNCOM)) != 0)
7871a7bec91SWarner Losh 		return next_transition;
7881a7bec91SWarner Losh 
7891a7bec91SWarner Losh 	runcom_mode = AUTOBOOT;		/* the default */
7901a7bec91SWarner Losh 	/* NB: should send a message to the session logger to avoid blocking. */
79114c69f21SEd Schouten 	utx.ut_type = BOOT_TIME;
79214c69f21SEd Schouten 	gettimeofday(&utx.ut_tv, NULL);
79314c69f21SEd Schouten 	pututxline(&utx);
7941a7bec91SWarner Losh 	return (state_func_t) read_ttys;
7951a7bec91SWarner Losh }
7961a7bec91SWarner Losh 
7971a7bec91SWarner Losh /*
7981a7bec91SWarner Losh  * Run a shell script.
7991a7bec91SWarner Losh  * Returns 0 on success, otherwise the next transition to enter:
8001a7bec91SWarner Losh  *  - single_user if fork/execv/waitpid failed, or if the script
8011a7bec91SWarner Losh  *    terminated with a signal or exit code != 0.
802acf0ab06SJilles Tjoelker  *  - death_single if a SIGTERM was delivered to init(8).
8031a7bec91SWarner Losh  */
80445cfb1dcSXin LI static state_func_t
8051a7bec91SWarner Losh run_script(const char *script)
8061a7bec91SWarner Losh {
8078fae3551SRodney W. Grimes 	pid_t pid, wpid;
8088fae3551SRodney W. Grimes 	int status;
8098fae3551SRodney W. Grimes 	char *argv[4];
8101a7bec91SWarner Losh 	const char *shell;
8118fae3551SRodney W. Grimes 	struct sigaction sa;
8128fae3551SRodney W. Grimes 
8131a7bec91SWarner Losh 	shell = get_shell();
8141a7bec91SWarner Losh 
8158fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
8168fae3551SRodney W. Grimes 		sigemptyset(&sa.sa_mask);
8178fae3551SRodney W. Grimes 		sa.sa_flags = 0;
8188fae3551SRodney W. Grimes 		sa.sa_handler = SIG_IGN;
819091abe40SDavid E. O'Brien 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
820091abe40SDavid E. O'Brien 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
8218fae3551SRodney W. Grimes 
8228fae3551SRodney W. Grimes 		setctty(_PATH_CONSOLE);
8238fae3551SRodney W. Grimes 
824ab03e6d5SXin LI 		char _sh[]	 	= "sh";
825ab03e6d5SXin LI 		char _autoboot[]	= "autoboot";
826ab03e6d5SXin LI 
827ab03e6d5SXin LI 		argv[0] = _sh;
8281a7bec91SWarner Losh 		argv[1] = __DECONST(char *, script);
829ab03e6d5SXin LI 		argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0;
8308fae3551SRodney W. Grimes 		argv[3] = 0;
8318fae3551SRodney W. Grimes 
8328fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
8338fae3551SRodney W. Grimes 
8341ef60eb1SDavid Nugent #ifdef LOGIN_CAP
8351ef60eb1SDavid Nugent 		setprocresources(RESOURCE_RC);
8361ef60eb1SDavid Nugent #endif
8371a7bec91SWarner Losh 		execv(shell, argv);
8381a7bec91SWarner Losh 		stall("can't exec %s for %s: %m", shell, script);
8398fae3551SRodney W. Grimes 		_exit(1);	/* force single user mode */
8408fae3551SRodney W. Grimes 	}
8418fae3551SRodney W. Grimes 
8428fae3551SRodney W. Grimes 	if (pid == -1) {
8431a7bec91SWarner Losh 		emergency("can't fork for %s on %s: %m", shell, script);
8448fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
8458fae3551SRodney W. Grimes 			continue;
8468fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
8478fae3551SRodney W. Grimes 		return (state_func_t) single_user;
8488fae3551SRodney W. Grimes 	}
8498fae3551SRodney W. Grimes 
8508fae3551SRodney W. Grimes 	/*
8518fae3551SRodney W. Grimes 	 * Copied from single_user().  This is a bit paranoid.
8528fae3551SRodney W. Grimes 	 */
8536e8ff8b7SDag-Erling Smørgrav 	requested_transition = 0;
8548fae3551SRodney W. Grimes 	do {
8558fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
8568fae3551SRodney W. Grimes 			collect_child(wpid);
8578fae3551SRodney W. Grimes 		if (wpid == -1) {
858acf0ab06SJilles Tjoelker 			if (requested_transition == death_single)
859acf0ab06SJilles Tjoelker 				return (state_func_t) death_single;
8608fae3551SRodney W. Grimes 			if (errno == EINTR)
8618fae3551SRodney W. Grimes 				continue;
8621a7bec91SWarner Losh 			warning("wait for %s on %s failed: %m; going to "
8631a7bec91SWarner Losh 			    "single user mode", shell, script);
8648fae3551SRodney W. Grimes 			return (state_func_t) single_user;
8658fae3551SRodney W. Grimes 		}
8668fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
8678fae3551SRodney W. Grimes 			warning("init: %s on %s stopped, restarting\n",
8681a7bec91SWarner Losh 			    shell, script);
8698fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
8708fae3551SRodney W. Grimes 			wpid = -1;
8718fae3551SRodney W. Grimes 		}
8728fae3551SRodney W. Grimes 	} while (wpid != pid);
8738fae3551SRodney W. Grimes 
8748fae3551SRodney W. Grimes 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
8758fae3551SRodney W. Grimes 	    requested_transition == catatonia) {
8768fae3551SRodney W. Grimes 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
8778fae3551SRodney W. Grimes 		sigset_t s;
8788fae3551SRodney W. Grimes 
8798fae3551SRodney W. Grimes 		sigfillset(&s);
8808fae3551SRodney W. Grimes 		for (;;)
8818fae3551SRodney W. Grimes 			sigsuspend(&s);
8828fae3551SRodney W. Grimes 	}
8838fae3551SRodney W. Grimes 
8848fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
8851a7bec91SWarner Losh 		warning("%s on %s terminated abnormally, going to single "
8861a7bec91SWarner Losh 		    "user mode", shell, script);
8878fae3551SRodney W. Grimes 		return (state_func_t) single_user;
8888fae3551SRodney W. Grimes 	}
8898fae3551SRodney W. Grimes 
8908fae3551SRodney W. Grimes 	if (WEXITSTATUS(status))
8918fae3551SRodney W. Grimes 		return (state_func_t) single_user;
8928fae3551SRodney W. Grimes 
8931a7bec91SWarner Losh 	return (state_func_t) 0;
8948fae3551SRodney W. Grimes }
8958fae3551SRodney W. Grimes 
8968fae3551SRodney W. Grimes /*
8978fae3551SRodney W. Grimes  * Open the session database.
8988fae3551SRodney W. Grimes  *
8998fae3551SRodney W. Grimes  * NB: We could pass in the size here; is it necessary?
9008fae3551SRodney W. Grimes  */
90145cfb1dcSXin LI static int
90273bf18edSWarner Losh start_session_db(void)
9038fae3551SRodney W. Grimes {
9048fae3551SRodney W. Grimes 	if (session_db && (*session_db->close)(session_db))
9058fae3551SRodney W. Grimes 		emergency("session database close: %s", strerror(errno));
9068fae3551SRodney W. Grimes 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
9078fae3551SRodney W. Grimes 		emergency("session database open: %s", strerror(errno));
9088fae3551SRodney W. Grimes 		return (1);
9098fae3551SRodney W. Grimes 	}
9108fae3551SRodney W. Grimes 	return (0);
9118fae3551SRodney W. Grimes 
9128fae3551SRodney W. Grimes }
9138fae3551SRodney W. Grimes 
9148fae3551SRodney W. Grimes /*
9158fae3551SRodney W. Grimes  * Add a new login session.
9168fae3551SRodney W. Grimes  */
91745cfb1dcSXin LI static void
91873bf18edSWarner Losh add_session(session_t *sp)
9198fae3551SRodney W. Grimes {
9208fae3551SRodney W. Grimes 	DBT key;
9218fae3551SRodney W. Grimes 	DBT data;
9228fae3551SRodney W. Grimes 
9238fae3551SRodney W. Grimes 	key.data = &sp->se_process;
9248fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
9258fae3551SRodney W. Grimes 	data.data = &sp;
9268fae3551SRodney W. Grimes 	data.size = sizeof sp;
9278fae3551SRodney W. Grimes 
9288fae3551SRodney W. Grimes 	if ((*session_db->put)(session_db, &key, &data, 0))
9298fae3551SRodney W. Grimes 		emergency("insert %d: %s", sp->se_process, strerror(errno));
9308fae3551SRodney W. Grimes }
9318fae3551SRodney W. Grimes 
9328fae3551SRodney W. Grimes /*
9338fae3551SRodney W. Grimes  * Delete an old login session.
9348fae3551SRodney W. Grimes  */
93545cfb1dcSXin LI static void
93673bf18edSWarner Losh del_session(session_t *sp)
9378fae3551SRodney W. Grimes {
9388fae3551SRodney W. Grimes 	DBT key;
9398fae3551SRodney W. Grimes 
9408fae3551SRodney W. Grimes 	key.data = &sp->se_process;
9418fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
9428fae3551SRodney W. Grimes 
9438fae3551SRodney W. Grimes 	if ((*session_db->del)(session_db, &key, 0))
9448fae3551SRodney W. Grimes 		emergency("delete %d: %s", sp->se_process, strerror(errno));
9458fae3551SRodney W. Grimes }
9468fae3551SRodney W. Grimes 
9478fae3551SRodney W. Grimes /*
9488fae3551SRodney W. Grimes  * Look up a login session by pid.
9498fae3551SRodney W. Grimes  */
95045cfb1dcSXin LI static session_t *
9518fae3551SRodney W. Grimes find_session(pid_t pid)
9528fae3551SRodney W. Grimes {
9538fae3551SRodney W. Grimes 	DBT key;
9548fae3551SRodney W. Grimes 	DBT data;
9558fae3551SRodney W. Grimes 	session_t *ret;
9568fae3551SRodney W. Grimes 
9578fae3551SRodney W. Grimes 	key.data = &pid;
9588fae3551SRodney W. Grimes 	key.size = sizeof pid;
9598fae3551SRodney W. Grimes 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
9608fae3551SRodney W. Grimes 		return 0;
9618fae3551SRodney W. Grimes 	bcopy(data.data, (char *)&ret, sizeof(ret));
9628fae3551SRodney W. Grimes 	return ret;
9638fae3551SRodney W. Grimes }
9648fae3551SRodney W. Grimes 
9658fae3551SRodney W. Grimes /*
9668fae3551SRodney W. Grimes  * Construct an argument vector from a command line.
9678fae3551SRodney W. Grimes  */
96845cfb1dcSXin LI static char **
96973bf18edSWarner Losh construct_argv(char *command)
9708fae3551SRodney W. Grimes {
9713d438ad6SDavid E. O'Brien 	int argc = 0;
9723d438ad6SDavid E. O'Brien 	char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
9738fae3551SRodney W. Grimes 						* sizeof (char *));
9748fae3551SRodney W. Grimes 
9756be40c95SRuslan Ermilov 	if ((argv[argc++] = strk(command)) == 0) {
9766be40c95SRuslan Ermilov 		free(argv);
9776be40c95SRuslan Ermilov 		return (NULL);
9786be40c95SRuslan Ermilov 	}
9798889c700SDavid Nugent 	while ((argv[argc++] = strk((char *) 0)) != NULL)
9808fae3551SRodney W. Grimes 		continue;
9818fae3551SRodney W. Grimes 	return argv;
9828fae3551SRodney W. Grimes }
9838fae3551SRodney W. Grimes 
9848fae3551SRodney W. Grimes /*
9858fae3551SRodney W. Grimes  * Deallocate a session descriptor.
9868fae3551SRodney W. Grimes  */
98745cfb1dcSXin LI static void
98873bf18edSWarner Losh free_session(session_t *sp)
9898fae3551SRodney W. Grimes {
9908fae3551SRodney W. Grimes 	free(sp->se_device);
9918fae3551SRodney W. Grimes 	if (sp->se_getty) {
9928fae3551SRodney W. Grimes 		free(sp->se_getty);
993b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
9948fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
9958fae3551SRodney W. Grimes 	}
9968fae3551SRodney W. Grimes 	if (sp->se_window) {
9978fae3551SRodney W. Grimes 		free(sp->se_window);
998b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
9998fae3551SRodney W. Grimes 		free(sp->se_window_argv);
10008fae3551SRodney W. Grimes 	}
1001b5df27e2SAndrey A. Chernov 	if (sp->se_type)
1002b5df27e2SAndrey A. Chernov 		free(sp->se_type);
10038fae3551SRodney W. Grimes 	free(sp);
10048fae3551SRodney W. Grimes }
10058fae3551SRodney W. Grimes 
10068fae3551SRodney W. Grimes /*
10078fae3551SRodney W. Grimes  * Allocate a new session descriptor.
1008b0b670eeSAlfred Perlstein  * Mark it SE_PRESENT.
10098fae3551SRodney W. Grimes  */
101045cfb1dcSXin LI static session_t *
101173bf18edSWarner Losh new_session(session_t *sprev, int session_index, struct ttyent *typ)
10128fae3551SRodney W. Grimes {
10133d438ad6SDavid E. O'Brien 	session_t *sp;
10144cbf8903SPaul Traina 	int fd;
10158fae3551SRodney W. Grimes 
10168fae3551SRodney W. Grimes 	if ((typ->ty_status & TTY_ON) == 0 ||
10178fae3551SRodney W. Grimes 	    typ->ty_name == 0 ||
10188fae3551SRodney W. Grimes 	    typ->ty_getty == 0)
10198fae3551SRodney W. Grimes 		return 0;
10208fae3551SRodney W. Grimes 
10211054bb1eSAndrey A. Chernov 	sp = (session_t *) calloc(1, sizeof (session_t));
10228fae3551SRodney W. Grimes 
10238fae3551SRodney W. Grimes 	sp->se_index = session_index;
1024b0b670eeSAlfred Perlstein 	sp->se_flags |= SE_PRESENT;
10258fae3551SRodney W. Grimes 
10268fae3551SRodney W. Grimes 	sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
1027091abe40SDavid E. O'Brien 	sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
10288fae3551SRodney W. Grimes 
10294cbf8903SPaul Traina 	/*
10304cbf8903SPaul Traina 	 * Attempt to open the device, if we get "device not configured"
10314cbf8903SPaul Traina 	 * then don't add the device to the session list.
10324cbf8903SPaul Traina 	 */
10334cbf8903SPaul Traina 	if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) {
103489e3b380SWarner Losh 		if (errno == ENXIO) {
10354cbf8903SPaul Traina 			free_session(sp);
10364cbf8903SPaul Traina 			return (0);
10374cbf8903SPaul Traina 		}
10384cbf8903SPaul Traina 	} else
10394cbf8903SPaul Traina 		close(fd);
10404cbf8903SPaul Traina 
10418fae3551SRodney W. Grimes 	if (setupargv(sp, typ) == 0) {
10428fae3551SRodney W. Grimes 		free_session(sp);
10438fae3551SRodney W. Grimes 		return (0);
10448fae3551SRodney W. Grimes 	}
10458fae3551SRodney W. Grimes 
10468fae3551SRodney W. Grimes 	sp->se_next = 0;
10478fae3551SRodney W. Grimes 	if (sprev == 0) {
10488fae3551SRodney W. Grimes 		sessions = sp;
10498fae3551SRodney W. Grimes 		sp->se_prev = 0;
10508fae3551SRodney W. Grimes 	} else {
10518fae3551SRodney W. Grimes 		sprev->se_next = sp;
10528fae3551SRodney W. Grimes 		sp->se_prev = sprev;
10538fae3551SRodney W. Grimes 	}
10548fae3551SRodney W. Grimes 
10558fae3551SRodney W. Grimes 	return sp;
10568fae3551SRodney W. Grimes }
10578fae3551SRodney W. Grimes 
10588fae3551SRodney W. Grimes /*
10598fae3551SRodney W. Grimes  * Calculate getty and if useful window argv vectors.
10608fae3551SRodney W. Grimes  */
106145cfb1dcSXin LI static int
106273bf18edSWarner Losh setupargv(session_t *sp, struct ttyent *typ)
10638fae3551SRodney W. Grimes {
10648fae3551SRodney W. Grimes 
10658fae3551SRodney W. Grimes 	if (sp->se_getty) {
10668fae3551SRodney W. Grimes 		free(sp->se_getty);
1067b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
10688fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
10698fae3551SRodney W. Grimes 	}
10708fae3551SRodney W. Grimes 	sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
1071091abe40SDavid E. O'Brien 	sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
1072b5df27e2SAndrey A. Chernov 	sp->se_getty_argv_space = strdup(sp->se_getty);
1073b5df27e2SAndrey A. Chernov 	sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
10748fae3551SRodney W. Grimes 	if (sp->se_getty_argv == 0) {
10758fae3551SRodney W. Grimes 		warning("can't parse getty for port %s", sp->se_device);
10768fae3551SRodney W. Grimes 		free(sp->se_getty);
1077b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
1078b5df27e2SAndrey A. Chernov 		sp->se_getty = sp->se_getty_argv_space = 0;
10798fae3551SRodney W. Grimes 		return (0);
10808fae3551SRodney W. Grimes 	}
1081b5df27e2SAndrey A. Chernov 	if (sp->se_window) {
10828fae3551SRodney W. Grimes 		free(sp->se_window);
1083b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
1084b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv);
1085b5df27e2SAndrey A. Chernov 	}
1086b5df27e2SAndrey A. Chernov 	sp->se_window = sp->se_window_argv_space = 0;
1087b5df27e2SAndrey A. Chernov 	sp->se_window_argv = 0;
1088b5df27e2SAndrey A. Chernov 	if (typ->ty_window) {
10898fae3551SRodney W. Grimes 		sp->se_window = strdup(typ->ty_window);
1090b5df27e2SAndrey A. Chernov 		sp->se_window_argv_space = strdup(sp->se_window);
1091b5df27e2SAndrey A. Chernov 		sp->se_window_argv = construct_argv(sp->se_window_argv_space);
10928fae3551SRodney W. Grimes 		if (sp->se_window_argv == 0) {
10938fae3551SRodney W. Grimes 			warning("can't parse window for port %s",
10948fae3551SRodney W. Grimes 			    sp->se_device);
1095b5df27e2SAndrey A. Chernov 			free(sp->se_window_argv_space);
10968fae3551SRodney W. Grimes 			free(sp->se_window);
1097b5df27e2SAndrey A. Chernov 			sp->se_window = sp->se_window_argv_space = 0;
10988fae3551SRodney W. Grimes 			return (0);
10998fae3551SRodney W. Grimes 		}
11008fae3551SRodney W. Grimes 	}
1101b5df27e2SAndrey A. Chernov 	if (sp->se_type)
1102b5df27e2SAndrey A. Chernov 		free(sp->se_type);
1103b5df27e2SAndrey A. Chernov 	sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
11048fae3551SRodney W. Grimes 	return (1);
11058fae3551SRodney W. Grimes }
11068fae3551SRodney W. Grimes 
11078fae3551SRodney W. Grimes /*
11088fae3551SRodney W. Grimes  * Walk the list of ttys and create sessions for each active line.
11098fae3551SRodney W. Grimes  */
111045cfb1dcSXin LI static state_func_t
111173bf18edSWarner Losh read_ttys(void)
11128fae3551SRodney W. Grimes {
11138fae3551SRodney W. Grimes 	int session_index = 0;
11143d438ad6SDavid E. O'Brien 	session_t *sp, *snext;
11153d438ad6SDavid E. O'Brien 	struct ttyent *typ;
11168fae3551SRodney W. Grimes 
11178fae3551SRodney W. Grimes 	/*
11188fae3551SRodney W. Grimes 	 * Destroy any previous session state.
11198fae3551SRodney W. Grimes 	 * There shouldn't be any, but just in case...
11208fae3551SRodney W. Grimes 	 */
11218fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = snext) {
11228fae3551SRodney W. Grimes 		if (sp->se_process)
11238fae3551SRodney W. Grimes 			clear_session_logs(sp);
11248fae3551SRodney W. Grimes 		snext = sp->se_next;
11258fae3551SRodney W. Grimes 		free_session(sp);
11268fae3551SRodney W. Grimes 	}
11278fae3551SRodney W. Grimes 	sessions = 0;
11288fae3551SRodney W. Grimes 	if (start_session_db())
11298fae3551SRodney W. Grimes 		return (state_func_t) single_user;
11308fae3551SRodney W. Grimes 
11318fae3551SRodney W. Grimes 	/*
11328fae3551SRodney W. Grimes 	 * Allocate a session entry for each active port.
11338fae3551SRodney W. Grimes 	 * Note that sp starts at 0.
11348fae3551SRodney W. Grimes 	 */
11358889c700SDavid Nugent 	while ((typ = getttyent()) != NULL)
11368889c700SDavid Nugent 		if ((snext = new_session(sp, ++session_index, typ)) != NULL)
11378fae3551SRodney W. Grimes 			sp = snext;
11388fae3551SRodney W. Grimes 
11398fae3551SRodney W. Grimes 	endttyent();
11408fae3551SRodney W. Grimes 
11418fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
11428fae3551SRodney W. Grimes }
11438fae3551SRodney W. Grimes 
11448fae3551SRodney W. Grimes /*
11458fae3551SRodney W. Grimes  * Start a window system running.
11468fae3551SRodney W. Grimes  */
114745cfb1dcSXin LI static void
114873bf18edSWarner Losh start_window_system(session_t *sp)
11498fae3551SRodney W. Grimes {
11508fae3551SRodney W. Grimes 	pid_t pid;
11518fae3551SRodney W. Grimes 	sigset_t mask;
1152b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
11535010c3b6SKonstantin Belousov 	int status;
11548fae3551SRodney W. Grimes 
11558fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
11568fae3551SRodney W. Grimes 		emergency("can't fork for window system on port %s: %m",
11578fae3551SRodney W. Grimes 		    sp->se_device);
11588fae3551SRodney W. Grimes 		/* hope that getty fails and we can try again */
11598fae3551SRodney W. Grimes 		return;
11608fae3551SRodney W. Grimes 	}
1161091abe40SDavid E. O'Brien 	if (pid) {
11625010c3b6SKonstantin Belousov 		waitpid(-1, &status, 0);
11638fae3551SRodney W. Grimes 		return;
11645010c3b6SKonstantin Belousov 	}
11655010c3b6SKonstantin Belousov 
11665010c3b6SKonstantin Belousov 	/* reparent window process to the init to not make a zombie on exit */
11675010c3b6SKonstantin Belousov 	if ((pid = fork()) == -1) {
11685010c3b6SKonstantin Belousov 		emergency("can't fork for window system on port %s: %m",
11695010c3b6SKonstantin Belousov 		    sp->se_device);
11705010c3b6SKonstantin Belousov 		_exit(1);
11715010c3b6SKonstantin Belousov 	}
11725010c3b6SKonstantin Belousov 	if (pid)
11735010c3b6SKonstantin Belousov 		_exit(0);
11748fae3551SRodney W. Grimes 
11758fae3551SRodney W. Grimes 	sigemptyset(&mask);
11768fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
11778fae3551SRodney W. Grimes 
11788fae3551SRodney W. Grimes 	if (setsid() < 0)
11798fae3551SRodney W. Grimes 		emergency("setsid failed (window) %m");
11808fae3551SRodney W. Grimes 
11811ef60eb1SDavid Nugent #ifdef LOGIN_CAP
11821ef60eb1SDavid Nugent 	setprocresources(RESOURCE_WINDOW);
11831ef60eb1SDavid Nugent #endif
1184b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1185b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1186b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
118733a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1188b5df27e2SAndrey A. Chernov 		env[0] = term;
1189b5df27e2SAndrey A. Chernov 		env[1] = 0;
1190b5df27e2SAndrey A. Chernov 	}
1191b5df27e2SAndrey A. Chernov 	else
1192b5df27e2SAndrey A. Chernov 		env[0] = 0;
1193b5df27e2SAndrey A. Chernov 	execve(sp->se_window_argv[0], sp->se_window_argv, env);
11948fae3551SRodney W. Grimes 	stall("can't exec window system '%s' for port %s: %m",
11958fae3551SRodney W. Grimes 		sp->se_window_argv[0], sp->se_device);
11968fae3551SRodney W. Grimes 	_exit(1);
11978fae3551SRodney W. Grimes }
11988fae3551SRodney W. Grimes 
11998fae3551SRodney W. Grimes /*
12008fae3551SRodney W. Grimes  * Start a login session running.
12018fae3551SRodney W. Grimes  */
120245cfb1dcSXin LI static pid_t
120373bf18edSWarner Losh start_getty(session_t *sp)
12048fae3551SRodney W. Grimes {
12058fae3551SRodney W. Grimes 	pid_t pid;
12068fae3551SRodney W. Grimes 	sigset_t mask;
12078fae3551SRodney W. Grimes 	time_t current_time = time((time_t *) 0);
1208228d7ef2SAndrey A. Chernov 	int too_quick = 0;
1209b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
12108fae3551SRodney W. Grimes 
1211bb2e87c4SMike Pritchard 	if (current_time >= sp->se_started &&
1212228d7ef2SAndrey A. Chernov 	    current_time - sp->se_started < GETTY_SPACING) {
1213228d7ef2SAndrey A. Chernov 		if (++sp->se_nspace > GETTY_NSPACE) {
1214228d7ef2SAndrey A. Chernov 			sp->se_nspace = 0;
1215228d7ef2SAndrey A. Chernov 			too_quick = 1;
1216228d7ef2SAndrey A. Chernov 		}
1217228d7ef2SAndrey A. Chernov 	} else
1218228d7ef2SAndrey A. Chernov 		sp->se_nspace = 0;
1219228d7ef2SAndrey A. Chernov 
12208fae3551SRodney W. Grimes 	/*
12218fae3551SRodney W. Grimes 	 * fork(), not vfork() -- we can't afford to block.
12228fae3551SRodney W. Grimes 	 */
12238fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
12248fae3551SRodney W. Grimes 		emergency("can't fork for getty on port %s: %m", sp->se_device);
12258fae3551SRodney W. Grimes 		return -1;
12268fae3551SRodney W. Grimes 	}
12278fae3551SRodney W. Grimes 
12288fae3551SRodney W. Grimes 	if (pid)
12298fae3551SRodney W. Grimes 		return pid;
12308fae3551SRodney W. Grimes 
1231228d7ef2SAndrey A. Chernov 	if (too_quick) {
1232b5df27e2SAndrey A. Chernov 		warning("getty repeating too quickly on port %s, sleeping %d secs",
1233b5df27e2SAndrey A. Chernov 		    sp->se_device, GETTY_SLEEP);
12348fae3551SRodney W. Grimes 		sleep((unsigned) GETTY_SLEEP);
12358fae3551SRodney W. Grimes 	}
12368fae3551SRodney W. Grimes 
12378fae3551SRodney W. Grimes 	if (sp->se_window) {
12388fae3551SRodney W. Grimes 		start_window_system(sp);
12398fae3551SRodney W. Grimes 		sleep(WINDOW_WAIT);
12408fae3551SRodney W. Grimes 	}
12418fae3551SRodney W. Grimes 
12428fae3551SRodney W. Grimes 	sigemptyset(&mask);
12438fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
12448fae3551SRodney W. Grimes 
12451ef60eb1SDavid Nugent #ifdef LOGIN_CAP
12461ef60eb1SDavid Nugent 	setprocresources(RESOURCE_GETTY);
12471ef60eb1SDavid Nugent #endif
1248b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1249b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1250b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
125133a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1252b5df27e2SAndrey A. Chernov 		env[0] = term;
1253b5df27e2SAndrey A. Chernov 		env[1] = 0;
1254091abe40SDavid E. O'Brien 	} else
1255b5df27e2SAndrey A. Chernov 		env[0] = 0;
1256b5df27e2SAndrey A. Chernov 	execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
12578fae3551SRodney W. Grimes 	stall("can't exec getty '%s' for port %s: %m",
12588fae3551SRodney W. Grimes 		sp->se_getty_argv[0], sp->se_device);
12598fae3551SRodney W. Grimes 	_exit(1);
12608fae3551SRodney W. Grimes }
12618fae3551SRodney W. Grimes 
12628fae3551SRodney W. Grimes /*
12638fae3551SRodney W. Grimes  * Collect exit status for a child.
12648fae3551SRodney W. Grimes  * If an exiting login, start a new login running.
12658fae3551SRodney W. Grimes  */
126645cfb1dcSXin LI static void
12678fae3551SRodney W. Grimes collect_child(pid_t pid)
12688fae3551SRodney W. Grimes {
12693d438ad6SDavid E. O'Brien 	session_t *sp, *sprev, *snext;
12708fae3551SRodney W. Grimes 
12718fae3551SRodney W. Grimes 	if (! sessions)
12728fae3551SRodney W. Grimes 		return;
12738fae3551SRodney W. Grimes 
12748fae3551SRodney W. Grimes 	if (! (sp = find_session(pid)))
12758fae3551SRodney W. Grimes 		return;
12768fae3551SRodney W. Grimes 
12778fae3551SRodney W. Grimes 	clear_session_logs(sp);
12788fae3551SRodney W. Grimes 	del_session(sp);
12798fae3551SRodney W. Grimes 	sp->se_process = 0;
12808fae3551SRodney W. Grimes 
12818fae3551SRodney W. Grimes 	if (sp->se_flags & SE_SHUTDOWN) {
12828889c700SDavid Nugent 		if ((sprev = sp->se_prev) != NULL)
12838fae3551SRodney W. Grimes 			sprev->se_next = sp->se_next;
12848fae3551SRodney W. Grimes 		else
12858fae3551SRodney W. Grimes 			sessions = sp->se_next;
12868889c700SDavid Nugent 		if ((snext = sp->se_next) != NULL)
12878fae3551SRodney W. Grimes 			snext->se_prev = sp->se_prev;
12888fae3551SRodney W. Grimes 		free_session(sp);
12898fae3551SRodney W. Grimes 		return;
12908fae3551SRodney W. Grimes 	}
12918fae3551SRodney W. Grimes 
12928fae3551SRodney W. Grimes 	if ((pid = start_getty(sp)) == -1) {
12938fae3551SRodney W. Grimes 		/* serious trouble */
12948fae3551SRodney W. Grimes 		requested_transition = clean_ttys;
12958fae3551SRodney W. Grimes 		return;
12968fae3551SRodney W. Grimes 	}
12978fae3551SRodney W. Grimes 
12988fae3551SRodney W. Grimes 	sp->se_process = pid;
12998fae3551SRodney W. Grimes 	sp->se_started = time((time_t *) 0);
13008fae3551SRodney W. Grimes 	add_session(sp);
13018fae3551SRodney W. Grimes }
13028fae3551SRodney W. Grimes 
13038fae3551SRodney W. Grimes /*
13048fae3551SRodney W. Grimes  * Catch a signal and request a state transition.
13058fae3551SRodney W. Grimes  */
130645cfb1dcSXin LI static void
130773bf18edSWarner Losh transition_handler(int sig)
13088fae3551SRodney W. Grimes {
13098fae3551SRodney W. Grimes 
13108fae3551SRodney W. Grimes 	switch (sig) {
13118fae3551SRodney W. Grimes 	case SIGHUP:
1312acf0ab06SJilles Tjoelker 		if (current_state == read_ttys || current_state == multi_user ||
1313acf0ab06SJilles Tjoelker 		    current_state == clean_ttys || current_state == catatonia)
13148fae3551SRodney W. Grimes 			requested_transition = clean_ttys;
13158fae3551SRodney W. Grimes 		break;
1316a0a549c7SRuslan Ermilov 	case SIGUSR2:
1317a0a549c7SRuslan Ermilov 		howto = RB_POWEROFF;
1318a0a549c7SRuslan Ermilov 	case SIGUSR1:
1319a0a549c7SRuslan Ermilov 		howto |= RB_HALT;
1320e460cfd3SNate Williams 	case SIGINT:
1321db8ad19dSJordan K. Hubbard 		Reboot = TRUE;
13228fae3551SRodney W. Grimes 	case SIGTERM:
1323acf0ab06SJilles Tjoelker 		if (current_state == read_ttys || current_state == multi_user ||
1324acf0ab06SJilles Tjoelker 		    current_state == clean_ttys || current_state == catatonia)
13258fae3551SRodney W. Grimes 			requested_transition = death;
1326acf0ab06SJilles Tjoelker 		else
1327acf0ab06SJilles Tjoelker 			requested_transition = death_single;
13288fae3551SRodney W. Grimes 		break;
13298fae3551SRodney W. Grimes 	case SIGTSTP:
1330acf0ab06SJilles Tjoelker 		if (current_state == runcom || current_state == read_ttys ||
1331acf0ab06SJilles Tjoelker 		    current_state == clean_ttys ||
1332acf0ab06SJilles Tjoelker 		    current_state == multi_user || current_state == catatonia)
13338fae3551SRodney W. Grimes 			requested_transition = catatonia;
13348fae3551SRodney W. Grimes 		break;
13358fae3551SRodney W. Grimes 	default:
13368fae3551SRodney W. Grimes 		requested_transition = 0;
13378fae3551SRodney W. Grimes 		break;
13388fae3551SRodney W. Grimes 	}
13398fae3551SRodney W. Grimes }
13408fae3551SRodney W. Grimes 
13418fae3551SRodney W. Grimes /*
13428fae3551SRodney W. Grimes  * Take the system multiuser.
13438fae3551SRodney W. Grimes  */
134445cfb1dcSXin LI static state_func_t
134573bf18edSWarner Losh multi_user(void)
13468fae3551SRodney W. Grimes {
13478fae3551SRodney W. Grimes 	pid_t pid;
13483d438ad6SDavid E. O'Brien 	session_t *sp;
13498fae3551SRodney W. Grimes 
13508fae3551SRodney W. Grimes 	requested_transition = 0;
13518fae3551SRodney W. Grimes 
13528fae3551SRodney W. Grimes 	/*
13538fae3551SRodney W. Grimes 	 * If the administrator has not set the security level to -1
13548fae3551SRodney W. Grimes 	 * to indicate that the kernel should not run multiuser in secure
13558fae3551SRodney W. Grimes 	 * mode, and the run script has not set a higher level of security
13568fae3551SRodney W. Grimes 	 * than level 1, then put the kernel into secure mode.
13578fae3551SRodney W. Grimes 	 */
13588fae3551SRodney W. Grimes 	if (getsecuritylevel() == 0)
13598fae3551SRodney W. Grimes 		setsecuritylevel(1);
13608fae3551SRodney W. Grimes 
13618fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next) {
13628fae3551SRodney W. Grimes 		if (sp->se_process)
13638fae3551SRodney W. Grimes 			continue;
13648fae3551SRodney W. Grimes 		if ((pid = start_getty(sp)) == -1) {
13658fae3551SRodney W. Grimes 			/* serious trouble */
13668fae3551SRodney W. Grimes 			requested_transition = clean_ttys;
13678fae3551SRodney W. Grimes 			break;
13688fae3551SRodney W. Grimes 		}
13698fae3551SRodney W. Grimes 		sp->se_process = pid;
13708fae3551SRodney W. Grimes 		sp->se_started = time((time_t *) 0);
13718fae3551SRodney W. Grimes 		add_session(sp);
13728fae3551SRodney W. Grimes 	}
13738fae3551SRodney W. Grimes 
13748fae3551SRodney W. Grimes 	while (!requested_transition)
13758fae3551SRodney W. Grimes 		if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
13768fae3551SRodney W. Grimes 			collect_child(pid);
13778fae3551SRodney W. Grimes 
13788fae3551SRodney W. Grimes 	return (state_func_t) requested_transition;
13798fae3551SRodney W. Grimes }
13808fae3551SRodney W. Grimes 
13818fae3551SRodney W. Grimes /*
1382b0b670eeSAlfred Perlstein  * This is an (n*2)+(n^2) algorithm.  We hope it isn't run often...
13838fae3551SRodney W. Grimes  */
138445cfb1dcSXin LI static state_func_t
138573bf18edSWarner Losh clean_ttys(void)
13868fae3551SRodney W. Grimes {
13873d438ad6SDavid E. O'Brien 	session_t *sp, *sprev;
13883d438ad6SDavid E. O'Brien 	struct ttyent *typ;
13893d438ad6SDavid E. O'Brien 	int session_index = 0;
13903d438ad6SDavid E. O'Brien 	int devlen;
1391b5df27e2SAndrey A. Chernov 	char *old_getty, *old_window, *old_type;
13928fae3551SRodney W. Grimes 
1393b0b670eeSAlfred Perlstein 	/*
1394b0b670eeSAlfred Perlstein 	 * mark all sessions for death, (!SE_PRESENT)
1395b0b670eeSAlfred Perlstein 	 * as we find or create new ones they'll be marked as keepers,
1396b0b670eeSAlfred Perlstein 	 * we'll later nuke all the ones not found in /etc/ttys
1397b0b670eeSAlfred Perlstein 	 */
1398b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next)
1399b0b670eeSAlfred Perlstein 		sp->se_flags &= ~SE_PRESENT;
1400b0b670eeSAlfred Perlstein 
14018fae3551SRodney W. Grimes 	devlen = sizeof(_PATH_DEV) - 1;
14028889c700SDavid Nugent 	while ((typ = getttyent()) != NULL) {
14038fae3551SRodney W. Grimes 		++session_index;
14048fae3551SRodney W. Grimes 
14058fae3551SRodney W. Grimes 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
14068fae3551SRodney W. Grimes 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
14078fae3551SRodney W. Grimes 				break;
14088fae3551SRodney W. Grimes 
14098fae3551SRodney W. Grimes 		if (sp) {
1410b0b670eeSAlfred Perlstein 			/* we want this one to live */
1411b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_PRESENT;
14128fae3551SRodney W. Grimes 			if (sp->se_index != session_index) {
14138fae3551SRodney W. Grimes 				warning("port %s changed utmp index from %d to %d",
14148fae3551SRodney W. Grimes 				       sp->se_device, sp->se_index,
14158fae3551SRodney W. Grimes 				       session_index);
14168fae3551SRodney W. Grimes 				sp->se_index = session_index;
14178fae3551SRodney W. Grimes 			}
14188fae3551SRodney W. Grimes 			if ((typ->ty_status & TTY_ON) == 0 ||
14198fae3551SRodney W. Grimes 			    typ->ty_getty == 0) {
14208fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
14218fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
14228fae3551SRodney W. Grimes 				continue;
14238fae3551SRodney W. Grimes 			}
14248fae3551SRodney W. Grimes 			sp->se_flags &= ~SE_SHUTDOWN;
1425b5df27e2SAndrey A. Chernov 			old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1426b5df27e2SAndrey A. Chernov 			old_window = sp->se_window ? strdup(sp->se_window) : 0;
1427b5df27e2SAndrey A. Chernov 			old_type = sp->se_type ? strdup(sp->se_type) : 0;
14288fae3551SRodney W. Grimes 			if (setupargv(sp, typ) == 0) {
14298fae3551SRodney W. Grimes 				warning("can't parse getty for port %s",
14308fae3551SRodney W. Grimes 					sp->se_device);
14318fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
14328fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
14338fae3551SRodney W. Grimes 			}
1434b5df27e2SAndrey A. Chernov 			else if (   !old_getty
14358889c700SDavid Nugent 				 || (!old_type && sp->se_type)
14368889c700SDavid Nugent 				 || (old_type && !sp->se_type)
14378889c700SDavid Nugent 				 || (!old_window && sp->se_window)
14388889c700SDavid Nugent 				 || (old_window && !sp->se_window)
14398889c700SDavid Nugent 				 || (strcmp(old_getty, sp->se_getty) != 0)
14408889c700SDavid Nugent 				 || (old_window && strcmp(old_window, sp->se_window) != 0)
14418889c700SDavid Nugent 				 || (old_type && strcmp(old_type, sp->se_type) != 0)
1442b5df27e2SAndrey A. Chernov 				) {
1443b5df27e2SAndrey A. Chernov 				/* Don't set SE_SHUTDOWN here */
1444b5df27e2SAndrey A. Chernov 				sp->se_nspace = 0;
1445b5df27e2SAndrey A. Chernov 				sp->se_started = 0;
1446b5df27e2SAndrey A. Chernov 				kill(sp->se_process, SIGHUP);
1447b5df27e2SAndrey A. Chernov 			}
1448b5df27e2SAndrey A. Chernov 			if (old_getty)
1449b5df27e2SAndrey A. Chernov 				free(old_getty);
14502d887af5SMike Heffner 			if (old_window)
1451b5df27e2SAndrey A. Chernov 				free(old_window);
1452b5df27e2SAndrey A. Chernov 			if (old_type)
1453b5df27e2SAndrey A. Chernov 				free(old_type);
14548fae3551SRodney W. Grimes 			continue;
14558fae3551SRodney W. Grimes 		}
14568fae3551SRodney W. Grimes 
14578fae3551SRodney W. Grimes 		new_session(sprev, session_index, typ);
14588fae3551SRodney W. Grimes 	}
14598fae3551SRodney W. Grimes 
14608fae3551SRodney W. Grimes 	endttyent();
14618fae3551SRodney W. Grimes 
1462b0b670eeSAlfred Perlstein 	/*
1463b0b670eeSAlfred Perlstein 	 * sweep through and kill all deleted sessions
1464b0b670eeSAlfred Perlstein 	 * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1465b0b670eeSAlfred Perlstein 	 */
1466b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next) {
1467b0b670eeSAlfred Perlstein 		if ((sp->se_flags & SE_PRESENT) == 0) {
1468b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_SHUTDOWN;
1469b0b670eeSAlfred Perlstein 			kill(sp->se_process, SIGHUP);
1470b0b670eeSAlfred Perlstein 		}
1471b0b670eeSAlfred Perlstein 	}
1472b0b670eeSAlfred Perlstein 
14738fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
14748fae3551SRodney W. Grimes }
14758fae3551SRodney W. Grimes 
14768fae3551SRodney W. Grimes /*
14778fae3551SRodney W. Grimes  * Block further logins.
14788fae3551SRodney W. Grimes  */
147945cfb1dcSXin LI static state_func_t
148073bf18edSWarner Losh catatonia(void)
14818fae3551SRodney W. Grimes {
14823d438ad6SDavid E. O'Brien 	session_t *sp;
14838fae3551SRodney W. Grimes 
14848fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next)
14858fae3551SRodney W. Grimes 		sp->se_flags |= SE_SHUTDOWN;
14868fae3551SRodney W. Grimes 
14878fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
14888fae3551SRodney W. Grimes }
14898fae3551SRodney W. Grimes 
14908fae3551SRodney W. Grimes /*
14918fae3551SRodney W. Grimes  * Note SIGALRM.
14928fae3551SRodney W. Grimes  */
149345cfb1dcSXin LI static void
149473bf18edSWarner Losh alrm_handler(int sig)
14958fae3551SRodney W. Grimes {
1496091abe40SDavid E. O'Brien 
14978889c700SDavid Nugent 	(void)sig;
14988fae3551SRodney W. Grimes 	clang = 1;
14998fae3551SRodney W. Grimes }
15008fae3551SRodney W. Grimes 
15018fae3551SRodney W. Grimes /*
15028fae3551SRodney W. Grimes  * Bring the system down to single user.
15038fae3551SRodney W. Grimes  */
150445cfb1dcSXin LI static state_func_t
150573bf18edSWarner Losh death(void)
15068fae3551SRodney W. Grimes {
150714c69f21SEd Schouten 	struct utmpx utx;
15083d438ad6SDavid E. O'Brien 	session_t *sp;
15098fae3551SRodney W. Grimes 
15103f31fb33SAndrey A. Chernov 	/* NB: should send a message to the session logger to avoid blocking. */
151114c69f21SEd Schouten 	utx.ut_type = SHUTDOWN_TIME;
151214c69f21SEd Schouten 	gettimeofday(&utx.ut_tv, NULL);
151314c69f21SEd Schouten 	pututxline(&utx);
15143f31fb33SAndrey A. Chernov 
15154ae35b5dSEd Schouten 	/*
15164ae35b5dSEd Schouten 	 * Also revoke the TTY here.  Because runshutdown() may reopen
15174ae35b5dSEd Schouten 	 * the TTY whose getty we're killing here, there is no guarantee
15184ae35b5dSEd Schouten 	 * runshutdown() will perform the initial open() call, causing
15194ae35b5dSEd Schouten 	 * the terminal attributes to be misconfigured.
15204ae35b5dSEd Schouten 	 */
15211054bb1eSAndrey A. Chernov 	for (sp = sessions; sp; sp = sp->se_next) {
15228fae3551SRodney W. Grimes 		sp->se_flags |= SE_SHUTDOWN;
152325cf4a54SAndrey A. Chernov 		kill(sp->se_process, SIGHUP);
15244ae35b5dSEd Schouten 		revoke(sp->se_device);
15251054bb1eSAndrey A. Chernov 	}
15268fae3551SRodney W. Grimes 
15278889c700SDavid Nugent 	/* Try to run the rc.shutdown script within a period of time */
1528091abe40SDavid E. O'Brien 	runshutdown();
15298889c700SDavid Nugent 
1530acf0ab06SJilles Tjoelker 	return (state_func_t) death_single;
1531acf0ab06SJilles Tjoelker }
1532acf0ab06SJilles Tjoelker 
1533acf0ab06SJilles Tjoelker /*
1534acf0ab06SJilles Tjoelker  * Do what is necessary to reinitialize single user mode or reboot
1535acf0ab06SJilles Tjoelker  * from an incomplete state.
1536acf0ab06SJilles Tjoelker  */
1537acf0ab06SJilles Tjoelker static state_func_t
1538acf0ab06SJilles Tjoelker death_single(void)
1539acf0ab06SJilles Tjoelker {
1540acf0ab06SJilles Tjoelker 	int i;
1541acf0ab06SJilles Tjoelker 	pid_t pid;
1542acf0ab06SJilles Tjoelker 	static const int death_sigs[2] = { SIGTERM, SIGKILL };
1543acf0ab06SJilles Tjoelker 
1544acf0ab06SJilles Tjoelker 	revoke(_PATH_CONSOLE);
1545acf0ab06SJilles Tjoelker 
1546c3d7c52eSAndrey A. Chernov 	for (i = 0; i < 2; ++i) {
15478fae3551SRodney W. Grimes 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
15488fae3551SRodney W. Grimes 			return (state_func_t) single_user;
15498fae3551SRodney W. Grimes 
15508fae3551SRodney W. Grimes 		clang = 0;
15518fae3551SRodney W. Grimes 		alarm(DEATH_WATCH);
15528fae3551SRodney W. Grimes 		do
15538fae3551SRodney W. Grimes 			if ((pid = waitpid(-1, (int *)0, 0)) != -1)
15548fae3551SRodney W. Grimes 				collect_child(pid);
15558fae3551SRodney W. Grimes 		while (clang == 0 && errno != ECHILD);
15568fae3551SRodney W. Grimes 
15578fae3551SRodney W. Grimes 		if (errno == ECHILD)
15588fae3551SRodney W. Grimes 			return (state_func_t) single_user;
15598fae3551SRodney W. Grimes 	}
15608fae3551SRodney W. Grimes 
15618fae3551SRodney W. Grimes 	warning("some processes would not die; ps axl advised");
15628fae3551SRodney W. Grimes 
15638fae3551SRodney W. Grimes 	return (state_func_t) single_user;
15648fae3551SRodney W. Grimes }
15658889c700SDavid Nugent 
15668889c700SDavid Nugent /*
15678889c700SDavid Nugent  * Run the system shutdown script.
15688889c700SDavid Nugent  *
15698889c700SDavid Nugent  * Exit codes:      XXX I should document more
15708889c700SDavid Nugent  * -2       shutdown script terminated abnormally
15718889c700SDavid Nugent  * -1       fatal error - can't run script
15728889c700SDavid Nugent  * 0        good.
15738889c700SDavid Nugent  * >0       some error (exit code)
15748889c700SDavid Nugent  */
157545cfb1dcSXin LI static int
157673bf18edSWarner Losh runshutdown(void)
15778889c700SDavid Nugent {
15788889c700SDavid Nugent 	pid_t pid, wpid;
15798889c700SDavid Nugent 	int status;
15808889c700SDavid Nugent 	int shutdowntimeout;
15818889c700SDavid Nugent 	size_t len;
1582a69497d7SMatthew Dillon 	char *argv[4];
15831a7bec91SWarner Losh 	const char *shell;
15848889c700SDavid Nugent 	struct sigaction sa;
158586bf62dcSDavid Nugent 	struct stat sb;
158686bf62dcSDavid Nugent 
158786bf62dcSDavid Nugent 	/*
158886bf62dcSDavid Nugent 	 * rc.shutdown is optional, so to prevent any unnecessary
158986bf62dcSDavid Nugent 	 * complaints from the shell we simply don't run it if the
159086bf62dcSDavid Nugent 	 * file does not exist. If the stat() here fails for other
159186bf62dcSDavid Nugent 	 * reasons, we'll let the shell complain.
159286bf62dcSDavid Nugent 	 */
159386bf62dcSDavid Nugent 	if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
159486bf62dcSDavid Nugent 		return 0;
15958889c700SDavid Nugent 
15961a7bec91SWarner Losh 	shell = get_shell();
15971a7bec91SWarner Losh 
15988889c700SDavid Nugent 	if ((pid = fork()) == 0) {
15998889c700SDavid Nugent 		sigemptyset(&sa.sa_mask);
16008889c700SDavid Nugent 		sa.sa_flags = 0;
16018889c700SDavid Nugent 		sa.sa_handler = SIG_IGN;
1602091abe40SDavid E. O'Brien 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
1603091abe40SDavid E. O'Brien 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
16048889c700SDavid Nugent 
160543273e33SEd Schouten 		setctty(_PATH_CONSOLE);
1606ab03e6d5SXin LI 
1607ab03e6d5SXin LI 		char _sh[]	= "sh";
1608ab03e6d5SXin LI 		char _reboot[]	= "reboot";
1609ab03e6d5SXin LI 		char _single[]	= "single";
1610ab03e6d5SXin LI 		char _path_rundown[] = _PATH_RUNDOWN;
1611ab03e6d5SXin LI 
1612ab03e6d5SXin LI 		argv[0] = _sh;
1613ab03e6d5SXin LI 		argv[1] = _path_rundown;
1614ab03e6d5SXin LI 		argv[2] = Reboot ? _reboot : _single;
1615a69497d7SMatthew Dillon 		argv[3] = 0;
16168889c700SDavid Nugent 
16178889c700SDavid Nugent 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
16188889c700SDavid Nugent 
161925cf4a54SAndrey A. Chernov #ifdef LOGIN_CAP
162025cf4a54SAndrey A. Chernov 		setprocresources(RESOURCE_RC);
162125cf4a54SAndrey A. Chernov #endif
16221a7bec91SWarner Losh 		execv(shell, argv);
16231a7bec91SWarner Losh 		warning("can't exec %s for %s: %m", shell, _PATH_RUNDOWN);
16248889c700SDavid Nugent 		_exit(1);	/* force single user mode */
16258889c700SDavid Nugent 	}
16268889c700SDavid Nugent 
16278889c700SDavid Nugent 	if (pid == -1) {
16281a7bec91SWarner Losh 		emergency("can't fork for %s on %s: %m", shell, _PATH_RUNDOWN);
16298889c700SDavid Nugent 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
16308889c700SDavid Nugent 			continue;
16318889c700SDavid Nugent 		sleep(STALL_TIMEOUT);
16328889c700SDavid Nugent 		return -1;
16338889c700SDavid Nugent 	}
16348889c700SDavid Nugent 
16358889c700SDavid Nugent 	len = sizeof(shutdowntimeout);
1636091abe40SDavid E. O'Brien 	if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len,
1637091abe40SDavid E. O'Brien 	    NULL, 0) == -1 || shutdowntimeout < 2)
16388889c700SDavid Nugent 		shutdowntimeout = DEATH_SCRIPT;
16398889c700SDavid Nugent 	alarm(shutdowntimeout);
16408889c700SDavid Nugent 	clang = 0;
16418889c700SDavid Nugent 	/*
16428889c700SDavid Nugent 	 * Copied from single_user().  This is a bit paranoid.
16438889c700SDavid Nugent 	 * Use the same ALRM handler.
16448889c700SDavid Nugent 	 */
16458889c700SDavid Nugent 	do {
16468889c700SDavid Nugent 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
16478889c700SDavid Nugent 			collect_child(wpid);
16488889c700SDavid Nugent 		if (clang == 1) {
16498889c700SDavid Nugent 			/* we were waiting for the sub-shell */
16508889c700SDavid Nugent 			kill(wpid, SIGTERM);
16511a7bec91SWarner Losh 			warning("timeout expired for %s on %s: %m; going to "
16521a7bec91SWarner Losh 			    "single user mode", shell, _PATH_RUNDOWN);
16538889c700SDavid Nugent 			return -1;
16548889c700SDavid Nugent 		}
16558889c700SDavid Nugent 		if (wpid == -1) {
16568889c700SDavid Nugent 			if (errno == EINTR)
16578889c700SDavid Nugent 				continue;
16581a7bec91SWarner Losh 			warning("wait for %s on %s failed: %m; going to "
16591a7bec91SWarner Losh 			    "single user mode", shell, _PATH_RUNDOWN);
16608889c700SDavid Nugent 			return -1;
16618889c700SDavid Nugent 		}
16628889c700SDavid Nugent 		if (wpid == pid && WIFSTOPPED(status)) {
16638889c700SDavid Nugent 			warning("init: %s on %s stopped, restarting\n",
16641a7bec91SWarner Losh 				shell, _PATH_RUNDOWN);
16658889c700SDavid Nugent 			kill(pid, SIGCONT);
16668889c700SDavid Nugent 			wpid = -1;
16678889c700SDavid Nugent 		}
16688889c700SDavid Nugent 	} while (wpid != pid && !clang);
16698889c700SDavid Nugent 
16708889c700SDavid Nugent 	/* Turn off the alarm */
16718889c700SDavid Nugent 	alarm(0);
16728889c700SDavid Nugent 
16738889c700SDavid Nugent 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
16748889c700SDavid Nugent 	    requested_transition == catatonia) {
16758889c700SDavid Nugent 		/*
16768889c700SDavid Nugent 		 * /etc/rc.shutdown executed /sbin/reboot;
16778889c700SDavid Nugent 		 * wait for the end quietly
16788889c700SDavid Nugent 		 */
16798889c700SDavid Nugent 		sigset_t s;
16808889c700SDavid Nugent 
16818889c700SDavid Nugent 		sigfillset(&s);
16828889c700SDavid Nugent 		for (;;)
16838889c700SDavid Nugent 			sigsuspend(&s);
16848889c700SDavid Nugent 	}
16858889c700SDavid Nugent 
16868889c700SDavid Nugent 	if (!WIFEXITED(status)) {
16871a7bec91SWarner Losh 		warning("%s on %s terminated abnormally, going to "
16881a7bec91SWarner Losh 		    "single user mode", shell, _PATH_RUNDOWN);
16898889c700SDavid Nugent 		return -2;
16908889c700SDavid Nugent 	}
16918889c700SDavid Nugent 
16928889c700SDavid Nugent 	if ((status = WEXITSTATUS(status)) != 0)
16938889c700SDavid Nugent 		warning("%s returned status %d", _PATH_RUNDOWN, status);
16948889c700SDavid Nugent 
16958889c700SDavid Nugent 	return status;
16968889c700SDavid Nugent }
16978889c700SDavid Nugent 
1698ab03e6d5SXin LI static char *
169981ab7fb2SAndrey A. Chernov strk(char *p)
170081ab7fb2SAndrey A. Chernov {
170181ab7fb2SAndrey A. Chernov 	static char *t;
170281ab7fb2SAndrey A. Chernov 	char *q;
170381ab7fb2SAndrey A. Chernov 	int c;
170481ab7fb2SAndrey A. Chernov 
170581ab7fb2SAndrey A. Chernov 	if (p)
170681ab7fb2SAndrey A. Chernov 		t = p;
170781ab7fb2SAndrey A. Chernov 	if (!t)
170881ab7fb2SAndrey A. Chernov 		return 0;
170981ab7fb2SAndrey A. Chernov 
171081ab7fb2SAndrey A. Chernov 	c = *t;
171181ab7fb2SAndrey A. Chernov 	while (c == ' ' || c == '\t' )
171281ab7fb2SAndrey A. Chernov 		c = *++t;
171381ab7fb2SAndrey A. Chernov 	if (!c) {
171481ab7fb2SAndrey A. Chernov 		t = 0;
171581ab7fb2SAndrey A. Chernov 		return 0;
171681ab7fb2SAndrey A. Chernov 	}
171781ab7fb2SAndrey A. Chernov 	q = t;
171881ab7fb2SAndrey A. Chernov 	if (c == '\'') {
171981ab7fb2SAndrey A. Chernov 		c = *++t;
172081ab7fb2SAndrey A. Chernov 		q = t;
172181ab7fb2SAndrey A. Chernov 		while (c && c != '\'')
172281ab7fb2SAndrey A. Chernov 			c = *++t;
172381ab7fb2SAndrey A. Chernov 		if (!c)  /* unterminated string */
172481ab7fb2SAndrey A. Chernov 			q = t = 0;
172581ab7fb2SAndrey A. Chernov 		else
172681ab7fb2SAndrey A. Chernov 			*t++ = 0;
172781ab7fb2SAndrey A. Chernov 	} else {
172881ab7fb2SAndrey A. Chernov 		while (c && c != ' ' && c != '\t' )
172981ab7fb2SAndrey A. Chernov 			c = *++t;
173081ab7fb2SAndrey A. Chernov 		*t++ = 0;
173181ab7fb2SAndrey A. Chernov 		if (!c)
173281ab7fb2SAndrey A. Chernov 			t = 0;
173381ab7fb2SAndrey A. Chernov 	}
173481ab7fb2SAndrey A. Chernov 	return q;
173581ab7fb2SAndrey A. Chernov }
17361ef60eb1SDavid Nugent 
17371ef60eb1SDavid Nugent #ifdef LOGIN_CAP
173845cfb1dcSXin LI static void
173973bf18edSWarner Losh setprocresources(const char *cname)
17401ef60eb1SDavid Nugent {
1741e82d5545SDavid Nugent 	login_cap_t *lc;
1742a2ee73bcSAndrey A. Chernov 	if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
1743091abe40SDavid E. O'Brien 		setusercontext(lc, (struct passwd*)NULL, 0,
1744091abe40SDavid E. O'Brien 		    LOGIN_SETPRIORITY | LOGIN_SETRESOURCES);
17451ef60eb1SDavid Nugent 		login_close(lc);
17461ef60eb1SDavid Nugent 	}
17471ef60eb1SDavid Nugent }
17481ef60eb1SDavid Nugent #endif
1749