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