Home | History | Annotate | Line # | Download | only in init
init.c revision 1.53
      1 /*	$NetBSD: init.c,v 1.53 2002/10/04 13:19:05 simonb 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.53 2002/10/04 13:19:05 simonb 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 	for (;;)
    472 		s = (state_t)(*s)();
    473 }
    474 
    475 /*
    476  * Close out the accounting files for a login session.
    477  * NB: should send a message to the session logger to avoid blocking.
    478  */
    479 void
    480 clear_session_logs(session_t *sp, int status)
    481 {
    482 	char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
    483 #ifdef SUPPORT_UTMPX
    484 	if (logoutx(line, status, DEAD_PROCESS))
    485 		logwtmpx(line, "", "", status, DEAD_PROCESS);
    486 #endif
    487 #ifdef SUPPORT_UTMP
    488 	if (logout(line))
    489 		logwtmp(line, "", "");
    490 #endif
    491 }
    492 
    493 /*
    494  * Start a session and allocate a controlling terminal.
    495  * Only called by children of init after forking.
    496  */
    497 void
    498 setctty(const char *name)
    499 {
    500 	int fd;
    501 
    502 	(void) revoke(name);
    503 	sleep(2);			/* leave DTR low */
    504 	if ((fd = open(name, O_RDWR)) == -1) {
    505 		stall("can't open %s: %m", name);
    506 		_exit(1);
    507 	}
    508 	if (login_tty(fd) == -1) {
    509 		stall("can't get %s for controlling terminal: %m", name);
    510 		_exit(1);
    511 	}
    512 }
    513 
    514 /*
    515  * Bring the system up single user.
    516  */
    517 state_func_t
    518 single_user(void)
    519 {
    520 	pid_t pid, wpid;
    521 	int status;
    522 	int from_securitylevel;
    523 	sigset_t mask;
    524 #ifdef ALTSHELL
    525 	const char *shell = _PATH_ALTSHELL;
    526 #endif
    527 	char *argv[2];
    528 #ifdef SECURE
    529 	struct ttyent *typ;
    530 	struct passwd *pp;
    531 	char *clear, *password;
    532 #endif
    533 #ifdef ALTSHELL
    534 	char altshell[128];
    535 #endif /* ALTSHELL */
    536 
    537 	/*
    538 	 * If the kernel is in secure mode, downgrade it to insecure mode.
    539 	 */
    540 	from_securitylevel = getsecuritylevel();
    541 	if (from_securitylevel > 0)
    542 		setsecuritylevel(0);
    543 
    544 	if ((pid = fork()) == 0) {
    545 		/*
    546 		 * Start the single user session.
    547 		 */
    548 		setctty(_PATH_CONSOLE);
    549 
    550 #ifdef SECURE
    551 		/*
    552 		 * Check the root password.
    553 		 * We don't care if the console is 'on' by default;
    554 		 * it's the only tty that can be 'off' and 'secure'.
    555 		 */
    556 		typ = getttynam("console");
    557 		pp = getpwnam("root");
    558 		if (typ && (from_securitylevel >=2 || (typ->ty_status
    559 		    & TTY_SECURE) == 0) && pp && *pp->pw_passwd != '\0') {
    560 			(void)fprintf(stderr,
    561 			    "Enter root password, or ^D to go multi-user\n");
    562 			for (;;) {
    563 				clear = getpass("Password:");
    564 				if (clear == 0 || *clear == '\0')
    565 					_exit(0);
    566 				password = crypt(clear, pp->pw_passwd);
    567 				(void)memset(clear, 0, _PASSWORD_LEN);
    568 				if (strcmp(password, pp->pw_passwd) == 0)
    569 					break;
    570 				warning("single-user login failed\n");
    571 			}
    572 		}
    573 		endttyent();
    574 		endpwent();
    575 #endif /* SECURE */
    576 
    577 #ifdef ALTSHELL
    578 		(void)fprintf(stderr,
    579 		    "Enter pathname of shell or RETURN for %s: ", shell);
    580 		if (fgets(altshell, sizeof(altshell), stdin) == NULL) {
    581 			altshell[0] = '\0';
    582 		} else {
    583 			/* nuke \n */
    584 			char *p;
    585 
    586 			if ((p = strchr(altshell, '\n')) != NULL)
    587 				*p = '\0';
    588 		}
    589 
    590 		if (altshell[0])
    591 			shell = altshell;
    592 #endif /* ALTSHELL */
    593 
    594 		/*
    595 		 * Unblock signals.
    596 		 * We catch all the interesting ones,
    597 		 * and those are reset to SIG_DFL on exec.
    598 		 */
    599 		(void)sigemptyset(&mask);
    600 		(void)sigprocmask(SIG_SETMASK, &mask, NULL);
    601 
    602 		/*
    603 		 * Fire off a shell.
    604 		 * If the default one doesn't work, try the Bourne shell.
    605 		 */
    606 		argv[0] = "-sh";
    607 		argv[1] = 0;
    608 		setenv("PATH", _PATH_STDPATH, 1);
    609 #ifdef ALTSHELL
    610 		if (altshell[0])
    611 			argv[0] = altshell;
    612 		(void)execv(shell, argv);
    613 		emergency("can't exec %s for single user: %m", shell);
    614 		argv[0] = "-sh";
    615 #endif /* ALTSHELL */
    616 		(void)execv(_PATH_BSHELL, argv);
    617 		emergency("can't exec %s for single user: %m", _PATH_BSHELL);
    618 		(void)sleep(STALL_TIMEOUT);
    619 		_exit(1);
    620 	}
    621 
    622 	if (pid == -1) {
    623 		/*
    624 		 * We are seriously hosed.  Do our best.
    625 		 */
    626 		emergency("can't fork single-user shell, trying again");
    627 		while (waitpid(-1, NULL, WNOHANG) > 0)
    628 			continue;
    629 		return (state_func_t) single_user;
    630 	}
    631 
    632 	requested_transition = 0;
    633 	do {
    634 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    635 			collect_child(wpid, status);
    636 		if (wpid == -1) {
    637 			if (errno == EINTR)
    638 				continue;
    639 			warning("wait for single-user shell failed: %m; "
    640 			    "restarting");
    641 			return (state_func_t)single_user;
    642 		}
    643 		if (wpid == pid && WIFSTOPPED(status)) {
    644 			warning("init: shell stopped, restarting\n");
    645 			kill(pid, SIGCONT);
    646 			wpid = -1;
    647 		}
    648 	} while (wpid != pid && !requested_transition);
    649 
    650 	if (requested_transition)
    651 		return (state_func_t)requested_transition;
    652 
    653 	if (WIFSIGNALED(status)) {
    654 		if (WTERMSIG(status) == SIGKILL) {
    655 			/* executed /sbin/reboot; wait for the end quietly */
    656 			sigset_t s;
    657 
    658 			(void)sigfillset(&s);
    659 			for (;;)
    660 				(void)sigsuspend(&s);
    661 		} else {
    662 			warning("single user shell terminated, restarting");
    663 			return (state_func_t) single_user;
    664 		}
    665 	}
    666 
    667 	runcom_mode = FASTBOOT;
    668 #ifndef LETS_GET_SMALL
    669 	return (state_func_t) runcom;
    670 #else /* LETS_GET_SMALL */
    671 	return (state_func_t) single_user;
    672 #endif /* LETS_GET_SMALL */
    673 }
    674 
    675 #ifndef LETS_GET_SMALL
    676 /*
    677  * Run the system startup script.
    678  */
    679 state_func_t
    680 runcom(void)
    681 {
    682 	pid_t pid, wpid;
    683 	int status;
    684 	char *argv[4];
    685 	struct sigaction sa;
    686 
    687 	switch ((pid = fork())) {
    688 	case 0:
    689 		(void)sigemptyset(&sa.sa_mask);
    690 		sa.sa_flags = 0;
    691 		sa.sa_handler = SIG_IGN;
    692 		(void)sigaction(SIGTSTP, &sa, NULL);
    693 		(void)sigaction(SIGHUP, &sa, NULL);
    694 
    695 		setctty(_PATH_CONSOLE);
    696 
    697 		argv[0] = "sh";
    698 		argv[1] = _PATH_RUNCOM;
    699 		argv[2] = runcom_mode == AUTOBOOT ? "autoboot" : 0;
    700 		argv[3] = 0;
    701 
    702 		(void)sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
    703 
    704 		(void)execv(_PATH_BSHELL, argv);
    705 		stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM);
    706 		_exit(1);	/* force single user mode */
    707 		/*NOTREACHED*/
    708 	case -1:
    709 		emergency("can't fork for %s on %s: %m", _PATH_BSHELL,
    710 		    _PATH_RUNCOM);
    711 		while (waitpid(-1, NULL, WNOHANG) > 0)
    712 			continue;
    713 		(void)sleep(STALL_TIMEOUT);
    714 		return (state_func_t)single_user;
    715 	default:
    716 		break;
    717 	}
    718 
    719 	/*
    720 	 * Copied from single_user().  This is a bit paranoid.
    721 	 */
    722 	do {
    723 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    724 			collect_child(wpid, status);
    725 		if (wpid == -1) {
    726 			if (errno == EINTR)
    727 				continue;
    728 			warning("wait for %s on %s failed: %m; going to "
    729 			    "single user mode", _PATH_BSHELL, _PATH_RUNCOM);
    730 			return (state_func_t)single_user;
    731 		}
    732 		if (wpid == pid && WIFSTOPPED(status)) {
    733 			warning("init: %s on %s stopped, restarting\n",
    734 			    _PATH_BSHELL, _PATH_RUNCOM);
    735 			(void)kill(pid, SIGCONT);
    736 			wpid = -1;
    737 		}
    738 	} while (wpid != pid);
    739 
    740 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
    741 	    requested_transition == catatonia) {
    742 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
    743 		sigset_t s;
    744 
    745 		(void)sigfillset(&s);
    746 		for (;;)
    747 			(void)sigsuspend(&s);
    748 	}
    749 
    750 	if (!WIFEXITED(status)) {
    751 		warning("%s on %s terminated abnormally, going to "
    752 		    "single user mode", _PATH_BSHELL, _PATH_RUNCOM);
    753 		return (state_func_t)single_user;
    754 	}
    755 
    756 	if (WEXITSTATUS(status))
    757 		return (state_func_t)single_user;
    758 
    759 	runcom_mode = AUTOBOOT;		/* the default */
    760 	/* NB: should send a message to the session logger to avoid blocking. */
    761 #ifdef SUPPORT_UTMPX
    762 	logwtmpx("~", "reboot", "", 0, INIT_PROCESS);
    763 #endif
    764 #ifdef SUPPORT_UTMP
    765 	logwtmp("~", "reboot", "");
    766 #endif
    767 	return (state_func_t) read_ttys;
    768 }
    769 
    770 /*
    771  * Open the session database.
    772  *
    773  * NB: We could pass in the size here; is it necessary?
    774  */
    775 int
    776 start_session_db(void)
    777 {
    778 
    779 	if (session_db && (*session_db->close)(session_db))
    780 		emergency("session database close: %m");
    781 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
    782 		emergency("session database open: %m");
    783 		return (1);
    784 	}
    785 	return (0);
    786 
    787 }
    788 
    789 /*
    790  * Add a new login session.
    791  */
    792 void
    793 add_session(session_t *sp)
    794 {
    795 	DBT key;
    796 	DBT data;
    797 
    798 	key.data = &sp->se_process;
    799 	key.size = sizeof sp->se_process;
    800 	data.data = &sp;
    801 	data.size = sizeof sp;
    802 
    803 	if ((*session_db->put)(session_db, &key, &data, 0))
    804 		emergency("insert %d: %m", sp->se_process);
    805 }
    806 
    807 /*
    808  * Delete an old login session.
    809  */
    810 void
    811 del_session(session_t *sp)
    812 {
    813 	DBT key;
    814 
    815 	key.data = &sp->se_process;
    816 	key.size = sizeof sp->se_process;
    817 
    818 	if ((*session_db->del)(session_db, &key, 0))
    819 		emergency("delete %d: %m", sp->se_process);
    820 }
    821 
    822 /*
    823  * Look up a login session by pid.
    824  */
    825 session_t *
    826 find_session(pid_t pid)
    827 {
    828 	DBT key;
    829 	DBT data;
    830 	session_t *ret;
    831 
    832 	key.data = &pid;
    833 	key.size = sizeof pid;
    834 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
    835 		return 0;
    836 	(void)memmove(&ret, data.data, sizeof(ret));
    837 	return ret;
    838 }
    839 
    840 /*
    841  * Construct an argument vector from a command line.
    842  */
    843 char **
    844 construct_argv(char *command)
    845 {
    846 	int argc = 0;
    847 	char **argv = malloc(((strlen(command) + 1) / 2 + 1) * sizeof (char *));
    848 	static const char separators[] = " \t";
    849 
    850 	if ((argv[argc++] = strtok(command, separators)) == 0)
    851 		return (NULL);
    852 	while ((argv[argc++] = strtok(NULL, separators)) != NULL)
    853 		continue;
    854 	return (argv);
    855 }
    856 
    857 /*
    858  * Deallocate a session descriptor.
    859  */
    860 void
    861 free_session(session_t *sp)
    862 {
    863 
    864 	free(sp->se_device);
    865 	if (sp->se_getty) {
    866 		free(sp->se_getty);
    867 		free(sp->se_getty_argv);
    868 	}
    869 	if (sp->se_window) {
    870 		free(sp->se_window);
    871 		free(sp->se_window_argv);
    872 	}
    873 	free(sp);
    874 }
    875 
    876 /*
    877  * Allocate a new session descriptor.
    878  */
    879 session_t *
    880 new_session(session_t *sprev, int session_index, struct ttyent *typ)
    881 {
    882 	session_t *sp;
    883 
    884 	if ((typ->ty_status & TTY_ON) == 0 || typ->ty_name == NULL ||
    885 	    typ->ty_getty == NULL)
    886 		return (NULL);
    887 
    888 	sp = malloc(sizeof (session_t));
    889 	memset(sp, 0, sizeof *sp);
    890 
    891 	sp->se_flags = SE_PRESENT;
    892 	sp->se_index = session_index;
    893 
    894 	sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name));
    895 	(void)sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
    896 
    897 	if (setupargv(sp, typ) == 0) {
    898 		free_session(sp);
    899 		return (NULL);
    900 	}
    901 
    902 	sp->se_next = NULL;
    903 	if (sprev == NULL) {
    904 		sessions = sp;
    905 		sp->se_prev = NULL;
    906 	} else {
    907 		sprev->se_next = sp;
    908 		sp->se_prev = sprev;
    909 	}
    910 
    911 	return (sp);
    912 }
    913 
    914 /*
    915  * Calculate getty and if useful window argv vectors.
    916  */
    917 int
    918 setupargv(session_t *sp, struct ttyent *typ)
    919 {
    920 
    921 	if (sp->se_getty) {
    922 		free(sp->se_getty);
    923 		free(sp->se_getty_argv);
    924 	}
    925 	sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2);
    926 	(void) sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
    927 	sp->se_getty_argv = construct_argv(sp->se_getty);
    928 	if (sp->se_getty_argv == NULL) {
    929 		warning("can't parse getty for port %s", sp->se_device);
    930 		free(sp->se_getty);
    931 		sp->se_getty = NULL;
    932 		return (0);
    933 	}
    934 	if (typ->ty_window) {
    935 		if (sp->se_window)
    936 			free(sp->se_window);
    937 		sp->se_window = strdup(typ->ty_window);
    938 		sp->se_window_argv = construct_argv(sp->se_window);
    939 		if (sp->se_window_argv == NULL) {
    940 			warning("can't parse window for port %s",
    941 			    sp->se_device);
    942 			free(sp->se_window);
    943 			sp->se_window = NULL;
    944 			return (0);
    945 		}
    946 	}
    947 	return (1);
    948 }
    949 
    950 /*
    951  * Walk the list of ttys and create sessions for each active line.
    952  */
    953 state_func_t
    954 read_ttys(void)
    955 {
    956 	int session_index = 0;
    957 	session_t *sp, *snext;
    958 	struct ttyent *typ;
    959 
    960 	/*
    961 	 * Destroy any previous session state.
    962 	 * There shouldn't be any, but just in case...
    963 	 */
    964 	for (sp = sessions; sp; sp = snext) {
    965 		if (sp->se_process)
    966 			clear_session_logs(sp, 0);
    967 		snext = sp->se_next;
    968 		free_session(sp);
    969 	}
    970 	sessions = NULL;
    971 	if (start_session_db())
    972 		return (state_func_t)single_user;
    973 
    974 	/*
    975 	 * Allocate a session entry for each active port.
    976 	 * Note that sp starts at 0.
    977 	 */
    978 	while ((typ = getttyent()) != NULL)
    979 		if ((snext = new_session(sp, ++session_index, typ)) != NULL)
    980 			sp = snext;
    981 
    982 	endttyent();
    983 
    984 	return (state_func_t)multi_user;
    985 }
    986 
    987 /*
    988  * Start a window system running.
    989  */
    990 void
    991 start_window_system(session_t *sp)
    992 {
    993 	pid_t pid;
    994 	sigset_t mask;
    995 
    996 	if ((pid = fork()) == -1) {
    997 		emergency("can't fork for window system on port %s: %m",
    998 		    sp->se_device);
    999 		/* hope that getty fails and we can try again */
   1000 		return;
   1001 	}
   1002 
   1003 	if (pid)
   1004 		return;
   1005 
   1006 	sigemptyset(&mask);
   1007 	sigprocmask(SIG_SETMASK, &mask, NULL);
   1008 
   1009 	if (setsid() < 0)
   1010 		emergency("setsid failed (window) %m");
   1011 
   1012 	(void)execv(sp->se_window_argv[0], sp->se_window_argv);
   1013 	stall("can't exec window system '%s' for port %s: %m",
   1014 	    sp->se_window_argv[0], sp->se_device);
   1015 	_exit(1);
   1016 }
   1017 
   1018 /*
   1019  * Start a login session running.
   1020  */
   1021 pid_t
   1022 start_getty(session_t *sp)
   1023 {
   1024 	pid_t pid;
   1025 	sigset_t mask;
   1026 	time_t current_time = time(NULL);
   1027 
   1028 	/*
   1029 	 * fork(), not vfork() -- we can't afford to block.
   1030 	 */
   1031 	if ((pid = fork()) == -1) {
   1032 		emergency("can't fork for getty on port %s: %m", sp->se_device);
   1033 		return -1;
   1034 	}
   1035 
   1036 	if (pid)
   1037 		return pid;
   1038 
   1039 	if (current_time > sp->se_started &&
   1040 	    current_time - sp->se_started < GETTY_SPACING) {
   1041 		warning("getty repeating too quickly on port %s, sleeping",
   1042 		    sp->se_device);
   1043 		(void)sleep(GETTY_SLEEP);
   1044 	}
   1045 
   1046 	if (sp->se_window) {
   1047 		start_window_system(sp);
   1048 		(void)sleep(WINDOW_WAIT);
   1049 	}
   1050 
   1051 	(void)sigemptyset(&mask);
   1052 	(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
   1053 
   1054 	(void)execv(sp->se_getty_argv[0], sp->se_getty_argv);
   1055 	stall("can't exec getty '%s' for port %s: %m",
   1056 	    sp->se_getty_argv[0], sp->se_device);
   1057 	_exit(1);
   1058 	/*NOTREACHED*/
   1059 }
   1060 #endif /* LETS_GET_SMALL */
   1061 
   1062 /*
   1063  * Collect exit status for a child.
   1064  * If an exiting login, start a new login running.
   1065  */
   1066 void
   1067 collect_child(pid_t pid, int status)
   1068 {
   1069 #ifndef LETS_GET_SMALL
   1070 	session_t *sp, *sprev, *snext;
   1071 
   1072 	if (! sessions)
   1073 		return;
   1074 
   1075 	if ((sp = find_session(pid)) == NULL)
   1076 		return;
   1077 
   1078 	clear_session_logs(sp, status);
   1079 	del_session(sp);
   1080 	sp->se_process = 0;
   1081 
   1082 	if (sp->se_flags & SE_SHUTDOWN) {
   1083 		if ((sprev = sp->se_prev) != NULL)
   1084 			sprev->se_next = sp->se_next;
   1085 		else
   1086 			sessions = sp->se_next;
   1087 		if ((snext = sp->se_next) != NULL)
   1088 			snext->se_prev = sp->se_prev;
   1089 		free_session(sp);
   1090 		return;
   1091 	}
   1092 
   1093 	if ((pid = start_getty(sp)) == -1) {
   1094 		/* serious trouble */
   1095 		requested_transition = clean_ttys;
   1096 		return;
   1097 	}
   1098 
   1099 	sp->se_process = pid;
   1100 	sp->se_started = time(NULL);
   1101 	add_session(sp);
   1102 #endif /* LETS_GET_SMALL */
   1103 }
   1104 
   1105 /*
   1106  * Catch a signal and request a state transition.
   1107  */
   1108 void
   1109 transition_handler(int sig)
   1110 {
   1111 
   1112 	switch (sig) {
   1113 #ifndef LETS_GET_SMALL
   1114 	case SIGHUP:
   1115 		requested_transition = clean_ttys;
   1116 		break;
   1117 	case SIGTERM:
   1118 		requested_transition = death;
   1119 		break;
   1120 	case SIGTSTP:
   1121 		requested_transition = catatonia;
   1122 		break;
   1123 #endif /* LETS_GET_SMALL */
   1124 	default:
   1125 		requested_transition = 0;
   1126 		break;
   1127 	}
   1128 }
   1129 
   1130 #ifndef LETS_GET_SMALL
   1131 /*
   1132  * Take the system multiuser.
   1133  */
   1134 state_func_t
   1135 multi_user(void)
   1136 {
   1137 	pid_t pid;
   1138 	int status;
   1139 	session_t *sp;
   1140 
   1141 	requested_transition = 0;
   1142 
   1143 	/*
   1144 	 * If the administrator has not set the security level to -1
   1145 	 * to indicate that the kernel should not run multiuser in secure
   1146 	 * mode, and the run script has not set a higher level of security
   1147 	 * than level 1, then put the kernel into secure mode.
   1148 	 */
   1149 	if (getsecuritylevel() == 0)
   1150 		setsecuritylevel(1);
   1151 
   1152 	for (sp = sessions; sp; sp = sp->se_next) {
   1153 		if (sp->se_process)
   1154 			continue;
   1155 		if ((pid = start_getty(sp)) == -1) {
   1156 			/* serious trouble */
   1157 			requested_transition = clean_ttys;
   1158 			break;
   1159 		}
   1160 		sp->se_process = pid;
   1161 		sp->se_started = time(NULL);
   1162 		add_session(sp);
   1163 	}
   1164 
   1165 	while (!requested_transition)
   1166 		if ((pid = waitpid(-1, &status, 0)) != -1)
   1167 			collect_child(pid, status);
   1168 
   1169 	return (state_func_t)requested_transition;
   1170 }
   1171 
   1172 /*
   1173  * This is an n-squared algorithm.  We hope it isn't run often...
   1174  */
   1175 state_func_t
   1176 clean_ttys(void)
   1177 {
   1178 	session_t *sp, *sprev;
   1179 	struct ttyent *typ;
   1180 	int session_index = 0;
   1181 	int devlen;
   1182 
   1183 	for (sp = sessions; sp; sp = sp->se_next)
   1184 		sp->se_flags &= ~SE_PRESENT;
   1185 
   1186 	devlen = sizeof(_PATH_DEV) - 1;
   1187 	while ((typ = getttyent()) != NULL) {
   1188 		++session_index;
   1189 
   1190 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
   1191 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
   1192 				break;
   1193 
   1194 		if (sp) {
   1195 			sp->se_flags |= SE_PRESENT;
   1196 			if (sp->se_index != session_index) {
   1197 				warning("port %s changed utmp index from "
   1198 				    "%d to %d", sp->se_device, sp->se_index,
   1199 				    session_index);
   1200 				sp->se_index = session_index;
   1201 			}
   1202 			if ((typ->ty_status & TTY_ON) == 0 ||
   1203 			    typ->ty_getty == 0) {
   1204 				sp->se_flags |= SE_SHUTDOWN;
   1205 				(void)kill(sp->se_process, SIGHUP);
   1206 				continue;
   1207 			}
   1208 			sp->se_flags &= ~SE_SHUTDOWN;
   1209 			if (setupargv(sp, typ) == 0) {
   1210 				warning("can't parse getty for port %s",
   1211 				    sp->se_device);
   1212 				sp->se_flags |= SE_SHUTDOWN;
   1213 				(void)kill(sp->se_process, SIGHUP);
   1214 			}
   1215 			continue;
   1216 		}
   1217 
   1218 		new_session(sprev, session_index, typ);
   1219 	}
   1220 
   1221 	endttyent();
   1222 
   1223 	for (sp = sessions; sp; sp = sp->se_next)
   1224 		if ((sp->se_flags & SE_PRESENT) == 0) {
   1225 			sp->se_flags |= SE_SHUTDOWN;
   1226 			(void)kill(sp->se_process, SIGHUP);
   1227 		}
   1228 
   1229 	return (state_func_t)multi_user;
   1230 }
   1231 
   1232 /*
   1233  * Block further logins.
   1234  */
   1235 state_func_t
   1236 catatonia(void)
   1237 {
   1238 	session_t *sp;
   1239 
   1240 	for (sp = sessions; sp; sp = sp->se_next)
   1241 		sp->se_flags |= SE_SHUTDOWN;
   1242 
   1243 	return (state_func_t)multi_user;
   1244 }
   1245 #endif /* LETS_GET_SMALL */
   1246 
   1247 /*
   1248  * Note SIGALRM.
   1249  */
   1250 void
   1251 /*ARGSUSED*/
   1252 alrm_handler(int sig)
   1253 {
   1254 
   1255 	clang = 1;
   1256 }
   1257 
   1258 #ifndef LETS_GET_SMALL
   1259 /*
   1260  * Bring the system down to single user.
   1261  */
   1262 state_func_t
   1263 death(void)
   1264 {
   1265 	session_t *sp;
   1266 	int i, status;
   1267 	pid_t pid;
   1268 	static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL };
   1269 
   1270 	for (sp = sessions; sp; sp = sp->se_next)
   1271 		sp->se_flags |= SE_SHUTDOWN;
   1272 
   1273 	/* NB: should send a message to the session logger to avoid blocking. */
   1274 #ifdef SUPPORT_UTMPX
   1275 	logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
   1276 #endif
   1277 #ifdef SUPPORT_UTMP
   1278 	logwtmp("~", "shutdown", "");
   1279 #endif
   1280 
   1281 	for (i = 0; i < 3; ++i) {
   1282 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
   1283 			return (state_func_t)single_user;
   1284 
   1285 		clang = 0;
   1286 		alarm(DEATH_WATCH);
   1287 		do
   1288 			if ((pid = waitpid(-1, &status, 0)) != -1)
   1289 				collect_child(pid, status);
   1290 		while (clang == 0 && errno != ECHILD);
   1291 
   1292 		if (errno == ECHILD)
   1293 			return (state_func_t)single_user;
   1294 	}
   1295 
   1296 	warning("some processes would not die; ps axl advised");
   1297 
   1298 	return (state_func_t)single_user;
   1299 }
   1300 #endif /* LETS_GET_SMALL */
   1301 
   1302 #ifdef MFS_DEV_IF_NO_CONSOLE
   1303 
   1304 static int
   1305 mfs_dev(void)
   1306 {
   1307 	/*
   1308 	 * We cannot print errors so we bail out silently...
   1309 	 */
   1310 	int fd;
   1311 	struct stat st;
   1312 	pid_t pid;
   1313 	int status;
   1314 	void *makedev = 0;
   1315 	void *makedev_local = 0;
   1316 	size_t makedev_size;
   1317 	size_t makedev_local_size;
   1318 	dev_t dev;
   1319 #ifdef CPU_CONSDEV
   1320 	static int name[2] = { CTL_MACHDEP, CPU_CONSDEV };
   1321 	size_t olen;
   1322 #endif
   1323 
   1324 
   1325 	/* If we have /dev/console, assume all is OK  */
   1326 	if (access(_PATH_CONSOLE, F_OK) != -1)
   1327 		return(0);
   1328 
   1329 	/* Grab the contents of MAKEDEV */
   1330 	if ((fd = open("/dev/MAKEDEV", O_RDONLY)) != -1) {
   1331 		if (fstat(fd, &st) != -1 && (makedev = mmap(0,
   1332 		    (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
   1333 		    (off_t)0)) != MAP_FAILED)
   1334 			makedev_size = (size_t)st.st_size;
   1335 		else
   1336 			makedev = 0;
   1337 		(void)close(fd);
   1338 	}
   1339 
   1340 	/* Grab the contents of MAKEDEV.local */
   1341 	if ((fd = open("/dev/MAKEDEV.local", O_RDONLY)) != -1) {
   1342 		if (fstat(fd, &st) != -1 && (makedev_local = mmap(0,
   1343 		    (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
   1344 		    (off_t)0)) != MAP_FAILED)
   1345 			makedev_local_size = (size_t)st.st_size;
   1346 		else
   1347 			makedev_local = 0;
   1348 		(void)close(fd);
   1349 	}
   1350 
   1351 	/* Mount an mfs over /dev so we can create devices */
   1352 	switch ((pid = fork())) {
   1353 	case 0:
   1354 		(void)execl("/sbin/mount_mfs", "mount_mfs", "-i", "192",
   1355 		    "-s", "768", "-b", "4096", "-f", "512", "swap", "/dev",
   1356 		    NULL);
   1357 		_exit(1);
   1358 		/*NOTREACHED*/
   1359 
   1360 	case -1:
   1361 		return(-1);
   1362 
   1363 	default:
   1364 		if (waitpid(pid, &status, 0) == -1)
   1365 			return(-1);
   1366 		if (status != 0)
   1367 			return(-1);
   1368 		break;
   1369 	}
   1370 
   1371 #ifdef CPU_CONSDEV
   1372 	olen = sizeof(dev);
   1373 	if (sysctl(name, sizeof(name) / sizeof(name[0]), &dev, &olen,
   1374 	    NULL, 0) == -1)
   1375 #endif
   1376 		dev = makedev(0, 0);
   1377 
   1378 	/* Make a console for us, so we can see things happening */
   1379 	if (mknod(_PATH_CONSOLE, 0666 | S_IFCHR, dev) == -1)
   1380 		return(-1);
   1381 
   1382 	(void)freopen(_PATH_CONSOLE, "a", stderr);
   1383 
   1384 	warnx("Creating mfs /dev");
   1385 
   1386 	/* Create a MAKEDEV script in the mfs /dev */
   1387 	if (makedev && (fd = open("/dev/MAKEDEV", O_WRONLY|O_CREAT|O_TRUNC,
   1388 	    0755)) != -1) {
   1389 		(void)write(fd, makedev, makedev_size);
   1390 		(void)munmap(makedev, makedev_size);
   1391 		(void)close(fd);
   1392 	}
   1393 
   1394 	/* Create a MAKEDEV.local script in the mfs /dev */
   1395 	if (makedev_local && (fd = open("/dev/MAKEDEV.local",
   1396 	    O_WRONLY|O_CREAT|O_TRUNC, 0755)) != -1) {
   1397 		(void)write(fd, makedev_local, makedev_local_size);
   1398 		(void)munmap(makedev_local, makedev_local_size);
   1399 		(void)close(fd);
   1400 	}
   1401 
   1402 	/* Run the makedev script to create devices */
   1403 	switch ((pid = fork())) {
   1404 	case 0:
   1405 		if (chdir("/dev") == -1)
   1406 			goto fail;
   1407 		(void)execl("/bin/sh", "sh", "./MAKEDEV", "all", NULL);
   1408 		goto fail;
   1409 
   1410 	case -1:
   1411 		goto fail;
   1412 
   1413 	default:
   1414 		if (waitpid(pid, &status, 0) == -1)
   1415 			goto fail;
   1416 		if (status != 0) {
   1417 			errno = EINVAL;
   1418 			goto fail;
   1419 		}
   1420 		break;
   1421 	}
   1422 	return(0);
   1423 fail:
   1424 	warn("Unable to run MAKEDEV");
   1425 	return(-1);
   1426 }
   1427 #endif
   1428