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