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