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