Home | History | Annotate | Line # | Download | only in init
init.c revision 1.55
      1 /*	$NetBSD: init.c,v 1.55 2003/04/20 17:16:31 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Donn Seeley at Berkeley Software Design, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n"
     42 "	The Regents of the University of California.  All rights reserved.\n");
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)init.c	8.2 (Berkeley) 4/28/95";
     48 #else
     49 __RCSID("$NetBSD: init.c,v 1.55 2003/04/20 17:16:31 christos Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/sysctl.h>
     55 #include <sys/wait.h>
     56 #include <sys/mman.h>
     57 #include <sys/stat.h>
     58 #include <sys/mount.h>
     59 #ifdef DEBUG
     60 #include <sys/sysctl.h>
     61 #include <machine/cpu.h>
     62 #endif
     63 
     64 #include <db.h>
     65 #include <errno.h>
     66 #include <fcntl.h>
     67 #include <signal.h>
     68 #include <stdio.h>
     69 #include <stdlib.h>
     70 #include <string.h>
     71 #include <syslog.h>
     72 #include <time.h>
     73 #include <ttyent.h>
     74 #include <unistd.h>
     75 #include <util.h>
     76 #include <paths.h>
     77 #include <err.h>
     78 
     79 #include <stdarg.h>
     80 
     81 #ifdef SECURE
     82 #include <pwd.h>
     83 #endif
     84 
     85 #include "pathnames.h"
     86 
     87 /*
     88  * Sleep times; used to prevent thrashing.
     89  */
     90 #define	GETTY_SPACING		 5	/* N secs minimum getty spacing */
     91 #define	GETTY_SLEEP		30	/* sleep N secs after spacing problem */
     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 
     96 #ifdef ALTSHELL
     97 #ifndef	_PATH_ALTSHELL
     98 #define	_PATH_ALTSHELL	_PATH_BSHELL
     99 #endif
    100 #endif
    101 
    102 int main(int, char *[]);
    103 
    104 void handle(sig_t, ...);
    105 void delset(sigset_t *, ...);
    106 
    107 void stall(const char *, ...)
    108     __attribute__((__format__(__printf__,1,2)));
    109 void warning(const char *, ...)
    110     __attribute__((__format__(__printf__,1,2)));
    111 void emergency(const char *, ...)
    112     __attribute__((__format__(__printf__,1,2)));
    113 void disaster(int);
    114 void badsys(int);
    115 
    116 /*
    117  * We really need a recursive typedef...
    118  * The following at least guarantees that the return type of (*state_t)()
    119  * is sufficiently wide to hold a function pointer.
    120  */
    121 typedef long (*state_func_t)(void);
    122 typedef state_func_t (*state_t)(void);
    123 
    124 state_func_t single_user(void);
    125 state_func_t runcom(void);
    126 state_func_t read_ttys(void);
    127 state_func_t multi_user(void);
    128 state_func_t clean_ttys(void);
    129 state_func_t catatonia(void);
    130 state_func_t death(void);
    131 
    132 enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
    133 
    134 void transition(state_t);
    135 #ifndef LETS_GET_SMALL
    136 state_t requested_transition = runcom;
    137 #else /* LETS_GET_SMALL */
    138 state_t requested_transition = single_user;
    139 #endif /* LETS_GET_SMALL */
    140 
    141 void setctty(const char *);
    142 
    143 typedef struct init_session {
    144 	int	se_index;		/* index of entry in ttys file */
    145 	pid_t	se_process;		/* controlling process */
    146 	time_t	se_started;		/* used to avoid thrashing */
    147 	int	se_flags;		/* status of session */
    148 #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
    149 #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
    150 	char	*se_device;		/* filename of port */
    151 	char	*se_getty;		/* what to run on that port */
    152 	char	**se_getty_argv;	/* pre-parsed argument array */
    153 	char	*se_window;		/* window system (started only once) */
    154 	char	**se_window_argv;	/* pre-parsed argument array */
    155 	struct	init_session *se_prev;
    156 	struct	init_session *se_next;
    157 } session_t;
    158 
    159 void free_session(session_t *);
    160 session_t *new_session(session_t *, int, struct ttyent *);
    161 session_t *sessions;
    162 
    163 char **construct_argv(char *);
    164 void start_window_system(session_t *);
    165 void collect_child(pid_t, int);
    166 pid_t start_getty(session_t *);
    167 void transition_handler(int);
    168 void alrm_handler(int);
    169 void setsecuritylevel(int);
    170 int getsecuritylevel(void);
    171 int setupargv(session_t *, struct ttyent *);
    172 int clang;
    173 
    174 void clear_session_logs(session_t *, int);
    175 
    176 int start_session_db(void);
    177 void add_session(session_t *);
    178 void del_session(session_t *);
    179 session_t *find_session(pid_t);
    180 DB *session_db;
    181 
    182 #ifdef MFS_DEV_IF_NO_CONSOLE
    183 static int mfs_dev(void);
    184 #endif
    185 
    186 /*
    187  * The mother of all processes.
    188  */
    189 int
    190 main(int argc, char **argv)
    191 {
    192 	struct sigaction sa;
    193 	sigset_t mask;
    194 #ifndef LETS_GET_SMALL
    195 	int c;
    196 
    197 	/* Dispose of random users. */
    198 	if (getuid() != 0) {
    199 		errno = EPERM;
    200 		err(1, NULL);
    201 	}
    202 
    203 	/* System V users like to reexec init. */
    204 	if (getpid() != 1)
    205 		errx(1, "already running");
    206 #endif
    207 
    208 	/*
    209 	 * Create an initial session.
    210 	 */
    211 	if (setsid() < 0)
    212 		warn("initial setsid() failed");
    213 
    214 	/*
    215 	 * Establish an initial user so that programs running
    216 	 * single user do not freak out and die (like passwd).
    217 	 */
    218 	if (setlogin("root") < 0)
    219 		warn("setlogin() failed");
    220 
    221 
    222 #ifdef MFS_DEV_IF_NO_CONSOLE
    223 	if (mfs_dev() == -1)
    224 		requested_transition = single_user;
    225 #endif
    226 
    227 #ifndef LETS_GET_SMALL
    228 	/*
    229 	 * Note that this does NOT open a file...
    230 	 * Does 'init' deserve its own facility number?
    231 	 */
    232 	openlog("init", LOG_CONS, LOG_AUTH);
    233 #endif /* LETS_GET_SMALL */
    234 
    235 
    236 #ifndef LETS_GET_SMALL
    237 	/*
    238 	 * This code assumes that we always get arguments through flags,
    239 	 * never through bits set in some random machine register.
    240 	 */
    241 	while ((c = getopt(argc, argv, "sf")) != -1)
    242 		switch (c) {
    243 		case 's':
    244 			requested_transition = single_user;
    245 			break;
    246 		case 'f':
    247 			runcom_mode = FASTBOOT;
    248 			break;
    249 		default:
    250 			warning("unrecognized flag '-%c'", c);
    251 			break;
    252 		}
    253 
    254 	if (optind != argc)
    255 		warning("ignoring excess arguments");
    256 #else /* LETS_GET_SMALL */
    257 	requested_transition = single_user;
    258 #endif /* LETS_GET_SMALL */
    259 
    260 	/*
    261 	 * We catch or block signals rather than ignore them,
    262 	 * so that they get reset on exec.
    263 	 */
    264 	handle(badsys, SIGSYS, 0);
    265 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
    266 	       SIGBUS, SIGXCPU, SIGXFSZ, 0);
    267 	handle(transition_handler, SIGHUP, SIGTERM, SIGTSTP, 0);
    268 	handle(alrm_handler, SIGALRM, 0);
    269 	(void)sigfillset(&mask);
    270 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
    271 	    SIGXCPU, SIGXFSZ, SIGHUP, SIGTERM, SIGTSTP, SIGALRM, 0);
    272 	(void)sigprocmask(SIG_SETMASK, &mask, NULL);
    273 	(void)sigemptyset(&sa.sa_mask);
    274 	sa.sa_flags = 0;
    275 	sa.sa_handler = SIG_IGN;
    276 	(void)sigaction(SIGTTIN, &sa, NULL);
    277 	(void)sigaction(SIGTTOU, &sa, NULL);
    278 
    279 	/*
    280 	 * Paranoia.
    281 	 */
    282 	(void)close(0);
    283 	(void)close(1);
    284 	(void)close(2);
    285 
    286 	/*
    287 	 * Start the state machine.
    288 	 */
    289 	transition(requested_transition);
    290 
    291 	/*
    292 	 * Should never reach here.
    293 	 */
    294 	return 1;
    295 }
    296 
    297 /*
    298  * Associate a function with a signal handler.
    299  */
    300 void
    301 handle(sig_t handler, ...)
    302 {
    303 	int sig;
    304 	struct sigaction sa;
    305 	sigset_t mask_everything;
    306 	va_list ap;
    307 
    308 	va_start(ap, handler);
    309 
    310 	sa.sa_handler = handler;
    311 	(void)sigfillset(&mask_everything);
    312 
    313 	while ((sig = va_arg(ap, int)) != 0) {
    314 		sa.sa_mask = mask_everything;
    315 		/* XXX SA_RESTART? */
    316 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
    317 		(void)sigaction(sig, &sa, NULL);
    318 	}
    319 	va_end(ap);
    320 }
    321 
    322 /*
    323  * Delete a set of signals from a mask.
    324  */
    325 void
    326 delset(sigset_t *maskp, ...)
    327 {
    328 	int sig;
    329 	va_list ap;
    330 
    331 	va_start(ap, maskp);
    332 
    333 	while ((sig = va_arg(ap, int)) != 0)
    334 		(void)sigdelset(maskp, sig);
    335 	va_end(ap);
    336 }
    337 
    338 /*
    339  * Log a message and sleep for a while (to give someone an opportunity
    340  * to read it and to save log or hardcopy output if the problem is chronic).
    341  * NB: should send a message to the session logger to avoid blocking.
    342  */
    343 void
    344 stall(const char *message, ...)
    345 {
    346 	va_list ap;
    347 
    348 	va_start(ap, message);
    349 	vsyslog(LOG_ALERT, message, ap);
    350 	va_end(ap);
    351 	closelog();
    352 	(void)sleep(STALL_TIMEOUT);
    353 }
    354 
    355 /*
    356  * Like stall(), but doesn't sleep.
    357  * If cpp had variadic macros, the two functions could be #defines for another.
    358  * NB: should send a message to the session logger to avoid blocking.
    359  */
    360 void
    361 warning(const char *message, ...)
    362 {
    363 	va_list ap;
    364 
    365 	va_start(ap, message);
    366 	vsyslog(LOG_ALERT, message, ap);
    367 	va_end(ap);
    368 	closelog();
    369 }
    370 
    371 /*
    372  * Log an emergency message.
    373  * NB: should send a message to the session logger to avoid blocking.
    374  */
    375 void
    376 emergency(const char *message, ...)
    377 {
    378 	va_list ap;
    379 
    380 	va_start(ap, message);
    381 	vsyslog(LOG_EMERG, message, ap);
    382 	va_end(ap);
    383 	closelog();
    384 }
    385 
    386 /*
    387  * Catch a SIGSYS signal.
    388  *
    389  * These may arise if a system does not support sysctl.
    390  * We tolerate up to 25 of these, then throw in the towel.
    391  */
    392 void
    393 badsys(int sig)
    394 {
    395 	static int badcount = 0;
    396 
    397 	if (badcount++ < 25)
    398 		return;
    399 	disaster(sig);
    400 }
    401 
    402 /*
    403  * Catch an unexpected signal.
    404  */
    405 void
    406 disaster(int sig)
    407 {
    408 
    409 	emergency("fatal signal: %s", strsignal(sig));
    410 	(void)sleep(STALL_TIMEOUT);
    411 	_exit(sig);		/* reboot */
    412 }
    413 
    414 /*
    415  * Get the security level of the kernel.
    416  */
    417 int
    418 getsecuritylevel(void)
    419 {
    420 #ifdef KERN_SECURELVL
    421 	int name[2], curlevel;
    422 	size_t len;
    423 
    424 	name[0] = CTL_KERN;
    425 	name[1] = KERN_SECURELVL;
    426 	len = sizeof curlevel;
    427 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
    428 		emergency("cannot get kernel security level: %m");
    429 		return (-1);
    430 	}
    431 	return (curlevel);
    432 #else
    433 	return (-1);
    434 #endif
    435 }
    436 
    437 /*
    438  * Set the security level of the kernel.
    439  */
    440 void
    441 setsecuritylevel(int newlevel)
    442 {
    443 #ifdef KERN_SECURELVL
    444 	int name[2], curlevel;
    445 
    446 	curlevel = getsecuritylevel();
    447 	if (newlevel == curlevel)
    448 		return;
    449 	name[0] = CTL_KERN;
    450 	name[1] = KERN_SECURELVL;
    451 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
    452 		emergency( "cannot change kernel security level from"
    453 		    " %d to %d: %m", curlevel, newlevel);
    454 		return;
    455 	}
    456 #ifdef SECURE
    457 	warning("kernel security level changed from %d to %d",
    458 	    curlevel, newlevel);
    459 #endif
    460 #endif
    461 }
    462 
    463 /*
    464  * Change states in the finite state machine.
    465  * The initial state is passed as an argument.
    466  */
    467 void
    468 transition(state_t s)
    469 {
    470 
    471 	if (s == NULL)
    472 		return;
    473 	for (;;)
    474 		s = (state_t)(*s)();
    475 }
    476 
    477 /*
    478  * Close out the accounting files for a login session.
    479  * NB: should send a message to the session logger to avoid blocking.
    480  */
    481 void
    482 clear_session_logs(session_t *sp, int status)
    483 {
    484 	char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
    485 
    486 #ifdef SUPPORT_UTMPX
    487 	if (logoutx(line, status, DEAD_PROCESS))
    488 		logwtmpx(line, "", "", status, DEAD_PROCESS);
    489 #endif
    490 #ifdef SUPPORT_UTMP
    491 	if (logout(line))
    492 		logwtmp(line, "", "");
    493 #endif
    494 }
    495 
    496 /*
    497  * Start a session and allocate a controlling terminal.
    498  * Only called by children of init after forking.
    499  */
    500 void
    501 setctty(const char *name)
    502 {
    503 	int fd;
    504 
    505 	(void) revoke(name);
    506 	sleep(2);			/* leave DTR low */
    507 	if ((fd = open(name, O_RDWR)) == -1) {
    508 		stall("can't open %s: %m", name);
    509 		_exit(1);
    510 	}
    511 	if (login_tty(fd) == -1) {
    512 		stall("can't get %s for controlling terminal: %m", name);
    513 		_exit(1);
    514 	}
    515 }
    516 
    517 /*
    518  * Bring the system up single user.
    519  */
    520 state_func_t
    521 single_user(void)
    522 {
    523 	pid_t pid, wpid;
    524 	int status;
    525 	int from_securitylevel;
    526 	sigset_t mask;
    527 	struct sigaction sa, satstp, sahup;
    528 #ifdef ALTSHELL
    529 	const char *shell = _PATH_ALTSHELL;
    530 #endif
    531 	char *argv[2];
    532 #ifdef SECURE
    533 	struct ttyent *typ;
    534 	struct passwd *pp;
    535 	char *clear, *password;
    536 #endif
    537 #ifdef ALTSHELL
    538 	char altshell[128];
    539 #endif /* ALTSHELL */
    540 
    541 	/*
    542 	 * If the kernel is in secure mode, downgrade it to insecure mode.
    543 	 */
    544 	from_securitylevel = getsecuritylevel();
    545 	if (from_securitylevel > 0)
    546 		setsecuritylevel(0);
    547 
    548 	(void)sigemptyset(&sa.sa_mask);
    549 	sa.sa_flags = 0;
    550 	sa.sa_handler = SIG_IGN;
    551 	(void)sigaction(SIGTSTP, &sa, &satstp);
    552 	(void)sigaction(SIGHUP, &sa, &sahup);
    553 	if ((pid = fork()) == 0) {
    554 		/*
    555 		 * Start the single user session.
    556 		 */
    557 		setctty(_PATH_CONSOLE);
    558 
    559 #ifdef SECURE
    560 		/*
    561 		 * Check the root password.
    562 		 * We don't care if the console is 'on' by default;
    563 		 * it's the only tty that can be 'off' and 'secure'.
    564 		 */
    565 		typ = getttynam("console");
    566 		pp = getpwnam("root");
    567 		if (typ && (from_securitylevel >=2 || (typ->ty_status
    568 		    & TTY_SECURE) == 0) && pp && *pp->pw_passwd != '\0') {
    569 			(void)fprintf(stderr,
    570 			    "Enter root password, or ^D to go multi-user\n");
    571 			for (;;) {
    572 				clear = getpass("Password:");
    573 				if (clear == 0 || *clear == '\0')
    574 					_exit(0);
    575 				password = crypt(clear, pp->pw_passwd);
    576 				(void)memset(clear, 0, _PASSWORD_LEN);
    577 				if (strcmp(password, pp->pw_passwd) == 0)
    578 					break;
    579 				warning("single-user login failed\n");
    580 			}
    581 		}
    582 		endttyent();
    583 		endpwent();
    584 #endif /* SECURE */
    585 
    586 #ifdef ALTSHELL
    587 		(void)fprintf(stderr,
    588 		    "Enter pathname of shell or RETURN for %s: ", shell);
    589 		if (fgets(altshell, sizeof(altshell), stdin) == NULL) {
    590 			altshell[0] = '\0';
    591 		} else {
    592 			/* nuke \n */
    593 			char *p;
    594 
    595 			if ((p = strchr(altshell, '\n')) != NULL)
    596 				*p = '\0';
    597 		}
    598 
    599 		if (altshell[0])
    600 			shell = altshell;
    601 #endif /* ALTSHELL */
    602 
    603 		/*
    604 		 * Unblock signals.
    605 		 * We catch all the interesting ones,
    606 		 * and those are reset to SIG_DFL on exec.
    607 		 */
    608 		(void)sigemptyset(&mask);
    609 		(void)sigprocmask(SIG_SETMASK, &mask, NULL);
    610 
    611 		/*
    612 		 * Fire off a shell.
    613 		 * If the default one doesn't work, try the Bourne shell.
    614 		 */
    615 		argv[0] = "-sh";
    616 		argv[1] = 0;
    617 		setenv("PATH", _PATH_STDPATH, 1);
    618 #ifdef ALTSHELL
    619 		if (altshell[0])
    620 			argv[0] = altshell;
    621 		(void)execv(shell, argv);
    622 		emergency("can't exec %s for single user: %m", shell);
    623 		argv[0] = "-sh";
    624 #endif /* ALTSHELL */
    625 		(void)execv(_PATH_BSHELL, argv);
    626 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
    627 		(void)sleep(STALL_TIMEOUT);
    628 		_exit(1);
    629 	}
    630 
    631 	if (pid == -1) {
    632 		/*
    633 		 * We are seriously hosed.  Do our best.
    634 		 */
    635 		emergency("can't fork single-user shell, trying again");
    636 		while (waitpid(-1, NULL, WNOHANG) > 0)
    637 			continue;
    638 		(void)sigaction(SIGTSTP, &satstp, NULL);
    639 		(void)sigaction(SIGHUP, &sahup, NULL);
    640 		return (state_func_t) single_user;
    641 	}
    642 
    643 	requested_transition = 0;
    644 	do {
    645 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    646 			collect_child(wpid, status);
    647 		if (wpid == -1) {
    648 			if (errno == EINTR)
    649 				continue;
    650 			warning("wait for single-user shell failed: %m; "
    651 			    "restarting");
    652 			return (state_func_t)single_user;
    653 		}
    654 		if (wpid == pid && WIFSTOPPED(status)) {
    655 			warning("init: shell stopped, restarting\n");
    656 			kill(pid, SIGCONT);
    657 			wpid = -1;
    658 		}
    659 	} while (wpid != pid && !requested_transition);
    660 
    661 	if (requested_transition) {
    662 		(void)sigaction(SIGTSTP, &satstp, NULL);
    663 		(void)sigaction(SIGHUP, &sahup, NULL);
    664 		return (state_func_t)requested_transition;
    665 	}
    666 
    667 	if (WIFSIGNALED(status)) {
    668 		if (WTERMSIG(status) == SIGKILL) {
    669 			/* executed /sbin/reboot; wait for the end quietly */
    670 			sigset_t s;
    671 
    672 			(void)sigfillset(&s);
    673 			for (;;)
    674 				(void)sigsuspend(&s);
    675 		} else {
    676 			warning("single user shell terminated, restarting");
    677 			(void)sigaction(SIGTSTP, &satstp, NULL);
    678 			(void)sigaction(SIGHUP, &sahup, NULL);
    679 			return (state_func_t) single_user;
    680 		}
    681 	}
    682 
    683 	runcom_mode = FASTBOOT;
    684 	(void)sigaction(SIGTSTP, &satstp, NULL);
    685 	(void)sigaction(SIGHUP, &sahup, NULL);
    686 #ifndef LETS_GET_SMALL
    687 	return (state_func_t) runcom;
    688 #else /* LETS_GET_SMALL */
    689 	return (state_func_t) single_user;
    690 #endif /* LETS_GET_SMALL */
    691 }
    692 
    693 #ifndef LETS_GET_SMALL
    694 /*
    695  * Run the system startup script.
    696  */
    697 state_func_t
    698 runcom(void)
    699 {
    700 	pid_t pid, wpid;
    701 	int status;
    702 	char *argv[4];
    703 	struct sigaction sa;
    704 
    705 	switch ((pid = fork())) {
    706 	case 0:
    707 		(void)sigemptyset(&sa.sa_mask);
    708 		sa.sa_flags = 0;
    709 		sa.sa_handler = SIG_IGN;
    710 		(void)sigaction(SIGTSTP, &sa, NULL);
    711 		(void)sigaction(SIGHUP, &sa, NULL);
    712 
    713 		setctty(_PATH_CONSOLE);
    714 
    715 		argv[0] = "sh";
    716 		argv[1] = _PATH_RUNCOM;
    717 		argv[2] = runcom_mode == AUTOBOOT ? "autoboot" : 0;
    718 		argv[3] = 0;
    719 
    720 		(void)sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
    721 
    722 		(void)execv(_PATH_BSHELL, argv);
    723 		stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
    724 		_exit(1);	/* force single user mode */
    725 		/*NOTREACHED*/
    726 	case -1:
    727 		emergency("can't fork for %s on %s: %m", _PATH_BSHELL,
    728 		    _PATH_RUNCOM);
    729 		while (waitpid(-1, NULL, WNOHANG) > 0)
    730 			continue;
    731 		(void)sleep(STALL_TIMEOUT);
    732 		return (state_func_t)single_user;
    733 	default:
    734 		break;
    735 	}
    736 
    737 	/*
    738 	 * Copied from single_user().  This is a bit paranoid.
    739 	 */
    740 	do {
    741 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    742 			collect_child(wpid, status);
    743 		if (wpid == -1) {
    744 			if (errno == EINTR)
    745 				continue;
    746 			warning("wait for %s on %s failed: %m; going to "
    747 			    "single user mode", _PATH_BSHELL, _PATH_RUNCOM);
    748 			return (state_func_t)single_user;
    749 		}
    750 		if (wpid == pid && WIFSTOPPED(status)) {
    751 			warning("init: %s on %s stopped, restarting\n",
    752 			    _PATH_BSHELL, _PATH_RUNCOM);
    753 			(void)kill(pid, SIGCONT);
    754 			wpid = -1;
    755 		}
    756 	} while (wpid != pid);
    757 
    758 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
    759 	    requested_transition == catatonia) {
    760 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
    761 		sigset_t s;
    762 
    763 		(void)sigfillset(&s);
    764 		for (;;)
    765 			(void)sigsuspend(&s);
    766 	}
    767 
    768 	if (!WIFEXITED(status)) {
    769 		warning("%s on %s terminated abnormally, going to "
    770 		    "single user mode", _PATH_BSHELL, _PATH_RUNCOM);
    771 		return (state_func_t)single_user;
    772 	}
    773 
    774 	if (WEXITSTATUS(status))
    775 		return (state_func_t)single_user;
    776 
    777 	runcom_mode = AUTOBOOT;		/* the default */
    778 	/* NB: should send a message to the session logger to avoid blocking. */
    779 #ifdef SUPPORT_UTMPX
    780 	logwtmpx("~", "reboot", "", 0, INIT_PROCESS);
    781 #endif
    782 #ifdef SUPPORT_UTMP
    783 	logwtmp("~", "reboot", "");
    784 #endif
    785 	return (state_func_t) read_ttys;
    786 }
    787 
    788 /*
    789  * Open the session database.
    790  *
    791  * NB: We could pass in the size here; is it necessary?
    792  */
    793 int
    794 start_session_db(void)
    795 {
    796 
    797 	if (session_db && (*session_db->close)(session_db))
    798 		emergency("session database close: %m");
    799 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
    800 		emergency("session database open: %m");
    801 		return (1);
    802 	}
    803 	return (0);
    804 
    805 }
    806 
    807 /*
    808  * Add a new login session.
    809  */
    810 void
    811 add_session(session_t *sp)
    812 {
    813 	DBT key;
    814 	DBT data;
    815 
    816 	if (session_db == NULL)
    817 		return;
    818 
    819 	key.data = &sp->se_process;
    820 	key.size = sizeof sp->se_process;
    821 	data.data = &sp;
    822 	data.size = sizeof sp;
    823 
    824 	if ((*session_db->put)(session_db, &key, &data, 0))
    825 		emergency("insert %d: %m", sp->se_process);
    826 }
    827 
    828 /*
    829  * Delete an old login session.
    830  */
    831 void
    832 del_session(session_t *sp)
    833 {
    834 	DBT key;
    835 
    836 	key.data = &sp->se_process;
    837 	key.size = sizeof sp->se_process;
    838 
    839 	if ((*session_db->del)(session_db, &key, 0))
    840 		emergency("delete %d: %m", sp->se_process);
    841 }
    842 
    843 /*
    844  * Look up a login session by pid.
    845  */
    846 session_t *
    847 find_session(pid_t pid)
    848 {
    849 	DBT key;
    850 	DBT data;
    851 	session_t *ret;
    852 
    853 	if (session_db == NULL)
    854 		return NULL;
    855 
    856 	key.data = &pid;
    857 	key.size = sizeof pid;
    858 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
    859 		return 0;
    860 	(void)memmove(&ret, data.data, sizeof(ret));
    861 	return ret;
    862 }
    863 
    864 /*
    865  * Construct an argument vector from a command line.
    866  */
    867 char **
    868 construct_argv(char *command)
    869 {
    870 	int argc = 0;
    871 	char **argv = malloc(((strlen(command) + 1) / 2 + 1) * sizeof (char *));
    872 	static const char separators[] = " \t";
    873 
    874 	if ((argv[argc++] = strtok(command, separators)) == 0)
    875 		return (NULL);
    876 	while ((argv[argc++] = strtok(NULL, separators)) != NULL)
    877 		continue;
    878 	return (argv);
    879 }
    880 
    881 /*
    882  * Deallocate a session descriptor.
    883  */
    884 void
    885 free_session(session_t *sp)
    886 {
    887 
    888 	free(sp->se_device);
    889 	if (sp->se_getty) {
    890 		free(sp->se_getty);
    891 		free(sp->se_getty_argv);
    892 	}
    893 	if (sp->se_window) {
    894 		free(sp->se_window);
    895 		free(sp->se_window_argv);
    896 	}
    897 	free(sp);
    898 }
    899 
    900 /*
    901  * Allocate a new session descriptor.
    902  */
    903 session_t *
    904 new_session(session_t *sprev, int session_index, struct ttyent *typ)
    905 {
    906 	session_t *sp;
    907 
    908 	if ((typ->ty_status & TTY_ON) == 0 || typ->ty_name == NULL ||
    909 	    typ->ty_getty == NULL)
    910 		return (NULL);
    911 
    912 	sp = malloc(sizeof (session_t));
    913 	if (sp == NULL)
    914 		return NULL;
    915 	memset(sp, 0, sizeof *sp);
    916 
    917 	sp->se_flags = SE_PRESENT;
    918 	sp->se_index = session_index;
    919 
    920 	sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
    921 	(void)sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
    922 
    923 	if (setupargv(sp, typ) == 0) {
    924 		free_session(sp);
    925 		return (NULL);
    926 	}
    927 
    928 	sp->se_next = NULL;
    929 	if (sprev == NULL) {
    930 		sessions = sp;
    931 		sp->se_prev = NULL;
    932 	} else {
    933 		sprev->se_next = sp;
    934 		sp->se_prev = sprev;
    935 	}
    936 
    937 	return (sp);
    938 }
    939 
    940 /*
    941  * Calculate getty and if useful window argv vectors.
    942  */
    943 int
    944 setupargv(session_t *sp, struct ttyent *typ)
    945 {
    946 
    947 	if (sp->se_getty) {
    948 		free(sp->se_getty);
    949 		free(sp->se_getty_argv);
    950 	}
    951 	sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
    952 	(void) sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
    953 	sp->se_getty_argv = construct_argv(sp->se_getty);
    954 	if (sp->se_getty_argv == NULL) {
    955 		warning("can't parse getty for port %s", sp->se_device);
    956 		free(sp->se_getty);
    957 		sp->se_getty = NULL;
    958 		return (0);
    959 	}
    960 	if (typ->ty_window) {
    961 		if (sp->se_window)
    962 			free(sp->se_window);
    963 		sp->se_window = strdup(typ->ty_window);
    964 		sp->se_window_argv = construct_argv(sp->se_window);
    965 		if (sp->se_window_argv == NULL) {
    966 			warning("can't parse window for port %s",
    967 			    sp->se_device);
    968 			free(sp->se_window);
    969 			sp->se_window = NULL;
    970 			return (0);
    971 		}
    972 	}
    973 	return (1);
    974 }
    975 
    976 /*
    977  * Walk the list of ttys and create sessions for each active line.
    978  */
    979 state_func_t
    980 read_ttys(void)
    981 {
    982 	int session_index = 0;
    983 	session_t *sp, *snext;
    984 	struct ttyent *typ;
    985 
    986 	/*
    987 	 * Destroy any previous session state.
    988 	 * There shouldn't be any, but just in case...
    989 	 */
    990 	for (sp = sessions; sp; sp = snext) {
    991 		if (sp->se_process)
    992 			clear_session_logs(sp, 0);
    993 		snext = sp->se_next;
    994 		free_session(sp);
    995 	}
    996 	sessions = NULL;
    997 	if (start_session_db())
    998 		return (state_func_t)single_user;
    999 
   1000 	/*
   1001 	 * Allocate a session entry for each active port.
   1002 	 * Note that sp starts at 0.
   1003 	 */
   1004 	while ((typ = getttyent()) != NULL)
   1005 		if ((snext = new_session(sp, ++session_index, typ)) != NULL)
   1006 			sp = snext;
   1007 
   1008 	endttyent();
   1009 
   1010 	return (state_func_t)multi_user;
   1011 }
   1012 
   1013 /*
   1014  * Start a window system running.
   1015  */
   1016 void
   1017 start_window_system(session_t *sp)
   1018 {
   1019 	pid_t pid;
   1020 	sigset_t mask;
   1021 
   1022 	if ((pid = fork()) == -1) {
   1023 		emergency("can't fork for window system on port %s: %m",
   1024 		    sp->se_device);
   1025 		/* hope that getty fails and we can try again */
   1026 		return;
   1027 	}
   1028 
   1029 	if (pid)
   1030 		return;
   1031 
   1032 	sigemptyset(&mask);
   1033 	sigprocmask(SIG_SETMASK, &mask, NULL);
   1034 
   1035 	if (setsid() < 0)
   1036 		emergency("setsid failed (window) %m");
   1037 
   1038 	(void)execv(sp->se_window_argv[0], sp->se_window_argv);
   1039 	stall("can't exec window system '%s' for port %s: %m",
   1040 	    sp->se_window_argv[0], sp->se_device);
   1041 	_exit(1);
   1042 }
   1043 
   1044 /*
   1045  * Start a login session running.
   1046  */
   1047 pid_t
   1048 start_getty(session_t *sp)
   1049 {
   1050 	pid_t pid;
   1051 	sigset_t mask;
   1052 	time_t current_time = time(NULL);
   1053 
   1054 	/*
   1055 	 * fork(), not vfork() -- we can't afford to block.
   1056 	 */
   1057 	if ((pid = fork()) == -1) {
   1058 		emergency("can't fork for getty on port %s: %m", sp->se_device);
   1059 		return -1;
   1060 	}
   1061 
   1062 	if (pid)
   1063 		return pid;
   1064 
   1065 	if (current_time > sp->se_started &&
   1066 	    current_time - sp->se_started < GETTY_SPACING) {
   1067 		warning("getty repeating too quickly on port %s, sleeping",
   1068 		    sp->se_device);
   1069 		(void)sleep(GETTY_SLEEP);
   1070 	}
   1071 
   1072 	if (sp->se_window) {
   1073 		start_window_system(sp);
   1074 		(void)sleep(WINDOW_WAIT);
   1075 	}
   1076 
   1077 	(void)sigemptyset(&mask);
   1078 	(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
   1079 
   1080 	(void)execv(sp->se_getty_argv[0], sp->se_getty_argv);
   1081 	stall("can't exec getty '%s' for port %s: %m",
   1082 	    sp->se_getty_argv[0], sp->se_device);
   1083 	_exit(1);
   1084 	/*NOTREACHED*/
   1085 }
   1086 #endif /* LETS_GET_SMALL */
   1087 
   1088 /*
   1089  * Collect exit status for a child.
   1090  * If an exiting login, start a new login running.
   1091  */
   1092 void
   1093 collect_child(pid_t pid, int status)
   1094 {
   1095 #ifndef LETS_GET_SMALL
   1096 	session_t *sp, *sprev, *snext;
   1097 
   1098 	if (! sessions)
   1099 		return;
   1100 
   1101 	if ((sp = find_session(pid)) == NULL)
   1102 		return;
   1103 
   1104 	clear_session_logs(sp, status);
   1105 	del_session(sp);
   1106 	sp->se_process = 0;
   1107 
   1108 	if (sp->se_flags & SE_SHUTDOWN) {
   1109 		if ((sprev = sp->se_prev) != NULL)
   1110 			sprev->se_next = sp->se_next;
   1111 		else
   1112 			sessions = sp->se_next;
   1113 		if ((snext = sp->se_next) != NULL)
   1114 			snext->se_prev = sp->se_prev;
   1115 		free_session(sp);
   1116 		return;
   1117 	}
   1118 
   1119 	if ((pid = start_getty(sp)) == -1) {
   1120 		/* serious trouble */
   1121 		requested_transition = clean_ttys;
   1122 		return;
   1123 	}
   1124 
   1125 	sp->se_process = pid;
   1126 	sp->se_started = time(NULL);
   1127 	add_session(sp);
   1128 #endif /* LETS_GET_SMALL */
   1129 }
   1130 
   1131 /*
   1132  * Catch a signal and request a state transition.
   1133  */
   1134 void
   1135 transition_handler(int sig)
   1136 {
   1137 
   1138 	switch (sig) {
   1139 #ifndef LETS_GET_SMALL
   1140 	case SIGHUP:
   1141 		requested_transition = clean_ttys;
   1142 		break;
   1143 	case SIGTERM:
   1144 		requested_transition = death;
   1145 		break;
   1146 	case SIGTSTP:
   1147 		requested_transition = catatonia;
   1148 		break;
   1149 #endif /* LETS_GET_SMALL */
   1150 	default:
   1151 		requested_transition = 0;
   1152 		break;
   1153 	}
   1154 }
   1155 
   1156 #ifndef LETS_GET_SMALL
   1157 /*
   1158  * Take the system multiuser.
   1159  */
   1160 state_func_t
   1161 multi_user(void)
   1162 {
   1163 	pid_t pid;
   1164 	int status;
   1165 	session_t *sp;
   1166 
   1167 	requested_transition = 0;
   1168 
   1169 	/*
   1170 	 * If the administrator has not set the security level to -1
   1171 	 * to indicate that the kernel should not run multiuser in secure
   1172 	 * mode, and the run script has not set a higher level of security
   1173 	 * than level 1, then put the kernel into secure mode.
   1174 	 */
   1175 	if (getsecuritylevel() == 0)
   1176 		setsecuritylevel(1);
   1177 
   1178 	for (sp = sessions; sp; sp = sp->se_next) {
   1179 		if (sp->se_process)
   1180 			continue;
   1181 		if ((pid = start_getty(sp)) == -1) {
   1182 			/* serious trouble */
   1183 			requested_transition = clean_ttys;
   1184 			break;
   1185 		}
   1186 		sp->se_process = pid;
   1187 		sp->se_started = time(NULL);
   1188 		add_session(sp);
   1189 	}
   1190 
   1191 	while (!requested_transition)
   1192 		if ((pid = waitpid(-1, &status, 0)) != -1)
   1193 			collect_child(pid, status);
   1194 
   1195 	return (state_func_t)requested_transition;
   1196 }
   1197 
   1198 /*
   1199  * This is an n-squared algorithm.  We hope it isn't run often...
   1200  */
   1201 state_func_t
   1202 clean_ttys(void)
   1203 {
   1204 	session_t *sp, *sprev;
   1205 	struct ttyent *typ;
   1206 	int session_index = 0;
   1207 	int devlen;
   1208 
   1209 	for (sp = sessions; sp; sp = sp->se_next)
   1210 		sp->se_flags &= ~SE_PRESENT;
   1211 
   1212 	devlen = sizeof(_PATH_DEV) - 1;
   1213 	while ((typ = getttyent()) != NULL) {
   1214 		++session_index;
   1215 
   1216 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
   1217 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
   1218 				break;
   1219 
   1220 		if (sp) {
   1221 			sp->se_flags |= SE_PRESENT;
   1222 			if (sp->se_index != session_index) {
   1223 				warning("port %s changed utmp index from "
   1224 				    "%d to %d", sp->se_device, sp->se_index,
   1225 				    session_index);
   1226 				sp->se_index = session_index;
   1227 			}
   1228 			if ((typ->ty_status & TTY_ON) == 0 ||
   1229 			    typ->ty_getty == 0) {
   1230 				sp->se_flags |= SE_SHUTDOWN;
   1231 				if (sp->se_process != 0)
   1232 					(void)kill(sp->se_process, SIGHUP);
   1233 				continue;
   1234 			}
   1235 			sp->se_flags &= ~SE_SHUTDOWN;
   1236 			if (setupargv(sp, typ) == 0) {
   1237 				warning("can't parse getty for port %s",
   1238 				    sp->se_device);
   1239 				sp->se_flags |= SE_SHUTDOWN;
   1240 				if (sp->se_process != 0)
   1241 					(void)kill(sp->se_process, SIGHUP);
   1242 			}
   1243 			continue;
   1244 		}
   1245 
   1246 		new_session(sprev, session_index, typ);
   1247 	}
   1248 
   1249 	endttyent();
   1250 
   1251 	for (sp = sessions; sp; sp = sp->se_next)
   1252 		if ((sp->se_flags & SE_PRESENT) == 0) {
   1253 			sp->se_flags |= SE_SHUTDOWN;
   1254 			if (sp->se_process != 0)
   1255 				(void)kill(sp->se_process, SIGHUP);
   1256 		}
   1257 
   1258 	return (state_func_t)multi_user;
   1259 }
   1260 
   1261 /*
   1262  * Block further logins.
   1263  */
   1264 state_func_t
   1265 catatonia(void)
   1266 {
   1267 	session_t *sp;
   1268 
   1269 	for (sp = sessions; sp; sp = sp->se_next)
   1270 		sp->se_flags |= SE_SHUTDOWN;
   1271 
   1272 	return (state_func_t)multi_user;
   1273 }
   1274 #endif /* LETS_GET_SMALL */
   1275 
   1276 /*
   1277  * Note SIGALRM.
   1278  */
   1279 void
   1280 /*ARGSUSED*/
   1281 alrm_handler(int sig)
   1282 {
   1283 
   1284 	clang = 1;
   1285 }
   1286 
   1287 #ifndef LETS_GET_SMALL
   1288 /*
   1289  * Bring the system down to single user.
   1290  */
   1291 state_func_t
   1292 death(void)
   1293 {
   1294 	session_t *sp;
   1295 	int i, status;
   1296 	pid_t pid;
   1297 	static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL };
   1298 
   1299 	for (sp = sessions; sp; sp = sp->se_next)
   1300 		sp->se_flags |= SE_SHUTDOWN;
   1301 
   1302 	/* NB: should send a message to the session logger to avoid blocking. */
   1303 #ifdef SUPPORT_UTMPX
   1304 	logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
   1305 #endif
   1306 #ifdef SUPPORT_UTMP
   1307 	logwtmp("~", "shutdown", "");
   1308 #endif
   1309 
   1310 	for (i = 0; i < 3; ++i) {
   1311 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
   1312 			return (state_func_t)single_user;
   1313 
   1314 		clang = 0;
   1315 		alarm(DEATH_WATCH);
   1316 		do
   1317 			if ((pid = waitpid(-1, &status, 0)) != -1)
   1318 				collect_child(pid, status);
   1319 		while (clang == 0 && errno != ECHILD);
   1320 
   1321 		if (errno == ECHILD)
   1322 			return (state_func_t)single_user;
   1323 	}
   1324 
   1325 	warning("some processes would not die; ps axl advised");
   1326 
   1327 	return (state_func_t)single_user;
   1328 }
   1329 #endif /* LETS_GET_SMALL */
   1330 
   1331 #ifdef MFS_DEV_IF_NO_CONSOLE
   1332 
   1333 static int
   1334 mfs_dev(void)
   1335 {
   1336 	/*
   1337 	 * We cannot print errors so we bail out silently...
   1338 	 */
   1339 	int fd;
   1340 	struct stat st;
   1341 	pid_t pid;
   1342 	int status;
   1343 	void *makedev = 0;
   1344 	void *makedev_local = 0;
   1345 	size_t makedev_size;
   1346 	size_t makedev_local_size;
   1347 	dev_t dev;
   1348 #ifdef CPU_CONSDEV
   1349 	static int name[2] = { CTL_MACHDEP, CPU_CONSDEV };
   1350 	size_t olen;
   1351 #endif
   1352 
   1353 
   1354 	/* If we have /dev/console, assume all is OK  */
   1355 	if (access(_PATH_CONSOLE, F_OK) != -1)
   1356 		return(0);
   1357 
   1358 	/* Grab the contents of MAKEDEV */
   1359 	if ((fd = open("/dev/MAKEDEV", O_RDONLY)) != -1) {
   1360 		if (fstat(fd, &st) != -1 && (makedev = mmap(0,
   1361 		    (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
   1362 		    (off_t)0)) != MAP_FAILED)
   1363 			makedev_size = (size_t)st.st_size;
   1364 		else
   1365 			makedev = 0;
   1366 		(void)close(fd);
   1367 	}
   1368 
   1369 	/* Grab the contents of MAKEDEV.local */
   1370 	if ((fd = open("/dev/MAKEDEV.local", O_RDONLY)) != -1) {
   1371 		if (fstat(fd, &st) != -1 && (makedev_local = mmap(0,
   1372 		    (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
   1373 		    (off_t)0)) != MAP_FAILED)
   1374 			makedev_local_size = (size_t)st.st_size;
   1375 		else
   1376 			makedev_local = 0;
   1377 		(void)close(fd);
   1378 	}
   1379 
   1380 	/* Mount an mfs over /dev so we can create devices */
   1381 	switch ((pid = fork())) {
   1382 	case 0:
   1383 		(void)execl("/sbin/mount_mfs", "mount_mfs", "-i", "192",
   1384 		    "-s", "768", "-b", "4096", "-f", "512", "swap", "/dev",
   1385 		    NULL);
   1386 		_exit(1);
   1387 		/*NOTREACHED*/
   1388 
   1389 	case -1:
   1390 		return(-1);
   1391 
   1392 	default:
   1393 		if (waitpid(pid, &status, 0) == -1)
   1394 			return(-1);
   1395 		if (status != 0)
   1396 			return(-1);
   1397 		break;
   1398 	}
   1399 
   1400 #ifdef CPU_CONSDEV
   1401 	olen = sizeof(dev);
   1402 	if (sysctl(name, sizeof(name) / sizeof(name[0]), &dev, &olen,
   1403 	    NULL, 0) == -1)
   1404 #endif
   1405 		dev = makedev(0, 0);
   1406 
   1407 	/* Make a console for us, so we can see things happening */
   1408 	if (mknod(_PATH_CONSOLE, 0666 | S_IFCHR, dev) == -1)
   1409 		return(-1);
   1410 
   1411 	(void)freopen(_PATH_CONSOLE, "a", stderr);
   1412 
   1413 	warnx("Creating mfs /dev");
   1414 
   1415 	/* Create a MAKEDEV script in the mfs /dev */
   1416 	if (makedev && (fd = open("/dev/MAKEDEV", O_WRONLY|O_CREAT|O_TRUNC,
   1417 	    0755)) != -1) {
   1418 		(void)write(fd, makedev, makedev_size);
   1419 		(void)munmap(makedev, makedev_size);
   1420 		(void)close(fd);
   1421 	}
   1422 
   1423 	/* Create a MAKEDEV.local script in the mfs /dev */
   1424 	if (makedev_local && (fd = open("/dev/MAKEDEV.local",
   1425 	    O_WRONLY|O_CREAT|O_TRUNC, 0755)) != -1) {
   1426 		(void)write(fd, makedev_local, makedev_local_size);
   1427 		(void)munmap(makedev_local, makedev_local_size);
   1428 		(void)close(fd);
   1429 	}
   1430 
   1431 	/* Run the makedev script to create devices */
   1432 	switch ((pid = fork())) {
   1433 	case 0:
   1434 		if (chdir("/dev") == -1)
   1435 			goto fail;
   1436 		(void)execl("/bin/sh", "sh", "./MAKEDEV", "init", NULL);
   1437 		goto fail;
   1438 
   1439 	case -1:
   1440 		goto fail;
   1441 
   1442 	default:
   1443 		if (waitpid(pid, &status, 0) == -1)
   1444 			goto fail;
   1445 		if (status != 0) {
   1446 			errno = EINVAL;
   1447 			goto fail;
   1448 		}
   1449 		break;
   1450 	}
   1451 	return(0);
   1452 fail:
   1453 	warn("Unable to run MAKEDEV");
   1454 	return(-1);
   1455 }
   1456 #endif
   1457