Home | History | Annotate | Line # | Download | only in init
init.c revision 1.109
      1  1.109  jmcneill /*	$NetBSD: init.c,v 1.109 2021/10/11 20:23:25 jmcneill 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.96     lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     38   1.96     lukem  The Regents of the University of California.  All rights reserved.");
     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.109  jmcneill __RCSID("$NetBSD: init.c,v 1.109 2021/10/11 20:23:25 jmcneill 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 <machine/cpu.h>
     56    1.8       cgd 
     57    1.8       cgd #include <db.h>
     58    1.8       cgd #include <errno.h>
     59    1.8       cgd #include <fcntl.h>
     60    1.8       cgd #include <signal.h>
     61    1.8       cgd #include <stdio.h>
     62    1.8       cgd #include <stdlib.h>
     63    1.8       cgd #include <string.h>
     64    1.8       cgd #include <syslog.h>
     65    1.8       cgd #include <time.h>
     66    1.1       cgd #include <ttyent.h>
     67    1.1       cgd #include <unistd.h>
     68   1.22       jtc #include <util.h>
     69   1.28  christos #include <paths.h>
     70   1.28  christos #include <err.h>
     71  1.104  christos #ifdef SUPPORT_UTMP
     72  1.104  christos #include <utmp.h>
     73  1.104  christos #endif
     74  1.104  christos #ifdef SUPPORT_UTMPX
     75  1.104  christos #include <utmpx.h>
     76  1.104  christos #endif
     77    1.1       cgd 
     78    1.8       cgd #include <stdarg.h>
     79    1.8       cgd 
     80    1.8       cgd #ifdef SECURE
     81    1.5       cgd #include <pwd.h>
     82    1.5       cgd #endif
     83    1.5       cgd 
     84    1.8       cgd #include "pathnames.h"
     85    1.8       cgd 
     86   1.62       dsl #define XSTR(x) #x
     87   1.62       dsl #define STR(x) XSTR(x)
     88   1.62       dsl 
     89    1.8       cgd /*
     90    1.8       cgd  * Sleep times; used to prevent thrashing.
     91    1.8       cgd  */
     92    1.8       cgd #define	GETTY_SPACING		 5	/* N secs minimum getty spacing */
     93    1.8       cgd #define	GETTY_SLEEP		30	/* sleep N secs after spacing problem */
     94  1.109  jmcneill #define	GETTY_NSPACE		 5	/* N retries before spacing problem */
     95    1.8       cgd #define	WINDOW_WAIT		 3	/* wait N secs after starting window */
     96    1.8       cgd #define	STALL_TIMEOUT		30	/* wait N secs after warning */
     97    1.8       cgd #define	DEATH_WATCH		10	/* wait N secs for procs to die */
     98    1.8       cgd 
     99  1.101     joerg static const struct timespec dtrtime = {.tv_sec = 0, .tv_nsec = 250000};
    100   1.56     lukem 
    101   1.56     lukem #if defined(RESCUEDIR)
    102   1.56     lukem #define	INIT_BSHELL	RESCUEDIR "/sh"
    103   1.56     lukem #define	INIT_MOUNT_MFS	RESCUEDIR "/mount_mfs"
    104   1.56     lukem #define	INIT_PATH	RESCUEDIR ":" _PATH_STDPATH
    105   1.56     lukem #else
    106   1.56     lukem #define	INIT_BSHELL	_PATH_BSHELL
    107   1.56     lukem #define	INIT_MOUNT_MFS	"/sbin/mount_mfs"
    108   1.56     lukem #define	INIT_PATH	_PATH_STDPATH
    109   1.52     lukem #endif
    110   1.52     lukem 
    111  1.101     joerg static void handle(sig_t, ...);
    112  1.101     joerg static void delset(sigset_t *, ...);
    113   1.27     perry 
    114  1.107  christos static void stall(const char *, ...) __sysloglike(1, 2);
    115  1.107  christos static void warning(const char *, ...) __sysloglike(1, 2);
    116  1.107  christos static void emergency(const char *, ...) __sysloglike(1, 2);
    117  1.101     joerg __dead static void disaster(int);
    118  1.101     joerg static void badsys(int);
    119    1.8       cgd 
    120    1.8       cgd /*
    121    1.8       cgd  * We really need a recursive typedef...
    122    1.8       cgd  * The following at least guarantees that the return type of (*state_t)()
    123    1.8       cgd  * is sufficiently wide to hold a function pointer.
    124    1.8       cgd  */
    125   1.39       wiz typedef long (*state_func_t)(void);
    126   1.39       wiz typedef state_func_t (*state_t)(void);
    127    1.8       cgd 
    128   1.77  christos #define	DEATH		'd'
    129   1.77  christos #define	SINGLE_USER	's'
    130   1.77  christos #define	RUNCOM		'r'
    131   1.77  christos #define	READ_TTYS	't'
    132   1.77  christos #define	MULTI_USER	'm'
    133   1.77  christos #define	CLEAN_TTYS	'T'
    134   1.77  christos #define	CATATONIA	'c'
    135   1.77  christos 
    136  1.101     joerg static state_func_t single_user(void);
    137  1.102  christos #ifndef LETS_GET_SMALL
    138  1.101     joerg static state_func_t runcom(void);
    139  1.101     joerg static state_func_t read_ttys(void);
    140  1.101     joerg static state_func_t multi_user(void);
    141  1.101     joerg static state_func_t clean_ttys(void);
    142  1.101     joerg static state_func_t catatonia(void);
    143  1.101     joerg static state_func_t death(void);
    144  1.102  christos #endif
    145    1.8       cgd 
    146  1.101     joerg static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
    147    1.8       cgd 
    148  1.101     joerg static void transition(state_t);
    149  1.101     joerg static void setctty(const char *);
    150    1.8       cgd 
    151    1.8       cgd typedef struct init_session {
    152    1.8       cgd 	int	se_index;		/* index of entry in ttys file */
    153    1.8       cgd 	pid_t	se_process;		/* controlling process */
    154   1.77  christos 	struct timeval	se_started;	/* used to avoid thrashing */
    155  1.109  jmcneill 	int	se_nspace;		/* spacing count */
    156    1.8       cgd 	int	se_flags;		/* status of session */
    157    1.8       cgd #define	SE_SHUTDOWN	0x1		/* session won't be restarted */
    158   1.21   mycroft #define	SE_PRESENT	0x2		/* session is in /etc/ttys */
    159    1.8       cgd 	char	*se_device;		/* filename of port */
    160    1.8       cgd 	char	*se_getty;		/* what to run on that port */
    161    1.8       cgd 	char	**se_getty_argv;	/* pre-parsed argument array */
    162    1.8       cgd 	char	*se_window;		/* window system (started only once) */
    163    1.8       cgd 	char	**se_window_argv;	/* pre-parsed argument array */
    164    1.8       cgd 	struct	init_session *se_prev;
    165    1.8       cgd 	struct	init_session *se_next;
    166    1.8       cgd } session_t;
    167    1.8       cgd 
    168  1.101     joerg static void collect_child(pid_t, int);
    169  1.102  christos static int clang;
    170  1.101     joerg static void transition_handler(int);
    171  1.101     joerg static void alrm_handler(int);
    172  1.101     joerg static int has_securelevel(void);
    173  1.102  christos static int securelevel_present;
    174  1.102  christos 
    175  1.102  christos #ifndef LETS_GET_SMALL
    176  1.102  christos static int do_setttyent(void);
    177  1.102  christos static void start_window_system(session_t *);
    178  1.102  christos static char **construct_argv(char *);
    179  1.102  christos static int setupargv(session_t *, struct ttyent *);
    180  1.102  christos static pid_t start_getty(session_t *);
    181  1.102  christos static void free_session(session_t *);
    182  1.102  christos static session_t *new_session(session_t *, int, struct ttyent *);
    183  1.102  christos static session_t *sessions;
    184  1.101     joerg static void setsecuritylevel(int);
    185  1.101     joerg static int getsecuritylevel(void);
    186  1.101     joerg static int start_session_db(void);
    187  1.101     joerg static void add_session(session_t *);
    188  1.101     joerg static void del_session(session_t *);
    189  1.101     joerg static session_t *find_session(pid_t);
    190  1.101     joerg static DB *session_db;
    191  1.101     joerg static state_t requested_transition = runcom;
    192   1.73      salo 
    193  1.101     joerg static void clear_session_logs(session_t *, int);
    194  1.101     joerg static state_func_t runetcrc(int);
    195   1.77  christos #ifdef SUPPORT_UTMPX
    196   1.77  christos static struct timeval boot_time;
    197  1.101     joerg static state_t current_state = death;
    198   1.77  christos static void session_utmpx(const session_t *, int);
    199   1.77  christos static void make_utmpx(const char *, const char *, int, pid_t,
    200   1.77  christos     const struct timeval *, int);
    201   1.77  christos static char get_runlevel(const state_t);
    202   1.77  christos static void utmpx_set_runlevel(char, char);
    203   1.77  christos #endif
    204   1.73      salo 
    205   1.73      salo #ifdef CHROOT
    206  1.101     joerg static int did_multiuser_chroot = 0;
    207  1.101     joerg static char rootdir[PATH_MAX];
    208  1.101     joerg static int shouldchroot(void);
    209  1.101     joerg static int createsysctlnode(void);
    210   1.73      salo #endif /* CHROOT */
    211   1.73      salo 
    212   1.73      salo #else /* LETS_GET_SMALL */
    213  1.101     joerg static state_t requested_transition = single_user;
    214   1.73      salo #endif /* !LETS_GET_SMALL */
    215   1.73      salo 
    216   1.45       abs #ifdef MFS_DEV_IF_NO_CONSOLE
    217   1.58  christos 
    218   1.45       abs static int mfs_dev(void);
    219   1.58  christos 
    220   1.28  christos #endif
    221   1.28  christos 
    222    1.8       cgd /*
    223    1.8       cgd  * The mother of all processes.
    224    1.8       cgd  */
    225    1.8       cgd int
    226   1.43     lukem main(int argc, char **argv)
    227    1.8       cgd {
    228    1.8       cgd 	struct sigaction sa;
    229    1.8       cgd 	sigset_t mask;
    230   1.37     soren #ifndef LETS_GET_SMALL
    231   1.37     soren 	int c;
    232    1.8       cgd 
    233   1.77  christos #ifdef SUPPORT_UTMPX
    234   1.77  christos 	(void)gettimeofday(&boot_time, NULL);
    235   1.77  christos #endif /* SUPPORT_UTMPX */
    236   1.77  christos 
    237    1.8       cgd 	/* Dispose of random users. */
    238    1.8       cgd 	if (getuid() != 0) {
    239   1.28  christos 		errno = EPERM;
    240   1.87    cbiere 		err(1, NULL);
    241    1.8       cgd 	}
    242    1.8       cgd 
    243    1.8       cgd 	/* System V users like to reexec init. */
    244   1.28  christos 	if (getpid() != 1)
    245   1.87    cbiere 		errx(1, "already running");
    246   1.28  christos #endif
    247    1.8       cgd 
    248    1.8       cgd 	/*
    249    1.8       cgd 	 * Create an initial session.
    250    1.8       cgd 	 */
    251    1.8       cgd 	if (setsid() < 0)
    252   1.28  christos 		warn("initial setsid() failed");
    253   1.12       cgd 
    254   1.12       cgd 	/*
    255   1.12       cgd 	 * Establish an initial user so that programs running
    256   1.12       cgd 	 * single user do not freak out and die (like passwd).
    257   1.12       cgd 	 */
    258   1.12       cgd 	if (setlogin("root") < 0)
    259   1.28  christos 		warn("setlogin() failed");
    260   1.28  christos 
    261   1.28  christos 
    262   1.45       abs #ifdef MFS_DEV_IF_NO_CONSOLE
    263   1.45       abs 	if (mfs_dev() == -1)
    264   1.45       abs 		requested_transition = single_user;
    265   1.28  christos #endif
    266   1.28  christos 
    267   1.28  christos #ifndef LETS_GET_SMALL
    268   1.28  christos 	/*
    269   1.28  christos 	 * Note that this does NOT open a file...
    270   1.28  christos 	 * Does 'init' deserve its own facility number?
    271   1.28  christos 	 */
    272   1.42     lukem 	openlog("init", LOG_CONS, LOG_AUTH);
    273   1.28  christos #endif /* LETS_GET_SMALL */
    274   1.28  christos 
    275    1.8       cgd 
    276   1.13       cgd #ifndef LETS_GET_SMALL
    277    1.8       cgd 	/*
    278    1.8       cgd 	 * This code assumes that we always get arguments through flags,
    279    1.8       cgd 	 * never through bits set in some random machine register.
    280    1.8       cgd 	 */
    281    1.8       cgd 	while ((c = getopt(argc, argv, "sf")) != -1)
    282    1.8       cgd 		switch (c) {
    283    1.8       cgd 		case 's':
    284    1.8       cgd 			requested_transition = single_user;
    285    1.8       cgd 			break;
    286    1.8       cgd 		case 'f':
    287    1.8       cgd 			runcom_mode = FASTBOOT;
    288    1.8       cgd 			break;
    289    1.8       cgd 		default:
    290   1.80  christos 			warning("unrecognized flag `%c'", c);
    291    1.8       cgd 			break;
    292    1.8       cgd 		}
    293    1.8       cgd 
    294    1.8       cgd 	if (optind != argc)
    295    1.8       cgd 		warning("ignoring excess arguments");
    296   1.13       cgd #else /* LETS_GET_SMALL */
    297   1.13       cgd 	requested_transition = single_user;
    298   1.13       cgd #endif /* LETS_GET_SMALL */
    299    1.8       cgd 
    300    1.8       cgd 	/*
    301    1.8       cgd 	 * We catch or block signals rather than ignore them,
    302    1.8       cgd 	 * so that they get reset on exec.
    303    1.8       cgd 	 */
    304    1.8       cgd 	handle(badsys, SIGSYS, 0);
    305    1.8       cgd 	handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV,
    306    1.8       cgd 	       SIGBUS, SIGXCPU, SIGXFSZ, 0);
    307    1.8       cgd 	handle(transition_handler, SIGHUP, SIGTERM, SIGTSTP, 0);
    308    1.8       cgd 	handle(alrm_handler, SIGALRM, 0);
    309   1.47  christos 	(void)sigfillset(&mask);
    310    1.8       cgd 	delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
    311   1.47  christos 	    SIGXCPU, SIGXFSZ, SIGHUP, SIGTERM, SIGTSTP, SIGALRM, 0);
    312   1.47  christos 	(void)sigprocmask(SIG_SETMASK, &mask, NULL);
    313   1.47  christos 	(void)sigemptyset(&sa.sa_mask);
    314    1.8       cgd 	sa.sa_flags = 0;
    315    1.8       cgd 	sa.sa_handler = SIG_IGN;
    316   1.47  christos 	(void)sigaction(SIGTTIN, &sa, NULL);
    317   1.47  christos 	(void)sigaction(SIGTTOU, &sa, NULL);
    318    1.8       cgd 
    319    1.8       cgd 	/*
    320    1.8       cgd 	 * Paranoia.
    321    1.8       cgd 	 */
    322   1.47  christos 	(void)close(0);
    323   1.47  christos 	(void)close(1);
    324   1.47  christos 	(void)close(2);
    325    1.8       cgd 
    326   1.73      salo #if !defined(LETS_GET_SMALL) && defined(CHROOT)
    327   1.73      salo 	/* Create "init.root" sysctl node. */
    328   1.84  christos 	(void)createsysctlnode();
    329   1.73      salo #endif /* !LETS_GET_SMALL && CHROOT*/
    330   1.73      salo 
    331    1.8       cgd 	/*
    332  1.100      elad 	 * Securelevel might not be supported by the kernel. Query for it, and
    333  1.100      elad 	 * set a variable indicating whether we should attempt anything with it
    334  1.100      elad 	 * or not.
    335  1.100      elad 	 */
    336  1.100      elad 	securelevel_present = has_securelevel();
    337  1.100      elad 
    338  1.100      elad 	/*
    339    1.8       cgd 	 * Start the state machine.
    340    1.8       cgd 	 */
    341    1.8       cgd 	transition(requested_transition);
    342    1.8       cgd 
    343    1.8       cgd 	/*
    344    1.8       cgd 	 * Should never reach here.
    345    1.8       cgd 	 */
    346    1.8       cgd 	return 1;
    347    1.8       cgd }
    348    1.8       cgd 
    349    1.8       cgd /*
    350    1.8       cgd  * Associate a function with a signal handler.
    351    1.8       cgd  */
    352  1.101     joerg static void
    353    1.8       cgd handle(sig_t handler, ...)
    354    1.8       cgd {
    355    1.8       cgd 	int sig;
    356    1.8       cgd 	struct sigaction sa;
    357   1.33   thorpej 	sigset_t mask_everything;
    358    1.8       cgd 	va_list ap;
    359    1.8       cgd 
    360    1.8       cgd 	va_start(ap, handler);
    361    1.7       cgd 
    362    1.8       cgd 	sa.sa_handler = handler;
    363   1.47  christos 	(void)sigfillset(&mask_everything);
    364    1.1       cgd 
    365   1.27     perry 	while ((sig = va_arg(ap, int)) != 0) {
    366    1.8       cgd 		sa.sa_mask = mask_everything;
    367    1.8       cgd 		/* XXX SA_RESTART? */
    368    1.8       cgd 		sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
    369   1.47  christos 		(void)sigaction(sig, &sa, NULL);
    370    1.8       cgd 	}
    371    1.8       cgd 	va_end(ap);
    372    1.8       cgd }
    373    1.1       cgd 
    374    1.8       cgd /*
    375    1.8       cgd  * Delete a set of signals from a mask.
    376    1.8       cgd  */
    377  1.101     joerg static void
    378    1.8       cgd delset(sigset_t *maskp, ...)
    379    1.8       cgd {
    380    1.8       cgd 	int sig;
    381    1.8       cgd 	va_list ap;
    382    1.8       cgd 
    383    1.8       cgd 	va_start(ap, maskp);
    384    1.1       cgd 
    385   1.27     perry 	while ((sig = va_arg(ap, int)) != 0)
    386   1.47  christos 		(void)sigdelset(maskp, sig);
    387    1.8       cgd 	va_end(ap);
    388    1.8       cgd }
    389    1.8       cgd 
    390   1.91       dsl #if 0	/* Enable to get error messages from init ! */
    391   1.91       dsl #define vsyslog(level, fmt, ap) print_console(level, fmt, ap)
    392   1.91       dsl #define closelog()
    393   1.91       dsl 
    394   1.91       dsl static void
    395   1.91       dsl print_console(int level, const char *message, va_list ap)
    396   1.91       dsl {
    397   1.91       dsl 	/*
    398   1.91       dsl 	 * XXX: syslog seems to just plain not work in console-only
    399   1.91       dsl 	 * XXX: situation... that should be fixed.  Let's leave this
    400   1.91       dsl 	 * XXX: note + code here in case someone gets in trouble and
    401   1.91       dsl 	 * XXX: wants to debug. -- Jachym Holecek <freza (at) liberouter.org>
    402   1.91       dsl 	 */
    403   1.91       dsl 	char errbuf[1024];
    404   1.91       dsl 	int fd, len;
    405   1.91       dsl 
    406   1.91       dsl 	/* We can't do anything on errors, anyway... */
    407   1.91       dsl 	fd = open(_PATH_CONSOLE, O_WRONLY);
    408   1.91       dsl 	if (fd == -1)
    409   1.91       dsl 		return ;
    410   1.91       dsl 
    411   1.91       dsl 	/* %m will get lost... */
    412   1.91       dsl 	len = vsnprintf(errbuf, sizeof(errbuf), message, ap);
    413   1.91       dsl 	(void)write(fd, (void *)errbuf, len);
    414   1.91       dsl 	(void)close(fd);
    415   1.91       dsl }
    416   1.91       dsl #endif
    417   1.91       dsl 
    418    1.8       cgd /*
    419    1.8       cgd  * Log a message and sleep for a while (to give someone an opportunity
    420    1.8       cgd  * to read it and to save log or hardcopy output if the problem is chronic).
    421    1.8       cgd  * NB: should send a message to the session logger to avoid blocking.
    422    1.8       cgd  */
    423  1.101     joerg static void
    424   1.28  christos stall(const char *message, ...)
    425    1.8       cgd {
    426    1.8       cgd 	va_list ap;
    427    1.8       cgd 
    428    1.8       cgd 	va_start(ap, message);
    429    1.8       cgd 	vsyslog(LOG_ALERT, message, ap);
    430    1.8       cgd 	va_end(ap);
    431   1.24   mycroft 	closelog();
    432   1.47  christos 	(void)sleep(STALL_TIMEOUT);
    433    1.8       cgd }
    434    1.8       cgd 
    435    1.8       cgd /*
    436    1.8       cgd  * Like stall(), but doesn't sleep.
    437    1.8       cgd  * If cpp had variadic macros, the two functions could be #defines for another.
    438    1.8       cgd  * NB: should send a message to the session logger to avoid blocking.
    439    1.8       cgd  */
    440  1.101     joerg static void
    441   1.28  christos warning(const char *message, ...)
    442    1.8       cgd {
    443    1.8       cgd 	va_list ap;
    444    1.8       cgd 
    445    1.8       cgd 	va_start(ap, message);
    446    1.8       cgd 	vsyslog(LOG_ALERT, message, ap);
    447    1.8       cgd 	va_end(ap);
    448   1.24   mycroft 	closelog();
    449    1.8       cgd }
    450    1.1       cgd 
    451    1.8       cgd /*
    452    1.8       cgd  * Log an emergency message.
    453    1.8       cgd  * NB: should send a message to the session logger to avoid blocking.
    454    1.8       cgd  */
    455  1.101     joerg static void
    456   1.28  christos emergency(const char *message, ...)
    457    1.8       cgd {
    458    1.8       cgd 	va_list ap;
    459    1.8       cgd 
    460    1.8       cgd 	va_start(ap, message);
    461    1.8       cgd 	vsyslog(LOG_EMERG, message, ap);
    462    1.8       cgd 	va_end(ap);
    463   1.24   mycroft 	closelog();
    464    1.8       cgd }
    465    1.1       cgd 
    466    1.8       cgd /*
    467    1.8       cgd  * Catch a SIGSYS signal.
    468    1.8       cgd  *
    469    1.8       cgd  * These may arise if a system does not support sysctl.
    470    1.8       cgd  * We tolerate up to 25 of these, then throw in the towel.
    471    1.8       cgd  */
    472  1.101     joerg static void
    473   1.39       wiz badsys(int sig)
    474    1.1       cgd {
    475    1.8       cgd 	static int badcount = 0;
    476    1.8       cgd 
    477    1.8       cgd 	if (badcount++ < 25)
    478    1.8       cgd 		return;
    479    1.8       cgd 	disaster(sig);
    480    1.1       cgd }
    481    1.1       cgd 
    482    1.8       cgd /*
    483    1.8       cgd  * Catch an unexpected signal.
    484    1.8       cgd  */
    485  1.101     joerg static void
    486   1.39       wiz disaster(int sig)
    487    1.8       cgd {
    488   1.43     lukem 
    489   1.20       jtc 	emergency("fatal signal: %s", strsignal(sig));
    490   1.47  christos 	(void)sleep(STALL_TIMEOUT);
    491    1.8       cgd 	_exit(sig);		/* reboot */
    492    1.8       cgd }
    493    1.8       cgd 
    494    1.8       cgd /*
    495  1.100      elad  * Check if securelevel is present.
    496  1.100      elad  */
    497  1.101     joerg static int
    498  1.100      elad has_securelevel(void)
    499  1.100      elad {
    500  1.100      elad #ifdef KERN_SECURELVL
    501  1.100      elad 	int name[2], curlevel;
    502  1.100      elad 	size_t len;
    503  1.100      elad 
    504  1.100      elad 	name[0] = CTL_KERN;
    505  1.100      elad 	name[1] = KERN_SECURELVL;
    506  1.100      elad 	len = sizeof curlevel;
    507  1.100      elad 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
    508  1.100      elad 		/* If it doesn't exist, it's okay. */
    509  1.100      elad 		if (errno == ENOENT)
    510  1.100      elad 			return 0;
    511  1.100      elad 	}
    512  1.100      elad 	return 1;
    513  1.100      elad #else
    514  1.100      elad 	return 0;
    515  1.100      elad #endif
    516  1.100      elad }
    517  1.100      elad 
    518  1.100      elad /*
    519    1.8       cgd  * Get the security level of the kernel.
    520    1.8       cgd  */
    521  1.101     joerg static int
    522   1.39       wiz getsecuritylevel(void)
    523    1.1       cgd {
    524    1.8       cgd #ifdef KERN_SECURELVL
    525    1.8       cgd 	int name[2], curlevel;
    526    1.8       cgd 	size_t len;
    527    1.8       cgd 
    528  1.100      elad 	if (!securelevel_present)
    529  1.100      elad 		return -1;
    530  1.100      elad 
    531    1.8       cgd 	name[0] = CTL_KERN;
    532    1.8       cgd 	name[1] = KERN_SECURELVL;
    533    1.8       cgd 	len = sizeof curlevel;
    534    1.8       cgd 	if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
    535   1.47  christos 		emergency("cannot get kernel security level: %m");
    536   1.93    dyoung 		return -1;
    537    1.8       cgd 	}
    538   1.93    dyoung 	return curlevel;
    539    1.8       cgd #else
    540   1.93    dyoung 	return -1;
    541    1.8       cgd #endif
    542    1.1       cgd }
    543    1.1       cgd 
    544    1.8       cgd /*
    545    1.8       cgd  * Set the security level of the kernel.
    546    1.8       cgd  */
    547  1.101     joerg static void
    548   1.39       wiz setsecuritylevel(int newlevel)
    549    1.1       cgd {
    550    1.8       cgd #ifdef KERN_SECURELVL
    551    1.8       cgd 	int name[2], curlevel;
    552    1.1       cgd 
    553  1.100      elad 	if (!securelevel_present)
    554  1.100      elad 		return;
    555  1.100      elad 
    556    1.8       cgd 	curlevel = getsecuritylevel();
    557    1.8       cgd 	if (newlevel == curlevel)
    558    1.8       cgd 		return;
    559    1.8       cgd 	name[0] = CTL_KERN;
    560    1.8       cgd 	name[1] = KERN_SECURELVL;
    561    1.8       cgd 	if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
    562   1.80  christos 		emergency("cannot change kernel security level from"
    563   1.47  christos 		    " %d to %d: %m", curlevel, newlevel);
    564    1.8       cgd 		return;
    565    1.1       cgd 	}
    566    1.8       cgd #ifdef SECURE
    567    1.8       cgd 	warning("kernel security level changed from %d to %d",
    568    1.8       cgd 	    curlevel, newlevel);
    569    1.8       cgd #endif
    570    1.8       cgd #endif
    571    1.1       cgd }
    572    1.1       cgd 
    573    1.8       cgd /*
    574    1.8       cgd  * Change states in the finite state machine.
    575    1.8       cgd  * The initial state is passed as an argument.
    576    1.8       cgd  */
    577  1.101     joerg static void
    578   1.39       wiz transition(state_t s)
    579    1.1       cgd {
    580   1.43     lukem 
    581   1.55  christos 	if (s == NULL)
    582   1.55  christos 		return;
    583   1.77  christos 	for (;;) {
    584   1.78        he #ifdef SUPPORT_UTMPX
    585   1.77  christos #ifndef LETS_GET_SMALL
    586   1.77  christos 		utmpx_set_runlevel(get_runlevel(current_state),
    587   1.77  christos 		    get_runlevel(s));
    588   1.79        he 		current_state = s;
    589   1.77  christos #endif
    590   1.77  christos #endif
    591   1.47  christos 		s = (state_t)(*s)();
    592   1.77  christos 	}
    593    1.1       cgd }
    594    1.1       cgd 
    595   1.64  christos #ifndef LETS_GET_SMALL
    596    1.8       cgd /*
    597    1.8       cgd  * Close out the accounting files for a login session.
    598    1.8       cgd  * NB: should send a message to the session logger to avoid blocking.
    599    1.8       cgd  */
    600  1.101     joerg static void
    601   1.50  christos clear_session_logs(session_t *sp, int status)
    602    1.8       cgd {
    603   1.83     isaki #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
    604    1.8       cgd 	char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
    605   1.83     isaki #endif
    606   1.55  christos 
    607   1.50  christos #ifdef SUPPORT_UTMPX
    608   1.51  christos 	if (logoutx(line, status, DEAD_PROCESS))
    609   1.50  christos 		logwtmpx(line, "", "", status, DEAD_PROCESS);
    610   1.50  christos #endif
    611   1.50  christos #ifdef SUPPORT_UTMP
    612    1.8       cgd 	if (logout(line))
    613    1.8       cgd 		logwtmp(line, "", "");
    614   1.50  christos #endif
    615    1.8       cgd }
    616   1.64  christos #endif
    617    1.1       cgd 
    618    1.8       cgd /*
    619    1.8       cgd  * Start a session and allocate a controlling terminal.
    620    1.8       cgd  * Only called by children of init after forking.
    621    1.8       cgd  */
    622  1.101     joerg static void
    623   1.40       wiz setctty(const char *name)
    624    1.1       cgd {
    625    1.8       cgd 	int fd;
    626    1.1       cgd 
    627   1.84  christos 	(void)revoke(name);
    628   1.84  christos 	(void)nanosleep(&dtrtime, NULL);	/* leave DTR low for a bit */
    629    1.8       cgd 	if ((fd = open(name, O_RDWR)) == -1) {
    630    1.8       cgd 		stall("can't open %s: %m", name);
    631   1.87    cbiere 		_exit(1);
    632    1.7       cgd 	}
    633    1.8       cgd 	if (login_tty(fd) == -1) {
    634    1.8       cgd 		stall("can't get %s for controlling terminal: %m", name);
    635   1.91       dsl 		_exit(2);
    636    1.8       cgd 	}
    637    1.8       cgd }
    638    1.8       cgd 
    639    1.8       cgd /*
    640    1.8       cgd  * Bring the system up single user.
    641    1.8       cgd  */
    642  1.101     joerg static state_func_t
    643   1.39       wiz single_user(void)
    644    1.8       cgd {
    645    1.8       cgd 	pid_t pid, wpid;
    646    1.8       cgd 	int status;
    647   1.34       tls 	int from_securitylevel;
    648    1.8       cgd 	sigset_t mask;
    649   1.55  christos 	struct sigaction sa, satstp, sahup;
    650   1.31     perry #ifdef ALTSHELL
    651   1.56     lukem 	const char *shell = INIT_BSHELL;
    652   1.31     perry #endif
    653   1.70  christos 	const char *argv[2];
    654    1.8       cgd #ifdef SECURE
    655    1.8       cgd 	struct ttyent *typ;
    656   1.10       cgd 	struct passwd *pp;
    657    1.8       cgd 	char *clear, *password;
    658    1.7       cgd #endif
    659   1.26     perry #ifdef ALTSHELL
    660   1.26     perry 	char altshell[128];
    661   1.26     perry #endif /* ALTSHELL */
    662    1.7       cgd 
    663   1.73      salo #if !defined(LETS_GET_SMALL) && defined(CHROOT)
    664   1.73      salo 	/* Clear previous idea, just in case. */
    665   1.73      salo 	did_multiuser_chroot = 0;
    666   1.73      salo #endif /* !LETS_GET_SMALL && CHROOT */
    667   1.73      salo 
    668    1.8       cgd 	/*
    669    1.8       cgd 	 * If the kernel is in secure mode, downgrade it to insecure mode.
    670    1.8       cgd 	 */
    671   1.34       tls 	from_securitylevel = getsecuritylevel();
    672   1.34       tls 	if (from_securitylevel > 0)
    673    1.8       cgd 		setsecuritylevel(0);
    674    1.8       cgd 
    675   1.55  christos 	(void)sigemptyset(&sa.sa_mask);
    676   1.55  christos 	sa.sa_flags = 0;
    677   1.55  christos 	sa.sa_handler = SIG_IGN;
    678   1.55  christos 	(void)sigaction(SIGTSTP, &sa, &satstp);
    679   1.55  christos 	(void)sigaction(SIGHUP, &sa, &sahup);
    680    1.8       cgd 	if ((pid = fork()) == 0) {
    681    1.8       cgd 		/*
    682    1.8       cgd 		 * Start the single user session.
    683    1.8       cgd 		 */
    684   1.63       dsl 		if (access(_PATH_CONSTTY, F_OK) == 0)
    685   1.63       dsl 			setctty(_PATH_CONSTTY);
    686   1.63       dsl 		else
    687   1.63       dsl 			setctty(_PATH_CONSOLE);
    688    1.8       cgd 
    689    1.8       cgd #ifdef SECURE
    690    1.8       cgd 		/*
    691    1.8       cgd 		 * Check the root password.
    692    1.8       cgd 		 * We don't care if the console is 'on' by default;
    693    1.8       cgd 		 * it's the only tty that can be 'off' and 'secure'.
    694    1.8       cgd 		 */
    695   1.10       cgd 		typ = getttynam("console");
    696   1.10       cgd 		pp = getpwnam("root");
    697   1.34       tls 		if (typ && (from_securitylevel >=2 || (typ->ty_status
    698   1.34       tls 		    & TTY_SECURE) == 0) && pp && *pp->pw_passwd != '\0') {
    699   1.47  christos 			(void)fprintf(stderr,
    700   1.26     perry 			    "Enter root password, or ^D to go multi-user\n");
    701    1.8       cgd 			for (;;) {
    702    1.8       cgd 				clear = getpass("Password:");
    703    1.8       cgd 				if (clear == 0 || *clear == '\0')
    704   1.87    cbiere 					_exit(0);
    705    1.8       cgd 				password = crypt(clear, pp->pw_passwd);
    706   1.47  christos 				(void)memset(clear, 0, _PASSWORD_LEN);
    707    1.8       cgd 				if (strcmp(password, pp->pw_passwd) == 0)
    708    1.8       cgd 					break;
    709   1.80  christos 				warning("single-user login failed");
    710    1.1       cgd 			}
    711    1.8       cgd 		}
    712   1.84  christos 		(void)endttyent();
    713   1.10       cgd 		endpwent();
    714    1.9       cgd #endif /* SECURE */
    715    1.1       cgd 
    716   1.26     perry #ifdef ALTSHELL
    717   1.47  christos 		(void)fprintf(stderr,
    718   1.52     lukem 		    "Enter pathname of shell or RETURN for %s: ", shell);
    719   1.38       wiz 		if (fgets(altshell, sizeof(altshell), stdin) == NULL) {
    720   1.38       wiz 			altshell[0] = '\0';
    721   1.38       wiz 		} else {
    722   1.38       wiz 			/* nuke \n */
    723   1.38       wiz 			char *p;
    724   1.38       wiz 
    725   1.41       wiz 			if ((p = strchr(altshell, '\n')) != NULL)
    726   1.38       wiz 				*p = '\0';
    727   1.38       wiz 		}
    728   1.26     perry 
    729   1.26     perry 		if (altshell[0])
    730   1.26     perry 			shell = altshell;
    731   1.26     perry #endif /* ALTSHELL */
    732    1.8       cgd 
    733    1.8       cgd 		/*
    734    1.8       cgd 		 * Unblock signals.
    735    1.8       cgd 		 * We catch all the interesting ones,
    736    1.8       cgd 		 * and those are reset to SIG_DFL on exec.
    737    1.8       cgd 		 */
    738   1.47  christos 		(void)sigemptyset(&mask);
    739   1.47  christos 		(void)sigprocmask(SIG_SETMASK, &mask, NULL);
    740    1.8       cgd 
    741    1.8       cgd 		/*
    742    1.8       cgd 		 * Fire off a shell.
    743    1.8       cgd 		 * If the default one doesn't work, try the Bourne shell.
    744    1.8       cgd 		 */
    745    1.8       cgd 		argv[0] = "-sh";
    746    1.8       cgd 		argv[1] = 0;
    747   1.84  christos 		(void)setenv("PATH", INIT_PATH, 1);
    748   1.26     perry #ifdef ALTSHELL
    749   1.26     perry 		if (altshell[0])
    750   1.26     perry 			argv[0] = altshell;
    751   1.70  christos 		(void)execv(shell, __UNCONST(argv));
    752   1.80  christos 		emergency("can't exec `%s' for single user: %m", shell);
    753   1.26     perry 		argv[0] = "-sh";
    754   1.26     perry #endif /* ALTSHELL */
    755   1.70  christos 		(void)execv(INIT_BSHELL, __UNCONST(argv));
    756   1.80  christos 		emergency("can't exec `%s' for single user: %m", INIT_BSHELL);
    757   1.67   mycroft 		(void)sleep(STALL_TIMEOUT);
    758   1.91       dsl 		_exit(3);
    759    1.8       cgd 	}
    760    1.8       cgd 
    761    1.8       cgd 	if (pid == -1) {
    762    1.8       cgd 		/*
    763    1.8       cgd 		 * We are seriously hosed.  Do our best.
    764    1.8       cgd 		 */
    765   1.80  christos 		emergency("can't fork single-user shell: %m, trying again");
    766   1.47  christos 		while (waitpid(-1, NULL, WNOHANG) > 0)
    767    1.8       cgd 			continue;
    768   1.55  christos 		(void)sigaction(SIGTSTP, &satstp, NULL);
    769   1.55  christos 		(void)sigaction(SIGHUP, &sahup, NULL);
    770   1.93    dyoung 		return (state_func_t)single_user;
    771    1.1       cgd 	}
    772    1.8       cgd 
    773    1.8       cgd 	requested_transition = 0;
    774    1.8       cgd 	do {
    775    1.8       cgd 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    776   1.50  christos 			collect_child(wpid, status);
    777    1.8       cgd 		if (wpid == -1) {
    778    1.8       cgd 			if (errno == EINTR)
    779    1.8       cgd 				continue;
    780   1.47  christos 			warning("wait for single-user shell failed: %m; "
    781   1.47  christos 			    "restarting");
    782   1.47  christos 			return (state_func_t)single_user;
    783    1.8       cgd 		}
    784    1.8       cgd 		if (wpid == pid && WIFSTOPPED(status)) {
    785   1.80  christos 			warning("shell stopped, restarting");
    786   1.84  christos 			(void)kill(pid, SIGCONT);
    787    1.8       cgd 			wpid = -1;
    788    1.8       cgd 		}
    789    1.8       cgd 	} while (wpid != pid && !requested_transition);
    790    1.1       cgd 
    791   1.55  christos 	if (requested_transition) {
    792   1.55  christos 		(void)sigaction(SIGTSTP, &satstp, NULL);
    793   1.55  christos 		(void)sigaction(SIGHUP, &sahup, NULL);
    794   1.47  christos 		return (state_func_t)requested_transition;
    795   1.55  christos 	}
    796    1.1       cgd 
    797   1.49   mycroft 	if (WIFSIGNALED(status)) {
    798   1.49   mycroft 		if (WTERMSIG(status) == SIGKILL) {
    799   1.49   mycroft 			/* executed /sbin/reboot; wait for the end quietly */
    800   1.49   mycroft 			sigset_t s;
    801   1.49   mycroft 
    802   1.49   mycroft 			(void)sigfillset(&s);
    803   1.49   mycroft 			for (;;)
    804   1.49   mycroft 				(void)sigsuspend(&s);
    805    1.8       cgd 		} else {
    806   1.91       dsl 			warning("single user shell terminated (%x), restarting",
    807   1.91       dsl 				status);
    808   1.55  christos 			(void)sigaction(SIGTSTP, &satstp, NULL);
    809   1.55  christos 			(void)sigaction(SIGHUP, &sahup, NULL);
    810   1.93    dyoung 			return (state_func_t)single_user;
    811    1.1       cgd 		}
    812    1.8       cgd 	}
    813    1.8       cgd 
    814    1.8       cgd 	runcom_mode = FASTBOOT;
    815   1.55  christos 	(void)sigaction(SIGTSTP, &satstp, NULL);
    816   1.55  christos 	(void)sigaction(SIGHUP, &sahup, NULL);
    817   1.13       cgd #ifndef LETS_GET_SMALL
    818   1.93    dyoung 	return (state_func_t)runcom;
    819   1.13       cgd #else /* LETS_GET_SMALL */
    820   1.93    dyoung 	return (state_func_t)single_user;
    821   1.13       cgd #endif /* LETS_GET_SMALL */
    822    1.8       cgd }
    823    1.8       cgd 
    824   1.13       cgd #ifndef LETS_GET_SMALL
    825   1.73      salo 
    826   1.73      salo /* ARGSUSED */
    827  1.101     joerg static state_func_t
    828   1.73      salo runetcrc(int trychroot)
    829    1.8       cgd {
    830    1.8       cgd 	pid_t pid, wpid;
    831    1.8       cgd 	int status;
    832   1.70  christos 	const char *argv[4];
    833    1.8       cgd 	struct sigaction sa;
    834    1.8       cgd 
    835   1.47  christos 	switch ((pid = fork())) {
    836   1.47  christos 	case 0:
    837   1.47  christos 		(void)sigemptyset(&sa.sa_mask);
    838    1.8       cgd 		sa.sa_flags = 0;
    839    1.8       cgd 		sa.sa_handler = SIG_IGN;
    840   1.47  christos 		(void)sigaction(SIGTSTP, &sa, NULL);
    841   1.47  christos 		(void)sigaction(SIGHUP, &sa, NULL);
    842    1.8       cgd 
    843    1.8       cgd 		setctty(_PATH_CONSOLE);
    844    1.8       cgd 
    845    1.8       cgd 		argv[0] = "sh";
    846    1.8       cgd 		argv[1] = _PATH_RUNCOM;
    847   1.73      salo 		argv[2] = (runcom_mode == AUTOBOOT ? "autoboot" : 0);
    848    1.8       cgd 		argv[3] = 0;
    849    1.8       cgd 
    850   1.47  christos 		(void)sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
    851    1.8       cgd 
    852   1.73      salo #ifdef CHROOT
    853   1.73      salo 		if (trychroot)
    854   1.73      salo 			if (chroot(rootdir) != 0) {
    855   1.80  christos 				warning("failed to chroot to `%s': %m",
    856   1.73      salo 				    rootdir);
    857   1.91       dsl 				_exit(4); 	/* force single user mode */
    858   1.73      salo 			}
    859   1.73      salo #endif /* CHROOT */
    860   1.73      salo 
    861   1.70  christos 		(void)execv(INIT_BSHELL, __UNCONST(argv));
    862   1.80  christos 		stall("can't exec `%s' for `%s': %m", INIT_BSHELL, _PATH_RUNCOM);
    863   1.91       dsl 		_exit(5);	/* force single user mode */
    864   1.47  christos 		/*NOTREACHED*/
    865   1.47  christos 	case -1:
    866   1.80  christos 		emergency("can't fork for `%s' on `%s': %m", INIT_BSHELL,
    867   1.47  christos 		    _PATH_RUNCOM);
    868   1.47  christos 		while (waitpid(-1, NULL, WNOHANG) > 0)
    869    1.1       cgd 			continue;
    870   1.47  christos 		(void)sleep(STALL_TIMEOUT);
    871   1.47  christos 		return (state_func_t)single_user;
    872   1.47  christos 	default:
    873   1.47  christos 		break;
    874    1.8       cgd 	}
    875    1.8       cgd 
    876    1.8       cgd 	/*
    877    1.8       cgd 	 * Copied from single_user().  This is a bit paranoid.
    878    1.8       cgd 	 */
    879    1.8       cgd 	do {
    880    1.8       cgd 		if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
    881   1.50  christos 			collect_child(wpid, status);
    882    1.8       cgd 		if (wpid == -1) {
    883    1.8       cgd 			if (errno == EINTR)
    884    1.8       cgd 				continue;
    885   1.80  christos 			warning("wait for `%s' on `%s' failed: %m; going to "
    886   1.56     lukem 			    "single user mode", INIT_BSHELL, _PATH_RUNCOM);
    887   1.47  christos 			return (state_func_t)single_user;
    888    1.8       cgd 		}
    889    1.8       cgd 		if (wpid == pid && WIFSTOPPED(status)) {
    890   1.80  christos 			warning("`%s' on `%s' stopped, restarting",
    891   1.56     lukem 			    INIT_BSHELL, _PATH_RUNCOM);
    892   1.47  christos 			(void)kill(pid, SIGCONT);
    893    1.8       cgd 			wpid = -1;
    894    1.8       cgd 		}
    895    1.8       cgd 	} while (wpid != pid);
    896    1.8       cgd 
    897    1.8       cgd 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
    898    1.8       cgd 	    requested_transition == catatonia) {
    899    1.8       cgd 		/* /etc/rc executed /sbin/reboot; wait for the end quietly */
    900    1.8       cgd 		sigset_t s;
    901    1.8       cgd 
    902   1.47  christos 		(void)sigfillset(&s);
    903    1.8       cgd 		for (;;)
    904   1.47  christos 			(void)sigsuspend(&s);
    905    1.8       cgd 	}
    906    1.8       cgd 
    907    1.8       cgd 	if (!WIFEXITED(status)) {
    908   1.80  christos 		warning("`%s' on `%s' terminated abnormally, going to "
    909   1.56     lukem 		    "single user mode", INIT_BSHELL, _PATH_RUNCOM);
    910   1.47  christos 		return (state_func_t)single_user;
    911    1.8       cgd 	}
    912    1.8       cgd 
    913    1.8       cgd 	if (WEXITSTATUS(status))
    914   1.47  christos 		return (state_func_t)single_user;
    915    1.8       cgd 
    916   1.93    dyoung 	return (state_func_t)read_ttys;
    917   1.73      salo }
    918   1.73      salo 
    919   1.73      salo /*
    920   1.73      salo  * Run the system startup script.
    921   1.73      salo  */
    922  1.101     joerg static state_func_t
    923   1.73      salo runcom(void)
    924   1.73      salo {
    925   1.73      salo 	state_func_t next_step;
    926   1.73      salo 
    927   1.73      salo 	/* Run /etc/rc and choose next state depending on the result. */
    928   1.73      salo 	next_step = runetcrc(0);
    929   1.93    dyoung 	if (next_step != (state_func_t)read_ttys)
    930   1.93    dyoung 		return (state_func_t)next_step;
    931   1.73      salo 
    932   1.73      salo #ifdef CHROOT
    933   1.73      salo 	/*
    934   1.73      salo 	 * If init.root sysctl does not point to "/", we'll chroot and run
    935   1.73      salo 	 * The Real(tm) /etc/rc now.  Global variable rootdir will tell us
    936   1.73      salo 	 * where to go.
    937   1.73      salo 	 */
    938   1.73      salo 	if (shouldchroot()) {
    939   1.73      salo 		next_step = runetcrc(1);
    940   1.93    dyoung 		if (next_step != (state_func_t)read_ttys)
    941   1.93    dyoung 			return (state_func_t)next_step;
    942   1.73      salo 
    943   1.73      salo 		did_multiuser_chroot = 1;
    944   1.73      salo 	} else {
    945   1.73      salo 		did_multiuser_chroot = 0;
    946   1.73      salo 	}
    947   1.73      salo #endif /* CHROOT */
    948   1.73      salo 
    949   1.73      salo 	/*
    950  1.108   msaitoh 	 * Regardless of whether in chroot or not, we booted successfully.
    951   1.73      salo 	 * It's time to spawn gettys (ie. next_step's value at this point).
    952   1.73      salo 	 */
    953    1.8       cgd 	runcom_mode = AUTOBOOT;		/* the default */
    954    1.8       cgd 	/* NB: should send a message to the session logger to avoid blocking. */
    955   1.51  christos #ifdef SUPPORT_UTMPX
    956   1.50  christos 	logwtmpx("~", "reboot", "", 0, INIT_PROCESS);
    957   1.50  christos #endif
    958   1.51  christos #ifdef SUPPORT_UTMP
    959    1.8       cgd 	logwtmp("~", "reboot", "");
    960   1.50  christos #endif
    961   1.93    dyoung 	return (state_func_t)read_ttys;
    962    1.8       cgd }
    963    1.8       cgd 
    964    1.8       cgd /*
    965    1.8       cgd  * Open the session database.
    966    1.8       cgd  *
    967    1.8       cgd  * NB: We could pass in the size here; is it necessary?
    968    1.8       cgd  */
    969  1.101     joerg static int
    970   1.39       wiz start_session_db(void)
    971    1.8       cgd {
    972   1.43     lukem 
    973    1.8       cgd 	if (session_db && (*session_db->close)(session_db))
    974   1.47  christos 		emergency("session database close: %m");
    975    1.8       cgd 	if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) {
    976   1.47  christos 		emergency("session database open: %m");
    977   1.93    dyoung 		return 1;
    978    1.8       cgd 	}
    979   1.93    dyoung 	return 0;
    980    1.8       cgd 
    981    1.8       cgd }
    982    1.8       cgd 
    983    1.8       cgd /*
    984    1.8       cgd  * Add a new login session.
    985    1.8       cgd  */
    986  1.101     joerg static void
    987   1.39       wiz add_session(session_t *sp)
    988    1.8       cgd {
    989    1.8       cgd 	DBT key;
    990    1.8       cgd 	DBT data;
    991    1.8       cgd 
    992   1.55  christos 	if (session_db == NULL)
    993   1.55  christos 		return;
    994   1.55  christos 
    995    1.8       cgd 	key.data = &sp->se_process;
    996    1.8       cgd 	key.size = sizeof sp->se_process;
    997    1.8       cgd 	data.data = &sp;
    998    1.8       cgd 	data.size = sizeof sp;
    999    1.8       cgd 
   1000    1.8       cgd 	if ((*session_db->put)(session_db, &key, &data, 0))
   1001   1.47  christos 		emergency("insert %d: %m", sp->se_process);
   1002   1.77  christos #ifdef SUPPORT_UTMPX
   1003   1.77  christos 	session_utmpx(sp, 1);
   1004   1.77  christos #endif
   1005    1.8       cgd }
   1006    1.8       cgd 
   1007    1.8       cgd /*
   1008    1.8       cgd  * Delete an old login session.
   1009    1.8       cgd  */
   1010  1.101     joerg static void
   1011   1.39       wiz del_session(session_t *sp)
   1012    1.8       cgd {
   1013    1.8       cgd 	DBT key;
   1014    1.8       cgd 
   1015    1.8       cgd 	key.data = &sp->se_process;
   1016    1.8       cgd 	key.size = sizeof sp->se_process;
   1017    1.8       cgd 
   1018    1.8       cgd 	if ((*session_db->del)(session_db, &key, 0))
   1019   1.47  christos 		emergency("delete %d: %m", sp->se_process);
   1020   1.77  christos #ifdef SUPPORT_UTMPX
   1021   1.77  christos 	session_utmpx(sp, 0);
   1022   1.77  christos #endif
   1023    1.8       cgd }
   1024    1.8       cgd 
   1025    1.8       cgd /*
   1026    1.8       cgd  * Look up a login session by pid.
   1027    1.8       cgd  */
   1028  1.101     joerg static session_t *
   1029    1.8       cgd find_session(pid_t pid)
   1030    1.8       cgd {
   1031    1.8       cgd 	DBT key;
   1032    1.8       cgd 	DBT data;
   1033    1.8       cgd 	session_t *ret;
   1034    1.8       cgd 
   1035   1.55  christos 	if (session_db == NULL)
   1036   1.55  christos 		return NULL;
   1037   1.55  christos 
   1038    1.8       cgd 	key.data = &pid;
   1039    1.8       cgd 	key.size = sizeof pid;
   1040    1.8       cgd 	if ((*session_db->get)(session_db, &key, &data, 0) != 0)
   1041    1.8       cgd 		return 0;
   1042   1.47  christos 	(void)memmove(&ret, data.data, sizeof(ret));
   1043    1.8       cgd 	return ret;
   1044    1.8       cgd }
   1045    1.8       cgd 
   1046    1.8       cgd /*
   1047    1.8       cgd  * Construct an argument vector from a command line.
   1048    1.8       cgd  */
   1049  1.101     joerg static char **
   1050   1.39       wiz construct_argv(char *command)
   1051    1.8       cgd {
   1052   1.27     perry 	int argc = 0;
   1053   1.47  christos 	char **argv = malloc(((strlen(command) + 1) / 2 + 1) * sizeof (char *));
   1054    1.8       cgd 	static const char separators[] = " \t";
   1055    1.8       cgd 
   1056   1.71     chris 	if (argv == NULL)
   1057   1.93    dyoung 		return NULL;
   1058   1.71     chris 
   1059   1.71     chris 	if ((argv[argc++] = strtok(command, separators)) == 0) {
   1060   1.71     chris 		free(argv);
   1061   1.93    dyoung 		return NULL;
   1062   1.71     chris 	}
   1063   1.47  christos 	while ((argv[argc++] = strtok(NULL, separators)) != NULL)
   1064    1.8       cgd 		continue;
   1065   1.93    dyoung 	return argv;
   1066    1.8       cgd }
   1067    1.8       cgd 
   1068    1.8       cgd /*
   1069    1.8       cgd  * Deallocate a session descriptor.
   1070    1.8       cgd  */
   1071  1.101     joerg static void
   1072   1.39       wiz free_session(session_t *sp)
   1073    1.8       cgd {
   1074   1.43     lukem 
   1075    1.8       cgd 	free(sp->se_device);
   1076    1.8       cgd 	if (sp->se_getty) {
   1077    1.8       cgd 		free(sp->se_getty);
   1078    1.8       cgd 		free(sp->se_getty_argv);
   1079    1.8       cgd 	}
   1080    1.8       cgd 	if (sp->se_window) {
   1081    1.8       cgd 		free(sp->se_window);
   1082    1.8       cgd 		free(sp->se_window_argv);
   1083    1.8       cgd 	}
   1084    1.8       cgd 	free(sp);
   1085    1.8       cgd }
   1086    1.8       cgd 
   1087    1.8       cgd /*
   1088    1.8       cgd  * Allocate a new session descriptor.
   1089    1.8       cgd  */
   1090  1.101     joerg static session_t *
   1091   1.39       wiz new_session(session_t *sprev, int session_index, struct ttyent *typ)
   1092    1.8       cgd {
   1093   1.27     perry 	session_t *sp;
   1094    1.8       cgd 
   1095   1.47  christos 	if ((typ->ty_status & TTY_ON) == 0 || typ->ty_name == NULL ||
   1096   1.27     perry 	    typ->ty_getty == NULL)
   1097   1.93    dyoung 		return NULL;
   1098    1.8       cgd 
   1099   1.47  christos 	sp = malloc(sizeof (session_t));
   1100   1.55  christos 	if (sp == NULL)
   1101   1.55  christos 		return NULL;
   1102   1.84  christos 	(void)memset(sp, 0, sizeof *sp);
   1103    1.8       cgd 
   1104   1.21   mycroft 	sp->se_flags = SE_PRESENT;
   1105    1.8       cgd 	sp->se_index = session_index;
   1106    1.8       cgd 
   1107   1.59    itojun 	(void)asprintf(&sp->se_device, "%s%s", _PATH_DEV, typ->ty_name);
   1108  1.105   msaitoh 	if (!sp->se_device) {
   1109  1.105   msaitoh 		free(sp);
   1110   1.59    itojun 		return NULL;
   1111  1.105   msaitoh 	}
   1112    1.8       cgd 
   1113    1.8       cgd 	if (setupargv(sp, typ) == 0) {
   1114    1.8       cgd 		free_session(sp);
   1115   1.93    dyoung 		return NULL;
   1116    1.8       cgd 	}
   1117    1.8       cgd 
   1118   1.27     perry 	sp->se_next = NULL;
   1119   1.27     perry 	if (sprev == NULL) {
   1120    1.8       cgd 		sessions = sp;
   1121   1.27     perry 		sp->se_prev = NULL;
   1122    1.8       cgd 	} else {
   1123    1.8       cgd 		sprev->se_next = sp;
   1124    1.8       cgd 		sp->se_prev = sprev;
   1125    1.8       cgd 	}
   1126    1.8       cgd 
   1127   1.93    dyoung 	return sp;
   1128    1.8       cgd }
   1129    1.8       cgd 
   1130    1.8       cgd /*
   1131    1.8       cgd  * Calculate getty and if useful window argv vectors.
   1132    1.8       cgd  */
   1133  1.101     joerg static int
   1134   1.39       wiz setupargv(session_t *sp, struct ttyent *typ)
   1135    1.8       cgd {
   1136    1.8       cgd 
   1137    1.8       cgd 	if (sp->se_getty) {
   1138    1.8       cgd 		free(sp->se_getty);
   1139    1.8       cgd 		free(sp->se_getty_argv);
   1140    1.8       cgd 	}
   1141   1.59    itojun 	(void)asprintf(&sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name);
   1142   1.59    itojun 	if (!sp->se_getty)
   1143   1.93    dyoung 		return 0;
   1144    1.8       cgd 	sp->se_getty_argv = construct_argv(sp->se_getty);
   1145   1.27     perry 	if (sp->se_getty_argv == NULL) {
   1146   1.80  christos 		warning("can't parse getty for port `%s'", sp->se_device);
   1147    1.8       cgd 		free(sp->se_getty);
   1148   1.27     perry 		sp->se_getty = NULL;
   1149   1.93    dyoung 		return 0;
   1150    1.8       cgd 	}
   1151    1.8       cgd 	if (typ->ty_window) {
   1152    1.8       cgd 		if (sp->se_window)
   1153    1.8       cgd 			free(sp->se_window);
   1154    1.8       cgd 		sp->se_window = strdup(typ->ty_window);
   1155    1.8       cgd 		sp->se_window_argv = construct_argv(sp->se_window);
   1156   1.27     perry 		if (sp->se_window_argv == NULL) {
   1157   1.80  christos 			warning("can't parse window for port `%s'",
   1158   1.47  christos 			    sp->se_device);
   1159    1.8       cgd 			free(sp->se_window);
   1160   1.27     perry 			sp->se_window = NULL;
   1161   1.93    dyoung 			return 0;
   1162    1.1       cgd 		}
   1163    1.8       cgd 	}
   1164   1.93    dyoung 	return 1;
   1165    1.8       cgd }
   1166    1.8       cgd 
   1167    1.8       cgd /*
   1168    1.8       cgd  * Walk the list of ttys and create sessions for each active line.
   1169    1.8       cgd  */
   1170  1.101     joerg static state_func_t
   1171   1.39       wiz read_ttys(void)
   1172    1.8       cgd {
   1173    1.8       cgd 	int session_index = 0;
   1174   1.27     perry 	session_t *sp, *snext;
   1175   1.27     perry 	struct ttyent *typ;
   1176    1.8       cgd 
   1177   1.77  christos #ifdef SUPPORT_UTMPX
   1178   1.77  christos 	if (sessions == NULL) {
   1179   1.77  christos 		struct stat st;
   1180   1.77  christos 
   1181   1.77  christos 		make_utmpx("", BOOT_MSG, BOOT_TIME, 0, &boot_time, 0);
   1182   1.77  christos 
   1183   1.77  christos 		/*
   1184   1.99   mbalmer 		 * If wtmpx is not empty, pick the down time from there
   1185   1.77  christos 		 */
   1186   1.87    cbiere 		if (stat(_PATH_WTMPX, &st) != -1 && st.st_size != 0) {
   1187   1.77  christos 			struct timeval down_time;
   1188   1.77  christos 
   1189   1.77  christos 			TIMESPEC_TO_TIMEVAL(&down_time,
   1190   1.77  christos 			    st.st_atime > st.st_mtime ?
   1191   1.77  christos 			    &st.st_atimespec : &st.st_mtimespec);
   1192   1.77  christos 			make_utmpx("", DOWN_MSG, DOWN_TIME, 0, &down_time, 0);
   1193   1.77  christos 		}
   1194   1.77  christos 	}
   1195   1.77  christos #endif
   1196    1.8       cgd 	/*
   1197    1.8       cgd 	 * Destroy any previous session state.
   1198    1.8       cgd 	 * There shouldn't be any, but just in case...
   1199    1.8       cgd 	 */
   1200    1.8       cgd 	for (sp = sessions; sp; sp = snext) {
   1201   1.64  christos #ifndef LETS_GET_SMALL
   1202    1.8       cgd 		if (sp->se_process)
   1203   1.50  christos 			clear_session_logs(sp, 0);
   1204   1.64  christos #endif
   1205    1.8       cgd 		snext = sp->se_next;
   1206    1.8       cgd 		free_session(sp);
   1207    1.8       cgd 	}
   1208   1.27     perry 	sessions = NULL;
   1209   1.73      salo 
   1210   1.73      salo 	if (start_session_db()) {
   1211   1.80  christos 		warning("start_session_db failed, death");
   1212   1.73      salo #ifdef CHROOT
   1213   1.73      salo 		/* If /etc/rc ran in chroot, we want to kill any survivors. */
   1214   1.73      salo 		if (did_multiuser_chroot)
   1215   1.73      salo 			return (state_func_t)death;
   1216   1.73      salo 		else
   1217   1.73      salo #endif /* CHROOT */
   1218   1.73      salo 			return (state_func_t)single_user;
   1219   1.73      salo 	}
   1220   1.73      salo 
   1221   1.84  christos 	(void)do_setttyent();
   1222    1.8       cgd 
   1223    1.8       cgd 	/*
   1224    1.8       cgd 	 * Allocate a session entry for each active port.
   1225    1.8       cgd 	 * Note that sp starts at 0.
   1226    1.8       cgd 	 */
   1227   1.27     perry 	while ((typ = getttyent()) != NULL)
   1228   1.27     perry 		if ((snext = new_session(sp, ++session_index, typ)) != NULL)
   1229    1.8       cgd 			sp = snext;
   1230   1.84  christos 	(void)endttyent();
   1231    1.8       cgd 
   1232   1.47  christos 	return (state_func_t)multi_user;
   1233    1.8       cgd }
   1234    1.8       cgd 
   1235    1.8       cgd /*
   1236    1.8       cgd  * Start a window system running.
   1237    1.8       cgd  */
   1238  1.101     joerg static void
   1239   1.39       wiz start_window_system(session_t *sp)
   1240    1.8       cgd {
   1241    1.8       cgd 	pid_t pid;
   1242    1.8       cgd 	sigset_t mask;
   1243    1.8       cgd 
   1244    1.8       cgd 	if ((pid = fork()) == -1) {
   1245   1.80  christos 		emergency("can't fork for window system on port `%s': %m",
   1246   1.47  christos 		    sp->se_device);
   1247    1.8       cgd 		/* hope that getty fails and we can try again */
   1248    1.8       cgd 		return;
   1249    1.8       cgd 	}
   1250    1.8       cgd 
   1251    1.8       cgd 	if (pid)
   1252    1.8       cgd 		return;
   1253    1.8       cgd 
   1254   1.84  christos 	(void)sigemptyset(&mask);
   1255   1.84  christos 	(void)sigprocmask(SIG_SETMASK, &mask, NULL);
   1256    1.8       cgd 
   1257    1.8       cgd 	if (setsid() < 0)
   1258   1.80  christos 		emergency("setsid failed (window): %m");
   1259    1.8       cgd 
   1260   1.47  christos 	(void)execv(sp->se_window_argv[0], sp->se_window_argv);
   1261   1.80  christos 	stall("can't exec window system `%s' for port `%s': %m",
   1262   1.47  christos 	    sp->se_window_argv[0], sp->se_device);
   1263   1.91       dsl 	_exit(6);
   1264    1.8       cgd }
   1265    1.8       cgd 
   1266    1.8       cgd /*
   1267    1.8       cgd  * Start a login session running.
   1268    1.8       cgd  */
   1269  1.101     joerg static pid_t
   1270   1.39       wiz start_getty(session_t *sp)
   1271    1.8       cgd {
   1272    1.8       cgd 	pid_t pid;
   1273    1.8       cgd 	sigset_t mask;
   1274   1.47  christos 	time_t current_time = time(NULL);
   1275  1.109  jmcneill 	bool do_sleep = false;
   1276  1.109  jmcneill 
   1277  1.109  jmcneill 	if (current_time >= sp->se_started.tv_sec &&
   1278  1.109  jmcneill 	    current_time - sp->se_started.tv_sec < GETTY_SPACING) {
   1279  1.109  jmcneill 		if (++sp->se_nspace > GETTY_NSPACE) {
   1280  1.109  jmcneill 			sp->se_nspace = 0;
   1281  1.109  jmcneill 			do_sleep = true;
   1282  1.109  jmcneill 		}
   1283  1.109  jmcneill 	} else {
   1284  1.109  jmcneill 		sp->se_nspace = 0;
   1285  1.109  jmcneill 	}
   1286    1.8       cgd 
   1287    1.8       cgd 	/*
   1288    1.8       cgd 	 * fork(), not vfork() -- we can't afford to block.
   1289    1.8       cgd 	 */
   1290    1.8       cgd 	if ((pid = fork()) == -1) {
   1291   1.80  christos 		emergency("can't fork for getty on port `%s': %m",
   1292   1.80  christos 		    sp->se_device);
   1293    1.8       cgd 		return -1;
   1294    1.8       cgd 	}
   1295    1.8       cgd 
   1296    1.8       cgd 	if (pid)
   1297    1.8       cgd 		return pid;
   1298    1.8       cgd 
   1299   1.73      salo #ifdef CHROOT
   1300   1.73      salo 	/* If /etc/rc did proceed inside chroot, we have to try as well. */
   1301   1.73      salo 	if (did_multiuser_chroot)
   1302   1.73      salo 		if (chroot(rootdir) != 0) {
   1303   1.80  christos 			stall("can't chroot getty `%s' inside `%s': %m",
   1304   1.73      salo 			    sp->se_getty_argv[0], rootdir);
   1305   1.91       dsl 			_exit(7);
   1306   1.73      salo 		}
   1307   1.73      salo #endif /* CHROOT */
   1308   1.73      salo 
   1309  1.109  jmcneill 	if (do_sleep) {
   1310   1.80  christos 		warning("getty repeating too quickly on port `%s', sleeping",
   1311   1.47  christos 		    sp->se_device);
   1312   1.47  christos 		(void)sleep(GETTY_SLEEP);
   1313    1.8       cgd 	}
   1314    1.8       cgd 
   1315    1.8       cgd 	if (sp->se_window) {
   1316    1.8       cgd 		start_window_system(sp);
   1317   1.47  christos 		(void)sleep(WINDOW_WAIT);
   1318    1.1       cgd 	}
   1319    1.8       cgd 
   1320   1.47  christos 	(void)sigemptyset(&mask);
   1321   1.47  christos 	(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0);
   1322    1.8       cgd 
   1323   1.47  christos 	(void)execv(sp->se_getty_argv[0], sp->se_getty_argv);
   1324   1.80  christos 	stall("can't exec getty `%s' for port `%s': %m",
   1325   1.47  christos 	    sp->se_getty_argv[0], sp->se_device);
   1326   1.91       dsl 	_exit(8);
   1327   1.47  christos 	/*NOTREACHED*/
   1328    1.1       cgd }
   1329   1.77  christos #ifdef SUPPORT_UTMPX
   1330   1.77  christos static void
   1331   1.77  christos session_utmpx(const session_t *sp, int add)
   1332   1.77  christos {
   1333   1.77  christos 	const char *name = sp->se_getty ? sp->se_getty :
   1334   1.77  christos 	    (sp->se_window ? sp->se_window : "");
   1335   1.77  christos 	const char *line = sp->se_device + sizeof(_PATH_DEV) - 1;
   1336   1.77  christos 
   1337   1.77  christos 	make_utmpx(name, line, add ? LOGIN_PROCESS : DEAD_PROCESS,
   1338   1.77  christos 	    sp->se_process, &sp->se_started, sp->se_index);
   1339   1.77  christos }
   1340   1.77  christos 
   1341   1.77  christos static void
   1342   1.77  christos make_utmpx(const char *name, const char *line, int type, pid_t pid,
   1343   1.77  christos     const struct timeval *tv, int session)
   1344   1.77  christos {
   1345   1.77  christos 	struct utmpx ut;
   1346   1.87    cbiere 	const char *eline;
   1347   1.77  christos 
   1348   1.77  christos 	(void)memset(&ut, 0, sizeof(ut));
   1349   1.77  christos 	(void)strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
   1350   1.77  christos 	ut.ut_type = type;
   1351   1.77  christos 	(void)strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
   1352   1.77  christos 	ut.ut_pid = pid;
   1353   1.77  christos 	if (tv)
   1354   1.77  christos 		ut.ut_tv = *tv;
   1355   1.77  christos 	else
   1356   1.77  christos 		(void)gettimeofday(&ut.ut_tv, NULL);
   1357   1.77  christos 	ut.ut_session = session;
   1358   1.77  christos 
   1359   1.87    cbiere 	eline = line + strlen(line);
   1360   1.97     lukem 	if ((size_t)(eline - line) >= sizeof(ut.ut_id))
   1361   1.87    cbiere 		line = eline - sizeof(ut.ut_id);
   1362   1.77  christos 	(void)strncpy(ut.ut_id, line, sizeof(ut.ut_id));
   1363   1.77  christos 
   1364   1.77  christos 	if (pututxline(&ut) == NULL)
   1365   1.80  christos 		warning("can't add utmpx record for `%s': %m", ut.ut_line);
   1366   1.84  christos 	endutxent();
   1367   1.77  christos }
   1368   1.77  christos 
   1369   1.77  christos static char
   1370   1.77  christos get_runlevel(const state_t s)
   1371   1.77  christos {
   1372   1.77  christos 	if (s == (state_t)single_user)
   1373   1.77  christos 		return SINGLE_USER;
   1374   1.77  christos 	if (s == (state_t)runcom)
   1375   1.77  christos 		return RUNCOM;
   1376   1.77  christos 	if (s == (state_t)read_ttys)
   1377   1.77  christos 		return READ_TTYS;
   1378   1.77  christos 	if (s == (state_t)multi_user)
   1379   1.77  christos 		return MULTI_USER;
   1380   1.77  christos 	if (s == (state_t)clean_ttys)
   1381   1.77  christos 		return CLEAN_TTYS;
   1382   1.77  christos 	if (s == (state_t)catatonia)
   1383   1.77  christos 		return CATATONIA;
   1384   1.77  christos 	return DEATH;
   1385   1.77  christos }
   1386   1.77  christos 
   1387   1.77  christos static void
   1388   1.77  christos utmpx_set_runlevel(char old, char new)
   1389   1.77  christos {
   1390   1.77  christos 	struct utmpx ut;
   1391   1.77  christos 
   1392   1.81  christos 	/*
   1393   1.81  christos 	 * Don't record any transitions until we did the first transition
   1394   1.81  christos 	 * to read ttys, which is when we are guaranteed to have a read-write
   1395   1.81  christos 	 * /var. Perhaps use a different variable for this?
   1396   1.81  christos 	 */
   1397   1.81  christos 	if (sessions == NULL)
   1398   1.81  christos 		return;
   1399   1.81  christos 
   1400   1.77  christos 	(void)memset(&ut, 0, sizeof(ut));
   1401   1.77  christos 	(void)snprintf(ut.ut_line, sizeof(ut.ut_line), RUNLVL_MSG, new);
   1402   1.77  christos 	ut.ut_type = RUN_LVL;
   1403   1.77  christos 	(void)gettimeofday(&ut.ut_tv, NULL);
   1404   1.77  christos 	ut.ut_exit.e_exit = old;
   1405   1.77  christos 	ut.ut_exit.e_termination = new;
   1406   1.77  christos 	if (pututxline(&ut) == NULL)
   1407   1.80  christos 		warning("can't add utmpx record for `runlevel': %m");
   1408   1.84  christos 	endutxent();
   1409   1.77  christos }
   1410   1.77  christos #endif /* SUPPORT_UTMPX */
   1411   1.77  christos 
   1412   1.13       cgd #endif /* LETS_GET_SMALL */
   1413    1.1       cgd 
   1414    1.8       cgd /*
   1415    1.8       cgd  * Collect exit status for a child.
   1416    1.8       cgd  * If an exiting login, start a new login running.
   1417    1.8       cgd  */
   1418  1.101     joerg static void
   1419   1.50  christos collect_child(pid_t pid, int status)
   1420    1.1       cgd {
   1421   1.13       cgd #ifndef LETS_GET_SMALL
   1422   1.27     perry 	session_t *sp, *sprev, *snext;
   1423    1.8       cgd 
   1424    1.8       cgd 	if (! sessions)
   1425    1.8       cgd 		return;
   1426    1.8       cgd 
   1427   1.47  christos 	if ((sp = find_session(pid)) == NULL)
   1428    1.8       cgd 		return;
   1429    1.1       cgd 
   1430   1.50  christos 	clear_session_logs(sp, status);
   1431    1.8       cgd 	del_session(sp);
   1432    1.8       cgd 	sp->se_process = 0;
   1433    1.8       cgd 
   1434    1.8       cgd 	if (sp->se_flags & SE_SHUTDOWN) {
   1435   1.27     perry 		if ((sprev = sp->se_prev) != NULL)
   1436    1.8       cgd 			sprev->se_next = sp->se_next;
   1437    1.8       cgd 		else
   1438    1.8       cgd 			sessions = sp->se_next;
   1439   1.27     perry 		if ((snext = sp->se_next) != NULL)
   1440    1.8       cgd 			snext->se_prev = sp->se_prev;
   1441    1.8       cgd 		free_session(sp);
   1442    1.1       cgd 		return;
   1443    1.1       cgd 	}
   1444    1.8       cgd 
   1445    1.8       cgd 	if ((pid = start_getty(sp)) == -1) {
   1446    1.8       cgd 		/* serious trouble */
   1447    1.8       cgd 		requested_transition = clean_ttys;
   1448    1.1       cgd 		return;
   1449    1.1       cgd 	}
   1450    1.8       cgd 
   1451    1.8       cgd 	sp->se_process = pid;
   1452   1.77  christos 	(void)gettimeofday(&sp->se_started, NULL);
   1453    1.8       cgd 	add_session(sp);
   1454   1.13       cgd #endif /* LETS_GET_SMALL */
   1455    1.8       cgd }
   1456    1.8       cgd 
   1457    1.8       cgd /*
   1458    1.8       cgd  * Catch a signal and request a state transition.
   1459    1.8       cgd  */
   1460  1.101     joerg static void
   1461   1.39       wiz transition_handler(int sig)
   1462    1.8       cgd {
   1463    1.8       cgd 
   1464    1.8       cgd 	switch (sig) {
   1465   1.13       cgd #ifndef LETS_GET_SMALL
   1466    1.8       cgd 	case SIGHUP:
   1467    1.8       cgd 		requested_transition = clean_ttys;
   1468    1.8       cgd 		break;
   1469    1.8       cgd 	case SIGTERM:
   1470    1.8       cgd 		requested_transition = death;
   1471    1.8       cgd 		break;
   1472    1.8       cgd 	case SIGTSTP:
   1473    1.8       cgd 		requested_transition = catatonia;
   1474    1.8       cgd 		break;
   1475   1.13       cgd #endif /* LETS_GET_SMALL */
   1476    1.8       cgd 	default:
   1477    1.8       cgd 		requested_transition = 0;
   1478    1.8       cgd 		break;
   1479    1.8       cgd 	}
   1480    1.8       cgd }
   1481    1.8       cgd 
   1482   1.13       cgd #ifndef LETS_GET_SMALL
   1483    1.8       cgd /*
   1484    1.8       cgd  * Take the system multiuser.
   1485    1.8       cgd  */
   1486  1.101     joerg static state_func_t
   1487   1.39       wiz multi_user(void)
   1488    1.8       cgd {
   1489    1.8       cgd 	pid_t pid;
   1490   1.50  christos 	int status;
   1491   1.27     perry 	session_t *sp;
   1492    1.8       cgd 
   1493    1.8       cgd 	requested_transition = 0;
   1494    1.8       cgd 
   1495    1.8       cgd 	/*
   1496    1.8       cgd 	 * If the administrator has not set the security level to -1
   1497    1.8       cgd 	 * to indicate that the kernel should not run multiuser in secure
   1498    1.8       cgd 	 * mode, and the run script has not set a higher level of security
   1499    1.8       cgd 	 * than level 1, then put the kernel into secure mode.
   1500    1.8       cgd 	 */
   1501    1.8       cgd 	if (getsecuritylevel() == 0)
   1502    1.8       cgd 		setsecuritylevel(1);
   1503    1.8       cgd 
   1504    1.8       cgd 	for (sp = sessions; sp; sp = sp->se_next) {
   1505    1.8       cgd 		if (sp->se_process)
   1506    1.8       cgd 			continue;
   1507    1.8       cgd 		if ((pid = start_getty(sp)) == -1) {
   1508    1.8       cgd 			/* serious trouble */
   1509    1.8       cgd 			requested_transition = clean_ttys;
   1510    1.1       cgd 			break;
   1511    1.8       cgd 		}
   1512    1.8       cgd 		sp->se_process = pid;
   1513   1.77  christos 		(void)gettimeofday(&sp->se_started, NULL);
   1514    1.8       cgd 		add_session(sp);
   1515    1.1       cgd 	}
   1516    1.8       cgd 
   1517    1.8       cgd 	while (!requested_transition)
   1518   1.50  christos 		if ((pid = waitpid(-1, &status, 0)) != -1)
   1519   1.50  christos 			collect_child(pid, status);
   1520    1.8       cgd 
   1521   1.47  christos 	return (state_func_t)requested_transition;
   1522    1.1       cgd }
   1523    1.1       cgd 
   1524    1.8       cgd /*
   1525    1.8       cgd  * This is an n-squared algorithm.  We hope it isn't run often...
   1526    1.8       cgd  */
   1527  1.101     joerg static state_func_t
   1528   1.39       wiz clean_ttys(void)
   1529    1.1       cgd {
   1530   1.27     perry 	session_t *sp, *sprev;
   1531   1.27     perry 	struct ttyent *typ;
   1532   1.27     perry 	int session_index = 0;
   1533   1.27     perry 	int devlen;
   1534    1.8       cgd 
   1535   1.21   mycroft 	for (sp = sessions; sp; sp = sp->se_next)
   1536   1.21   mycroft 		sp->se_flags &= ~SE_PRESENT;
   1537   1.21   mycroft 
   1538   1.84  christos 	(void)do_setttyent();
   1539   1.73      salo 
   1540    1.8       cgd 	devlen = sizeof(_PATH_DEV) - 1;
   1541   1.27     perry 	while ((typ = getttyent()) != NULL) {
   1542    1.8       cgd 		++session_index;
   1543    1.8       cgd 
   1544    1.8       cgd 		for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
   1545    1.8       cgd 			if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
   1546    1.8       cgd 				break;
   1547    1.8       cgd 
   1548    1.8       cgd 		if (sp) {
   1549   1.21   mycroft 			sp->se_flags |= SE_PRESENT;
   1550    1.8       cgd 			if (sp->se_index != session_index) {
   1551   1.80  christos 				warning("port `%s' changed utmp index from "
   1552   1.47  christos 				    "%d to %d", sp->se_device, sp->se_index,
   1553   1.47  christos 				    session_index);
   1554    1.8       cgd 				sp->se_index = session_index;
   1555    1.8       cgd 			}
   1556    1.8       cgd 			if ((typ->ty_status & TTY_ON) == 0 ||
   1557    1.8       cgd 			    typ->ty_getty == 0) {
   1558    1.8       cgd 				sp->se_flags |= SE_SHUTDOWN;
   1559   1.55  christos 				if (sp->se_process != 0)
   1560   1.55  christos 					(void)kill(sp->se_process, SIGHUP);
   1561    1.8       cgd 				continue;
   1562    1.8       cgd 			}
   1563    1.8       cgd 			sp->se_flags &= ~SE_SHUTDOWN;
   1564    1.8       cgd 			if (setupargv(sp, typ) == 0) {
   1565   1.80  christos 				warning("can't parse getty for port `%s'",
   1566   1.47  christos 				    sp->se_device);
   1567    1.8       cgd 				sp->se_flags |= SE_SHUTDOWN;
   1568   1.55  christos 				if (sp->se_process != 0)
   1569   1.55  christos 					(void)kill(sp->se_process, SIGHUP);
   1570  1.109  jmcneill 			} else {
   1571  1.109  jmcneill 				sp->se_nspace = 0;
   1572    1.8       cgd 			}
   1573    1.8       cgd 			continue;
   1574    1.8       cgd 		}
   1575    1.8       cgd 
   1576   1.84  christos 		(void)new_session(sprev, session_index, typ);
   1577    1.8       cgd 	}
   1578    1.8       cgd 
   1579   1.84  christos 	(void)endttyent();
   1580   1.21   mycroft 
   1581   1.21   mycroft 	for (sp = sessions; sp; sp = sp->se_next)
   1582   1.21   mycroft 		if ((sp->se_flags & SE_PRESENT) == 0) {
   1583   1.21   mycroft 			sp->se_flags |= SE_SHUTDOWN;
   1584   1.55  christos 			if (sp->se_process != 0)
   1585   1.55  christos 				(void)kill(sp->se_process, SIGHUP);
   1586   1.21   mycroft 		}
   1587    1.1       cgd 
   1588   1.47  christos 	return (state_func_t)multi_user;
   1589    1.1       cgd }
   1590    1.1       cgd 
   1591    1.8       cgd /*
   1592    1.8       cgd  * Block further logins.
   1593    1.8       cgd  */
   1594  1.101     joerg static state_func_t
   1595   1.39       wiz catatonia(void)
   1596    1.1       cgd {
   1597   1.27     perry 	session_t *sp;
   1598    1.8       cgd 
   1599    1.8       cgd 	for (sp = sessions; sp; sp = sp->se_next)
   1600    1.8       cgd 		sp->se_flags |= SE_SHUTDOWN;
   1601    1.1       cgd 
   1602   1.47  christos 	return (state_func_t)multi_user;
   1603    1.1       cgd }
   1604   1.13       cgd #endif /* LETS_GET_SMALL */
   1605    1.1       cgd 
   1606    1.8       cgd /*
   1607    1.8       cgd  * Note SIGALRM.
   1608    1.8       cgd  */
   1609  1.101     joerg static void
   1610   1.47  christos /*ARGSUSED*/
   1611   1.39       wiz alrm_handler(int sig)
   1612    1.1       cgd {
   1613   1.43     lukem 
   1614    1.8       cgd 	clang = 1;
   1615    1.1       cgd }
   1616    1.1       cgd 
   1617   1.13       cgd #ifndef LETS_GET_SMALL
   1618    1.8       cgd /*
   1619    1.8       cgd  * Bring the system down to single user.
   1620    1.8       cgd  */
   1621  1.101     joerg static state_func_t
   1622   1.39       wiz death(void)
   1623    1.1       cgd {
   1624   1.27     perry 	session_t *sp;
   1625   1.50  christos 	int i, status;
   1626    1.8       cgd 	pid_t pid;
   1627    1.8       cgd 	static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL };
   1628    1.8       cgd 
   1629    1.8       cgd 	for (sp = sessions; sp; sp = sp->se_next)
   1630    1.8       cgd 		sp->se_flags |= SE_SHUTDOWN;
   1631    1.8       cgd 
   1632    1.8       cgd 	/* NB: should send a message to the session logger to avoid blocking. */
   1633   1.50  christos #ifdef SUPPORT_UTMPX
   1634   1.50  christos 	logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
   1635   1.50  christos #endif
   1636   1.50  christos #ifdef SUPPORT_UTMP
   1637    1.8       cgd 	logwtmp("~", "shutdown", "");
   1638   1.50  christos #endif
   1639    1.8       cgd 
   1640    1.8       cgd 	for (i = 0; i < 3; ++i) {
   1641    1.8       cgd 		if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
   1642   1.47  christos 			return (state_func_t)single_user;
   1643    1.8       cgd 
   1644    1.8       cgd 		clang = 0;
   1645   1.84  christos 		(void)alarm(DEATH_WATCH);
   1646    1.8       cgd 		do
   1647   1.50  christos 			if ((pid = waitpid(-1, &status, 0)) != -1)
   1648   1.50  christos 				collect_child(pid, status);
   1649    1.8       cgd 		while (clang == 0 && errno != ECHILD);
   1650    1.8       cgd 
   1651    1.8       cgd 		if (errno == ECHILD)
   1652   1.47  christos 			return (state_func_t)single_user;
   1653    1.8       cgd 	}
   1654    1.8       cgd 
   1655    1.8       cgd 	warning("some processes would not die; ps axl advised");
   1656    1.8       cgd 
   1657   1.47  christos 	return (state_func_t)single_user;
   1658    1.1       cgd }
   1659   1.13       cgd #endif /* LETS_GET_SMALL */
   1660   1.28  christos 
   1661   1.45       abs #ifdef MFS_DEV_IF_NO_CONSOLE
   1662   1.28  christos 
   1663   1.45       abs static int
   1664   1.45       abs mfs_dev(void)
   1665   1.28  christos {
   1666   1.28  christos 	/*
   1667   1.28  christos 	 * We cannot print errors so we bail out silently...
   1668   1.28  christos 	 */
   1669   1.28  christos 	pid_t pid;
   1670   1.30  drochner 	int status;
   1671   1.46     lukem 
   1672   1.45       abs 	/* If we have /dev/console, assume all is OK  */
   1673   1.91       dsl 	if (access(_PATH_CONSOLE, F_OK) == 0)
   1674   1.91       dsl 		return 0;
   1675   1.28  christos 
   1676   1.91       dsl #if 0	/* Useful for testing MAKEDEV */
   1677   1.91       dsl 	/* Mount an mfs over /mnt so we can create a console entry */
   1678   1.28  christos 	switch ((pid = fork())) {
   1679   1.28  christos 	case 0:
   1680   1.62       dsl 		(void)execl(INIT_MOUNT_MFS, "mount_mfs",
   1681   1.62       dsl 		    "-b", "4096", "-f", "512",
   1682   1.91       dsl 		    "-s", 64, "-n", 10,
   1683   1.68       dan 		    "-p", "0755",
   1684   1.91       dsl 		    "swap", "/mnt", NULL);
   1685   1.91       dsl 		_exit(9);
   1686   1.47  christos 		/*NOTREACHED*/
   1687   1.28  christos 
   1688   1.28  christos 	case -1:
   1689   1.45       abs 		return(-1);
   1690   1.28  christos 
   1691   1.28  christos 	default:
   1692   1.28  christos 		if (waitpid(pid, &status, 0) == -1)
   1693   1.45       abs 			return(-1);
   1694   1.28  christos 		if (status != 0)
   1695   1.45       abs 			return(-1);
   1696   1.28  christos 		break;
   1697   1.28  christos 	}
   1698   1.28  christos 
   1699   1.91       dsl 	{
   1700   1.95  christos 		dev_t dev;
   1701   1.28  christos #ifdef CPU_CONSDEV
   1702   1.91       dsl 		static int name[2] = { CTL_MACHDEP, CPU_CONSDEV };
   1703   1.91       dsl 		size_t olen;
   1704   1.91       dsl 		olen = sizeof(dev);
   1705   1.91       dsl 		if (sysctl(name, sizeof(name) / sizeof(name[0]), &dev, &olen,
   1706   1.91       dsl 		    NULL, 0) == -1)
   1707   1.46     lukem #endif
   1708   1.91       dsl 			dev = makedev(0, 0);
   1709   1.95  christos 
   1710   1.95  christos 		/* Make a console for us, so we can see things happening */
   1711   1.95  christos 		if (mknod("/mnt/console", 0666 | S_IFCHR, dev) == -1)
   1712   1.95  christos 			return(-1);
   1713   1.95  christos 		(void)freopen("/mnt/console", "a", stderr);
   1714   1.91       dsl 	}
   1715   1.28  christos 
   1716   1.91       dsl #endif
   1717   1.28  christos 
   1718   1.28  christos 	/* Run the makedev script to create devices */
   1719   1.28  christos 	switch ((pid = fork())) {
   1720   1.28  christos 	case 0:
   1721   1.84  christos 		(void)dup2(2, 1);	/* Give the script stdout */
   1722   1.60       dsl 		if (chdir("/dev") == 0)
   1723   1.62       dsl 			(void)execl(INIT_BSHELL, "sh",
   1724   1.91       dsl 			    access("./MAKEDEV", X_OK) == 0
   1725   1.91       dsl 				? "./MAKEDEV" : "/etc/MAKEDEV",
   1726   1.94       apb 			    "-MM", "init", NULL);
   1727   1.91       dsl 		_exit(10);
   1728   1.73      salo 		/* NOTREACHED */
   1729   1.28  christos 
   1730   1.28  christos 	case -1:
   1731   1.60       dsl 		break;
   1732   1.28  christos 
   1733   1.28  christos 	default:
   1734   1.28  christos 		if (waitpid(pid, &status, 0) == -1)
   1735   1.60       dsl 			break;
   1736   1.98       apb 		if (status != 0)
   1737  1.106  christos 			warn("MAKEDEV exit status %d", status);
   1738   1.98       apb 		/*
   1739   1.98       apb 		 * If /dev/console got created, then return 0
   1740   1.98       apb 		 * regardless of MAKEDEV exit status.
   1741   1.98       apb 		 */
   1742   1.91       dsl 		if (access(_PATH_CONSOLE, F_OK) == 0)
   1743   1.91       dsl 			return 0;
   1744   1.91       dsl 		_exit(11);
   1745   1.28  christos 	}
   1746   1.47  christos 	warn("Unable to run MAKEDEV");
   1747   1.91       dsl 	_exit(12);
   1748   1.28  christos }
   1749   1.28  christos #endif
   1750   1.73      salo 
   1751  1.102  christos #ifndef LETS_GET_SMALL
   1752  1.101     joerg static int
   1753   1.73      salo do_setttyent(void)
   1754   1.73      salo {
   1755   1.84  christos 	(void)endttyent();
   1756   1.73      salo #ifdef CHROOT
   1757   1.73      salo 	if (did_multiuser_chroot) {
   1758   1.73      salo 		char path[PATH_MAX];
   1759   1.73      salo 
   1760   1.84  christos 		(void)snprintf(path, sizeof(path), "%s/%s", rootdir, _PATH_TTYS);
   1761   1.73      salo 
   1762   1.73      salo 		return setttyentpath(path);
   1763   1.73      salo 	} else
   1764   1.73      salo #endif /* CHROOT */
   1765   1.73      salo 		return setttyent();
   1766   1.73      salo }
   1767  1.102  christos #endif
   1768   1.73      salo 
   1769   1.73      salo #if !defined(LETS_GET_SMALL) && defined(CHROOT)
   1770   1.73      salo 
   1771  1.101     joerg static int
   1772  1.103      matt createsysctlnode(void)
   1773   1.73      salo {
   1774   1.73      salo 	struct sysctlnode node;
   1775   1.73      salo 	int mib[2];
   1776   1.73      salo 	size_t len;
   1777   1.73      salo 
   1778   1.73      salo 	/*
   1779   1.73      salo 	 * Create top-level dynamic sysctl node.  Its child nodes will only
   1780   1.73      salo 	 * be readable by the superuser, since regular mortals should not
   1781   1.76      elad 	 * care ("Sssh, it's a secret!").
   1782   1.73      salo 	 */
   1783   1.73      salo 	len = sizeof(struct sysctlnode);
   1784   1.73      salo 	mib[0] = CTL_CREATE;
   1785   1.73      salo 
   1786   1.84  christos 	(void)memset(&node, 0, len);
   1787   1.73      salo 	node.sysctl_flags = SYSCTL_VERSION | CTLFLAG_READWRITE |
   1788   1.74      elad 	    CTLFLAG_PRIVATE | CTLTYPE_NODE;
   1789   1.73      salo 	node.sysctl_num = CTL_CREATE;
   1790   1.84  christos 	(void)snprintf(node.sysctl_name, SYSCTL_NAMELEN, "init");
   1791   1.73      salo 	if (sysctl(&mib[0], 1, &node, &len, &node, len) == -1) {
   1792   1.80  christos 		warning("could not create init node: %m");
   1793   1.93    dyoung 		return -1;
   1794   1.73      salo 	}
   1795   1.73      salo 
   1796   1.73      salo 	/*
   1797   1.73      salo 	 * Create second level dynamic node capable of holding pathname.
   1798   1.73      salo 	 * Provide "/" as the default value.
   1799   1.73      salo 	 */
   1800   1.73      salo 	len = sizeof(struct sysctlnode);
   1801   1.73      salo 	mib[0] = node.sysctl_num;
   1802   1.73      salo 	mib[1] = CTL_CREATE;
   1803   1.73      salo 
   1804   1.84  christos 	(void)memset(&node, 0, len);
   1805   1.73      salo 	node.sysctl_flags = SYSCTL_VERSION | CTLFLAG_READWRITE |
   1806   1.75      elad 	    CTLTYPE_STRING | CTLFLAG_OWNDATA;
   1807   1.73      salo 	node.sysctl_size = _POSIX_PATH_MAX;
   1808   1.73      salo 	node.sysctl_data = __UNCONST("/");
   1809   1.73      salo 	node.sysctl_num = CTL_CREATE;
   1810   1.84  christos 	(void)snprintf(node.sysctl_name, SYSCTL_NAMELEN, "root");
   1811   1.73      salo 	if (sysctl(&mib[0], 2, NULL, NULL, &node, len) == -1) {
   1812   1.80  christos 		warning("could not create init.root node: %m");
   1813   1.93    dyoung 		return -1;
   1814   1.73      salo 	}
   1815   1.73      salo 
   1816   1.93    dyoung 	return 0;
   1817   1.73      salo }
   1818   1.73      salo 
   1819  1.101     joerg static int
   1820  1.101     joerg shouldchroot(void)
   1821   1.73      salo {
   1822   1.73      salo 	struct sysctlnode node;
   1823   1.73      salo 	size_t len, cnt;
   1824   1.73      salo 	int mib;
   1825   1.73      salo 
   1826   1.73      salo 	len = sizeof(struct sysctlnode);
   1827   1.73      salo 
   1828   1.73      salo 	if (sysctlbyname("init.root", rootdir, &len, NULL, 0) == -1) {
   1829   1.80  christos 		warning("could not read init.root: %m");
   1830   1.73      salo 
   1831   1.73      salo 		/* Child killed our node. Recreate it. */
   1832   1.73      salo 		if (errno == ENOENT) {
   1833   1.73      salo 			/* Destroy whatever is left, recreate from scratch. */
   1834   1.73      salo 			if (sysctlnametomib("init", &mib, &cnt) != -1) {
   1835   1.84  christos 				(void)memset(&node, 0, sizeof(node));
   1836   1.73      salo 				node.sysctl_flags = SYSCTL_VERSION;
   1837   1.73      salo 				node.sysctl_num = mib;
   1838   1.73      salo 				mib = CTL_DESTROY;
   1839   1.73      salo 
   1840   1.73      salo 				(void)sysctl(&mib, 1, NULL, NULL, &node,
   1841   1.73      salo 				    sizeof(node));
   1842   1.73      salo 			}
   1843   1.73      salo 
   1844   1.84  christos 			(void)createsysctlnode();
   1845   1.73      salo 		}
   1846   1.73      salo 
   1847   1.73      salo 		/* We certainly won't chroot. */
   1848   1.93    dyoung 		return 0;
   1849   1.73      salo 	}
   1850   1.73      salo 
   1851   1.73      salo 	if (rootdir[len] != '\0' || strlen(rootdir) != len - 1) {
   1852   1.73      salo 		warning("init.root is not a string");
   1853   1.93    dyoung 		return 0;
   1854   1.73      salo 	}
   1855   1.73      salo 
   1856   1.73      salo 	if (strcmp(rootdir, "/") == 0)
   1857   1.93    dyoung 		return 0;
   1858   1.73      salo 
   1859   1.93    dyoung 	return 1;
   1860   1.73      salo }
   1861   1.73      salo 
   1862   1.73      salo #endif /* !LETS_GET_SMALL && CHROOT */
   1863