xref: /freebsd/sbin/init/init.c (revision 5010c3b657c9497ce3e948324b487f1933bcf6eb)
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>
58423b6a39SAndrey A. Chernov #include <libutil.h>
591a37aa56SDavid E. O'Brien #include <paths.h>
608fae3551SRodney W. Grimes #include <signal.h>
618fae3551SRodney W. Grimes #include <stdio.h>
628fae3551SRodney W. Grimes #include <stdlib.h>
638fae3551SRodney W. Grimes #include <string.h>
648fae3551SRodney W. Grimes #include <syslog.h>
658fae3551SRodney W. Grimes #include <time.h>
668fae3551SRodney W. Grimes #include <ttyent.h>
678fae3551SRodney W. Grimes #include <unistd.h>
68e460cfd3SNate Williams #include <sys/reboot.h>
69c5842835SPhilippe Charnier #include <err.h>
708fae3551SRodney W. Grimes 
718fae3551SRodney W. Grimes #include <stdarg.h>
728fae3551SRodney W. Grimes 
738fae3551SRodney W. Grimes #ifdef SECURE
748fae3551SRodney W. Grimes #include <pwd.h>
758fae3551SRodney W. Grimes #endif
768fae3551SRodney W. Grimes 
771ef60eb1SDavid Nugent #ifdef LOGIN_CAP
781ef60eb1SDavid Nugent #include <login_cap.h>
791ef60eb1SDavid Nugent #endif
801ef60eb1SDavid Nugent 
818fae3551SRodney W. Grimes #include "pathnames.h"
828fae3551SRodney W. Grimes 
838fae3551SRodney W. Grimes /*
848fae3551SRodney W. Grimes  * Sleep times; used to prevent thrashing.
858fae3551SRodney W. Grimes  */
868fae3551SRodney W. Grimes #define	GETTY_SPACING		 5	/* N secs minimum getty spacing */
878fae3551SRodney W. Grimes #define	GETTY_SLEEP		30	/* sleep N secs after spacing problem */
88b5df27e2SAndrey A. Chernov #define GETTY_NSPACE             3      /* max. spacing count to bring reaction */
898fae3551SRodney W. Grimes #define	WINDOW_WAIT		 3	/* wait N secs after starting window */
908fae3551SRodney W. Grimes #define	STALL_TIMEOUT		30	/* wait N secs after warning */
918fae3551SRodney W. Grimes #define	DEATH_WATCH		10	/* wait N secs for procs to die */
925df42cf4SPhilippe Charnier #define DEATH_SCRIPT		120	/* wait for 2min for /etc/rc.shutdown */
93e82d5545SDavid Nugent #define RESOURCE_RC		"daemon"
94e82d5545SDavid Nugent #define RESOURCE_WINDOW 	"default"
95e82d5545SDavid Nugent #define RESOURCE_GETTY		"default"
968fae3551SRodney W. Grimes 
9773bf18edSWarner Losh void handle(sig_t, ...);
9873bf18edSWarner Losh void delset(sigset_t *, ...);
998fae3551SRodney W. Grimes 
10073bf18edSWarner Losh void stall(const char *, ...) __printflike(1, 2);
10173bf18edSWarner Losh void warning(const char *, ...) __printflike(1, 2);
10273bf18edSWarner Losh void emergency(const char *, ...) __printflike(1, 2);
10373bf18edSWarner Losh void disaster(int);
10473bf18edSWarner Losh void badsys(int);
10573bf18edSWarner Losh int  runshutdown(void);
106ab03e6d5SXin LI static char *strk(char *);
1078fae3551SRodney W. Grimes 
1088fae3551SRodney W. Grimes /*
1098fae3551SRodney W. Grimes  * We really need a recursive typedef...
1108fae3551SRodney W. Grimes  * The following at least guarantees that the return type of (*state_t)()
1118fae3551SRodney W. Grimes  * is sufficiently wide to hold a function pointer.
1128fae3551SRodney W. Grimes  */
11373bf18edSWarner Losh typedef long (*state_func_t)(void);
11473bf18edSWarner Losh typedef state_func_t (*state_t)(void);
1158fae3551SRodney W. Grimes 
11673bf18edSWarner Losh state_func_t single_user(void);
11773bf18edSWarner Losh state_func_t runcom(void);
11873bf18edSWarner Losh state_func_t read_ttys(void);
11973bf18edSWarner Losh state_func_t multi_user(void);
12073bf18edSWarner Losh state_func_t clean_ttys(void);
12173bf18edSWarner Losh state_func_t catatonia(void);
12273bf18edSWarner Losh state_func_t death(void);
1238fae3551SRodney W. Grimes 
1248fae3551SRodney W. Grimes enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
12577103ea3SPoul-Henning Kamp #define FALSE	0
12677103ea3SPoul-Henning Kamp #define TRUE	1
12777103ea3SPoul-Henning Kamp 
128db8ad19dSJordan K. Hubbard int Reboot = FALSE;
129a0a549c7SRuslan Ermilov int howto = RB_AUTOBOOT;
1308fae3551SRodney W. Grimes 
13157622f22SPoul-Henning Kamp int devfs;
13257622f22SPoul-Henning Kamp 
13373bf18edSWarner Losh void transition(state_t);
1348fae3551SRodney W. Grimes state_t requested_transition = runcom;
1358fae3551SRodney W. Grimes 
136ab03e6d5SXin LI void setctty(const char *);
1378fae3551SRodney W. Grimes 
1388fae3551SRodney W. Grimes typedef struct init_session {
1398fae3551SRodney W. Grimes 	int	se_index;		/* index of entry in ttys file */
1408fae3551SRodney W. Grimes 	pid_t	se_process;		/* controlling process */
1418fae3551SRodney W. Grimes 	time_t	se_started;		/* used to avoid thrashing */
1428fae3551SRodney W. Grimes 	int	se_flags;		/* status of session */
1438fae3551SRodney W. Grimes #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
144b0b670eeSAlfred Perlstein #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
145b5df27e2SAndrey A. Chernov 	int     se_nspace;              /* spacing count */
1468fae3551SRodney W. Grimes 	char	*se_device;		/* filename of port */
1478fae3551SRodney W. Grimes 	char	*se_getty;		/* what to run on that port */
148b5df27e2SAndrey A. Chernov 	char    *se_getty_argv_space;   /* pre-parsed argument array space */
1498fae3551SRodney W. Grimes 	char	**se_getty_argv;	/* pre-parsed argument array */
1508fae3551SRodney W. Grimes 	char	*se_window;		/* window system (started only once) */
151b5df27e2SAndrey A. Chernov 	char    *se_window_argv_space;  /* pre-parsed argument array space */
1528fae3551SRodney W. Grimes 	char	**se_window_argv;	/* pre-parsed argument array */
153b5df27e2SAndrey A. Chernov 	char    *se_type;               /* default terminal type */
1548fae3551SRodney W. Grimes 	struct	init_session *se_prev;
1558fae3551SRodney W. Grimes 	struct	init_session *se_next;
1568fae3551SRodney W. Grimes } session_t;
1578fae3551SRodney W. Grimes 
15873bf18edSWarner Losh void free_session(session_t *);
15973bf18edSWarner Losh session_t *new_session(session_t *, int, struct ttyent *);
1608fae3551SRodney W. Grimes session_t *sessions;
1618fae3551SRodney W. Grimes 
16273bf18edSWarner Losh char **construct_argv(char *);
16373bf18edSWarner Losh void start_window_system(session_t *);
16473bf18edSWarner Losh void collect_child(pid_t);
16573bf18edSWarner Losh pid_t start_getty(session_t *);
16673bf18edSWarner Losh void transition_handler(int);
16773bf18edSWarner Losh void alrm_handler(int);
16873bf18edSWarner Losh void setsecuritylevel(int);
16973bf18edSWarner Losh int getsecuritylevel(void);
17073bf18edSWarner Losh int setupargv(session_t *, struct ttyent *);
171e82d5545SDavid Nugent #ifdef LOGIN_CAP
17273bf18edSWarner Losh void setprocresources(const char *);
173e82d5545SDavid Nugent #endif
1748fae3551SRodney W. Grimes int clang;
1758fae3551SRodney W. Grimes 
17673bf18edSWarner Losh void clear_session_logs(session_t *);
1778fae3551SRodney W. Grimes 
17873bf18edSWarner Losh int start_session_db(void);
17973bf18edSWarner Losh void add_session(session_t *);
18073bf18edSWarner Losh void del_session(session_t *);
18173bf18edSWarner Losh session_t *find_session(pid_t);
1828fae3551SRodney W. Grimes DB *session_db;
1838fae3551SRodney W. Grimes 
1848fae3551SRodney W. Grimes /*
1858fae3551SRodney W. Grimes  * The mother of all processes.
1868fae3551SRodney W. Grimes  */
1878fae3551SRodney W. Grimes int
18873bf18edSWarner Losh main(int argc, char *argv[])
1898fae3551SRodney W. Grimes {
1908fae3551SRodney W. Grimes 	int c;
1918fae3551SRodney W. Grimes 	struct sigaction sa;
1928fae3551SRodney W. Grimes 	sigset_t mask;
1938fae3551SRodney W. Grimes 
1948fae3551SRodney W. Grimes 
1958fae3551SRodney W. Grimes 	/* Dispose of random users. */
196c5842835SPhilippe Charnier 	if (getuid() != 0)
197c5842835SPhilippe Charnier 		errx(1, "%s", strerror(EPERM));
1988fae3551SRodney W. Grimes 
1998fae3551SRodney W. Grimes 	/* System V users like to reexec init. */
2001681d659SRuslan Ermilov 	if (getpid() != 1) {
2011681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT
2021681d659SRuslan Ermilov 		/* So give them what they want */
2031681d659SRuslan Ermilov 		if (argc > 1) {
2041681d659SRuslan Ermilov 			if (strlen(argv[1]) == 1) {
2053d438ad6SDavid E. O'Brien 				char runlevel = *argv[1];
2063d438ad6SDavid E. O'Brien 				int sig;
2078fae3551SRodney W. Grimes 
2081681d659SRuslan Ermilov 				switch (runlevel) {
2091681d659SRuslan Ermilov 					case '0': /* halt + poweroff */
2101681d659SRuslan Ermilov 						sig = SIGUSR2;
2111681d659SRuslan Ermilov 						break;
2121681d659SRuslan Ermilov 					case '1': /* single-user */
2131681d659SRuslan Ermilov 						sig = SIGTERM;
2141681d659SRuslan Ermilov 						break;
2151681d659SRuslan Ermilov 					case '6': /* reboot */
2161681d659SRuslan Ermilov 						sig = SIGINT;
2171681d659SRuslan Ermilov 						break;
2181681d659SRuslan Ermilov 					case 'c': /* block further logins */
2191681d659SRuslan Ermilov 						sig = SIGTSTP;
2201681d659SRuslan Ermilov 						break;
2211681d659SRuslan Ermilov 					case 'q': /* rescan /etc/ttys */
2221681d659SRuslan Ermilov 						sig = SIGHUP;
2231681d659SRuslan Ermilov 						break;
2241681d659SRuslan Ermilov 					default:
2251681d659SRuslan Ermilov 						goto invalid;
2261681d659SRuslan Ermilov 				}
2271681d659SRuslan Ermilov 				kill(1, sig);
2281681d659SRuslan Ermilov 				_exit(0);
2291681d659SRuslan Ermilov 			} else
2301681d659SRuslan Ermilov invalid:
2311681d659SRuslan Ermilov 				errx(1, "invalid run-level ``%s''", argv[1]);
2321681d659SRuslan Ermilov 		} else
2331681d659SRuslan Ermilov #endif
2341681d659SRuslan Ermilov 			errx(1, "already running");
2351681d659SRuslan Ermilov 	}
2368fae3551SRodney W. Grimes 	/*
2378fae3551SRodney W. Grimes 	 * Note that this does NOT open a file...
2388fae3551SRodney W. Grimes 	 * Does 'init' deserve its own facility number?
2398fae3551SRodney W. Grimes 	 */
2408fae3551SRodney W. Grimes 	openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH);
2418fae3551SRodney W. Grimes 
2428fae3551SRodney W. Grimes 	/*
2438fae3551SRodney W. Grimes 	 * Create an initial session.
2448fae3551SRodney W. Grimes 	 */
2458fae3551SRodney W. Grimes 	if (setsid() < 0)
2468fae3551SRodney W. Grimes 		warning("initial setsid() failed: %m");
2478fae3551SRodney W. Grimes 
2488fae3551SRodney W. Grimes 	/*
2498fae3551SRodney W. Grimes 	 * Establish an initial user so that programs running
2508fae3551SRodney W. Grimes 	 * single user do not freak out and die (like passwd).
2518fae3551SRodney W. Grimes 	 */
2528fae3551SRodney W. Grimes 	if (setlogin("root") < 0)
2538fae3551SRodney W. Grimes 		warning("setlogin() failed: %m");
2548fae3551SRodney W. Grimes 
2558fae3551SRodney W. Grimes 	/*
2568fae3551SRodney W. Grimes 	 * This code assumes that we always get arguments through flags,
2578fae3551SRodney W. Grimes 	 * never through bits set in some random machine register.
2588fae3551SRodney W. Grimes 	 */
25957622f22SPoul-Henning Kamp 	while ((c = getopt(argc, argv, "dsf")) != -1)
2608fae3551SRodney W. Grimes 		switch (c) {
26157622f22SPoul-Henning Kamp 		case 'd':
26257622f22SPoul-Henning Kamp 			devfs = 1;
26357622f22SPoul-Henning Kamp 			break;
2648fae3551SRodney W. Grimes 		case 's':
2658fae3551SRodney W. Grimes 			requested_transition = single_user;
2668fae3551SRodney W. Grimes 			break;
2678fae3551SRodney W. Grimes 		case 'f':
2688fae3551SRodney W. Grimes 			runcom_mode = FASTBOOT;
2698fae3551SRodney W. Grimes 			break;
2708fae3551SRodney W. Grimes 		default:
2718fae3551SRodney W. Grimes 			warning("unrecognized flag '-%c'", c);
2728fae3551SRodney W. Grimes 			break;
2738fae3551SRodney W. Grimes 		}
2748fae3551SRodney W. Grimes 
2758fae3551SRodney W. Grimes 	if (optind != argc)
2768fae3551SRodney W. Grimes 		warning("ignoring excess arguments");
2778fae3551SRodney W. Grimes 
27857622f22SPoul-Henning Kamp 	if (devfs) {
2791f083b1eSMaxime Henrion 		struct iovec iov[4];
280421b0201SPoul-Henning Kamp 		char *s;
281421b0201SPoul-Henning Kamp 		int i;
282421b0201SPoul-Henning Kamp 
283ab03e6d5SXin LI 		char _fstype[]	= "fstype";
284ab03e6d5SXin LI 		char _devfs[]	= "devfs";
285ab03e6d5SXin LI 		char _fspath[]	= "fspath";
286ab03e6d5SXin LI 		char _path_dev[]= _PATH_DEV;
287ab03e6d5SXin LI 
288ab03e6d5SXin LI 		iov[0].iov_base = _fstype;
289ab03e6d5SXin LI 		iov[0].iov_len = sizeof(_fstype);
290ab03e6d5SXin LI 		iov[1].iov_base = _devfs;
291ab03e6d5SXin LI 		iov[1].iov_len = sizeof(_devfs);
292ab03e6d5SXin LI 		iov[2].iov_base = _fspath;
293ab03e6d5SXin LI 		iov[2].iov_len = sizeof(_fspath);
294421b0201SPoul-Henning Kamp 		/*
295421b0201SPoul-Henning Kamp 		 * Try to avoid the trailing slash in _PATH_DEV.
296421b0201SPoul-Henning Kamp 		 * Be *very* defensive.
297421b0201SPoul-Henning Kamp 		 */
298421b0201SPoul-Henning Kamp 		s = strdup(_PATH_DEV);
299421b0201SPoul-Henning Kamp 		if (s != NULL) {
300421b0201SPoul-Henning Kamp 			i = strlen(s);
301421b0201SPoul-Henning Kamp 			if (i > 0 && s[i - 1] == '/')
302421b0201SPoul-Henning Kamp 				s[i - 1] = '\0';
3031f083b1eSMaxime Henrion 			iov[3].iov_base = s;
3041f083b1eSMaxime Henrion 			iov[3].iov_len = strlen(s) + 1;
305421b0201SPoul-Henning Kamp 		} else {
306ab03e6d5SXin LI 			iov[3].iov_base = _path_dev;
307ab03e6d5SXin LI 			iov[3].iov_len = sizeof(_path_dev);
30857622f22SPoul-Henning Kamp 		}
3091f083b1eSMaxime Henrion 		nmount(iov, 4, 0);
3101f083b1eSMaxime Henrion 		if (s != NULL)
3111f083b1eSMaxime Henrion 			free(s);
312421b0201SPoul-Henning Kamp 	}
31357622f22SPoul-Henning Kamp 
3148fae3551SRodney W. Grimes 	/*
3158fae3551SRodney W. Grimes 	 * We catch or block signals rather than ignore them,
3168fae3551SRodney W. Grimes 	 * so that they get reset on exec.
3178fae3551SRodney W. Grimes 	 */
3188fae3551SRodney W. Grimes 	handle(badsys, SIGSYS, 0);
3198fae3551SRodney W. Grimes 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
3208fae3551SRodney W. Grimes 	       SIGBUS, SIGXCPU, SIGXFSZ, 0);
321a0a549c7SRuslan Ermilov 	handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP,
322a0a549c7SRuslan Ermilov 		SIGUSR1, SIGUSR2, 0);
3238fae3551SRodney W. Grimes 	handle(alrm_handler, SIGALRM, 0);
3248fae3551SRodney W. Grimes 	sigfillset(&mask);
3258fae3551SRodney W. Grimes 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
326a0a549c7SRuslan Ermilov 		SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM,
327a0a549c7SRuslan Ermilov 		SIGUSR1, SIGUSR2, 0);
3288fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
3298fae3551SRodney W. Grimes 	sigemptyset(&sa.sa_mask);
3308fae3551SRodney W. Grimes 	sa.sa_flags = 0;
3318fae3551SRodney W. Grimes 	sa.sa_handler = SIG_IGN;
3328fae3551SRodney W. Grimes 	(void) sigaction(SIGTTIN, &sa, (struct sigaction *)0);
3338fae3551SRodney W. Grimes 	(void) sigaction(SIGTTOU, &sa, (struct sigaction *)0);
3348fae3551SRodney W. Grimes 
3358fae3551SRodney W. Grimes 	/*
3368fae3551SRodney W. Grimes 	 * Paranoia.
3378fae3551SRodney W. Grimes 	 */
3388fae3551SRodney W. Grimes 	close(0);
3398fae3551SRodney W. Grimes 	close(1);
3408fae3551SRodney W. Grimes 	close(2);
3418fae3551SRodney W. Grimes 
3428fae3551SRodney W. Grimes 	/*
3438fae3551SRodney W. Grimes 	 * Start the state machine.
3448fae3551SRodney W. Grimes 	 */
3458fae3551SRodney W. Grimes 	transition(requested_transition);
3468fae3551SRodney W. Grimes 
3478fae3551SRodney W. Grimes 	/*
3488fae3551SRodney W. Grimes 	 * Should never reach here.
3498fae3551SRodney W. Grimes 	 */
3508fae3551SRodney W. Grimes 	return 1;
3518fae3551SRodney W. Grimes }
3528fae3551SRodney W. Grimes 
3538fae3551SRodney W. Grimes /*
3548fae3551SRodney W. Grimes  * Associate a function with a signal handler.
3558fae3551SRodney W. Grimes  */
3568fae3551SRodney W. Grimes void
3578fae3551SRodney W. Grimes handle(sig_t handler, ...)
3588fae3551SRodney W. Grimes {
3598fae3551SRodney W. Grimes 	int sig;
3608fae3551SRodney W. Grimes 	struct sigaction sa;
36139034633SJames Raynard 	sigset_t mask_everything;
3628fae3551SRodney W. Grimes 	va_list ap;
3638fae3551SRodney W. Grimes 	va_start(ap, handler);
3648fae3551SRodney W. Grimes 
3658fae3551SRodney W. Grimes 	sa.sa_handler = handler;
3668fae3551SRodney W. Grimes 	sigfillset(&mask_everything);
3678fae3551SRodney W. Grimes 
36830e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0) {
3698fae3551SRodney W. Grimes 		sa.sa_mask = mask_everything;
3708fae3551SRodney W. Grimes 		/* XXX SA_RESTART? */
3718fae3551SRodney W. Grimes 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
3728fae3551SRodney W. Grimes 		sigaction(sig, &sa, (struct sigaction *) 0);
3738fae3551SRodney W. Grimes 	}
3748fae3551SRodney W. Grimes 	va_end(ap);
3758fae3551SRodney W. Grimes }
3768fae3551SRodney W. Grimes 
3778fae3551SRodney W. Grimes /*
3788fae3551SRodney W. Grimes  * Delete a set of signals from a mask.
3798fae3551SRodney W. Grimes  */
3808fae3551SRodney W. Grimes void
3818fae3551SRodney W. Grimes delset(sigset_t *maskp, ...)
3828fae3551SRodney W. Grimes {
3838fae3551SRodney W. Grimes 	int sig;
3848fae3551SRodney W. Grimes 	va_list ap;
3858fae3551SRodney W. Grimes 	va_start(ap, maskp);
3868fae3551SRodney W. Grimes 
38730e8350cSBruce Evans 	while ((sig = va_arg(ap, int)) != 0)
3888fae3551SRodney W. Grimes 		sigdelset(maskp, sig);
3898fae3551SRodney W. Grimes 	va_end(ap);
3908fae3551SRodney W. Grimes }
3918fae3551SRodney W. Grimes 
3928fae3551SRodney W. Grimes /*
3938fae3551SRodney W. Grimes  * Log a message and sleep for a while (to give someone an opportunity
3948fae3551SRodney W. Grimes  * to read it and to save log or hardcopy output if the problem is chronic).
3958fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
3968fae3551SRodney W. Grimes  */
3978fae3551SRodney W. Grimes void
3985979df34SKris Kennaway stall(const char *message, ...)
3998fae3551SRodney W. Grimes {
4008fae3551SRodney W. Grimes 	va_list ap;
4018fae3551SRodney W. Grimes 	va_start(ap, message);
4028fae3551SRodney W. Grimes 
4038fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4048fae3551SRodney W. Grimes 	va_end(ap);
4058fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
4068fae3551SRodney W. Grimes }
4078fae3551SRodney W. Grimes 
4088fae3551SRodney W. Grimes /*
4098fae3551SRodney W. Grimes  * Like stall(), but doesn't sleep.
4108fae3551SRodney W. Grimes  * If cpp had variadic macros, the two functions could be #defines for another.
4118fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4128fae3551SRodney W. Grimes  */
4138fae3551SRodney W. Grimes void
4145979df34SKris Kennaway warning(const char *message, ...)
4158fae3551SRodney W. Grimes {
4168fae3551SRodney W. Grimes 	va_list ap;
4178fae3551SRodney W. Grimes 	va_start(ap, message);
4188fae3551SRodney W. Grimes 
4198fae3551SRodney W. Grimes 	vsyslog(LOG_ALERT, message, ap);
4208fae3551SRodney W. Grimes 	va_end(ap);
4218fae3551SRodney W. Grimes }
4228fae3551SRodney W. Grimes 
4238fae3551SRodney W. Grimes /*
4248fae3551SRodney W. Grimes  * Log an emergency message.
4258fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
4268fae3551SRodney W. Grimes  */
4278fae3551SRodney W. Grimes void
4285979df34SKris Kennaway emergency(const char *message, ...)
4298fae3551SRodney W. Grimes {
4308fae3551SRodney W. Grimes 	va_list ap;
4318fae3551SRodney W. Grimes 	va_start(ap, message);
4328fae3551SRodney W. Grimes 
4338fae3551SRodney W. Grimes 	vsyslog(LOG_EMERG, message, ap);
4348fae3551SRodney W. Grimes 	va_end(ap);
4358fae3551SRodney W. Grimes }
4368fae3551SRodney W. Grimes 
4378fae3551SRodney W. Grimes /*
4388fae3551SRodney W. Grimes  * Catch a SIGSYS signal.
4398fae3551SRodney W. Grimes  *
4408fae3551SRodney W. Grimes  * These may arise if a system does not support sysctl.
4418fae3551SRodney W. Grimes  * We tolerate up to 25 of these, then throw in the towel.
4428fae3551SRodney W. Grimes  */
4438fae3551SRodney W. Grimes void
44473bf18edSWarner Losh badsys(int sig)
4458fae3551SRodney W. Grimes {
4468fae3551SRodney W. Grimes 	static int badcount = 0;
4478fae3551SRodney W. Grimes 
4488fae3551SRodney W. Grimes 	if (badcount++ < 25)
4498fae3551SRodney W. Grimes 		return;
4508fae3551SRodney W. Grimes 	disaster(sig);
4518fae3551SRodney W. Grimes }
4528fae3551SRodney W. Grimes 
4538fae3551SRodney W. Grimes /*
4548fae3551SRodney W. Grimes  * Catch an unexpected signal.
4558fae3551SRodney W. Grimes  */
4568fae3551SRodney W. Grimes void
45773bf18edSWarner Losh disaster(int sig)
4588fae3551SRodney W. Grimes {
4598fae3551SRodney W. Grimes 	emergency("fatal signal: %s",
4608889c700SDavid Nugent 		(unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
4618fae3551SRodney W. Grimes 
4628fae3551SRodney W. Grimes 	sleep(STALL_TIMEOUT);
4638fae3551SRodney W. Grimes 	_exit(sig);		/* reboot */
4648fae3551SRodney W. Grimes }
4658fae3551SRodney W. Grimes 
4668fae3551SRodney W. Grimes /*
4678fae3551SRodney W. Grimes  * Get the security level of the kernel.
4688fae3551SRodney W. Grimes  */
4698fae3551SRodney W. Grimes int
47073bf18edSWarner Losh getsecuritylevel(void)
4718fae3551SRodney W. Grimes {
4728fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
4738fae3551SRodney W. Grimes 	int name[2], curlevel;
4748fae3551SRodney W. Grimes 	size_t len;
4758fae3551SRodney W. Grimes 
4768fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
4778fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
4788fae3551SRodney W. Grimes 	len = sizeof curlevel;
4798fae3551SRodney W. Grimes 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
4808fae3551SRodney W. Grimes 		emergency("cannot get kernel security level: %s",
4818fae3551SRodney W. Grimes 		    strerror(errno));
4828fae3551SRodney W. Grimes 		return (-1);
4838fae3551SRodney W. Grimes 	}
4848fae3551SRodney W. Grimes 	return (curlevel);
4858fae3551SRodney W. Grimes #else
4868fae3551SRodney W. Grimes 	return (-1);
4878fae3551SRodney W. Grimes #endif
4888fae3551SRodney W. Grimes }
4898fae3551SRodney W. Grimes 
4908fae3551SRodney W. Grimes /*
4918fae3551SRodney W. Grimes  * Set the security level of the kernel.
4928fae3551SRodney W. Grimes  */
4938fae3551SRodney W. Grimes void
49473bf18edSWarner Losh setsecuritylevel(int newlevel)
4958fae3551SRodney W. Grimes {
4968fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
4978fae3551SRodney W. Grimes 	int name[2], curlevel;
4988fae3551SRodney W. Grimes 
4998fae3551SRodney W. Grimes 	curlevel = getsecuritylevel();
5008fae3551SRodney W. Grimes 	if (newlevel == curlevel)
5018fae3551SRodney W. Grimes 		return;
5028fae3551SRodney W. Grimes 	name[0] = CTL_KERN;
5038fae3551SRodney W. Grimes 	name[1] = KERN_SECURELVL;
5048fae3551SRodney W. Grimes 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
5058fae3551SRodney W. Grimes 		emergency(
5068fae3551SRodney W. Grimes 		    "cannot change kernel security level from %d to %d: %s",
5078fae3551SRodney W. Grimes 		    curlevel, newlevel, strerror(errno));
5088fae3551SRodney W. Grimes 		return;
5098fae3551SRodney W. Grimes 	}
5108fae3551SRodney W. Grimes #ifdef SECURE
5118fae3551SRodney W. Grimes 	warning("kernel security level changed from %d to %d",
5128fae3551SRodney W. Grimes 	    curlevel, newlevel);
5138fae3551SRodney W. Grimes #endif
5148fae3551SRodney W. Grimes #endif
5158fae3551SRodney W. Grimes }
5168fae3551SRodney W. Grimes 
5178fae3551SRodney W. Grimes /*
5188fae3551SRodney W. Grimes  * Change states in the finite state machine.
5198fae3551SRodney W. Grimes  * The initial state is passed as an argument.
5208fae3551SRodney W. Grimes  */
5218fae3551SRodney W. Grimes void
52273bf18edSWarner Losh transition(state_t s)
5238fae3551SRodney W. Grimes {
5248fae3551SRodney W. Grimes 	for (;;)
5258fae3551SRodney W. Grimes 		s = (state_t) (*s)();
5268fae3551SRodney W. Grimes }
5278fae3551SRodney W. Grimes 
5288fae3551SRodney W. Grimes /*
5298fae3551SRodney W. Grimes  * Close out the accounting files for a login session.
5308fae3551SRodney W. Grimes  * NB: should send a message to the session logger to avoid blocking.
5318fae3551SRodney W. Grimes  */
5328fae3551SRodney W. Grimes void
53373bf18edSWarner Losh clear_session_logs(session_t *sp)
5348fae3551SRodney W. Grimes {
5358fae3551SRodney W. Grimes 	char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
5368fae3551SRodney W. Grimes 
5378fae3551SRodney W. Grimes 	if (logout(line))
5388fae3551SRodney W. Grimes 		logwtmp(line, "", "");
5398fae3551SRodney W. Grimes }
5408fae3551SRodney W. Grimes 
5418fae3551SRodney W. Grimes /*
5428fae3551SRodney W. Grimes  * Start a session and allocate a controlling terminal.
5438fae3551SRodney W. Grimes  * Only called by children of init after forking.
5448fae3551SRodney W. Grimes  */
5458fae3551SRodney W. Grimes void
546ab03e6d5SXin LI setctty(const char *name)
5478fae3551SRodney W. Grimes {
5488fae3551SRodney W. Grimes 	int fd;
5498fae3551SRodney W. Grimes 
5508fae3551SRodney W. Grimes 	(void) revoke(name);
5518fae3551SRodney W. Grimes 	if ((fd = open(name, O_RDWR)) == -1) {
5528fae3551SRodney W. Grimes 		stall("can't open %s: %m", name);
5538fae3551SRodney W. Grimes 		_exit(1);
5548fae3551SRodney W. Grimes 	}
5558fae3551SRodney W. Grimes 	if (login_tty(fd) == -1) {
5568fae3551SRodney W. Grimes 		stall("can't get %s for controlling terminal: %m", name);
5578fae3551SRodney W. Grimes 		_exit(1);
5588fae3551SRodney W. Grimes 	}
5598fae3551SRodney W. Grimes }
5608fae3551SRodney W. Grimes 
5618fae3551SRodney W. Grimes /*
5628fae3551SRodney W. Grimes  * Bring the system up single user.
5638fae3551SRodney W. Grimes  */
5648fae3551SRodney W. Grimes state_func_t
56573bf18edSWarner Losh single_user(void)
5668fae3551SRodney W. Grimes {
5678fae3551SRodney W. Grimes 	pid_t pid, wpid;
5688fae3551SRodney W. Grimes 	int status;
5698fae3551SRodney W. Grimes 	sigset_t mask;
570ab03e6d5SXin LI 	const char *shell = _PATH_BSHELL;
5718fae3551SRodney W. Grimes 	char *argv[2];
5728fae3551SRodney W. Grimes #ifdef SECURE
5738fae3551SRodney W. Grimes 	struct ttyent *typ;
5748fae3551SRodney W. Grimes 	struct passwd *pp;
5758fae3551SRodney W. Grimes 	static const char banner[] =
5768fae3551SRodney W. Grimes 		"Enter root password, or ^D to go multi-user\n";
5778fae3551SRodney W. Grimes 	char *clear, *password;
5788fae3551SRodney W. Grimes #endif
57963322c28SPoul-Henning Kamp #ifdef DEBUGSHELL
58063322c28SPoul-Henning Kamp 	char altshell[128];
58163322c28SPoul-Henning Kamp #endif
5828fae3551SRodney W. Grimes 
583db8ad19dSJordan K. Hubbard 	if (Reboot) {
584a0a549c7SRuslan Ermilov 		/* Instead of going single user, let's reboot the machine */
585e460cfd3SNate Williams 		sync();
586e460cfd3SNate Williams 		alarm(2);
587e460cfd3SNate Williams 		pause();
588a0a549c7SRuslan Ermilov 		reboot(howto);
589e460cfd3SNate Williams 		_exit(0);
590e460cfd3SNate Williams 	}
591e460cfd3SNate Williams 
5928fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
5938fae3551SRodney W. Grimes 		/*
5948fae3551SRodney W. Grimes 		 * Start the single user session.
5958fae3551SRodney W. Grimes 		 */
5968fae3551SRodney W. Grimes 		setctty(_PATH_CONSOLE);
5978fae3551SRodney W. Grimes 
5988fae3551SRodney W. Grimes #ifdef SECURE
5998fae3551SRodney W. Grimes 		/*
6008fae3551SRodney W. Grimes 		 * Check the root password.
6018fae3551SRodney W. Grimes 		 * We don't care if the console is 'on' by default;
6028fae3551SRodney W. Grimes 		 * it's the only tty that can be 'off' and 'secure'.
6038fae3551SRodney W. Grimes 		 */
6048fae3551SRodney W. Grimes 		typ = getttynam("console");
6058fae3551SRodney W. Grimes 		pp = getpwnam("root");
606a69497d7SMatthew Dillon 		if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
607a69497d7SMatthew Dillon 		    pp && *pp->pw_passwd) {
608e1b4d8d0SSheldon Hearn 			write(STDERR_FILENO, banner, sizeof banner - 1);
6098fae3551SRodney W. Grimes 			for (;;) {
6108fae3551SRodney W. Grimes 				clear = getpass("Password:");
6118fae3551SRodney W. Grimes 				if (clear == 0 || *clear == '\0')
6128fae3551SRodney W. Grimes 					_exit(0);
6138fae3551SRodney W. Grimes 				password = crypt(clear, pp->pw_passwd);
6148fae3551SRodney W. Grimes 				bzero(clear, _PASSWORD_LEN);
6158fae3551SRodney W. Grimes 				if (strcmp(password, pp->pw_passwd) == 0)
6168fae3551SRodney W. Grimes 					break;
6178fae3551SRodney W. Grimes 				warning("single-user login failed\n");
6188fae3551SRodney W. Grimes 			}
6198fae3551SRodney W. Grimes 		}
6208fae3551SRodney W. Grimes 		endttyent();
6218fae3551SRodney W. Grimes 		endpwent();
6228fae3551SRodney W. Grimes #endif /* SECURE */
6238fae3551SRodney W. Grimes 
6248fae3551SRodney W. Grimes #ifdef DEBUGSHELL
6258fae3551SRodney W. Grimes 		{
62663322c28SPoul-Henning Kamp 			char *cp = altshell;
6278fae3551SRodney W. Grimes 			int num;
6288fae3551SRodney W. Grimes 
6298fae3551SRodney W. Grimes #define	SHREQUEST \
6301bc5fcd0SPeter Wemm 	"Enter full pathname of shell or RETURN for " _PATH_BSHELL ": "
6318fae3551SRodney W. Grimes 			(void)write(STDERR_FILENO,
6328fae3551SRodney W. Grimes 			    SHREQUEST, sizeof(SHREQUEST) - 1);
6338fae3551SRodney W. Grimes 			while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
6348fae3551SRodney W. Grimes 			    num != 0 && *cp != '\n' && cp < &altshell[127])
6358fae3551SRodney W. Grimes 					cp++;
6368fae3551SRodney W. Grimes 			*cp = '\0';
6378fae3551SRodney W. Grimes 			if (altshell[0] != '\0')
6388fae3551SRodney W. Grimes 				shell = altshell;
6398fae3551SRodney W. Grimes 		}
6408fae3551SRodney W. Grimes #endif /* DEBUGSHELL */
6418fae3551SRodney W. Grimes 
6428fae3551SRodney W. Grimes 		/*
6438fae3551SRodney W. Grimes 		 * Unblock signals.
6448fae3551SRodney W. Grimes 		 * We catch all the interesting ones,
6458fae3551SRodney W. Grimes 		 * and those are reset to SIG_DFL on exec.
6468fae3551SRodney W. Grimes 		 */
6478fae3551SRodney W. Grimes 		sigemptyset(&mask);
6488fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
6498fae3551SRodney W. Grimes 
6508fae3551SRodney W. Grimes 		/*
6518fae3551SRodney W. Grimes 		 * Fire off a shell.
6528fae3551SRodney W. Grimes 		 * If the default one doesn't work, try the Bourne shell.
6538fae3551SRodney W. Grimes 		 */
654ab03e6d5SXin LI 
655ab03e6d5SXin LI 		char name[] = "-sh";
656ab03e6d5SXin LI 
657ab03e6d5SXin LI 		argv[0] = name;
6588fae3551SRodney W. Grimes 		argv[1] = 0;
6598fae3551SRodney W. Grimes 		execv(shell, argv);
6608fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", shell);
6618fae3551SRodney W. Grimes 		execv(_PATH_BSHELL, argv);
6628fae3551SRodney W. Grimes 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
6638fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
6648fae3551SRodney W. Grimes 		_exit(1);
6658fae3551SRodney W. Grimes 	}
6668fae3551SRodney W. Grimes 
6678fae3551SRodney W. Grimes 	if (pid == -1) {
6688fae3551SRodney W. Grimes 		/*
6698fae3551SRodney W. Grimes 		 * We are seriously hosed.  Do our best.
6708fae3551SRodney W. Grimes 		 */
6718fae3551SRodney W. Grimes 		emergency("can't fork single-user shell, trying again");
6728fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
6738fae3551SRodney W. Grimes 			continue;
6748fae3551SRodney W. Grimes 		return (state_func_t) single_user;
6758fae3551SRodney W. Grimes 	}
6768fae3551SRodney W. Grimes 
6778fae3551SRodney W. Grimes 	requested_transition = 0;
6788fae3551SRodney W. Grimes 	do {
6798fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
6808fae3551SRodney W. Grimes 			collect_child(wpid);
6818fae3551SRodney W. Grimes 		if (wpid == -1) {
6828fae3551SRodney W. Grimes 			if (errno == EINTR)
6838fae3551SRodney W. Grimes 				continue;
6848fae3551SRodney W. Grimes 			warning("wait for single-user shell failed: %m; restarting");
6858fae3551SRodney W. Grimes 			return (state_func_t) single_user;
6868fae3551SRodney W. Grimes 		}
6878fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
6888fae3551SRodney W. Grimes 			warning("init: shell stopped, restarting\n");
6898fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
6908fae3551SRodney W. Grimes 			wpid = -1;
6918fae3551SRodney W. Grimes 		}
6928fae3551SRodney W. Grimes 	} while (wpid != pid && !requested_transition);
6938fae3551SRodney W. Grimes 
6948fae3551SRodney W. Grimes 	if (requested_transition)
6958fae3551SRodney W. Grimes 		return (state_func_t) requested_transition;
6968fae3551SRodney W. Grimes 
6978fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
6988fae3551SRodney W. Grimes 		if (WTERMSIG(status) == SIGKILL) {
6998fae3551SRodney W. Grimes 			/*
7008fae3551SRodney W. Grimes 			 *  reboot(8) killed shell?
7018fae3551SRodney W. Grimes 			 */
7028fae3551SRodney W. Grimes 			warning("single user shell terminated.");
7038fae3551SRodney W. Grimes 			sleep(STALL_TIMEOUT);
7048fae3551SRodney W. Grimes 			_exit(0);
7058fae3551SRodney W. Grimes 		} else {
7068fae3551SRodney W. Grimes 			warning("single user shell terminated, restarting");
7078fae3551SRodney W. Grimes 			return (state_func_t) single_user;
7088fae3551SRodney W. Grimes 		}
7098fae3551SRodney W. Grimes 	}
7108fae3551SRodney W. Grimes 
7118fae3551SRodney W. Grimes 	runcom_mode = FASTBOOT;
7128fae3551SRodney W. Grimes 	return (state_func_t) runcom;
7138fae3551SRodney W. Grimes }
7148fae3551SRodney W. Grimes 
7158fae3551SRodney W. Grimes /*
7168fae3551SRodney W. Grimes  * Run the system startup script.
7178fae3551SRodney W. Grimes  */
7188fae3551SRodney W. Grimes state_func_t
71973bf18edSWarner Losh runcom(void)
7208fae3551SRodney W. Grimes {
7218fae3551SRodney W. Grimes 	pid_t pid, wpid;
7228fae3551SRodney W. Grimes 	int status;
7238fae3551SRodney W. Grimes 	char *argv[4];
7248fae3551SRodney W. Grimes 	struct sigaction sa;
7258fae3551SRodney W. Grimes 
7268fae3551SRodney W. Grimes 	if ((pid = fork()) == 0) {
7278fae3551SRodney W. Grimes 		sigemptyset(&sa.sa_mask);
7288fae3551SRodney W. Grimes 		sa.sa_flags = 0;
7298fae3551SRodney W. Grimes 		sa.sa_handler = SIG_IGN;
7308fae3551SRodney W. Grimes 		(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
7318fae3551SRodney W. Grimes 		(void) sigaction(SIGHUP, &sa, (struct sigaction *)0);
7328fae3551SRodney W. Grimes 
7338fae3551SRodney W. Grimes 		setctty(_PATH_CONSOLE);
7348fae3551SRodney W. Grimes 
735ab03e6d5SXin LI 		char _sh[]	 	= "sh";
736ab03e6d5SXin LI 		char _path_runcom[]	= _PATH_RUNCOM;
737ab03e6d5SXin LI 		char _autoboot[]	= "autoboot";
738ab03e6d5SXin LI 
739ab03e6d5SXin LI 		argv[0] = _sh;
740ab03e6d5SXin LI 		argv[1] = _path_runcom;
741ab03e6d5SXin LI 		argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0;
7428fae3551SRodney W. Grimes 		argv[3] = 0;
7438fae3551SRodney W. Grimes 
7448fae3551SRodney W. Grimes 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
7458fae3551SRodney W. Grimes 
7461ef60eb1SDavid Nugent #ifdef LOGIN_CAP
7471ef60eb1SDavid Nugent 		setprocresources(RESOURCE_RC);
7481ef60eb1SDavid Nugent #endif
7498fae3551SRodney W. Grimes 		execv(_PATH_BSHELL, argv);
7508fae3551SRodney W. Grimes 		stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
7518fae3551SRodney W. Grimes 		_exit(1);	/* force single user mode */
7528fae3551SRodney W. Grimes 	}
7538fae3551SRodney W. Grimes 
7548fae3551SRodney W. Grimes 	if (pid == -1) {
7558fae3551SRodney W. Grimes 		emergency("can't fork for %s on %s: %m",
7568fae3551SRodney W. Grimes 			_PATH_BSHELL, _PATH_RUNCOM);
7578fae3551SRodney W. Grimes 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
7588fae3551SRodney W. Grimes 			continue;
7598fae3551SRodney W. Grimes 		sleep(STALL_TIMEOUT);
7608fae3551SRodney W. Grimes 		return (state_func_t) single_user;
7618fae3551SRodney W. Grimes 	}
7628fae3551SRodney W. Grimes 
7638fae3551SRodney W. Grimes 	/*
7648fae3551SRodney W. Grimes 	 * Copied from single_user().  This is a bit paranoid.
7658fae3551SRodney W. Grimes 	 */
7666e8ff8b7SDag-Erling Smørgrav 	requested_transition = 0;
7678fae3551SRodney W. Grimes 	do {
7688fae3551SRodney W. Grimes 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
7698fae3551SRodney W. Grimes 			collect_child(wpid);
7708fae3551SRodney W. Grimes 		if (wpid == -1) {
7716e8ff8b7SDag-Erling Smørgrav 			if (requested_transition == death)
7726e8ff8b7SDag-Erling Smørgrav 				return (state_func_t) death;
7738fae3551SRodney W. Grimes 			if (errno == EINTR)
7748fae3551SRodney W. Grimes 				continue;
7758fae3551SRodney W. Grimes 			warning("wait for %s on %s failed: %m; going to single user mode",
7768fae3551SRodney W. Grimes 				_PATH_BSHELL, _PATH_RUNCOM);
7778fae3551SRodney W. Grimes 			return (state_func_t) single_user;
7788fae3551SRodney W. Grimes 		}
7798fae3551SRodney W. Grimes 		if (wpid == pid && WIFSTOPPED(status)) {
7808fae3551SRodney W. Grimes 			warning("init: %s on %s stopped, restarting\n",
7818fae3551SRodney W. Grimes 				_PATH_BSHELL, _PATH_RUNCOM);
7828fae3551SRodney W. Grimes 			kill(pid, SIGCONT);
7838fae3551SRodney W. Grimes 			wpid = -1;
7848fae3551SRodney W. Grimes 		}
7858fae3551SRodney W. Grimes 	} while (wpid != pid);
7868fae3551SRodney W. Grimes 
7878fae3551SRodney W. Grimes 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
7888fae3551SRodney W. Grimes 	    requested_transition == catatonia) {
7898fae3551SRodney W. Grimes 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
7908fae3551SRodney W. Grimes 		sigset_t s;
7918fae3551SRodney W. Grimes 
7928fae3551SRodney W. Grimes 		sigfillset(&s);
7938fae3551SRodney W. Grimes 		for (;;)
7948fae3551SRodney W. Grimes 			sigsuspend(&s);
7958fae3551SRodney W. Grimes 	}
7968fae3551SRodney W. Grimes 
7978fae3551SRodney W. Grimes 	if (!WIFEXITED(status)) {
7988fae3551SRodney W. Grimes 		warning("%s on %s terminated abnormally, going to single user mode",
7998fae3551SRodney W. Grimes 			_PATH_BSHELL, _PATH_RUNCOM);
8008fae3551SRodney W. Grimes 		return (state_func_t) single_user;
8018fae3551SRodney W. Grimes 	}
8028fae3551SRodney W. Grimes 
8038fae3551SRodney W. Grimes 	if (WEXITSTATUS(status))
8048fae3551SRodney W. Grimes 		return (state_func_t) single_user;
8058fae3551SRodney W. Grimes 
8068fae3551SRodney W. Grimes 	runcom_mode = AUTOBOOT;		/* the default */
8078fae3551SRodney W. Grimes 	/* NB: should send a message to the session logger to avoid blocking. */
8088fae3551SRodney W. Grimes 	logwtmp("~", "reboot", "");
8098fae3551SRodney W. Grimes 	return (state_func_t) read_ttys;
8108fae3551SRodney W. Grimes }
8118fae3551SRodney W. Grimes 
8128fae3551SRodney W. Grimes /*
8138fae3551SRodney W. Grimes  * Open the session database.
8148fae3551SRodney W. Grimes  *
8158fae3551SRodney W. Grimes  * NB: We could pass in the size here; is it necessary?
8168fae3551SRodney W. Grimes  */
8178fae3551SRodney W. Grimes int
81873bf18edSWarner Losh start_session_db(void)
8198fae3551SRodney W. Grimes {
8208fae3551SRodney W. Grimes 	if (session_db && (*session_db->close)(session_db))
8218fae3551SRodney W. Grimes 		emergency("session database close: %s", strerror(errno));
8228fae3551SRodney W. Grimes 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
8238fae3551SRodney W. Grimes 		emergency("session database open: %s", strerror(errno));
8248fae3551SRodney W. Grimes 		return (1);
8258fae3551SRodney W. Grimes 	}
8268fae3551SRodney W. Grimes 	return (0);
8278fae3551SRodney W. Grimes 
8288fae3551SRodney W. Grimes }
8298fae3551SRodney W. Grimes 
8308fae3551SRodney W. Grimes /*
8318fae3551SRodney W. Grimes  * Add a new login session.
8328fae3551SRodney W. Grimes  */
8338fae3551SRodney W. Grimes void
83473bf18edSWarner Losh add_session(session_t *sp)
8358fae3551SRodney W. Grimes {
8368fae3551SRodney W. Grimes 	DBT key;
8378fae3551SRodney W. Grimes 	DBT data;
8388fae3551SRodney W. Grimes 
8398fae3551SRodney W. Grimes 	key.data = &sp->se_process;
8408fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
8418fae3551SRodney W. Grimes 	data.data = &sp;
8428fae3551SRodney W. Grimes 	data.size = sizeof sp;
8438fae3551SRodney W. Grimes 
8448fae3551SRodney W. Grimes 	if ((*session_db->put)(session_db, &key, &data, 0))
8458fae3551SRodney W. Grimes 		emergency("insert %d: %s", sp->se_process, strerror(errno));
8468fae3551SRodney W. Grimes }
8478fae3551SRodney W. Grimes 
8488fae3551SRodney W. Grimes /*
8498fae3551SRodney W. Grimes  * Delete an old login session.
8508fae3551SRodney W. Grimes  */
8518fae3551SRodney W. Grimes void
85273bf18edSWarner Losh del_session(session_t *sp)
8538fae3551SRodney W. Grimes {
8548fae3551SRodney W. Grimes 	DBT key;
8558fae3551SRodney W. Grimes 
8568fae3551SRodney W. Grimes 	key.data = &sp->se_process;
8578fae3551SRodney W. Grimes 	key.size = sizeof sp->se_process;
8588fae3551SRodney W. Grimes 
8598fae3551SRodney W. Grimes 	if ((*session_db->del)(session_db, &key, 0))
8608fae3551SRodney W. Grimes 		emergency("delete %d: %s", sp->se_process, strerror(errno));
8618fae3551SRodney W. Grimes }
8628fae3551SRodney W. Grimes 
8638fae3551SRodney W. Grimes /*
8648fae3551SRodney W. Grimes  * Look up a login session by pid.
8658fae3551SRodney W. Grimes  */
8668fae3551SRodney W. Grimes session_t *
8678fae3551SRodney W. Grimes find_session(pid_t pid)
8688fae3551SRodney W. Grimes {
8698fae3551SRodney W. Grimes 	DBT key;
8708fae3551SRodney W. Grimes 	DBT data;
8718fae3551SRodney W. Grimes 	session_t *ret;
8728fae3551SRodney W. Grimes 
8738fae3551SRodney W. Grimes 	key.data = &pid;
8748fae3551SRodney W. Grimes 	key.size = sizeof pid;
8758fae3551SRodney W. Grimes 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
8768fae3551SRodney W. Grimes 		return 0;
8778fae3551SRodney W. Grimes 	bcopy(data.data, (char *)&ret, sizeof(ret));
8788fae3551SRodney W. Grimes 	return ret;
8798fae3551SRodney W. Grimes }
8808fae3551SRodney W. Grimes 
8818fae3551SRodney W. Grimes /*
8828fae3551SRodney W. Grimes  * Construct an argument vector from a command line.
8838fae3551SRodney W. Grimes  */
8848fae3551SRodney W. Grimes char **
88573bf18edSWarner Losh construct_argv(char *command)
8868fae3551SRodney W. Grimes {
8873d438ad6SDavid E. O'Brien 	int argc = 0;
8883d438ad6SDavid E. O'Brien 	char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
8898fae3551SRodney W. Grimes 						* sizeof (char *));
8908fae3551SRodney W. Grimes 
8916be40c95SRuslan Ermilov 	if ((argv[argc++] = strk(command)) == 0) {
8926be40c95SRuslan Ermilov 		free(argv);
8936be40c95SRuslan Ermilov 		return (NULL);
8946be40c95SRuslan Ermilov 	}
8958889c700SDavid Nugent 	while ((argv[argc++] = strk((char *) 0)) != NULL)
8968fae3551SRodney W. Grimes 		continue;
8978fae3551SRodney W. Grimes 	return argv;
8988fae3551SRodney W. Grimes }
8998fae3551SRodney W. Grimes 
9008fae3551SRodney W. Grimes /*
9018fae3551SRodney W. Grimes  * Deallocate a session descriptor.
9028fae3551SRodney W. Grimes  */
9038fae3551SRodney W. Grimes void
90473bf18edSWarner Losh free_session(session_t *sp)
9058fae3551SRodney W. Grimes {
9068fae3551SRodney W. Grimes 	free(sp->se_device);
9078fae3551SRodney W. Grimes 	if (sp->se_getty) {
9088fae3551SRodney W. Grimes 		free(sp->se_getty);
909b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
9108fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
9118fae3551SRodney W. Grimes 	}
9128fae3551SRodney W. Grimes 	if (sp->se_window) {
9138fae3551SRodney W. Grimes 		free(sp->se_window);
914b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
9158fae3551SRodney W. Grimes 		free(sp->se_window_argv);
9168fae3551SRodney W. Grimes 	}
917b5df27e2SAndrey A. Chernov 	if (sp->se_type)
918b5df27e2SAndrey A. Chernov 		free(sp->se_type);
9198fae3551SRodney W. Grimes 	free(sp);
9208fae3551SRodney W. Grimes }
9218fae3551SRodney W. Grimes 
9228fae3551SRodney W. Grimes /*
9238fae3551SRodney W. Grimes  * Allocate a new session descriptor.
924b0b670eeSAlfred Perlstein  * Mark it SE_PRESENT.
9258fae3551SRodney W. Grimes  */
9268fae3551SRodney W. Grimes session_t *
92773bf18edSWarner Losh new_session(session_t *sprev, int session_index, struct ttyent *typ)
9288fae3551SRodney W. Grimes {
9293d438ad6SDavid E. O'Brien 	session_t *sp;
9304cbf8903SPaul Traina 	int fd;
9318fae3551SRodney W. Grimes 
9328fae3551SRodney W. Grimes 	if ((typ->ty_status & TTY_ON) == 0 ||
9338fae3551SRodney W. Grimes 	    typ->ty_name == 0 ||
9348fae3551SRodney W. Grimes 	    typ->ty_getty == 0)
9358fae3551SRodney W. Grimes 		return 0;
9368fae3551SRodney W. Grimes 
9371054bb1eSAndrey A. Chernov 	sp = (session_t *) calloc(1, sizeof (session_t));
9388fae3551SRodney W. Grimes 
9398fae3551SRodney W. Grimes 	sp->se_index = session_index;
940b0b670eeSAlfred Perlstein 	sp->se_flags |= SE_PRESENT;
9418fae3551SRodney W. Grimes 
9428fae3551SRodney W. Grimes 	sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
9438fae3551SRodney W. Grimes 	(void) sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
9448fae3551SRodney W. Grimes 
9454cbf8903SPaul Traina 	/*
9464cbf8903SPaul Traina 	 * Attempt to open the device, if we get "device not configured"
9474cbf8903SPaul Traina 	 * then don't add the device to the session list.
9484cbf8903SPaul Traina 	 */
9494cbf8903SPaul Traina 	if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) {
95089e3b380SWarner Losh 		if (errno == ENXIO) {
9514cbf8903SPaul Traina 			free_session(sp);
9524cbf8903SPaul Traina 			return (0);
9534cbf8903SPaul Traina 		}
9544cbf8903SPaul Traina 	} else
9554cbf8903SPaul Traina 		close(fd);
9564cbf8903SPaul Traina 
9578fae3551SRodney W. Grimes 	if (setupargv(sp, typ) == 0) {
9588fae3551SRodney W. Grimes 		free_session(sp);
9598fae3551SRodney W. Grimes 		return (0);
9608fae3551SRodney W. Grimes 	}
9618fae3551SRodney W. Grimes 
9628fae3551SRodney W. Grimes 	sp->se_next = 0;
9638fae3551SRodney W. Grimes 	if (sprev == 0) {
9648fae3551SRodney W. Grimes 		sessions = sp;
9658fae3551SRodney W. Grimes 		sp->se_prev = 0;
9668fae3551SRodney W. Grimes 	} else {
9678fae3551SRodney W. Grimes 		sprev->se_next = sp;
9688fae3551SRodney W. Grimes 		sp->se_prev = sprev;
9698fae3551SRodney W. Grimes 	}
9708fae3551SRodney W. Grimes 
9718fae3551SRodney W. Grimes 	return sp;
9728fae3551SRodney W. Grimes }
9738fae3551SRodney W. Grimes 
9748fae3551SRodney W. Grimes /*
9758fae3551SRodney W. Grimes  * Calculate getty and if useful window argv vectors.
9768fae3551SRodney W. Grimes  */
9778fae3551SRodney W. Grimes int
97873bf18edSWarner Losh setupargv(session_t *sp, struct ttyent *typ)
9798fae3551SRodney W. Grimes {
9808fae3551SRodney W. Grimes 
9818fae3551SRodney W. Grimes 	if (sp->se_getty) {
9828fae3551SRodney W. Grimes 		free(sp->se_getty);
983b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
9848fae3551SRodney W. Grimes 		free(sp->se_getty_argv);
9858fae3551SRodney W. Grimes 	}
9868fae3551SRodney W. Grimes 	sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
9878fae3551SRodney W. Grimes 	(void) sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
988b5df27e2SAndrey A. Chernov 	sp->se_getty_argv_space = strdup(sp->se_getty);
989b5df27e2SAndrey A. Chernov 	sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
9908fae3551SRodney W. Grimes 	if (sp->se_getty_argv == 0) {
9918fae3551SRodney W. Grimes 		warning("can't parse getty for port %s", sp->se_device);
9928fae3551SRodney W. Grimes 		free(sp->se_getty);
993b5df27e2SAndrey A. Chernov 		free(sp->se_getty_argv_space);
994b5df27e2SAndrey A. Chernov 		sp->se_getty = sp->se_getty_argv_space = 0;
9958fae3551SRodney W. Grimes 		return (0);
9968fae3551SRodney W. Grimes 	}
997b5df27e2SAndrey A. Chernov 	if (sp->se_window) {
9988fae3551SRodney W. Grimes 		free(sp->se_window);
999b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv_space);
1000b5df27e2SAndrey A. Chernov 		free(sp->se_window_argv);
1001b5df27e2SAndrey A. Chernov 	}
1002b5df27e2SAndrey A. Chernov 	sp->se_window = sp->se_window_argv_space = 0;
1003b5df27e2SAndrey A. Chernov 	sp->se_window_argv = 0;
1004b5df27e2SAndrey A. Chernov 	if (typ->ty_window) {
10058fae3551SRodney W. Grimes 		sp->se_window = strdup(typ->ty_window);
1006b5df27e2SAndrey A. Chernov 		sp->se_window_argv_space = strdup(sp->se_window);
1007b5df27e2SAndrey A. Chernov 		sp->se_window_argv = construct_argv(sp->se_window_argv_space);
10088fae3551SRodney W. Grimes 		if (sp->se_window_argv == 0) {
10098fae3551SRodney W. Grimes 			warning("can't parse window for port %s",
10108fae3551SRodney W. Grimes 				sp->se_device);
1011b5df27e2SAndrey A. Chernov 			free(sp->se_window_argv_space);
10128fae3551SRodney W. Grimes 			free(sp->se_window);
1013b5df27e2SAndrey A. Chernov 			sp->se_window = sp->se_window_argv_space = 0;
10148fae3551SRodney W. Grimes 			return (0);
10158fae3551SRodney W. Grimes 		}
10168fae3551SRodney W. Grimes 	}
1017b5df27e2SAndrey A. Chernov 	if (sp->se_type)
1018b5df27e2SAndrey A. Chernov 		free(sp->se_type);
1019b5df27e2SAndrey A. Chernov 	sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
10208fae3551SRodney W. Grimes 	return (1);
10218fae3551SRodney W. Grimes }
10228fae3551SRodney W. Grimes 
10238fae3551SRodney W. Grimes /*
10248fae3551SRodney W. Grimes  * Walk the list of ttys and create sessions for each active line.
10258fae3551SRodney W. Grimes  */
10268fae3551SRodney W. Grimes state_func_t
102773bf18edSWarner Losh read_ttys(void)
10288fae3551SRodney W. Grimes {
10298fae3551SRodney W. Grimes 	int session_index = 0;
10303d438ad6SDavid E. O'Brien 	session_t *sp, *snext;
10313d438ad6SDavid E. O'Brien 	struct ttyent *typ;
10328fae3551SRodney W. Grimes 
10338fae3551SRodney W. Grimes 	/*
10348fae3551SRodney W. Grimes 	 * Destroy any previous session state.
10358fae3551SRodney W. Grimes 	 * There shouldn't be any, but just in case...
10368fae3551SRodney W. Grimes 	 */
10378fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = snext) {
10388fae3551SRodney W. Grimes 		if (sp->se_process)
10398fae3551SRodney W. Grimes 			clear_session_logs(sp);
10408fae3551SRodney W. Grimes 		snext = sp->se_next;
10418fae3551SRodney W. Grimes 		free_session(sp);
10428fae3551SRodney W. Grimes 	}
10438fae3551SRodney W. Grimes 	sessions = 0;
10448fae3551SRodney W. Grimes 	if (start_session_db())
10458fae3551SRodney W. Grimes 		return (state_func_t) single_user;
10468fae3551SRodney W. Grimes 
10478fae3551SRodney W. Grimes 	/*
10488fae3551SRodney W. Grimes 	 * Allocate a session entry for each active port.
10498fae3551SRodney W. Grimes 	 * Note that sp starts at 0.
10508fae3551SRodney W. Grimes 	 */
10518889c700SDavid Nugent 	while ((typ = getttyent()) != NULL)
10528889c700SDavid Nugent 		if ((snext = new_session(sp, ++session_index, typ)) != NULL)
10538fae3551SRodney W. Grimes 			sp = snext;
10548fae3551SRodney W. Grimes 
10558fae3551SRodney W. Grimes 	endttyent();
10568fae3551SRodney W. Grimes 
10578fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
10588fae3551SRodney W. Grimes }
10598fae3551SRodney W. Grimes 
10608fae3551SRodney W. Grimes /*
10618fae3551SRodney W. Grimes  * Start a window system running.
10628fae3551SRodney W. Grimes  */
10638fae3551SRodney W. Grimes void
106473bf18edSWarner Losh start_window_system(session_t *sp)
10658fae3551SRodney W. Grimes {
10668fae3551SRodney W. Grimes 	pid_t pid;
10678fae3551SRodney W. Grimes 	sigset_t mask;
1068b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
10695010c3b6SKonstantin Belousov 	int status;
10708fae3551SRodney W. Grimes 
10718fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
10728fae3551SRodney W. Grimes 		emergency("can't fork for window system on port %s: %m",
10738fae3551SRodney W. Grimes 			sp->se_device);
10748fae3551SRodney W. Grimes 		/* hope that getty fails and we can try again */
10758fae3551SRodney W. Grimes 		return;
10768fae3551SRodney W. Grimes 	}
10778fae3551SRodney W. Grimes 	if (pid)
10785010c3b6SKonstantin Belousov 	{
10795010c3b6SKonstantin Belousov 		waitpid(-1, &status, 0);
10808fae3551SRodney W. Grimes 		return;
10815010c3b6SKonstantin Belousov 	}
10825010c3b6SKonstantin Belousov 
10835010c3b6SKonstantin Belousov 	/* reparent window process to the init to not make a zombie on exit */
10845010c3b6SKonstantin Belousov 	if ((pid = fork()) == -1) {
10855010c3b6SKonstantin Belousov 		emergency("can't fork for window system on port %s: %m",
10865010c3b6SKonstantin Belousov 			sp->se_device);
10875010c3b6SKonstantin Belousov 		_exit(1);
10885010c3b6SKonstantin Belousov 	}
10895010c3b6SKonstantin Belousov 	if (pid)
10905010c3b6SKonstantin Belousov 		_exit(0);
10918fae3551SRodney W. Grimes 
10928fae3551SRodney W. Grimes 	sigemptyset(&mask);
10938fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
10948fae3551SRodney W. Grimes 
10958fae3551SRodney W. Grimes 	if (setsid() < 0)
10968fae3551SRodney W. Grimes 		emergency("setsid failed (window) %m");
10978fae3551SRodney W. Grimes 
10981ef60eb1SDavid Nugent #ifdef LOGIN_CAP
10991ef60eb1SDavid Nugent 	setprocresources(RESOURCE_WINDOW);
11001ef60eb1SDavid Nugent #endif
1101b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1102b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1103b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
110433a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1105b5df27e2SAndrey A. Chernov 		env[0] = term;
1106b5df27e2SAndrey A. Chernov 		env[1] = 0;
1107b5df27e2SAndrey A. Chernov 	}
1108b5df27e2SAndrey A. Chernov 	else
1109b5df27e2SAndrey A. Chernov 		env[0] = 0;
1110b5df27e2SAndrey A. Chernov 	execve(sp->se_window_argv[0], sp->se_window_argv, env);
11118fae3551SRodney W. Grimes 	stall("can't exec window system '%s' for port %s: %m",
11128fae3551SRodney W. Grimes 		sp->se_window_argv[0], sp->se_device);
11138fae3551SRodney W. Grimes 	_exit(1);
11148fae3551SRodney W. Grimes }
11158fae3551SRodney W. Grimes 
11168fae3551SRodney W. Grimes /*
11178fae3551SRodney W. Grimes  * Start a login session running.
11188fae3551SRodney W. Grimes  */
11198fae3551SRodney W. Grimes pid_t
112073bf18edSWarner Losh start_getty(session_t *sp)
11218fae3551SRodney W. Grimes {
11228fae3551SRodney W. Grimes 	pid_t pid;
11238fae3551SRodney W. Grimes 	sigset_t mask;
11248fae3551SRodney W. Grimes 	time_t current_time = time((time_t *) 0);
1125228d7ef2SAndrey A. Chernov 	int too_quick = 0;
1126b5df27e2SAndrey A. Chernov 	char term[64], *env[2];
11278fae3551SRodney W. Grimes 
1128bb2e87c4SMike Pritchard 	if (current_time >= sp->se_started &&
1129228d7ef2SAndrey A. Chernov 	    current_time - sp->se_started < GETTY_SPACING) {
1130228d7ef2SAndrey A. Chernov 		if (++sp->se_nspace > GETTY_NSPACE) {
1131228d7ef2SAndrey A. Chernov 			sp->se_nspace = 0;
1132228d7ef2SAndrey A. Chernov 			too_quick = 1;
1133228d7ef2SAndrey A. Chernov 		}
1134228d7ef2SAndrey A. Chernov 	} else
1135228d7ef2SAndrey A. Chernov 		sp->se_nspace = 0;
1136228d7ef2SAndrey A. Chernov 
11378fae3551SRodney W. Grimes 	/*
11388fae3551SRodney W. Grimes 	 * fork(), not vfork() -- we can't afford to block.
11398fae3551SRodney W. Grimes 	 */
11408fae3551SRodney W. Grimes 	if ((pid = fork()) == -1) {
11418fae3551SRodney W. Grimes 		emergency("can't fork for getty on port %s: %m", sp->se_device);
11428fae3551SRodney W. Grimes 		return -1;
11438fae3551SRodney W. Grimes 	}
11448fae3551SRodney W. Grimes 
11458fae3551SRodney W. Grimes 	if (pid)
11468fae3551SRodney W. Grimes 		return pid;
11478fae3551SRodney W. Grimes 
1148228d7ef2SAndrey A. Chernov 	if (too_quick) {
1149b5df27e2SAndrey A. Chernov 		warning("getty repeating too quickly on port %s, sleeping %d secs",
1150b5df27e2SAndrey A. Chernov 			sp->se_device, GETTY_SLEEP);
11518fae3551SRodney W. Grimes 		sleep((unsigned) GETTY_SLEEP);
11528fae3551SRodney W. Grimes 	}
11538fae3551SRodney W. Grimes 
11548fae3551SRodney W. Grimes 	if (sp->se_window) {
11558fae3551SRodney W. Grimes 		start_window_system(sp);
11568fae3551SRodney W. Grimes 		sleep(WINDOW_WAIT);
11578fae3551SRodney W. Grimes 	}
11588fae3551SRodney W. Grimes 
11598fae3551SRodney W. Grimes 	sigemptyset(&mask);
11608fae3551SRodney W. Grimes 	sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
11618fae3551SRodney W. Grimes 
11621ef60eb1SDavid Nugent #ifdef LOGIN_CAP
11631ef60eb1SDavid Nugent 	setprocresources(RESOURCE_GETTY);
11641ef60eb1SDavid Nugent #endif
1165b5df27e2SAndrey A. Chernov 	if (sp->se_type) {
1166b5df27e2SAndrey A. Chernov 		/* Don't use malloc after fork */
1167b5df27e2SAndrey A. Chernov 		strcpy(term, "TERM=");
116833a20f82SDavid Greenman 		strncat(term, sp->se_type, sizeof(term) - 6);
1169b5df27e2SAndrey A. Chernov 		env[0] = term;
1170b5df27e2SAndrey A. Chernov 		env[1] = 0;
1171b5df27e2SAndrey A. Chernov 	}
1172b5df27e2SAndrey A. Chernov 	else
1173b5df27e2SAndrey A. Chernov 		env[0] = 0;
1174b5df27e2SAndrey A. Chernov 	execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
11758fae3551SRodney W. Grimes 	stall("can't exec getty '%s' for port %s: %m",
11768fae3551SRodney W. Grimes 		sp->se_getty_argv[0], sp->se_device);
11778fae3551SRodney W. Grimes 	_exit(1);
11788fae3551SRodney W. Grimes }
11798fae3551SRodney W. Grimes 
11808fae3551SRodney W. Grimes /*
11818fae3551SRodney W. Grimes  * Collect exit status for a child.
11828fae3551SRodney W. Grimes  * If an exiting login, start a new login running.
11838fae3551SRodney W. Grimes  */
11848fae3551SRodney W. Grimes void
11858fae3551SRodney W. Grimes collect_child(pid_t pid)
11868fae3551SRodney W. Grimes {
11873d438ad6SDavid E. O'Brien 	session_t *sp, *sprev, *snext;
11888fae3551SRodney W. Grimes 
11898fae3551SRodney W. Grimes 	if (! sessions)
11908fae3551SRodney W. Grimes 		return;
11918fae3551SRodney W. Grimes 
11928fae3551SRodney W. Grimes 	if (! (sp = find_session(pid)))
11938fae3551SRodney W. Grimes 		return;
11948fae3551SRodney W. Grimes 
11958fae3551SRodney W. Grimes 	clear_session_logs(sp);
11968fae3551SRodney W. Grimes 	del_session(sp);
11978fae3551SRodney W. Grimes 	sp->se_process = 0;
11988fae3551SRodney W. Grimes 
11998fae3551SRodney W. Grimes 	if (sp->se_flags & SE_SHUTDOWN) {
12008889c700SDavid Nugent 		if ((sprev = sp->se_prev) != NULL)
12018fae3551SRodney W. Grimes 			sprev->se_next = sp->se_next;
12028fae3551SRodney W. Grimes 		else
12038fae3551SRodney W. Grimes 			sessions = sp->se_next;
12048889c700SDavid Nugent 		if ((snext = sp->se_next) != NULL)
12058fae3551SRodney W. Grimes 			snext->se_prev = sp->se_prev;
12068fae3551SRodney W. Grimes 		free_session(sp);
12078fae3551SRodney W. Grimes 		return;
12088fae3551SRodney W. Grimes 	}
12098fae3551SRodney W. Grimes 
12108fae3551SRodney W. Grimes 	if ((pid = start_getty(sp)) == -1) {
12118fae3551SRodney W. Grimes 		/* serious trouble */
12128fae3551SRodney W. Grimes 		requested_transition = clean_ttys;
12138fae3551SRodney W. Grimes 		return;
12148fae3551SRodney W. Grimes 	}
12158fae3551SRodney W. Grimes 
12168fae3551SRodney W. Grimes 	sp->se_process = pid;
12178fae3551SRodney W. Grimes 	sp->se_started = time((time_t *) 0);
12188fae3551SRodney W. Grimes 	add_session(sp);
12198fae3551SRodney W. Grimes }
12208fae3551SRodney W. Grimes 
12218fae3551SRodney W. Grimes /*
12228fae3551SRodney W. Grimes  * Catch a signal and request a state transition.
12238fae3551SRodney W. Grimes  */
12248fae3551SRodney W. Grimes void
122573bf18edSWarner Losh transition_handler(int sig)
12268fae3551SRodney W. Grimes {
12278fae3551SRodney W. Grimes 
12288fae3551SRodney W. Grimes 	switch (sig) {
12298fae3551SRodney W. Grimes 	case SIGHUP:
12308fae3551SRodney W. Grimes 		requested_transition = clean_ttys;
12318fae3551SRodney W. Grimes 		break;
1232a0a549c7SRuslan Ermilov 	case SIGUSR2:
1233a0a549c7SRuslan Ermilov 		howto = RB_POWEROFF;
1234a0a549c7SRuslan Ermilov 	case SIGUSR1:
1235a0a549c7SRuslan Ermilov 		howto |= RB_HALT;
1236e460cfd3SNate Williams 	case SIGINT:
1237db8ad19dSJordan K. Hubbard 		Reboot = TRUE;
12388fae3551SRodney W. Grimes 	case SIGTERM:
12398fae3551SRodney W. Grimes 		requested_transition = death;
12408fae3551SRodney W. Grimes 		break;
12418fae3551SRodney W. Grimes 	case SIGTSTP:
12428fae3551SRodney W. Grimes 		requested_transition = catatonia;
12438fae3551SRodney W. Grimes 		break;
12448fae3551SRodney W. Grimes 	default:
12458fae3551SRodney W. Grimes 		requested_transition = 0;
12468fae3551SRodney W. Grimes 		break;
12478fae3551SRodney W. Grimes 	}
12488fae3551SRodney W. Grimes }
12498fae3551SRodney W. Grimes 
12508fae3551SRodney W. Grimes /*
12518fae3551SRodney W. Grimes  * Take the system multiuser.
12528fae3551SRodney W. Grimes  */
12538fae3551SRodney W. Grimes state_func_t
125473bf18edSWarner Losh multi_user(void)
12558fae3551SRodney W. Grimes {
12568fae3551SRodney W. Grimes 	pid_t pid;
12573d438ad6SDavid E. O'Brien 	session_t *sp;
12588fae3551SRodney W. Grimes 
12598fae3551SRodney W. Grimes 	requested_transition = 0;
12608fae3551SRodney W. Grimes 
12618fae3551SRodney W. Grimes 	/*
12628fae3551SRodney W. Grimes 	 * If the administrator has not set the security level to -1
12638fae3551SRodney W. Grimes 	 * to indicate that the kernel should not run multiuser in secure
12648fae3551SRodney W. Grimes 	 * mode, and the run script has not set a higher level of security
12658fae3551SRodney W. Grimes 	 * than level 1, then put the kernel into secure mode.
12668fae3551SRodney W. Grimes 	 */
12678fae3551SRodney W. Grimes 	if (getsecuritylevel() == 0)
12688fae3551SRodney W. Grimes 		setsecuritylevel(1);
12698fae3551SRodney W. Grimes 
12708fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next) {
12718fae3551SRodney W. Grimes 		if (sp->se_process)
12728fae3551SRodney W. Grimes 			continue;
12738fae3551SRodney W. Grimes 		if ((pid = start_getty(sp)) == -1) {
12748fae3551SRodney W. Grimes 			/* serious trouble */
12758fae3551SRodney W. Grimes 			requested_transition = clean_ttys;
12768fae3551SRodney W. Grimes 			break;
12778fae3551SRodney W. Grimes 		}
12788fae3551SRodney W. Grimes 		sp->se_process = pid;
12798fae3551SRodney W. Grimes 		sp->se_started = time((time_t *) 0);
12808fae3551SRodney W. Grimes 		add_session(sp);
12818fae3551SRodney W. Grimes 	}
12828fae3551SRodney W. Grimes 
12838fae3551SRodney W. Grimes 	while (!requested_transition)
12848fae3551SRodney W. Grimes 		if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
12858fae3551SRodney W. Grimes 			collect_child(pid);
12868fae3551SRodney W. Grimes 
12878fae3551SRodney W. Grimes 	return (state_func_t) requested_transition;
12888fae3551SRodney W. Grimes }
12898fae3551SRodney W. Grimes 
12908fae3551SRodney W. Grimes /*
1291b0b670eeSAlfred Perlstein  * This is an (n*2)+(n^2) algorithm.  We hope it isn't run often...
12928fae3551SRodney W. Grimes  */
12938fae3551SRodney W. Grimes state_func_t
129473bf18edSWarner Losh clean_ttys(void)
12958fae3551SRodney W. Grimes {
12963d438ad6SDavid E. O'Brien 	session_t *sp, *sprev;
12973d438ad6SDavid E. O'Brien 	struct ttyent *typ;
12983d438ad6SDavid E. O'Brien 	int session_index = 0;
12993d438ad6SDavid E. O'Brien 	int devlen;
1300b5df27e2SAndrey A. Chernov 	char *old_getty, *old_window, *old_type;
13018fae3551SRodney W. Grimes 
1302b0b670eeSAlfred Perlstein 	/*
1303b0b670eeSAlfred Perlstein 	 * mark all sessions for death, (!SE_PRESENT)
1304b0b670eeSAlfred Perlstein 	 * as we find or create new ones they'll be marked as keepers,
1305b0b670eeSAlfred Perlstein 	 * we'll later nuke all the ones not found in /etc/ttys
1306b0b670eeSAlfred Perlstein 	 */
1307b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next)
1308b0b670eeSAlfred Perlstein 		sp->se_flags &= ~SE_PRESENT;
1309b0b670eeSAlfred Perlstein 
13108fae3551SRodney W. Grimes 	devlen = sizeof(_PATH_DEV) - 1;
13118889c700SDavid Nugent 	while ((typ = getttyent()) != NULL) {
13128fae3551SRodney W. Grimes 		++session_index;
13138fae3551SRodney W. Grimes 
13148fae3551SRodney W. Grimes 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
13158fae3551SRodney W. Grimes 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
13168fae3551SRodney W. Grimes 				break;
13178fae3551SRodney W. Grimes 
13188fae3551SRodney W. Grimes 		if (sp) {
1319b0b670eeSAlfred Perlstein 			/* we want this one to live */
1320b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_PRESENT;
13218fae3551SRodney W. Grimes 			if (sp->se_index != session_index) {
13228fae3551SRodney W. Grimes 				warning("port %s changed utmp index from %d to %d",
13238fae3551SRodney W. Grimes 				       sp->se_device, sp->se_index,
13248fae3551SRodney W. Grimes 				       session_index);
13258fae3551SRodney W. Grimes 				sp->se_index = session_index;
13268fae3551SRodney W. Grimes 			}
13278fae3551SRodney W. Grimes 			if ((typ->ty_status & TTY_ON) == 0 ||
13288fae3551SRodney W. Grimes 			    typ->ty_getty == 0) {
13298fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
13308fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
13318fae3551SRodney W. Grimes 				continue;
13328fae3551SRodney W. Grimes 			}
13338fae3551SRodney W. Grimes 			sp->se_flags &= ~SE_SHUTDOWN;
1334b5df27e2SAndrey A. Chernov 			old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1335b5df27e2SAndrey A. Chernov 			old_window = sp->se_window ? strdup(sp->se_window) : 0;
1336b5df27e2SAndrey A. Chernov 			old_type = sp->se_type ? strdup(sp->se_type) : 0;
13378fae3551SRodney W. Grimes 			if (setupargv(sp, typ) == 0) {
13388fae3551SRodney W. Grimes 				warning("can't parse getty for port %s",
13398fae3551SRodney W. Grimes 					sp->se_device);
13408fae3551SRodney W. Grimes 				sp->se_flags |= SE_SHUTDOWN;
13418fae3551SRodney W. Grimes 				kill(sp->se_process, SIGHUP);
13428fae3551SRodney W. Grimes 			}
1343b5df27e2SAndrey A. Chernov 			else if (   !old_getty
13448889c700SDavid Nugent 				 || (!old_type && sp->se_type)
13458889c700SDavid Nugent 				 || (old_type && !sp->se_type)
13468889c700SDavid Nugent 				 || (!old_window && sp->se_window)
13478889c700SDavid Nugent 				 || (old_window && !sp->se_window)
13488889c700SDavid Nugent 				 || (strcmp(old_getty, sp->se_getty) != 0)
13498889c700SDavid Nugent 				 || (old_window && strcmp(old_window, sp->se_window) != 0)
13508889c700SDavid Nugent 				 || (old_type && strcmp(old_type, sp->se_type) != 0)
1351b5df27e2SAndrey A. Chernov 				) {
1352b5df27e2SAndrey A. Chernov 				/* Don't set SE_SHUTDOWN here */
1353b5df27e2SAndrey A. Chernov 				sp->se_nspace = 0;
1354b5df27e2SAndrey A. Chernov 				sp->se_started = 0;
1355b5df27e2SAndrey A. Chernov 				kill(sp->se_process, SIGHUP);
1356b5df27e2SAndrey A. Chernov 			}
1357b5df27e2SAndrey A. Chernov 			if (old_getty)
1358b5df27e2SAndrey A. Chernov 				free(old_getty);
13592d887af5SMike Heffner 			if (old_window)
1360b5df27e2SAndrey A. Chernov 				free(old_window);
1361b5df27e2SAndrey A. Chernov 			if (old_type)
1362b5df27e2SAndrey A. Chernov 				free(old_type);
13638fae3551SRodney W. Grimes 			continue;
13648fae3551SRodney W. Grimes 		}
13658fae3551SRodney W. Grimes 
13668fae3551SRodney W. Grimes 		new_session(sprev, session_index, typ);
13678fae3551SRodney W. Grimes 	}
13688fae3551SRodney W. Grimes 
13698fae3551SRodney W. Grimes 	endttyent();
13708fae3551SRodney W. Grimes 
1371b0b670eeSAlfred Perlstein 	/*
1372b0b670eeSAlfred Perlstein 	 * sweep through and kill all deleted sessions
1373b0b670eeSAlfred Perlstein 	 * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1374b0b670eeSAlfred Perlstein 	 */
1375b0b670eeSAlfred Perlstein 	for (sp = sessions; sp != NULL; sp = sp->se_next) {
1376b0b670eeSAlfred Perlstein 		if ((sp->se_flags & SE_PRESENT) == 0) {
1377b0b670eeSAlfred Perlstein 			sp->se_flags |= SE_SHUTDOWN;
1378b0b670eeSAlfred Perlstein 			kill(sp->se_process, SIGHUP);
1379b0b670eeSAlfred Perlstein 		}
1380b0b670eeSAlfred Perlstein 	}
1381b0b670eeSAlfred Perlstein 
13828fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
13838fae3551SRodney W. Grimes }
13848fae3551SRodney W. Grimes 
13858fae3551SRodney W. Grimes /*
13868fae3551SRodney W. Grimes  * Block further logins.
13878fae3551SRodney W. Grimes  */
13888fae3551SRodney W. Grimes state_func_t
138973bf18edSWarner Losh catatonia(void)
13908fae3551SRodney W. Grimes {
13913d438ad6SDavid E. O'Brien 	session_t *sp;
13928fae3551SRodney W. Grimes 
13938fae3551SRodney W. Grimes 	for (sp = sessions; sp; sp = sp->se_next)
13948fae3551SRodney W. Grimes 		sp->se_flags |= SE_SHUTDOWN;
13958fae3551SRodney W. Grimes 
13968fae3551SRodney W. Grimes 	return (state_func_t) multi_user;
13978fae3551SRodney W. Grimes }
13988fae3551SRodney W. Grimes 
13998fae3551SRodney W. Grimes /*
14008fae3551SRodney W. Grimes  * Note SIGALRM.
14018fae3551SRodney W. Grimes  */
14028fae3551SRodney W. Grimes void
140373bf18edSWarner Losh alrm_handler(int sig)
14048fae3551SRodney W. Grimes {
14058889c700SDavid Nugent 	(void)sig;
14068fae3551SRodney W. Grimes 	clang = 1;
14078fae3551SRodney W. Grimes }
14088fae3551SRodney W. Grimes 
14098fae3551SRodney W. Grimes /*
14108fae3551SRodney W. Grimes  * Bring the system down to single user.
14118fae3551SRodney W. Grimes  */
14128fae3551SRodney W. Grimes state_func_t
141373bf18edSWarner Losh death(void)
14148fae3551SRodney W. Grimes {
14153d438ad6SDavid E. O'Brien 	session_t *sp;
14163d438ad6SDavid E. O'Brien 	int i;
14178fae3551SRodney W. Grimes 	pid_t pid;
1418c3d7c52eSAndrey A. Chernov 	static const int death_sigs[2] = { SIGTERM, SIGKILL };
14198fae3551SRodney W. Grimes 
14203f31fb33SAndrey A. Chernov 	/* NB: should send a message to the session logger to avoid blocking. */
14213f31fb33SAndrey A. Chernov 	logwtmp("~", "shutdown", "");
14223f31fb33SAndrey A. Chernov 
14231054bb1eSAndrey A. Chernov 	for (sp = sessions; sp; sp = sp->se_next) {
14248fae3551SRodney W. Grimes 		sp->se_flags |= SE_SHUTDOWN;
142525cf4a54SAndrey A. Chernov 		kill(sp->se_process, SIGHUP);
14261054bb1eSAndrey A. Chernov 	}
14278fae3551SRodney W. Grimes 
14288889c700SDavid Nugent 	/* Try to run the rc.shutdown script within a period of time */
142925cf4a54SAndrey A. Chernov 	(void) runshutdown();
14308889c700SDavid Nugent 
1431c3d7c52eSAndrey A. Chernov 	for (i = 0; i < 2; ++i) {
14328fae3551SRodney W. Grimes 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
14338fae3551SRodney W. Grimes 			return (state_func_t) single_user;
14348fae3551SRodney W. Grimes 
14358fae3551SRodney W. Grimes 		clang = 0;
14368fae3551SRodney W. Grimes 		alarm(DEATH_WATCH);
14378fae3551SRodney W. Grimes 		do
14388fae3551SRodney W. Grimes 			if ((pid = waitpid(-1, (int *)0, 0)) != -1)
14398fae3551SRodney W. Grimes 				collect_child(pid);
14408fae3551SRodney W. Grimes 		while (clang == 0 && errno != ECHILD);
14418fae3551SRodney W. Grimes 
14428fae3551SRodney W. Grimes 		if (errno == ECHILD)
14438fae3551SRodney W. Grimes 			return (state_func_t) single_user;
14448fae3551SRodney W. Grimes 	}
14458fae3551SRodney W. Grimes 
14468fae3551SRodney W. Grimes 	warning("some processes would not die; ps axl advised");
14478fae3551SRodney W. Grimes 
14488fae3551SRodney W. Grimes 	return (state_func_t) single_user;
14498fae3551SRodney W. Grimes }
14508889c700SDavid Nugent 
14518889c700SDavid Nugent /*
14528889c700SDavid Nugent  * Run the system shutdown script.
14538889c700SDavid Nugent  *
14548889c700SDavid Nugent  * Exit codes:      XXX I should document more
14558889c700SDavid Nugent  * -2       shutdown script terminated abnormally
14568889c700SDavid Nugent  * -1       fatal error - can't run script
14578889c700SDavid Nugent  * 0        good.
14588889c700SDavid Nugent  * >0       some error (exit code)
14598889c700SDavid Nugent  */
14608889c700SDavid Nugent int
146173bf18edSWarner Losh runshutdown(void)
14628889c700SDavid Nugent {
14638889c700SDavid Nugent 	pid_t pid, wpid;
14648889c700SDavid Nugent 	int status;
14658889c700SDavid Nugent 	int shutdowntimeout;
14668889c700SDavid Nugent 	size_t len;
1467a69497d7SMatthew Dillon 	char *argv[4];
14688889c700SDavid Nugent 	struct sigaction sa;
146986bf62dcSDavid Nugent 	struct stat sb;
147086bf62dcSDavid Nugent 
147186bf62dcSDavid Nugent 	/*
147286bf62dcSDavid Nugent 	 * rc.shutdown is optional, so to prevent any unnecessary
147386bf62dcSDavid Nugent 	 * complaints from the shell we simply don't run it if the
147486bf62dcSDavid Nugent 	 * file does not exist. If the stat() here fails for other
147586bf62dcSDavid Nugent 	 * reasons, we'll let the shell complain.
147686bf62dcSDavid Nugent 	 */
147786bf62dcSDavid Nugent 	if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
147886bf62dcSDavid Nugent 		return 0;
14798889c700SDavid Nugent 
14808889c700SDavid Nugent 	if ((pid = fork()) == 0) {
14818889c700SDavid Nugent 		int	fd;
14828889c700SDavid Nugent 
148325cf4a54SAndrey A. Chernov 		/* Assume that init already grab console as ctty before */
148425cf4a54SAndrey A. Chernov 
14858889c700SDavid Nugent 		sigemptyset(&sa.sa_mask);
14868889c700SDavid Nugent 		sa.sa_flags = 0;
14878889c700SDavid Nugent 		sa.sa_handler = SIG_IGN;
14888889c700SDavid Nugent 		(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
14898889c700SDavid Nugent 		(void) sigaction(SIGHUP, &sa, (struct sigaction *)0);
14908889c700SDavid Nugent 
14918889c700SDavid Nugent 		if ((fd = open(_PATH_CONSOLE, O_RDWR)) == -1)
14928889c700SDavid Nugent 		    warning("can't open %s: %m", _PATH_CONSOLE);
14938889c700SDavid Nugent 		else {
149425cf4a54SAndrey A. Chernov 		    (void) dup2(fd, 0);
14958889c700SDavid Nugent 		    (void) dup2(fd, 1);
14968889c700SDavid Nugent 		    (void) dup2(fd, 2);
149725cf4a54SAndrey A. Chernov 		    if (fd > 2)
149825cf4a54SAndrey A. Chernov 			close(fd);
14998889c700SDavid Nugent 		}
15008889c700SDavid Nugent 
15018889c700SDavid Nugent 		/*
15028889c700SDavid Nugent 		 * Run the shutdown script.
15038889c700SDavid Nugent 		 */
1504ab03e6d5SXin LI 
1505ab03e6d5SXin LI 		char _sh[]	= "sh";
1506ab03e6d5SXin LI 		char _reboot[]	= "reboot";
1507ab03e6d5SXin LI 		char _single[]	= "single";
1508ab03e6d5SXin LI 		char _path_rundown[] = _PATH_RUNDOWN;
1509ab03e6d5SXin LI 
1510ab03e6d5SXin LI 		argv[0] = _sh;
1511ab03e6d5SXin LI 		argv[1] = _path_rundown;
1512ab03e6d5SXin LI 		argv[2] = Reboot ? _reboot : _single;
1513a69497d7SMatthew Dillon 		argv[3] = 0;
15148889c700SDavid Nugent 
15158889c700SDavid Nugent 		sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0);
15168889c700SDavid Nugent 
151725cf4a54SAndrey A. Chernov #ifdef LOGIN_CAP
151825cf4a54SAndrey A. Chernov 		setprocresources(RESOURCE_RC);
151925cf4a54SAndrey A. Chernov #endif
15208889c700SDavid Nugent 		execv(_PATH_BSHELL, argv);
15218889c700SDavid Nugent 		warning("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNDOWN);
15228889c700SDavid Nugent 		_exit(1);	/* force single user mode */
15238889c700SDavid Nugent 	}
15248889c700SDavid Nugent 
15258889c700SDavid Nugent 	if (pid == -1) {
15268889c700SDavid Nugent 		emergency("can't fork for %s on %s: %m",
15278889c700SDavid Nugent 			_PATH_BSHELL, _PATH_RUNDOWN);
15288889c700SDavid Nugent 		while (waitpid(-1, (int *) 0, WNOHANG) > 0)
15298889c700SDavid Nugent 			continue;
15308889c700SDavid Nugent 		sleep(STALL_TIMEOUT);
15318889c700SDavid Nugent 		return -1;
15328889c700SDavid Nugent 	}
15338889c700SDavid Nugent 
15348889c700SDavid Nugent 	len = sizeof(shutdowntimeout);
1535724447acSRalf S. Engelschall 	if (sysctlbyname("kern.init_shutdown_timeout",
15368889c700SDavid Nugent 			 &shutdowntimeout,
15378889c700SDavid Nugent 			 &len, NULL, 0) == -1 || shutdowntimeout < 2)
15388889c700SDavid Nugent 	    shutdowntimeout = DEATH_SCRIPT;
15398889c700SDavid Nugent 	alarm(shutdowntimeout);
15408889c700SDavid Nugent 	clang = 0;
15418889c700SDavid Nugent 	/*
15428889c700SDavid Nugent 	 * Copied from single_user().  This is a bit paranoid.
15438889c700SDavid Nugent 	 * Use the same ALRM handler.
15448889c700SDavid Nugent 	 */
15458889c700SDavid Nugent 	do {
15468889c700SDavid Nugent 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
15478889c700SDavid Nugent 			collect_child(wpid);
15488889c700SDavid Nugent 		if (clang == 1) {
15498889c700SDavid Nugent 			/* we were waiting for the sub-shell */
15508889c700SDavid Nugent 			kill(wpid, SIGTERM);
15518a8d42eeSJeroen Ruigrok van der Werven 			warning("timeout expired for %s on %s: %m; going to single user mode",
15528889c700SDavid Nugent 				_PATH_BSHELL, _PATH_RUNDOWN);
15538889c700SDavid Nugent 			return -1;
15548889c700SDavid Nugent 		}
15558889c700SDavid Nugent 		if (wpid == -1) {
15568889c700SDavid Nugent 			if (errno == EINTR)
15578889c700SDavid Nugent 				continue;
15588889c700SDavid Nugent 			warning("wait for %s on %s failed: %m; going to single user mode",
15598889c700SDavid Nugent 				_PATH_BSHELL, _PATH_RUNDOWN);
15608889c700SDavid Nugent 			return -1;
15618889c700SDavid Nugent 		}
15628889c700SDavid Nugent 		if (wpid == pid && WIFSTOPPED(status)) {
15638889c700SDavid Nugent 			warning("init: %s on %s stopped, restarting\n",
15648889c700SDavid Nugent 				_PATH_BSHELL, _PATH_RUNDOWN);
15658889c700SDavid Nugent 			kill(pid, SIGCONT);
15668889c700SDavid Nugent 			wpid = -1;
15678889c700SDavid Nugent 		}
15688889c700SDavid Nugent 	} while (wpid != pid && !clang);
15698889c700SDavid Nugent 
15708889c700SDavid Nugent 	/* Turn off the alarm */
15718889c700SDavid Nugent 	alarm(0);
15728889c700SDavid Nugent 
15738889c700SDavid Nugent 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
15748889c700SDavid Nugent 	    requested_transition == catatonia) {
15758889c700SDavid Nugent 		/*
15768889c700SDavid Nugent 		 * /etc/rc.shutdown executed /sbin/reboot;
15778889c700SDavid Nugent 		 * wait for the end quietly
15788889c700SDavid Nugent 		 */
15798889c700SDavid Nugent 		sigset_t s;
15808889c700SDavid Nugent 
15818889c700SDavid Nugent 		sigfillset(&s);
15828889c700SDavid Nugent 		for (;;)
15838889c700SDavid Nugent 			sigsuspend(&s);
15848889c700SDavid Nugent 	}
15858889c700SDavid Nugent 
15868889c700SDavid Nugent 	if (!WIFEXITED(status)) {
15878889c700SDavid Nugent 		warning("%s on %s terminated abnormally, going to single user mode",
15888889c700SDavid Nugent 			_PATH_BSHELL, _PATH_RUNDOWN);
15898889c700SDavid Nugent 		return -2;
15908889c700SDavid Nugent 	}
15918889c700SDavid Nugent 
15928889c700SDavid Nugent 	if ((status = WEXITSTATUS(status)) != 0)
15938889c700SDavid Nugent 		warning("%s returned status %d", _PATH_RUNDOWN, status);
15948889c700SDavid Nugent 
15958889c700SDavid Nugent 	return status;
15968889c700SDavid Nugent }
15978889c700SDavid Nugent 
1598ab03e6d5SXin LI static char *
159981ab7fb2SAndrey A. Chernov strk(char *p)
160081ab7fb2SAndrey A. Chernov {
160181ab7fb2SAndrey A. Chernov     static char *t;
160281ab7fb2SAndrey A. Chernov     char *q;
160381ab7fb2SAndrey A. Chernov     int c;
160481ab7fb2SAndrey A. Chernov 
160581ab7fb2SAndrey A. Chernov     if (p)
160681ab7fb2SAndrey A. Chernov 	t = p;
160781ab7fb2SAndrey A. Chernov     if (!t)
160881ab7fb2SAndrey A. Chernov 	return 0;
160981ab7fb2SAndrey A. Chernov 
161081ab7fb2SAndrey A. Chernov     c = *t;
161181ab7fb2SAndrey A. Chernov     while (c == ' ' || c == '\t' )
161281ab7fb2SAndrey A. Chernov 	c = *++t;
161381ab7fb2SAndrey A. Chernov     if (!c) {
161481ab7fb2SAndrey A. Chernov 	t = 0;
161581ab7fb2SAndrey A. Chernov 	return 0;
161681ab7fb2SAndrey A. Chernov     }
161781ab7fb2SAndrey A. Chernov     q = t;
161881ab7fb2SAndrey A. Chernov     if (c == '\'') {
161981ab7fb2SAndrey A. Chernov 	c = *++t;
162081ab7fb2SAndrey A. Chernov 	q = t;
162181ab7fb2SAndrey A. Chernov 	while (c && c != '\'')
162281ab7fb2SAndrey A. Chernov 	    c = *++t;
162381ab7fb2SAndrey A. Chernov 	if (!c)  /* unterminated string */
162481ab7fb2SAndrey A. Chernov 	    q = t = 0;
162581ab7fb2SAndrey A. Chernov 	else
162681ab7fb2SAndrey A. Chernov 	    *t++ = 0;
162781ab7fb2SAndrey A. Chernov     } else {
162881ab7fb2SAndrey A. Chernov 	while (c && c != ' ' && c != '\t' )
162981ab7fb2SAndrey A. Chernov 	    c = *++t;
163081ab7fb2SAndrey A. Chernov 	*t++ = 0;
163181ab7fb2SAndrey A. Chernov 	if (!c)
163281ab7fb2SAndrey A. Chernov 	    t = 0;
163381ab7fb2SAndrey A. Chernov     }
163481ab7fb2SAndrey A. Chernov     return q;
163581ab7fb2SAndrey A. Chernov }
16361ef60eb1SDavid Nugent 
16371ef60eb1SDavid Nugent #ifdef LOGIN_CAP
1638e82d5545SDavid Nugent void
163973bf18edSWarner Losh setprocresources(const char *cname)
16401ef60eb1SDavid Nugent {
1641e82d5545SDavid Nugent 	login_cap_t *lc;
1642a2ee73bcSAndrey A. Chernov 	if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
1643e82d5545SDavid Nugent 		setusercontext(lc, (struct passwd*)NULL, 0, LOGIN_SETPRIORITY|LOGIN_SETRESOURCES);
16441ef60eb1SDavid Nugent 		login_close(lc);
16451ef60eb1SDavid Nugent 	}
16461ef60eb1SDavid Nugent }
16471ef60eb1SDavid Nugent #endif
1648