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