18fae3551SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
48fae3551SRodney W. Grimes * Copyright (c) 1991, 1993
58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved.
68fae3551SRodney W. Grimes *
78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by
88fae3551SRodney W. Grimes * Donn Seeley at Berkeley Software Design, Inc.
98fae3551SRodney W. Grimes *
108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions
128fae3551SRodney W. Grimes * are met:
138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software
208fae3551SRodney W. Grimes * without specific prior written permission.
218fae3551SRodney W. Grimes *
228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328fae3551SRodney W. Grimes * SUCH DAMAGE.
338fae3551SRodney W. Grimes */
348fae3551SRodney W. Grimes
358fae3551SRodney W. Grimes #include <sys/param.h>
367b0a665dSMitchell Horne #include <sys/boottrace.h>
378889c700SDavid Nugent #include <sys/ioctl.h>
383f5ac575SEdward Tomasz Napierala #include <sys/mman.h>
3957622f22SPoul-Henning Kamp #include <sys/mount.h>
407b0a665dSMitchell Horne #include <sys/reboot.h>
4186bf62dcSDavid Nugent #include <sys/stat.h>
427b0a665dSMitchell Horne #include <sys/sysctl.h>
431f083b1eSMaxime Henrion #include <sys/uio.h>
447b0a665dSMitchell Horne #include <sys/wait.h>
458fae3551SRodney W. Grimes
468fae3551SRodney W. Grimes #include <db.h>
477b0a665dSMitchell Horne #include <err.h>
488fae3551SRodney W. Grimes #include <errno.h>
498fae3551SRodney W. Grimes #include <fcntl.h>
501a7bec91SWarner Losh #include <kenv.h>
51423b6a39SAndrey A. Chernov #include <libutil.h>
521a37aa56SDavid E. O'Brien #include <paths.h>
538fae3551SRodney W. Grimes #include <signal.h>
547b0a665dSMitchell Horne #include <stdarg.h>
557b0a665dSMitchell Horne #include <stdbool.h>
568fae3551SRodney W. Grimes #include <stdio.h>
578fae3551SRodney W. Grimes #include <stdlib.h>
588fae3551SRodney W. Grimes #include <string.h>
598fae3551SRodney W. Grimes #include <syslog.h>
608fae3551SRodney W. Grimes #include <time.h>
618fae3551SRodney W. Grimes #include <ttyent.h>
628fae3551SRodney W. Grimes #include <unistd.h>
638fae3551SRodney W. Grimes
648fae3551SRodney W. Grimes #ifdef SECURE
658fae3551SRodney W. Grimes #include <pwd.h>
668fae3551SRodney W. Grimes #endif
678fae3551SRodney W. Grimes
681ef60eb1SDavid Nugent #ifdef LOGIN_CAP
691ef60eb1SDavid Nugent #include <login_cap.h>
701ef60eb1SDavid Nugent #endif
711ef60eb1SDavid Nugent
723f5ac575SEdward Tomasz Napierala #include "mntopts.h"
738fae3551SRodney W. Grimes #include "pathnames.h"
748fae3551SRodney W. Grimes
758fae3551SRodney W. Grimes /*
768fae3551SRodney W. Grimes * Sleep times; used to prevent thrashing.
778fae3551SRodney W. Grimes */
788fae3551SRodney W. Grimes #define GETTY_SPACING 5 /* N secs minimum getty spacing */
798fae3551SRodney W. Grimes #define GETTY_SLEEP 30 /* sleep N secs after spacing problem */
80b5df27e2SAndrey A. Chernov #define GETTY_NSPACE 3 /* max. spacing count to bring reaction */
818fae3551SRodney W. Grimes #define WINDOW_WAIT 3 /* wait N secs after starting window */
828fae3551SRodney W. Grimes #define STALL_TIMEOUT 30 /* wait N secs after warning */
838fae3551SRodney W. Grimes #define DEATH_WATCH 10 /* wait N secs for procs to die */
845df42cf4SPhilippe Charnier #define DEATH_SCRIPT 120 /* wait for 2min for /etc/rc.shutdown */
85e82d5545SDavid Nugent #define RESOURCE_RC "daemon"
86e82d5545SDavid Nugent #define RESOURCE_WINDOW "default"
87e82d5545SDavid Nugent #define RESOURCE_GETTY "default"
88*f3dba162SSebastien Bini #define SCRIPT_ARGV_SIZE 3 /* size of argv passed to execute_script, can be increased if needed */
898fae3551SRodney W. Grimes
9045cfb1dcSXin LI static void handle(sig_t, ...);
9145cfb1dcSXin LI static void delset(sigset_t *, ...);
928fae3551SRodney W. Grimes
9345cfb1dcSXin LI static void stall(const char *, ...) __printflike(1, 2);
9445cfb1dcSXin LI static void warning(const char *, ...) __printflike(1, 2);
9545cfb1dcSXin LI static void emergency(const char *, ...) __printflike(1, 2);
9645cfb1dcSXin LI static void disaster(int);
973f5ac575SEdward Tomasz Napierala static void revoke_ttys(void);
9845cfb1dcSXin LI static int runshutdown(void);
99ab03e6d5SXin LI static char *strk(char *);
1004d159769SKyle Evans static void runfinal(void);
1018fae3551SRodney W. Grimes
1028fae3551SRodney W. Grimes /*
1038fae3551SRodney W. Grimes * We really need a recursive typedef...
1048fae3551SRodney W. Grimes * The following at least guarantees that the return type of (*state_t)()
1058fae3551SRodney W. Grimes * is sufficiently wide to hold a function pointer.
1068fae3551SRodney W. Grimes */
10773bf18edSWarner Losh typedef long (*state_func_t)(void);
10873bf18edSWarner Losh typedef state_func_t (*state_t)(void);
1098fae3551SRodney W. Grimes
11045cfb1dcSXin LI static state_func_t single_user(void);
11145cfb1dcSXin LI static state_func_t runcom(void);
11245cfb1dcSXin LI static state_func_t read_ttys(void);
11345cfb1dcSXin LI static state_func_t multi_user(void);
11445cfb1dcSXin LI static state_func_t clean_ttys(void);
11545cfb1dcSXin LI static state_func_t catatonia(void);
11645cfb1dcSXin LI static state_func_t death(void);
117acf0ab06SJilles Tjoelker static state_func_t death_single(void);
1183f5ac575SEdward Tomasz Napierala static state_func_t reroot(void);
1193f5ac575SEdward Tomasz Napierala static state_func_t reroot_phase_two(void);
1208fae3551SRodney W. Grimes
12145cfb1dcSXin LI static state_func_t run_script(const char *);
1221a7bec91SWarner Losh
1231efe3c6bSEd Schouten static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT;
12477103ea3SPoul-Henning Kamp
125de08b516SMitchell Horne static bool Reboot = false;
1261efe3c6bSEd Schouten static int howto = RB_AUTOBOOT;
1278fae3551SRodney W. Grimes
128de08b516SMitchell Horne static bool devfs = false;
129377b6d1eSEdward Tomasz Napierala static char *init_path_argv0;
13057622f22SPoul-Henning Kamp
13145cfb1dcSXin LI static void transition(state_t);
13245cfb1dcSXin LI static state_t requested_transition;
133acf0ab06SJilles Tjoelker static state_t current_state = death_single;
1348fae3551SRodney W. Grimes
135f3c4a698SEdward Tomasz Napierala static void execute_script(char *argv[]);
1364c2c7b2cSEd Schouten static void open_console(void);
13745cfb1dcSXin LI static const char *get_shell(void);
138335fe94fSEdward Tomasz Napierala static void replace_init(char *path);
13945cfb1dcSXin LI static void write_stderr(const char *message);
1408fae3551SRodney W. Grimes
1418fae3551SRodney W. Grimes typedef struct init_session {
1428fae3551SRodney W. Grimes pid_t se_process; /* controlling process */
1438fae3551SRodney W. Grimes time_t se_started; /* used to avoid thrashing */
1448fae3551SRodney W. Grimes int se_flags; /* status of session */
1458fae3551SRodney W. Grimes #define SE_SHUTDOWN 0x1 /* session won't be restarted */
146b0b670eeSAlfred Perlstein #define SE_PRESENT 0x2 /* session is in /etc/ttys */
1471cde387cSEdward Tomasz Napierala #define SE_IFEXISTS 0x4 /* session defined as "onifexists" */
1481cde387cSEdward Tomasz Napierala #define SE_IFCONSOLE 0x8 /* session defined as "onifconsole" */
149b5df27e2SAndrey A. Chernov int se_nspace; /* spacing count */
1508fae3551SRodney W. Grimes char *se_device; /* filename of port */
1518fae3551SRodney W. Grimes char *se_getty; /* what to run on that port */
152b5df27e2SAndrey A. Chernov char *se_getty_argv_space; /* pre-parsed argument array space */
1538fae3551SRodney W. Grimes char **se_getty_argv; /* pre-parsed argument array */
1548fae3551SRodney W. Grimes char *se_window; /* window system (started only once) */
155b5df27e2SAndrey A. Chernov char *se_window_argv_space; /* pre-parsed argument array space */
1568fae3551SRodney W. Grimes char **se_window_argv; /* pre-parsed argument array */
157b5df27e2SAndrey A. Chernov char *se_type; /* default terminal type */
1588fae3551SRodney W. Grimes struct init_session *se_prev;
1598fae3551SRodney W. Grimes struct init_session *se_next;
1608fae3551SRodney W. Grimes } session_t;
1618fae3551SRodney W. Grimes
16245cfb1dcSXin LI static void free_session(session_t *);
1630b57dd6bSJilles Tjoelker static session_t *new_session(session_t *, struct ttyent *);
16445cfb1dcSXin LI static session_t *sessions;
1658fae3551SRodney W. Grimes
16645cfb1dcSXin LI static char **construct_argv(char *);
16745cfb1dcSXin LI static void start_window_system(session_t *);
16845cfb1dcSXin LI static void collect_child(pid_t);
16945cfb1dcSXin LI static pid_t start_getty(session_t *);
17045cfb1dcSXin LI static void transition_handler(int);
17145cfb1dcSXin LI static void alrm_handler(int);
17245cfb1dcSXin LI static void setsecuritylevel(int);
17345cfb1dcSXin LI static int getsecuritylevel(void);
17445cfb1dcSXin LI static int setupargv(session_t *, struct ttyent *);
175e82d5545SDavid Nugent #ifdef LOGIN_CAP
17645cfb1dcSXin LI static void setprocresources(const char *);
177e82d5545SDavid Nugent #endif
178de08b516SMitchell Horne static bool clang;
1798fae3551SRodney W. Grimes
18045cfb1dcSXin LI static int start_session_db(void);
18145cfb1dcSXin LI static void add_session(session_t *);
18245cfb1dcSXin LI static void del_session(session_t *);
18345cfb1dcSXin LI static session_t *find_session(pid_t);
18445cfb1dcSXin LI static DB *session_db;
1858fae3551SRodney W. Grimes
1868fae3551SRodney W. Grimes /*
1878fae3551SRodney W. Grimes * The mother of all processes.
1888fae3551SRodney W. Grimes */
1898fae3551SRodney W. Grimes int
main(int argc,char * argv[])19073bf18edSWarner Losh main(int argc, char *argv[])
1918fae3551SRodney W. Grimes {
1921a7bec91SWarner Losh state_t initial_transition = runcom;
1931a7bec91SWarner Losh char kenv_value[PATH_MAX];
1943f5ac575SEdward Tomasz Napierala int c, error;
1958fae3551SRodney W. Grimes struct sigaction sa;
1968fae3551SRodney W. Grimes sigset_t mask;
1978fae3551SRodney W. Grimes
1988fae3551SRodney W. Grimes /* Dispose of random users. */
199c5842835SPhilippe Charnier if (getuid() != 0)
200c5842835SPhilippe Charnier errx(1, "%s", strerror(EPERM));
2018fae3551SRodney W. Grimes
2027b0a665dSMitchell Horne BOOTTRACE("init(8) starting...");
2037b0a665dSMitchell Horne
2048fae3551SRodney W. Grimes /* System V users like to reexec init. */
2051681d659SRuslan Ermilov if (getpid() != 1) {
2061681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT
2071681d659SRuslan Ermilov /* So give them what they want */
2081681d659SRuslan Ermilov if (argc > 1) {
2091681d659SRuslan Ermilov if (strlen(argv[1]) == 1) {
2103d438ad6SDavid E. O'Brien char runlevel = *argv[1];
2113d438ad6SDavid E. O'Brien int sig;
2128fae3551SRodney W. Grimes
2131681d659SRuslan Ermilov switch (runlevel) {
2141681d659SRuslan Ermilov case '0': /* halt + poweroff */
2151681d659SRuslan Ermilov sig = SIGUSR2;
2161681d659SRuslan Ermilov break;
2171681d659SRuslan Ermilov case '1': /* single-user */
2181681d659SRuslan Ermilov sig = SIGTERM;
2191681d659SRuslan Ermilov break;
2201681d659SRuslan Ermilov case '6': /* reboot */
2211681d659SRuslan Ermilov sig = SIGINT;
2221681d659SRuslan Ermilov break;
2231681d659SRuslan Ermilov case 'c': /* block further logins */
2241681d659SRuslan Ermilov sig = SIGTSTP;
2251681d659SRuslan Ermilov break;
2261681d659SRuslan Ermilov case 'q': /* rescan /etc/ttys */
2271681d659SRuslan Ermilov sig = SIGHUP;
2281681d659SRuslan Ermilov break;
2293f5ac575SEdward Tomasz Napierala case 'r': /* remount root */
2303f5ac575SEdward Tomasz Napierala sig = SIGEMT;
2313f5ac575SEdward Tomasz Napierala break;
2321681d659SRuslan Ermilov default:
2331681d659SRuslan Ermilov goto invalid;
2341681d659SRuslan Ermilov }
2351681d659SRuslan Ermilov kill(1, sig);
2361681d659SRuslan Ermilov _exit(0);
2371681d659SRuslan Ermilov } else
2381681d659SRuslan Ermilov invalid:
2391681d659SRuslan Ermilov errx(1, "invalid run-level ``%s''", argv[1]);
2401681d659SRuslan Ermilov } else
2411681d659SRuslan Ermilov #endif
2421681d659SRuslan Ermilov errx(1, "already running");
2431681d659SRuslan Ermilov }
244377b6d1eSEdward Tomasz Napierala
245377b6d1eSEdward Tomasz Napierala init_path_argv0 = strdup(argv[0]);
246377b6d1eSEdward Tomasz Napierala if (init_path_argv0 == NULL)
247377b6d1eSEdward Tomasz Napierala err(1, "strdup");
248377b6d1eSEdward Tomasz Napierala
2498fae3551SRodney W. Grimes /*
2508fae3551SRodney W. Grimes * Note that this does NOT open a file...
2518fae3551SRodney W. Grimes * Does 'init' deserve its own facility number?
2528fae3551SRodney W. Grimes */
25306224a94SNeel Natu openlog("init", LOG_CONS, LOG_AUTH);
2548fae3551SRodney W. Grimes
2558fae3551SRodney W. Grimes /*
2568fae3551SRodney W. Grimes * Create an initial session.
2578fae3551SRodney W. Grimes */
2583f5ac575SEdward Tomasz Napierala if (setsid() < 0 && (errno != EPERM || getsid(0) != 1))
2598fae3551SRodney W. Grimes warning("initial setsid() failed: %m");
2608fae3551SRodney W. Grimes
2618fae3551SRodney W. Grimes /*
2628fae3551SRodney W. Grimes * Establish an initial user so that programs running
2638fae3551SRodney W. Grimes * single user do not freak out and die (like passwd).
2648fae3551SRodney W. Grimes */
2658fae3551SRodney W. Grimes if (setlogin("root") < 0)
2668fae3551SRodney W. Grimes warning("setlogin() failed: %m");
2678fae3551SRodney W. Grimes
2688fae3551SRodney W. Grimes /*
2698fae3551SRodney W. Grimes * This code assumes that we always get arguments through flags,
2708fae3551SRodney W. Grimes * never through bits set in some random machine register.
2718fae3551SRodney W. Grimes */
2723f5ac575SEdward Tomasz Napierala while ((c = getopt(argc, argv, "dsfr")) != -1)
2738fae3551SRodney W. Grimes switch (c) {
27457622f22SPoul-Henning Kamp case 'd':
275de08b516SMitchell Horne devfs = true;
27657622f22SPoul-Henning Kamp break;
2778fae3551SRodney W. Grimes case 's':
2781a7bec91SWarner Losh initial_transition = single_user;
2798fae3551SRodney W. Grimes break;
2808fae3551SRodney W. Grimes case 'f':
2818fae3551SRodney W. Grimes runcom_mode = FASTBOOT;
2828fae3551SRodney W. Grimes break;
2833f5ac575SEdward Tomasz Napierala case 'r':
2843f5ac575SEdward Tomasz Napierala initial_transition = reroot_phase_two;
2853f5ac575SEdward Tomasz Napierala break;
2868fae3551SRodney W. Grimes default:
2878fae3551SRodney W. Grimes warning("unrecognized flag '-%c'", c);
2888fae3551SRodney W. Grimes break;
2898fae3551SRodney W. Grimes }
2908fae3551SRodney W. Grimes
2918fae3551SRodney W. Grimes if (optind != argc)
2928fae3551SRodney W. Grimes warning("ignoring excess arguments");
2938fae3551SRodney W. Grimes
2941a7bec91SWarner Losh /*
2951a7bec91SWarner Losh * We catch or block signals rather than ignore them,
2961a7bec91SWarner Losh * so that they get reset on exec.
2971a7bec91SWarner Losh */
298a449e81eSBrooks Davis handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
299a449e81eSBrooks Davis SIGXCPU, SIGXFSZ, 0);
3003f5ac575SEdward Tomasz Napierala handle(transition_handler, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
30135c1d16eSWarner Losh SIGUSR1, SIGUSR2, SIGWINCH, 0);
3021a7bec91SWarner Losh handle(alrm_handler, SIGALRM, 0);
3031a7bec91SWarner Losh sigfillset(&mask);
3041a7bec91SWarner Losh delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS,
3053f5ac575SEdward Tomasz Napierala SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP,
30635c1d16eSWarner Losh SIGALRM, SIGUSR1, SIGUSR2, SIGWINCH, 0);
307d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL);
3081a7bec91SWarner Losh sigemptyset(&sa.sa_mask);
3091a7bec91SWarner Losh sa.sa_flags = 0;
3101a7bec91SWarner Losh sa.sa_handler = SIG_IGN;
311d1b1fe3aSEdward Tomasz Napierala sigaction(SIGTTIN, &sa, NULL);
312d1b1fe3aSEdward Tomasz Napierala sigaction(SIGTTOU, &sa, NULL);
3131a7bec91SWarner Losh
3141a7bec91SWarner Losh /*
3151a7bec91SWarner Losh * Paranoia.
3161a7bec91SWarner Losh */
3171a7bec91SWarner Losh close(0);
3181a7bec91SWarner Losh close(1);
3191a7bec91SWarner Losh close(2);
3201a7bec91SWarner Losh
321335fe94fSEdward Tomasz Napierala if (kenv(KENV_GET, "init_exec", kenv_value, sizeof(kenv_value)) > 0) {
322335fe94fSEdward Tomasz Napierala replace_init(kenv_value);
323335fe94fSEdward Tomasz Napierala _exit(0); /* reboot */
324335fe94fSEdward Tomasz Napierala }
325335fe94fSEdward Tomasz Napierala
3261a7bec91SWarner Losh if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) {
3271a7bec91SWarner Losh state_func_t next_transition;
3281a7bec91SWarner Losh
3292ef6931aSMarcelo Araujo if ((next_transition = run_script(kenv_value)) != NULL)
3301a7bec91SWarner Losh initial_transition = (state_t) next_transition;
3311a7bec91SWarner Losh }
3321a7bec91SWarner Losh
3331a7bec91SWarner Losh if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) {
3341a7bec91SWarner Losh if (chdir(kenv_value) != 0 || chroot(".") != 0)
3351a7bec91SWarner Losh warning("Can't chroot to %s: %m", kenv_value);
3361a7bec91SWarner Losh }
3371a7bec91SWarner Losh
3381a7bec91SWarner Losh /*
3391a7bec91SWarner Losh * Additional check if devfs needs to be mounted:
3401a7bec91SWarner Losh * If "/" and "/dev" have the same device number,
3411a7bec91SWarner Losh * then it hasn't been mounted yet.
3421a7bec91SWarner Losh */
3431a7bec91SWarner Losh if (!devfs) {
3441a7bec91SWarner Losh struct stat stst;
3451a7bec91SWarner Losh dev_t root_devno;
3461a7bec91SWarner Losh
3471a7bec91SWarner Losh stat("/", &stst);
3481a7bec91SWarner Losh root_devno = stst.st_dev;
3491a7bec91SWarner Losh if (stat("/dev", &stst) != 0)
3501a7bec91SWarner Losh warning("Can't stat /dev: %m");
3511a7bec91SWarner Losh else if (stst.st_dev == root_devno)
352de08b516SMitchell Horne devfs = true;
3531a7bec91SWarner Losh }
3541a7bec91SWarner Losh
35557622f22SPoul-Henning Kamp if (devfs) {
3561f083b1eSMaxime Henrion struct iovec iov[4];
357421b0201SPoul-Henning Kamp char *s;
358421b0201SPoul-Henning Kamp int i;
359421b0201SPoul-Henning Kamp
360ab03e6d5SXin LI char _fstype[] = "fstype";
361ab03e6d5SXin LI char _devfs[] = "devfs";
362ab03e6d5SXin LI char _fspath[] = "fspath";
363ab03e6d5SXin LI char _path_dev[]= _PATH_DEV;
364ab03e6d5SXin LI
365ab03e6d5SXin LI iov[0].iov_base = _fstype;
366ab03e6d5SXin LI iov[0].iov_len = sizeof(_fstype);
367ab03e6d5SXin LI iov[1].iov_base = _devfs;
368ab03e6d5SXin LI iov[1].iov_len = sizeof(_devfs);
369ab03e6d5SXin LI iov[2].iov_base = _fspath;
370ab03e6d5SXin LI iov[2].iov_len = sizeof(_fspath);
371421b0201SPoul-Henning Kamp /*
372421b0201SPoul-Henning Kamp * Try to avoid the trailing slash in _PATH_DEV.
373421b0201SPoul-Henning Kamp * Be *very* defensive.
374421b0201SPoul-Henning Kamp */
375421b0201SPoul-Henning Kamp s = strdup(_PATH_DEV);
376421b0201SPoul-Henning Kamp if (s != NULL) {
377421b0201SPoul-Henning Kamp i = strlen(s);
378421b0201SPoul-Henning Kamp if (i > 0 && s[i - 1] == '/')
379421b0201SPoul-Henning Kamp s[i - 1] = '\0';
3801f083b1eSMaxime Henrion iov[3].iov_base = s;
3811f083b1eSMaxime Henrion iov[3].iov_len = strlen(s) + 1;
382421b0201SPoul-Henning Kamp } else {
383ab03e6d5SXin LI iov[3].iov_base = _path_dev;
384ab03e6d5SXin LI iov[3].iov_len = sizeof(_path_dev);
38557622f22SPoul-Henning Kamp }
3861f083b1eSMaxime Henrion nmount(iov, 4, 0);
3871f083b1eSMaxime Henrion if (s != NULL)
3881f083b1eSMaxime Henrion free(s);
389421b0201SPoul-Henning Kamp }
39057622f22SPoul-Henning Kamp
3913f5ac575SEdward Tomasz Napierala if (initial_transition != reroot_phase_two) {
3923f5ac575SEdward Tomasz Napierala /*
3933f5ac575SEdward Tomasz Napierala * Unmount reroot leftovers. This runs after init(8)
3943f5ac575SEdward Tomasz Napierala * gets reexecuted after reroot_phase_two() is done.
3953f5ac575SEdward Tomasz Napierala */
3963f5ac575SEdward Tomasz Napierala error = unmount(_PATH_REROOT, MNT_FORCE);
3973f5ac575SEdward Tomasz Napierala if (error != 0 && errno != EINVAL)
3983f5ac575SEdward Tomasz Napierala warning("Cannot unmount %s: %m", _PATH_REROOT);
3993f5ac575SEdward Tomasz Napierala }
4003f5ac575SEdward Tomasz Napierala
4018fae3551SRodney W. Grimes /*
4028fae3551SRodney W. Grimes * Start the state machine.
4038fae3551SRodney W. Grimes */
4041a7bec91SWarner Losh transition(initial_transition);
4058fae3551SRodney W. Grimes
4068fae3551SRodney W. Grimes /*
4078fae3551SRodney W. Grimes * Should never reach here.
4088fae3551SRodney W. Grimes */
4098fae3551SRodney W. Grimes return 1;
4108fae3551SRodney W. Grimes }
4118fae3551SRodney W. Grimes
4128fae3551SRodney W. Grimes /*
4138fae3551SRodney W. Grimes * Associate a function with a signal handler.
4148fae3551SRodney W. Grimes */
41545cfb1dcSXin LI static void
handle(sig_t handler,...)4168fae3551SRodney W. Grimes handle(sig_t handler, ...)
4178fae3551SRodney W. Grimes {
4188fae3551SRodney W. Grimes int sig;
4198fae3551SRodney W. Grimes struct sigaction sa;
42039034633SJames Raynard sigset_t mask_everything;
4218fae3551SRodney W. Grimes va_list ap;
4228fae3551SRodney W. Grimes va_start(ap, handler);
4238fae3551SRodney W. Grimes
4248fae3551SRodney W. Grimes sa.sa_handler = handler;
4258fae3551SRodney W. Grimes sigfillset(&mask_everything);
4268fae3551SRodney W. Grimes
42730e8350cSBruce Evans while ((sig = va_arg(ap, int)) != 0) {
4288fae3551SRodney W. Grimes sa.sa_mask = mask_everything;
4298fae3551SRodney W. Grimes /* XXX SA_RESTART? */
4308fae3551SRodney W. Grimes sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0;
431d1b1fe3aSEdward Tomasz Napierala sigaction(sig, &sa, NULL);
4328fae3551SRodney W. Grimes }
4338fae3551SRodney W. Grimes va_end(ap);
4348fae3551SRodney W. Grimes }
4358fae3551SRodney W. Grimes
4368fae3551SRodney W. Grimes /*
4378fae3551SRodney W. Grimes * Delete a set of signals from a mask.
4388fae3551SRodney W. Grimes */
43945cfb1dcSXin LI static void
delset(sigset_t * maskp,...)4408fae3551SRodney W. Grimes delset(sigset_t *maskp, ...)
4418fae3551SRodney W. Grimes {
4428fae3551SRodney W. Grimes int sig;
4438fae3551SRodney W. Grimes va_list ap;
4448fae3551SRodney W. Grimes va_start(ap, maskp);
4458fae3551SRodney W. Grimes
44630e8350cSBruce Evans while ((sig = va_arg(ap, int)) != 0)
4478fae3551SRodney W. Grimes sigdelset(maskp, sig);
4488fae3551SRodney W. Grimes va_end(ap);
4498fae3551SRodney W. Grimes }
4508fae3551SRodney W. Grimes
4518fae3551SRodney W. Grimes /*
4528fae3551SRodney W. Grimes * Log a message and sleep for a while (to give someone an opportunity
4538fae3551SRodney W. Grimes * to read it and to save log or hardcopy output if the problem is chronic).
4548fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking.
4558fae3551SRodney W. Grimes */
45645cfb1dcSXin LI static void
stall(const char * message,...)4575979df34SKris Kennaway stall(const char *message, ...)
4588fae3551SRodney W. Grimes {
4598fae3551SRodney W. Grimes va_list ap;
4608fae3551SRodney W. Grimes va_start(ap, message);
4618fae3551SRodney W. Grimes
4628fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap);
4638fae3551SRodney W. Grimes va_end(ap);
4648fae3551SRodney W. Grimes sleep(STALL_TIMEOUT);
4658fae3551SRodney W. Grimes }
4668fae3551SRodney W. Grimes
4678fae3551SRodney W. Grimes /*
4688fae3551SRodney W. Grimes * Like stall(), but doesn't sleep.
4698fae3551SRodney W. Grimes * If cpp had variadic macros, the two functions could be #defines for another.
4708fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking.
4718fae3551SRodney W. Grimes */
47245cfb1dcSXin LI static void
warning(const char * message,...)4735979df34SKris Kennaway warning(const char *message, ...)
4748fae3551SRodney W. Grimes {
4758fae3551SRodney W. Grimes va_list ap;
4768fae3551SRodney W. Grimes va_start(ap, message);
4778fae3551SRodney W. Grimes
4788fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap);
4798fae3551SRodney W. Grimes va_end(ap);
4808fae3551SRodney W. Grimes }
4818fae3551SRodney W. Grimes
4828fae3551SRodney W. Grimes /*
4838fae3551SRodney W. Grimes * Log an emergency message.
4848fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking.
4858fae3551SRodney W. Grimes */
48645cfb1dcSXin LI static void
emergency(const char * message,...)4875979df34SKris Kennaway emergency(const char *message, ...)
4888fae3551SRodney W. Grimes {
4898fae3551SRodney W. Grimes va_list ap;
4908fae3551SRodney W. Grimes va_start(ap, message);
4918fae3551SRodney W. Grimes
4928fae3551SRodney W. Grimes vsyslog(LOG_EMERG, message, ap);
4938fae3551SRodney W. Grimes va_end(ap);
4948fae3551SRodney W. Grimes }
4958fae3551SRodney W. Grimes
4968fae3551SRodney W. Grimes /*
4978fae3551SRodney W. Grimes * Catch an unexpected signal.
4988fae3551SRodney W. Grimes */
49945cfb1dcSXin LI static void
disaster(int sig)50073bf18edSWarner Losh disaster(int sig)
5018fae3551SRodney W. Grimes {
502091abe40SDavid E. O'Brien
5038fae3551SRodney W. Grimes emergency("fatal signal: %s",
5048889c700SDavid Nugent (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal");
5058fae3551SRodney W. Grimes
5068fae3551SRodney W. Grimes sleep(STALL_TIMEOUT);
5078fae3551SRodney W. Grimes _exit(sig); /* reboot */
5088fae3551SRodney W. Grimes }
5098fae3551SRodney W. Grimes
5108fae3551SRodney W. Grimes /*
5118fae3551SRodney W. Grimes * Get the security level of the kernel.
5128fae3551SRodney W. Grimes */
51345cfb1dcSXin LI static int
getsecuritylevel(void)51473bf18edSWarner Losh getsecuritylevel(void)
5158fae3551SRodney W. Grimes {
5168fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5178fae3551SRodney W. Grimes int name[2], curlevel;
5188fae3551SRodney W. Grimes size_t len;
5198fae3551SRodney W. Grimes
5208fae3551SRodney W. Grimes name[0] = CTL_KERN;
5218fae3551SRodney W. Grimes name[1] = KERN_SECURELVL;
5228fae3551SRodney W. Grimes len = sizeof curlevel;
5238fae3551SRodney W. Grimes if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
524804b4afbSXin LI emergency("cannot get kernel security level: %m");
5258fae3551SRodney W. Grimes return (-1);
5268fae3551SRodney W. Grimes }
5278fae3551SRodney W. Grimes return (curlevel);
5288fae3551SRodney W. Grimes #else
5298fae3551SRodney W. Grimes return (-1);
5308fae3551SRodney W. Grimes #endif
5318fae3551SRodney W. Grimes }
5328fae3551SRodney W. Grimes
5338fae3551SRodney W. Grimes /*
5348fae3551SRodney W. Grimes * Set the security level of the kernel.
5358fae3551SRodney W. Grimes */
53645cfb1dcSXin LI static void
setsecuritylevel(int newlevel)53773bf18edSWarner Losh setsecuritylevel(int newlevel)
5388fae3551SRodney W. Grimes {
5398fae3551SRodney W. Grimes #ifdef KERN_SECURELVL
5408fae3551SRodney W. Grimes int name[2], curlevel;
5418fae3551SRodney W. Grimes
5428fae3551SRodney W. Grimes curlevel = getsecuritylevel();
5438fae3551SRodney W. Grimes if (newlevel == curlevel)
5448fae3551SRodney W. Grimes return;
5458fae3551SRodney W. Grimes name[0] = CTL_KERN;
5468fae3551SRodney W. Grimes name[1] = KERN_SECURELVL;
5478fae3551SRodney W. Grimes if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
5488fae3551SRodney W. Grimes emergency(
549804b4afbSXin LI "cannot change kernel security level from %d to %d: %m",
550804b4afbSXin LI curlevel, newlevel);
5518fae3551SRodney W. Grimes return;
5528fae3551SRodney W. Grimes }
5538fae3551SRodney W. Grimes #ifdef SECURE
5548fae3551SRodney W. Grimes warning("kernel security level changed from %d to %d",
5558fae3551SRodney W. Grimes curlevel, newlevel);
5568fae3551SRodney W. Grimes #endif
5578fae3551SRodney W. Grimes #endif
5588fae3551SRodney W. Grimes }
5598fae3551SRodney W. Grimes
5608fae3551SRodney W. Grimes /*
5618fae3551SRodney W. Grimes * Change states in the finite state machine.
5628fae3551SRodney W. Grimes * The initial state is passed as an argument.
5638fae3551SRodney W. Grimes */
56445cfb1dcSXin LI static void
transition(state_t s)56573bf18edSWarner Losh transition(state_t s)
5668fae3551SRodney W. Grimes {
567091abe40SDavid E. O'Brien
568acf0ab06SJilles Tjoelker current_state = s;
5698fae3551SRodney W. Grimes for (;;)
570acf0ab06SJilles Tjoelker current_state = (state_t) (*current_state)();
5718fae3551SRodney W. Grimes }
5728fae3551SRodney W. Grimes
5738fae3551SRodney W. Grimes /*
5748fae3551SRodney W. Grimes * Start a session and allocate a controlling terminal.
5758fae3551SRodney W. Grimes * Only called by children of init after forking.
5768fae3551SRodney W. Grimes */
57745cfb1dcSXin LI static void
open_console(void)5784c2c7b2cSEd Schouten open_console(void)
5798fae3551SRodney W. Grimes {
5808fae3551SRodney W. Grimes int fd;
5818fae3551SRodney W. Grimes
5826ee5808bSEd Schouten /*
5836ee5808bSEd Schouten * Try to open /dev/console. Open the device with O_NONBLOCK to
5846ee5808bSEd Schouten * prevent potential blocking on a carrier.
5856ee5808bSEd Schouten */
5864c2c7b2cSEd Schouten revoke(_PATH_CONSOLE);
5874c2c7b2cSEd Schouten if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
5886ee5808bSEd Schouten (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
5894c2c7b2cSEd Schouten if (login_tty(fd) == 0)
5904c2c7b2cSEd Schouten return;
5914c2c7b2cSEd Schouten close(fd);
5924c2c7b2cSEd Schouten }
5934c2c7b2cSEd Schouten
5944c2c7b2cSEd Schouten /* No luck. Log output to file if possible. */
5954c2c7b2cSEd Schouten if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
5964c2c7b2cSEd Schouten stall("cannot open null device.");
5978fae3551SRodney W. Grimes _exit(1);
5988fae3551SRodney W. Grimes }
5994c2c7b2cSEd Schouten if (fd != STDIN_FILENO) {
6004c2c7b2cSEd Schouten dup2(fd, STDIN_FILENO);
6014c2c7b2cSEd Schouten close(fd);
6028fae3551SRodney W. Grimes }
6034c2c7b2cSEd Schouten fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
6044c2c7b2cSEd Schouten if (fd == -1)
6054c2c7b2cSEd Schouten dup2(STDIN_FILENO, STDOUT_FILENO);
6064c2c7b2cSEd Schouten else if (fd != STDOUT_FILENO) {
6074c2c7b2cSEd Schouten dup2(fd, STDOUT_FILENO);
6084c2c7b2cSEd Schouten close(fd);
6094c2c7b2cSEd Schouten }
6104c2c7b2cSEd Schouten dup2(STDOUT_FILENO, STDERR_FILENO);
6118fae3551SRodney W. Grimes }
6128fae3551SRodney W. Grimes
61345cfb1dcSXin LI static const char *
get_shell(void)6141a7bec91SWarner Losh get_shell(void)
6151a7bec91SWarner Losh {
6161a7bec91SWarner Losh static char kenv_value[PATH_MAX];
6171a7bec91SWarner Losh
6181a7bec91SWarner Losh if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0)
6191a7bec91SWarner Losh return kenv_value;
6201a7bec91SWarner Losh else
6211a7bec91SWarner Losh return _PATH_BSHELL;
6221a7bec91SWarner Losh }
6231a7bec91SWarner Losh
62445cfb1dcSXin LI static void
write_stderr(const char * message)6251a7bec91SWarner Losh write_stderr(const char *message)
6261a7bec91SWarner Losh {
627091abe40SDavid E. O'Brien
6281a7bec91SWarner Losh write(STDERR_FILENO, message, strlen(message));
6291a7bec91SWarner Losh }
6301a7bec91SWarner Losh
6313f5ac575SEdward Tomasz Napierala static int
read_file(const char * path,void ** bufp,size_t * bufsizep)6323f5ac575SEdward Tomasz Napierala read_file(const char *path, void **bufp, size_t *bufsizep)
6333f5ac575SEdward Tomasz Napierala {
6343f5ac575SEdward Tomasz Napierala struct stat sb;
6353f5ac575SEdward Tomasz Napierala size_t bufsize;
6363f5ac575SEdward Tomasz Napierala void *buf;
6373f5ac575SEdward Tomasz Napierala ssize_t nbytes;
6383f5ac575SEdward Tomasz Napierala int error, fd;
6393f5ac575SEdward Tomasz Napierala
6403f5ac575SEdward Tomasz Napierala fd = open(path, O_RDONLY);
6413f5ac575SEdward Tomasz Napierala if (fd < 0) {
642804b4afbSXin LI emergency("%s: %m", path);
6433f5ac575SEdward Tomasz Napierala return (-1);
6443f5ac575SEdward Tomasz Napierala }
6453f5ac575SEdward Tomasz Napierala
6463f5ac575SEdward Tomasz Napierala error = fstat(fd, &sb);
6473f5ac575SEdward Tomasz Napierala if (error != 0) {
648804b4afbSXin LI emergency("fstat: %m");
649b9124fc3SEdward Tomasz Napierala close(fd);
6503f5ac575SEdward Tomasz Napierala return (error);
6513f5ac575SEdward Tomasz Napierala }
6523f5ac575SEdward Tomasz Napierala
6533f5ac575SEdward Tomasz Napierala bufsize = sb.st_size;
6543f5ac575SEdward Tomasz Napierala buf = malloc(bufsize);
6553f5ac575SEdward Tomasz Napierala if (buf == NULL) {
656804b4afbSXin LI emergency("malloc: %m");
657b9124fc3SEdward Tomasz Napierala close(fd);
6583f5ac575SEdward Tomasz Napierala return (error);
6593f5ac575SEdward Tomasz Napierala }
6603f5ac575SEdward Tomasz Napierala
6613f5ac575SEdward Tomasz Napierala nbytes = read(fd, buf, bufsize);
6623f5ac575SEdward Tomasz Napierala if (nbytes != (ssize_t)bufsize) {
663804b4afbSXin LI emergency("read: %m");
664b9124fc3SEdward Tomasz Napierala close(fd);
6653f5ac575SEdward Tomasz Napierala free(buf);
6663f5ac575SEdward Tomasz Napierala return (error);
6673f5ac575SEdward Tomasz Napierala }
6683f5ac575SEdward Tomasz Napierala
6693f5ac575SEdward Tomasz Napierala error = close(fd);
6703f5ac575SEdward Tomasz Napierala if (error != 0) {
671804b4afbSXin LI emergency("close: %m");
6723f5ac575SEdward Tomasz Napierala free(buf);
6733f5ac575SEdward Tomasz Napierala return (error);
6743f5ac575SEdward Tomasz Napierala }
6753f5ac575SEdward Tomasz Napierala
6763f5ac575SEdward Tomasz Napierala *bufp = buf;
6773f5ac575SEdward Tomasz Napierala *bufsizep = bufsize;
6783f5ac575SEdward Tomasz Napierala
6793f5ac575SEdward Tomasz Napierala return (0);
6803f5ac575SEdward Tomasz Napierala }
6813f5ac575SEdward Tomasz Napierala
6823f5ac575SEdward Tomasz Napierala static int
create_file(const char * path,const void * buf,size_t bufsize)683b9124fc3SEdward Tomasz Napierala create_file(const char *path, const void *buf, size_t bufsize)
6843f5ac575SEdward Tomasz Napierala {
6853f5ac575SEdward Tomasz Napierala ssize_t nbytes;
6863f5ac575SEdward Tomasz Napierala int error, fd;
6873f5ac575SEdward Tomasz Napierala
6883f5ac575SEdward Tomasz Napierala fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700);
6893f5ac575SEdward Tomasz Napierala if (fd < 0) {
690804b4afbSXin LI emergency("%s: %m", path);
6913f5ac575SEdward Tomasz Napierala return (-1);
6923f5ac575SEdward Tomasz Napierala }
6933f5ac575SEdward Tomasz Napierala
6943f5ac575SEdward Tomasz Napierala nbytes = write(fd, buf, bufsize);
6953f5ac575SEdward Tomasz Napierala if (nbytes != (ssize_t)bufsize) {
696804b4afbSXin LI emergency("write: %m");
697b9124fc3SEdward Tomasz Napierala close(fd);
6983f5ac575SEdward Tomasz Napierala return (-1);
6993f5ac575SEdward Tomasz Napierala }
7003f5ac575SEdward Tomasz Napierala
7013f5ac575SEdward Tomasz Napierala error = close(fd);
7023f5ac575SEdward Tomasz Napierala if (error != 0) {
703804b4afbSXin LI emergency("close: %m");
7043f5ac575SEdward Tomasz Napierala return (-1);
7053f5ac575SEdward Tomasz Napierala }
7063f5ac575SEdward Tomasz Napierala
7073f5ac575SEdward Tomasz Napierala return (0);
7083f5ac575SEdward Tomasz Napierala }
7093f5ac575SEdward Tomasz Napierala
7103f5ac575SEdward Tomasz Napierala static int
mount_tmpfs(const char * fspath)7113f5ac575SEdward Tomasz Napierala mount_tmpfs(const char *fspath)
7123f5ac575SEdward Tomasz Napierala {
7133f5ac575SEdward Tomasz Napierala struct iovec *iov;
7143f5ac575SEdward Tomasz Napierala char errmsg[255];
7153f5ac575SEdward Tomasz Napierala int error, iovlen;
7163f5ac575SEdward Tomasz Napierala
7173f5ac575SEdward Tomasz Napierala iov = NULL;
7183f5ac575SEdward Tomasz Napierala iovlen = 0;
7193f5ac575SEdward Tomasz Napierala memset(errmsg, 0, sizeof(errmsg));
7203f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "fstype",
7213f5ac575SEdward Tomasz Napierala __DECONST(void *, "tmpfs"), (size_t)-1);
7223f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "fspath",
7233f5ac575SEdward Tomasz Napierala __DECONST(void *, fspath), (size_t)-1);
7243f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "errmsg",
7253f5ac575SEdward Tomasz Napierala errmsg, sizeof(errmsg));
7263f5ac575SEdward Tomasz Napierala
7273f5ac575SEdward Tomasz Napierala error = nmount(iov, iovlen, 0);
7283f5ac575SEdward Tomasz Napierala if (error != 0) {
7293f5ac575SEdward Tomasz Napierala if (*errmsg != '\0') {
730804b4afbSXin LI emergency("cannot mount tmpfs on %s: %s: %m",
731804b4afbSXin LI fspath, errmsg);
7323f5ac575SEdward Tomasz Napierala } else {
733804b4afbSXin LI emergency("cannot mount tmpfs on %s: %m",
734804b4afbSXin LI fspath);
7353f5ac575SEdward Tomasz Napierala }
7363f5ac575SEdward Tomasz Napierala return (error);
7373f5ac575SEdward Tomasz Napierala }
7383f5ac575SEdward Tomasz Napierala return (0);
7393f5ac575SEdward Tomasz Napierala }
7403f5ac575SEdward Tomasz Napierala
7413f5ac575SEdward Tomasz Napierala static state_func_t
reroot(void)7423f5ac575SEdward Tomasz Napierala reroot(void)
7433f5ac575SEdward Tomasz Napierala {
7443f5ac575SEdward Tomasz Napierala void *buf;
745377b6d1eSEdward Tomasz Napierala size_t bufsize;
746377b6d1eSEdward Tomasz Napierala int error;
7473f5ac575SEdward Tomasz Napierala
748b9124fc3SEdward Tomasz Napierala buf = NULL;
749b9124fc3SEdward Tomasz Napierala bufsize = 0;
750b9124fc3SEdward Tomasz Napierala
7513f5ac575SEdward Tomasz Napierala revoke_ttys();
7523f5ac575SEdward Tomasz Napierala runshutdown();
7533f5ac575SEdward Tomasz Napierala
7543f5ac575SEdward Tomasz Napierala /*
7553f5ac575SEdward Tomasz Napierala * Make sure nobody can interfere with our scheme.
756126ba219SEdward Tomasz Napierala * Ignore ESRCH, which can apparently happen when
757126ba219SEdward Tomasz Napierala * there are no processes to kill.
7583f5ac575SEdward Tomasz Napierala */
7593f5ac575SEdward Tomasz Napierala error = kill(-1, SIGKILL);
760126ba219SEdward Tomasz Napierala if (error != 0 && errno != ESRCH) {
761804b4afbSXin LI emergency("kill(2) failed: %m");
7623f5ac575SEdward Tomasz Napierala goto out;
7633f5ac575SEdward Tomasz Napierala }
7643f5ac575SEdward Tomasz Napierala
7653f5ac575SEdward Tomasz Napierala /*
7663f5ac575SEdward Tomasz Napierala * Copy the init binary into tmpfs, so that we can unmount
7673f5ac575SEdward Tomasz Napierala * the old rootfs without committing suicide.
7683f5ac575SEdward Tomasz Napierala */
769377b6d1eSEdward Tomasz Napierala error = read_file(init_path_argv0, &buf, &bufsize);
7703f5ac575SEdward Tomasz Napierala if (error != 0)
7713f5ac575SEdward Tomasz Napierala goto out;
7723f5ac575SEdward Tomasz Napierala error = mount_tmpfs(_PATH_REROOT);
7733f5ac575SEdward Tomasz Napierala if (error != 0)
7743f5ac575SEdward Tomasz Napierala goto out;
7753f5ac575SEdward Tomasz Napierala error = create_file(_PATH_REROOT_INIT, buf, bufsize);
7763f5ac575SEdward Tomasz Napierala if (error != 0)
7773f5ac575SEdward Tomasz Napierala goto out;
7783f5ac575SEdward Tomasz Napierala
7793f5ac575SEdward Tomasz Napierala /*
7803f5ac575SEdward Tomasz Napierala * Execute the temporary init.
7813f5ac575SEdward Tomasz Napierala */
7823f5ac575SEdward Tomasz Napierala execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL);
783804b4afbSXin LI emergency("cannot exec %s: %m", _PATH_REROOT_INIT);
7843f5ac575SEdward Tomasz Napierala
7853f5ac575SEdward Tomasz Napierala out:
7863f5ac575SEdward Tomasz Napierala emergency("reroot failed; going to single user mode");
787b9124fc3SEdward Tomasz Napierala free(buf);
7883f5ac575SEdward Tomasz Napierala return (state_func_t) single_user;
7893f5ac575SEdward Tomasz Napierala }
7903f5ac575SEdward Tomasz Napierala
7913f5ac575SEdward Tomasz Napierala static state_func_t
reroot_phase_two(void)7923f5ac575SEdward Tomasz Napierala reroot_phase_two(void)
7933f5ac575SEdward Tomasz Napierala {
7943f5ac575SEdward Tomasz Napierala char init_path[PATH_MAX], *path, *path_component;
7953f5ac575SEdward Tomasz Napierala size_t init_path_len;
7963f5ac575SEdward Tomasz Napierala int nbytes, error;
7973f5ac575SEdward Tomasz Napierala
7983f5ac575SEdward Tomasz Napierala /*
7993f5ac575SEdward Tomasz Napierala * Ask the kernel to mount the new rootfs.
8003f5ac575SEdward Tomasz Napierala */
8013f5ac575SEdward Tomasz Napierala error = reboot(RB_REROOT);
8023f5ac575SEdward Tomasz Napierala if (error != 0) {
803804b4afbSXin LI emergency("RB_REBOOT failed: %m");
8043f5ac575SEdward Tomasz Napierala goto out;
8053f5ac575SEdward Tomasz Napierala }
8063f5ac575SEdward Tomasz Napierala
8073f5ac575SEdward Tomasz Napierala /*
8083f5ac575SEdward Tomasz Napierala * Figure out where the destination init(8) binary is. Note that
8093f5ac575SEdward Tomasz Napierala * the path could be different than what we've started with. Use
8103f5ac575SEdward Tomasz Napierala * the value from kenv, if set, or the one from sysctl otherwise.
8113f5ac575SEdward Tomasz Napierala * The latter defaults to a hardcoded value, but can be overridden
8123f5ac575SEdward Tomasz Napierala * by a build time option.
8133f5ac575SEdward Tomasz Napierala */
8143f5ac575SEdward Tomasz Napierala nbytes = kenv(KENV_GET, "init_path", init_path, sizeof(init_path));
8153f5ac575SEdward Tomasz Napierala if (nbytes <= 0) {
8163f5ac575SEdward Tomasz Napierala init_path_len = sizeof(init_path);
8173f5ac575SEdward Tomasz Napierala error = sysctlbyname("kern.init_path",
8183f5ac575SEdward Tomasz Napierala init_path, &init_path_len, NULL, 0);
8193f5ac575SEdward Tomasz Napierala if (error != 0) {
820804b4afbSXin LI emergency("failed to retrieve kern.init_path: %m");
8213f5ac575SEdward Tomasz Napierala goto out;
8223f5ac575SEdward Tomasz Napierala }
8233f5ac575SEdward Tomasz Napierala }
8243f5ac575SEdward Tomasz Napierala
8253f5ac575SEdward Tomasz Napierala /*
8263f5ac575SEdward Tomasz Napierala * Repeat the init search logic from sys/kern/init_path.c
8273f5ac575SEdward Tomasz Napierala */
8283f5ac575SEdward Tomasz Napierala path_component = init_path;
8293f5ac575SEdward Tomasz Napierala while ((path = strsep(&path_component, ":")) != NULL) {
8303f5ac575SEdward Tomasz Napierala /*
8313f5ac575SEdward Tomasz Napierala * Execute init(8) from the new rootfs.
8323f5ac575SEdward Tomasz Napierala */
8333f5ac575SEdward Tomasz Napierala execl(path, path, NULL);
8343f5ac575SEdward Tomasz Napierala }
835804b4afbSXin LI emergency("cannot exec init from %s: %m", init_path);
8363f5ac575SEdward Tomasz Napierala
8373f5ac575SEdward Tomasz Napierala out:
8383f5ac575SEdward Tomasz Napierala emergency("reroot failed; going to single user mode");
8393f5ac575SEdward Tomasz Napierala return (state_func_t) single_user;
8403f5ac575SEdward Tomasz Napierala }
8413f5ac575SEdward Tomasz Napierala
8428fae3551SRodney W. Grimes /*
8438fae3551SRodney W. Grimes * Bring the system up single user.
8448fae3551SRodney W. Grimes */
84545cfb1dcSXin LI static state_func_t
single_user(void)84673bf18edSWarner Losh single_user(void)
8478fae3551SRodney W. Grimes {
8488fae3551SRodney W. Grimes pid_t pid, wpid;
8498fae3551SRodney W. Grimes int status;
8508fae3551SRodney W. Grimes sigset_t mask;
8511a7bec91SWarner Losh const char *shell;
8528fae3551SRodney W. Grimes char *argv[2];
8538402d33aSKonstantin Belousov struct timeval tv, tn;
8548fae3551SRodney W. Grimes #ifdef SECURE
8558fae3551SRodney W. Grimes struct ttyent *typ;
8568fae3551SRodney W. Grimes struct passwd *pp;
8578fae3551SRodney W. Grimes static const char banner[] =
8588fae3551SRodney W. Grimes "Enter root password, or ^D to go multi-user\n";
8598fae3551SRodney W. Grimes char *clear, *password;
8608fae3551SRodney W. Grimes #endif
86163322c28SPoul-Henning Kamp #ifdef DEBUGSHELL
86263322c28SPoul-Henning Kamp char altshell[128];
86363322c28SPoul-Henning Kamp #endif
8648fae3551SRodney W. Grimes
865db8ad19dSJordan K. Hubbard if (Reboot) {
866a0a549c7SRuslan Ermilov /* Instead of going single user, let's reboot the machine */
8677b0a665dSMitchell Horne BOOTTRACE("shutting down the system");
868e460cfd3SNate Williams sync();
8694d159769SKyle Evans /* Run scripts after all processes have been terminated. */
8704d159769SKyle Evans runfinal();
871e10037dfSKonstantin Belousov if (reboot(howto) == -1) {
872804b4afbSXin LI emergency("reboot(%#x) failed, %m", howto);
873e10037dfSKonstantin Belousov _exit(1); /* panic and reboot */
874e10037dfSKonstantin Belousov }
875e10037dfSKonstantin Belousov warning("reboot(%#x) returned", howto);
876e10037dfSKonstantin Belousov _exit(0); /* panic as well */
877e460cfd3SNate Williams }
878e460cfd3SNate Williams
8797b0a665dSMitchell Horne BOOTTRACE("going to single user mode");
8801a7bec91SWarner Losh shell = get_shell();
8811a7bec91SWarner Losh
8828fae3551SRodney W. Grimes if ((pid = fork()) == 0) {
8838fae3551SRodney W. Grimes /*
8848fae3551SRodney W. Grimes * Start the single user session.
8858fae3551SRodney W. Grimes */
8864c2c7b2cSEd Schouten open_console();
8878fae3551SRodney W. Grimes
8888fae3551SRodney W. Grimes #ifdef SECURE
8898fae3551SRodney W. Grimes /*
8908fae3551SRodney W. Grimes * Check the root password.
8918fae3551SRodney W. Grimes * We don't care if the console is 'on' by default;
8928fae3551SRodney W. Grimes * it's the only tty that can be 'off' and 'secure'.
8938fae3551SRodney W. Grimes */
8948fae3551SRodney W. Grimes typ = getttynam("console");
8958fae3551SRodney W. Grimes pp = getpwnam("root");
896a69497d7SMatthew Dillon if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
897a69497d7SMatthew Dillon pp && *pp->pw_passwd) {
8981a7bec91SWarner Losh write_stderr(banner);
8998fae3551SRodney W. Grimes for (;;) {
9008fae3551SRodney W. Grimes clear = getpass("Password:");
9012ef6931aSMarcelo Araujo if (clear == NULL || *clear == '\0')
9028fae3551SRodney W. Grimes _exit(0);
9038fae3551SRodney W. Grimes password = crypt(clear, pp->pw_passwd);
904852f70b2SKyle Evans explicit_bzero(clear, _PASSWORD_LEN);
9052c9a33f5SConrad Meyer if (password != NULL &&
90629dcf726SKevin Lo strcmp(password, pp->pw_passwd) == 0)
9078fae3551SRodney W. Grimes break;
9088fae3551SRodney W. Grimes warning("single-user login failed\n");
9098fae3551SRodney W. Grimes }
9108fae3551SRodney W. Grimes }
9118fae3551SRodney W. Grimes endttyent();
9128fae3551SRodney W. Grimes endpwent();
9138fae3551SRodney W. Grimes #endif /* SECURE */
9148fae3551SRodney W. Grimes
9158fae3551SRodney W. Grimes #ifdef DEBUGSHELL
9168fae3551SRodney W. Grimes {
91763322c28SPoul-Henning Kamp char *cp = altshell;
9188fae3551SRodney W. Grimes int num;
9198fae3551SRodney W. Grimes
9201a7bec91SWarner Losh #define SHREQUEST "Enter full pathname of shell or RETURN for "
9211a7bec91SWarner Losh write_stderr(SHREQUEST);
9221a7bec91SWarner Losh write_stderr(shell);
9231a7bec91SWarner Losh write_stderr(": ");
9248fae3551SRodney W. Grimes while ((num = read(STDIN_FILENO, cp, 1)) != -1 &&
9258fae3551SRodney W. Grimes num != 0 && *cp != '\n' && cp < &altshell[127])
9268fae3551SRodney W. Grimes cp++;
9278fae3551SRodney W. Grimes *cp = '\0';
9288fae3551SRodney W. Grimes if (altshell[0] != '\0')
9298fae3551SRodney W. Grimes shell = altshell;
9308fae3551SRodney W. Grimes }
9318fae3551SRodney W. Grimes #endif /* DEBUGSHELL */
9328fae3551SRodney W. Grimes
9338fae3551SRodney W. Grimes /*
9348fae3551SRodney W. Grimes * Unblock signals.
9358fae3551SRodney W. Grimes * We catch all the interesting ones,
9368fae3551SRodney W. Grimes * and those are reset to SIG_DFL on exec.
9378fae3551SRodney W. Grimes */
9388fae3551SRodney W. Grimes sigemptyset(&mask);
939d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL);
9408fae3551SRodney W. Grimes
9418fae3551SRodney W. Grimes /*
9428fae3551SRodney W. Grimes * Fire off a shell.
9438fae3551SRodney W. Grimes * If the default one doesn't work, try the Bourne shell.
9448fae3551SRodney W. Grimes */
945ab03e6d5SXin LI
946ab03e6d5SXin LI char name[] = "-sh";
947ab03e6d5SXin LI
948ab03e6d5SXin LI argv[0] = name;
949be03cfddSEdward Tomasz Napierala argv[1] = NULL;
9508fae3551SRodney W. Grimes execv(shell, argv);
9518fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", shell);
9528fae3551SRodney W. Grimes execv(_PATH_BSHELL, argv);
9538fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", _PATH_BSHELL);
9548fae3551SRodney W. Grimes sleep(STALL_TIMEOUT);
9558fae3551SRodney W. Grimes _exit(1);
9568fae3551SRodney W. Grimes }
9578fae3551SRodney W. Grimes
9588fae3551SRodney W. Grimes if (pid == -1) {
9598fae3551SRodney W. Grimes /*
9608fae3551SRodney W. Grimes * We are seriously hosed. Do our best.
9618fae3551SRodney W. Grimes */
9628fae3551SRodney W. Grimes emergency("can't fork single-user shell, trying again");
9638fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0)
9648fae3551SRodney W. Grimes continue;
9658fae3551SRodney W. Grimes return (state_func_t) single_user;
9668fae3551SRodney W. Grimes }
9678fae3551SRodney W. Grimes
9688fae3551SRodney W. Grimes requested_transition = 0;
9698fae3551SRodney W. Grimes do {
9708fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
9718fae3551SRodney W. Grimes collect_child(wpid);
9728fae3551SRodney W. Grimes if (wpid == -1) {
9738fae3551SRodney W. Grimes if (errno == EINTR)
9748fae3551SRodney W. Grimes continue;
9758fae3551SRodney W. Grimes warning("wait for single-user shell failed: %m; restarting");
9768fae3551SRodney W. Grimes return (state_func_t) single_user;
9778fae3551SRodney W. Grimes }
9788fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) {
9798fae3551SRodney W. Grimes warning("init: shell stopped, restarting\n");
9808fae3551SRodney W. Grimes kill(pid, SIGCONT);
9818fae3551SRodney W. Grimes wpid = -1;
9828fae3551SRodney W. Grimes }
9838fae3551SRodney W. Grimes } while (wpid != pid && !requested_transition);
9848fae3551SRodney W. Grimes
9858fae3551SRodney W. Grimes if (requested_transition)
9868fae3551SRodney W. Grimes return (state_func_t) requested_transition;
9878fae3551SRodney W. Grimes
9888fae3551SRodney W. Grimes if (!WIFEXITED(status)) {
9898fae3551SRodney W. Grimes if (WTERMSIG(status) == SIGKILL) {
9908fae3551SRodney W. Grimes /*
9918fae3551SRodney W. Grimes * reboot(8) killed shell?
9928fae3551SRodney W. Grimes */
9938fae3551SRodney W. Grimes warning("single user shell terminated.");
9948402d33aSKonstantin Belousov gettimeofday(&tv, NULL);
9958402d33aSKonstantin Belousov tn = tv;
9968402d33aSKonstantin Belousov tv.tv_sec += STALL_TIMEOUT;
9978402d33aSKonstantin Belousov while (tv.tv_sec > tn.tv_sec || (tv.tv_sec ==
9988402d33aSKonstantin Belousov tn.tv_sec && tv.tv_usec > tn.tv_usec)) {
9998402d33aSKonstantin Belousov sleep(1);
10008402d33aSKonstantin Belousov gettimeofday(&tn, NULL);
10018402d33aSKonstantin Belousov }
10028fae3551SRodney W. Grimes _exit(0);
10038fae3551SRodney W. Grimes } else {
10048fae3551SRodney W. Grimes warning("single user shell terminated, restarting");
10058fae3551SRodney W. Grimes return (state_func_t) single_user;
10068fae3551SRodney W. Grimes }
10078fae3551SRodney W. Grimes }
10088fae3551SRodney W. Grimes
10098fae3551SRodney W. Grimes runcom_mode = FASTBOOT;
10108fae3551SRodney W. Grimes return (state_func_t) runcom;
10118fae3551SRodney W. Grimes }
10128fae3551SRodney W. Grimes
10138fae3551SRodney W. Grimes /*
10148fae3551SRodney W. Grimes * Run the system startup script.
10158fae3551SRodney W. Grimes */
101645cfb1dcSXin LI static state_func_t
runcom(void)101773bf18edSWarner Losh runcom(void)
10188fae3551SRodney W. Grimes {
10191a7bec91SWarner Losh state_func_t next_transition;
10201a7bec91SWarner Losh
10217b0a665dSMitchell Horne BOOTTRACE("/etc/rc starting...");
10222ef6931aSMarcelo Araujo if ((next_transition = run_script(_PATH_RUNCOM)) != NULL)
10231a7bec91SWarner Losh return next_transition;
10247b0a665dSMitchell Horne BOOTTRACE("/etc/rc finished");
10251a7bec91SWarner Losh
10261a7bec91SWarner Losh runcom_mode = AUTOBOOT; /* the default */
10271a7bec91SWarner Losh return (state_func_t) read_ttys;
10281a7bec91SWarner Losh }
10291a7bec91SWarner Losh
1030f3c4a698SEdward Tomasz Napierala static void
execute_script(char * argv[])1031f3c4a698SEdward Tomasz Napierala execute_script(char *argv[])
1032f3c4a698SEdward Tomasz Napierala {
1033f3c4a698SEdward Tomasz Napierala struct sigaction sa;
1034*f3dba162SSebastien Bini char* sh_argv[3 + SCRIPT_ARGV_SIZE];
1035f3c4a698SEdward Tomasz Napierala const char *shell, *script;
1036*f3dba162SSebastien Bini int error, sh_argv_len, i;
1037f3c4a698SEdward Tomasz Napierala
1038f3c4a698SEdward Tomasz Napierala bzero(&sa, sizeof(sa));
1039f3c4a698SEdward Tomasz Napierala sigemptyset(&sa.sa_mask);
1040f3c4a698SEdward Tomasz Napierala sa.sa_handler = SIG_IGN;
1041f3c4a698SEdward Tomasz Napierala sigaction(SIGTSTP, &sa, NULL);
1042f3c4a698SEdward Tomasz Napierala sigaction(SIGHUP, &sa, NULL);
1043f3c4a698SEdward Tomasz Napierala
1044f3c4a698SEdward Tomasz Napierala open_console();
1045f3c4a698SEdward Tomasz Napierala
1046f3c4a698SEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
1047f3c4a698SEdward Tomasz Napierala #ifdef LOGIN_CAP
1048f3c4a698SEdward Tomasz Napierala setprocresources(RESOURCE_RC);
1049f3c4a698SEdward Tomasz Napierala #endif
1050f3c4a698SEdward Tomasz Napierala
1051f3c4a698SEdward Tomasz Napierala /*
1052f3c4a698SEdward Tomasz Napierala * Try to directly execute the script first. If it
1053f3c4a698SEdward Tomasz Napierala * fails, try the old method of passing the script path
1054f3c4a698SEdward Tomasz Napierala * to sh(1). Don't complain if it fails because of
1055f3c4a698SEdward Tomasz Napierala * the missing execute bit.
1056f3c4a698SEdward Tomasz Napierala */
1057*f3dba162SSebastien Bini script = argv[0];
1058f3c4a698SEdward Tomasz Napierala error = access(script, X_OK);
1059f3c4a698SEdward Tomasz Napierala if (error == 0) {
1060*f3dba162SSebastien Bini execv(script, argv);
106198b1cf29SEd Maste warning("can't directly exec %s: %m", script);
1062f3c4a698SEdward Tomasz Napierala } else if (errno != EACCES) {
1063f3c4a698SEdward Tomasz Napierala warning("can't access %s: %m", script);
1064f3c4a698SEdward Tomasz Napierala }
1065f3c4a698SEdward Tomasz Napierala
1066f3c4a698SEdward Tomasz Napierala shell = get_shell();
1067*f3dba162SSebastien Bini sh_argv[0] = __DECONST(char*, shell);
1068*f3dba162SSebastien Bini sh_argv_len = 1;
1069*f3dba162SSebastien Bini #ifdef SECURE
1070*f3dba162SSebastien Bini if (strcmp(shell, _PATH_BSHELL) == 0) {
1071*f3dba162SSebastien Bini sh_argv[1] = __DECONST(char*, "-o");
1072*f3dba162SSebastien Bini sh_argv[2] = __DECONST(char*, "verify");
1073*f3dba162SSebastien Bini sh_argv_len = 3;
1074*f3dba162SSebastien Bini }
1075*f3dba162SSebastien Bini #endif
1076*f3dba162SSebastien Bini for (i = 0; i != SCRIPT_ARGV_SIZE; ++i)
1077*f3dba162SSebastien Bini sh_argv[i + sh_argv_len] = argv[i];
1078*f3dba162SSebastien Bini execv(shell, sh_argv);
1079f3c4a698SEdward Tomasz Napierala stall("can't exec %s for %s: %m", shell, script);
1080f3c4a698SEdward Tomasz Napierala }
1081f3c4a698SEdward Tomasz Napierala
10821a7bec91SWarner Losh /*
1083335fe94fSEdward Tomasz Napierala * Execute binary, replacing init(8) as PID 1.
1084335fe94fSEdward Tomasz Napierala */
1085335fe94fSEdward Tomasz Napierala static void
replace_init(char * path)1086335fe94fSEdward Tomasz Napierala replace_init(char *path)
1087335fe94fSEdward Tomasz Napierala {
1088*f3dba162SSebastien Bini char *argv[SCRIPT_ARGV_SIZE];
1089335fe94fSEdward Tomasz Napierala
1090*f3dba162SSebastien Bini argv[0] = path;
1091*f3dba162SSebastien Bini argv[1] = NULL;
1092335fe94fSEdward Tomasz Napierala
1093335fe94fSEdward Tomasz Napierala execute_script(argv);
1094335fe94fSEdward Tomasz Napierala }
1095335fe94fSEdward Tomasz Napierala
1096335fe94fSEdward Tomasz Napierala /*
10971a7bec91SWarner Losh * Run a shell script.
10981a7bec91SWarner Losh * Returns 0 on success, otherwise the next transition to enter:
10991a7bec91SWarner Losh * - single_user if fork/execv/waitpid failed, or if the script
11001a7bec91SWarner Losh * terminated with a signal or exit code != 0.
1101acf0ab06SJilles Tjoelker * - death_single if a SIGTERM was delivered to init(8).
11021a7bec91SWarner Losh */
110345cfb1dcSXin LI static state_func_t
run_script(const char * script)11041a7bec91SWarner Losh run_script(const char *script)
11051a7bec91SWarner Losh {
11068fae3551SRodney W. Grimes pid_t pid, wpid;
1107f3c4a698SEdward Tomasz Napierala int status;
1108*f3dba162SSebastien Bini char *argv[SCRIPT_ARGV_SIZE];
11091a7bec91SWarner Losh const char *shell;
11108fae3551SRodney W. Grimes
11111a7bec91SWarner Losh shell = get_shell();
11121a7bec91SWarner Losh
11138fae3551SRodney W. Grimes if ((pid = fork()) == 0) {
11148fae3551SRodney W. Grimes
1115ab03e6d5SXin LI char _autoboot[] = "autoboot";
1116ab03e6d5SXin LI
1117*f3dba162SSebastien Bini argv[0] = __DECONST(char *, script);
1118*f3dba162SSebastien Bini argv[1] = runcom_mode == AUTOBOOT ? _autoboot : NULL;
1119*f3dba162SSebastien Bini argv[2] = NULL;
11208fae3551SRodney W. Grimes
1121f3c4a698SEdward Tomasz Napierala execute_script(argv);
1122f3c4a698SEdward Tomasz Napierala sleep(STALL_TIMEOUT);
11238fae3551SRodney W. Grimes _exit(1); /* force single user mode */
11248fae3551SRodney W. Grimes }
11258fae3551SRodney W. Grimes
11268fae3551SRodney W. Grimes if (pid == -1) {
11271a7bec91SWarner Losh emergency("can't fork for %s on %s: %m", shell, script);
11288fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0)
11298fae3551SRodney W. Grimes continue;
11308fae3551SRodney W. Grimes sleep(STALL_TIMEOUT);
11318fae3551SRodney W. Grimes return (state_func_t) single_user;
11328fae3551SRodney W. Grimes }
11338fae3551SRodney W. Grimes
11348fae3551SRodney W. Grimes /*
11358fae3551SRodney W. Grimes * Copied from single_user(). This is a bit paranoid.
11368fae3551SRodney W. Grimes */
11376e8ff8b7SDag-Erling Smørgrav requested_transition = 0;
11388fae3551SRodney W. Grimes do {
11398fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
11408fae3551SRodney W. Grimes collect_child(wpid);
11413f5ac575SEdward Tomasz Napierala if (requested_transition == death_single ||
11423f5ac575SEdward Tomasz Napierala requested_transition == reroot)
11433f5ac575SEdward Tomasz Napierala return (state_func_t) requested_transition;
1144203f2c14SCorvin Köhne if (wpid == -1) {
11458fae3551SRodney W. Grimes if (errno == EINTR)
11468fae3551SRodney W. Grimes continue;
11471a7bec91SWarner Losh warning("wait for %s on %s failed: %m; going to "
11481a7bec91SWarner Losh "single user mode", shell, script);
11498fae3551SRodney W. Grimes return (state_func_t) single_user;
11508fae3551SRodney W. Grimes }
11518fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) {
11528fae3551SRodney W. Grimes warning("init: %s on %s stopped, restarting\n",
11531a7bec91SWarner Losh shell, script);
11548fae3551SRodney W. Grimes kill(pid, SIGCONT);
11558fae3551SRodney W. Grimes wpid = -1;
11568fae3551SRodney W. Grimes }
11578fae3551SRodney W. Grimes } while (wpid != pid);
11588fae3551SRodney W. Grimes
11598fae3551SRodney W. Grimes if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
11608fae3551SRodney W. Grimes requested_transition == catatonia) {
11618fae3551SRodney W. Grimes /* /etc/rc executed /sbin/reboot; wait for the end quietly */
11628fae3551SRodney W. Grimes sigset_t s;
11638fae3551SRodney W. Grimes
11648fae3551SRodney W. Grimes sigfillset(&s);
11658fae3551SRodney W. Grimes for (;;)
11668fae3551SRodney W. Grimes sigsuspend(&s);
11678fae3551SRodney W. Grimes }
11688fae3551SRodney W. Grimes
11698fae3551SRodney W. Grimes if (!WIFEXITED(status)) {
11701a7bec91SWarner Losh warning("%s on %s terminated abnormally, going to single "
11711a7bec91SWarner Losh "user mode", shell, script);
11728fae3551SRodney W. Grimes return (state_func_t) single_user;
11738fae3551SRodney W. Grimes }
11748fae3551SRodney W. Grimes
11758fae3551SRodney W. Grimes if (WEXITSTATUS(status))
11768fae3551SRodney W. Grimes return (state_func_t) single_user;
11778fae3551SRodney W. Grimes
11781a7bec91SWarner Losh return (state_func_t) 0;
11798fae3551SRodney W. Grimes }
11808fae3551SRodney W. Grimes
11818fae3551SRodney W. Grimes /*
11828fae3551SRodney W. Grimes * Open the session database.
11838fae3551SRodney W. Grimes *
11848fae3551SRodney W. Grimes * NB: We could pass in the size here; is it necessary?
11858fae3551SRodney W. Grimes */
118645cfb1dcSXin LI static int
start_session_db(void)118773bf18edSWarner Losh start_session_db(void)
11888fae3551SRodney W. Grimes {
11898fae3551SRodney W. Grimes if (session_db && (*session_db->close)(session_db))
1190804b4afbSXin LI emergency("session database close: %m");
119114adaa14SMarcelo Araujo if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL) {
1192804b4afbSXin LI emergency("session database open: %m");
11938fae3551SRodney W. Grimes return (1);
11948fae3551SRodney W. Grimes }
11958fae3551SRodney W. Grimes return (0);
11968fae3551SRodney W. Grimes
11978fae3551SRodney W. Grimes }
11988fae3551SRodney W. Grimes
11998fae3551SRodney W. Grimes /*
12008fae3551SRodney W. Grimes * Add a new login session.
12018fae3551SRodney W. Grimes */
120245cfb1dcSXin LI static void
add_session(session_t * sp)120373bf18edSWarner Losh add_session(session_t *sp)
12048fae3551SRodney W. Grimes {
12058fae3551SRodney W. Grimes DBT key;
12068fae3551SRodney W. Grimes DBT data;
12078fae3551SRodney W. Grimes
12088fae3551SRodney W. Grimes key.data = &sp->se_process;
12098fae3551SRodney W. Grimes key.size = sizeof sp->se_process;
12108fae3551SRodney W. Grimes data.data = &sp;
12118fae3551SRodney W. Grimes data.size = sizeof sp;
12128fae3551SRodney W. Grimes
12138fae3551SRodney W. Grimes if ((*session_db->put)(session_db, &key, &data, 0))
1214804b4afbSXin LI emergency("insert %d: %m", sp->se_process);
12158fae3551SRodney W. Grimes }
12168fae3551SRodney W. Grimes
12178fae3551SRodney W. Grimes /*
12188fae3551SRodney W. Grimes * Delete an old login session.
12198fae3551SRodney W. Grimes */
122045cfb1dcSXin LI static void
del_session(session_t * sp)122173bf18edSWarner Losh del_session(session_t *sp)
12228fae3551SRodney W. Grimes {
12238fae3551SRodney W. Grimes DBT key;
12248fae3551SRodney W. Grimes
12258fae3551SRodney W. Grimes key.data = &sp->se_process;
12268fae3551SRodney W. Grimes key.size = sizeof sp->se_process;
12278fae3551SRodney W. Grimes
12288fae3551SRodney W. Grimes if ((*session_db->del)(session_db, &key, 0))
1229804b4afbSXin LI emergency("delete %d: %m", sp->se_process);
12308fae3551SRodney W. Grimes }
12318fae3551SRodney W. Grimes
12328fae3551SRodney W. Grimes /*
12338fae3551SRodney W. Grimes * Look up a login session by pid.
12348fae3551SRodney W. Grimes */
123545cfb1dcSXin LI static session_t *
find_session(pid_t pid)12368fae3551SRodney W. Grimes find_session(pid_t pid)
12378fae3551SRodney W. Grimes {
12388fae3551SRodney W. Grimes DBT key;
12398fae3551SRodney W. Grimes DBT data;
12408fae3551SRodney W. Grimes session_t *ret;
12418fae3551SRodney W. Grimes
12428fae3551SRodney W. Grimes key.data = &pid;
12438fae3551SRodney W. Grimes key.size = sizeof pid;
12448fae3551SRodney W. Grimes if ((*session_db->get)(session_db, &key, &data, 0) != 0)
12458fae3551SRodney W. Grimes return 0;
12468fae3551SRodney W. Grimes bcopy(data.data, (char *)&ret, sizeof(ret));
12478fae3551SRodney W. Grimes return ret;
12488fae3551SRodney W. Grimes }
12498fae3551SRodney W. Grimes
12508fae3551SRodney W. Grimes /*
12518fae3551SRodney W. Grimes * Construct an argument vector from a command line.
12528fae3551SRodney W. Grimes */
125345cfb1dcSXin LI static char **
construct_argv(char * command)125473bf18edSWarner Losh construct_argv(char *command)
12558fae3551SRodney W. Grimes {
12563d438ad6SDavid E. O'Brien int argc = 0;
12573d438ad6SDavid E. O'Brien char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1)
12588fae3551SRodney W. Grimes * sizeof (char *));
12598fae3551SRodney W. Grimes
12602ef6931aSMarcelo Araujo if ((argv[argc++] = strk(command)) == NULL) {
12616be40c95SRuslan Ermilov free(argv);
12626be40c95SRuslan Ermilov return (NULL);
12636be40c95SRuslan Ermilov }
12648889c700SDavid Nugent while ((argv[argc++] = strk((char *) 0)) != NULL)
12658fae3551SRodney W. Grimes continue;
12668fae3551SRodney W. Grimes return argv;
12678fae3551SRodney W. Grimes }
12688fae3551SRodney W. Grimes
12698fae3551SRodney W. Grimes /*
12708fae3551SRodney W. Grimes * Deallocate a session descriptor.
12718fae3551SRodney W. Grimes */
127245cfb1dcSXin LI static void
free_session(session_t * sp)127373bf18edSWarner Losh free_session(session_t *sp)
12748fae3551SRodney W. Grimes {
12758fae3551SRodney W. Grimes free(sp->se_device);
12768fae3551SRodney W. Grimes if (sp->se_getty) {
12778fae3551SRodney W. Grimes free(sp->se_getty);
1278b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space);
12798fae3551SRodney W. Grimes free(sp->se_getty_argv);
12808fae3551SRodney W. Grimes }
12818fae3551SRodney W. Grimes if (sp->se_window) {
12828fae3551SRodney W. Grimes free(sp->se_window);
1283b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space);
12848fae3551SRodney W. Grimes free(sp->se_window_argv);
12858fae3551SRodney W. Grimes }
1286b5df27e2SAndrey A. Chernov if (sp->se_type)
1287b5df27e2SAndrey A. Chernov free(sp->se_type);
12888fae3551SRodney W. Grimes free(sp);
12898fae3551SRodney W. Grimes }
12908fae3551SRodney W. Grimes
12918fae3551SRodney W. Grimes /*
12928fae3551SRodney W. Grimes * Allocate a new session descriptor.
1293b0b670eeSAlfred Perlstein * Mark it SE_PRESENT.
12948fae3551SRodney W. Grimes */
129545cfb1dcSXin LI static session_t *
new_session(session_t * sprev,struct ttyent * typ)12960b57dd6bSJilles Tjoelker new_session(session_t *sprev, struct ttyent *typ)
12978fae3551SRodney W. Grimes {
12983d438ad6SDavid E. O'Brien session_t *sp;
12998fae3551SRodney W. Grimes
13008fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 ||
13018fae3551SRodney W. Grimes typ->ty_name == 0 ||
13028fae3551SRodney W. Grimes typ->ty_getty == 0)
13038fae3551SRodney W. Grimes return 0;
13048fae3551SRodney W. Grimes
13051054bb1eSAndrey A. Chernov sp = (session_t *) calloc(1, sizeof (session_t));
13068fae3551SRodney W. Grimes
1307b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT;
13088fae3551SRodney W. Grimes
13091cde387cSEdward Tomasz Napierala if ((typ->ty_status & TTY_IFEXISTS) != 0)
13101cde387cSEdward Tomasz Napierala sp->se_flags |= SE_IFEXISTS;
13111cde387cSEdward Tomasz Napierala
13121cde387cSEdward Tomasz Napierala if ((typ->ty_status & TTY_IFCONSOLE) != 0)
13131cde387cSEdward Tomasz Napierala sp->se_flags |= SE_IFCONSOLE;
13141cde387cSEdward Tomasz Napierala
131595595f99SXin LI if (asprintf(&sp->se_device, "%s%s", _PATH_DEV, typ->ty_name) < 0)
131695595f99SXin LI err(1, "asprintf");
13178fae3551SRodney W. Grimes
13188fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) {
13198fae3551SRodney W. Grimes free_session(sp);
13208fae3551SRodney W. Grimes return (0);
13218fae3551SRodney W. Grimes }
13228fae3551SRodney W. Grimes
13238fae3551SRodney W. Grimes sp->se_next = 0;
132414adaa14SMarcelo Araujo if (sprev == NULL) {
13258fae3551SRodney W. Grimes sessions = sp;
13268fae3551SRodney W. Grimes sp->se_prev = 0;
13278fae3551SRodney W. Grimes } else {
13288fae3551SRodney W. Grimes sprev->se_next = sp;
13298fae3551SRodney W. Grimes sp->se_prev = sprev;
13308fae3551SRodney W. Grimes }
13318fae3551SRodney W. Grimes
13328fae3551SRodney W. Grimes return sp;
13338fae3551SRodney W. Grimes }
13348fae3551SRodney W. Grimes
13358fae3551SRodney W. Grimes /*
13368fae3551SRodney W. Grimes * Calculate getty and if useful window argv vectors.
13378fae3551SRodney W. Grimes */
133845cfb1dcSXin LI static int
setupargv(session_t * sp,struct ttyent * typ)133973bf18edSWarner Losh setupargv(session_t *sp, struct ttyent *typ)
13408fae3551SRodney W. Grimes {
13418fae3551SRodney W. Grimes
13428fae3551SRodney W. Grimes if (sp->se_getty) {
13438fae3551SRodney W. Grimes free(sp->se_getty);
1344b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space);
13458fae3551SRodney W. Grimes free(sp->se_getty_argv);
13468fae3551SRodney W. Grimes }
134795595f99SXin LI if (asprintf(&sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name) < 0)
134895595f99SXin LI err(1, "asprintf");
1349b5df27e2SAndrey A. Chernov sp->se_getty_argv_space = strdup(sp->se_getty);
1350b5df27e2SAndrey A. Chernov sp->se_getty_argv = construct_argv(sp->se_getty_argv_space);
135114adaa14SMarcelo Araujo if (sp->se_getty_argv == NULL) {
13528fae3551SRodney W. Grimes warning("can't parse getty for port %s", sp->se_device);
13538fae3551SRodney W. Grimes free(sp->se_getty);
1354b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space);
1355b5df27e2SAndrey A. Chernov sp->se_getty = sp->se_getty_argv_space = 0;
13568fae3551SRodney W. Grimes return (0);
13578fae3551SRodney W. Grimes }
1358b5df27e2SAndrey A. Chernov if (sp->se_window) {
13598fae3551SRodney W. Grimes free(sp->se_window);
1360b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space);
1361b5df27e2SAndrey A. Chernov free(sp->se_window_argv);
1362b5df27e2SAndrey A. Chernov }
1363b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0;
1364b5df27e2SAndrey A. Chernov sp->se_window_argv = 0;
1365b5df27e2SAndrey A. Chernov if (typ->ty_window) {
13668fae3551SRodney W. Grimes sp->se_window = strdup(typ->ty_window);
1367b5df27e2SAndrey A. Chernov sp->se_window_argv_space = strdup(sp->se_window);
1368b5df27e2SAndrey A. Chernov sp->se_window_argv = construct_argv(sp->se_window_argv_space);
136914adaa14SMarcelo Araujo if (sp->se_window_argv == NULL) {
13708fae3551SRodney W. Grimes warning("can't parse window for port %s",
13718fae3551SRodney W. Grimes sp->se_device);
1372b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space);
13738fae3551SRodney W. Grimes free(sp->se_window);
1374b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0;
13758fae3551SRodney W. Grimes return (0);
13768fae3551SRodney W. Grimes }
13778fae3551SRodney W. Grimes }
1378b5df27e2SAndrey A. Chernov if (sp->se_type)
1379b5df27e2SAndrey A. Chernov free(sp->se_type);
1380b5df27e2SAndrey A. Chernov sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0;
13818fae3551SRodney W. Grimes return (1);
13828fae3551SRodney W. Grimes }
13838fae3551SRodney W. Grimes
13848fae3551SRodney W. Grimes /*
13858fae3551SRodney W. Grimes * Walk the list of ttys and create sessions for each active line.
13868fae3551SRodney W. Grimes */
138745cfb1dcSXin LI static state_func_t
read_ttys(void)138873bf18edSWarner Losh read_ttys(void)
13898fae3551SRodney W. Grimes {
13903d438ad6SDavid E. O'Brien session_t *sp, *snext;
13913d438ad6SDavid E. O'Brien struct ttyent *typ;
13928fae3551SRodney W. Grimes
13938fae3551SRodney W. Grimes /*
13948fae3551SRodney W. Grimes * Destroy any previous session state.
13958fae3551SRodney W. Grimes * There shouldn't be any, but just in case...
13968fae3551SRodney W. Grimes */
13978fae3551SRodney W. Grimes for (sp = sessions; sp; sp = snext) {
13988fae3551SRodney W. Grimes snext = sp->se_next;
13998fae3551SRodney W. Grimes free_session(sp);
14008fae3551SRodney W. Grimes }
14018fae3551SRodney W. Grimes sessions = 0;
14028fae3551SRodney W. Grimes if (start_session_db())
14038fae3551SRodney W. Grimes return (state_func_t) single_user;
14048fae3551SRodney W. Grimes
14058fae3551SRodney W. Grimes /*
14068fae3551SRodney W. Grimes * Allocate a session entry for each active port.
14078fae3551SRodney W. Grimes * Note that sp starts at 0.
14088fae3551SRodney W. Grimes */
14098889c700SDavid Nugent while ((typ = getttyent()) != NULL)
14100b57dd6bSJilles Tjoelker if ((snext = new_session(sp, typ)) != NULL)
14118fae3551SRodney W. Grimes sp = snext;
14128fae3551SRodney W. Grimes
14138fae3551SRodney W. Grimes endttyent();
14148fae3551SRodney W. Grimes
14158fae3551SRodney W. Grimes return (state_func_t) multi_user;
14168fae3551SRodney W. Grimes }
14178fae3551SRodney W. Grimes
14188fae3551SRodney W. Grimes /*
14198fae3551SRodney W. Grimes * Start a window system running.
14208fae3551SRodney W. Grimes */
142145cfb1dcSXin LI static void
start_window_system(session_t * sp)142273bf18edSWarner Losh start_window_system(session_t *sp)
14238fae3551SRodney W. Grimes {
14248fae3551SRodney W. Grimes pid_t pid;
14258fae3551SRodney W. Grimes sigset_t mask;
1426b5df27e2SAndrey A. Chernov char term[64], *env[2];
14275010c3b6SKonstantin Belousov int status;
14288fae3551SRodney W. Grimes
14298fae3551SRodney W. Grimes if ((pid = fork()) == -1) {
14308fae3551SRodney W. Grimes emergency("can't fork for window system on port %s: %m",
14318fae3551SRodney W. Grimes sp->se_device);
14328fae3551SRodney W. Grimes /* hope that getty fails and we can try again */
14338fae3551SRodney W. Grimes return;
14348fae3551SRodney W. Grimes }
1435091abe40SDavid E. O'Brien if (pid) {
14365010c3b6SKonstantin Belousov waitpid(-1, &status, 0);
14378fae3551SRodney W. Grimes return;
14385010c3b6SKonstantin Belousov }
14395010c3b6SKonstantin Belousov
14405010c3b6SKonstantin Belousov /* reparent window process to the init to not make a zombie on exit */
14415010c3b6SKonstantin Belousov if ((pid = fork()) == -1) {
14425010c3b6SKonstantin Belousov emergency("can't fork for window system on port %s: %m",
14435010c3b6SKonstantin Belousov sp->se_device);
14445010c3b6SKonstantin Belousov _exit(1);
14455010c3b6SKonstantin Belousov }
14465010c3b6SKonstantin Belousov if (pid)
14475010c3b6SKonstantin Belousov _exit(0);
14488fae3551SRodney W. Grimes
14498fae3551SRodney W. Grimes sigemptyset(&mask);
1450d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL);
14518fae3551SRodney W. Grimes
14528fae3551SRodney W. Grimes if (setsid() < 0)
14538fae3551SRodney W. Grimes emergency("setsid failed (window) %m");
14548fae3551SRodney W. Grimes
14551ef60eb1SDavid Nugent #ifdef LOGIN_CAP
14561ef60eb1SDavid Nugent setprocresources(RESOURCE_WINDOW);
14571ef60eb1SDavid Nugent #endif
1458b5df27e2SAndrey A. Chernov if (sp->se_type) {
1459b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */
1460b5df27e2SAndrey A. Chernov strcpy(term, "TERM=");
146195595f99SXin LI strlcat(term, sp->se_type, sizeof(term));
1462b5df27e2SAndrey A. Chernov env[0] = term;
1463be03cfddSEdward Tomasz Napierala env[1] = NULL;
1464b5df27e2SAndrey A. Chernov }
1465b5df27e2SAndrey A. Chernov else
1466be03cfddSEdward Tomasz Napierala env[0] = NULL;
1467b5df27e2SAndrey A. Chernov execve(sp->se_window_argv[0], sp->se_window_argv, env);
14688fae3551SRodney W. Grimes stall("can't exec window system '%s' for port %s: %m",
14698fae3551SRodney W. Grimes sp->se_window_argv[0], sp->se_device);
14708fae3551SRodney W. Grimes _exit(1);
14718fae3551SRodney W. Grimes }
14728fae3551SRodney W. Grimes
14738fae3551SRodney W. Grimes /*
14748fae3551SRodney W. Grimes * Start a login session running.
14758fae3551SRodney W. Grimes */
147645cfb1dcSXin LI static pid_t
start_getty(session_t * sp)147773bf18edSWarner Losh start_getty(session_t *sp)
14788fae3551SRodney W. Grimes {
14798fae3551SRodney W. Grimes pid_t pid;
14808fae3551SRodney W. Grimes sigset_t mask;
14818fae3551SRodney W. Grimes time_t current_time = time((time_t *) 0);
1482228d7ef2SAndrey A. Chernov int too_quick = 0;
1483b5df27e2SAndrey A. Chernov char term[64], *env[2];
14848fae3551SRodney W. Grimes
1485bb2e87c4SMike Pritchard if (current_time >= sp->se_started &&
1486228d7ef2SAndrey A. Chernov current_time - sp->se_started < GETTY_SPACING) {
1487228d7ef2SAndrey A. Chernov if (++sp->se_nspace > GETTY_NSPACE) {
1488228d7ef2SAndrey A. Chernov sp->se_nspace = 0;
1489228d7ef2SAndrey A. Chernov too_quick = 1;
1490228d7ef2SAndrey A. Chernov }
1491228d7ef2SAndrey A. Chernov } else
1492228d7ef2SAndrey A. Chernov sp->se_nspace = 0;
1493228d7ef2SAndrey A. Chernov
14948fae3551SRodney W. Grimes /*
14958fae3551SRodney W. Grimes * fork(), not vfork() -- we can't afford to block.
14968fae3551SRodney W. Grimes */
14978fae3551SRodney W. Grimes if ((pid = fork()) == -1) {
14988fae3551SRodney W. Grimes emergency("can't fork for getty on port %s: %m", sp->se_device);
14998fae3551SRodney W. Grimes return -1;
15008fae3551SRodney W. Grimes }
15018fae3551SRodney W. Grimes
15028fae3551SRodney W. Grimes if (pid)
15038fae3551SRodney W. Grimes return pid;
15048fae3551SRodney W. Grimes
1505228d7ef2SAndrey A. Chernov if (too_quick) {
1506b5df27e2SAndrey A. Chernov warning("getty repeating too quickly on port %s, sleeping %d secs",
1507b5df27e2SAndrey A. Chernov sp->se_device, GETTY_SLEEP);
15088fae3551SRodney W. Grimes sleep((unsigned) GETTY_SLEEP);
15098fae3551SRodney W. Grimes }
15108fae3551SRodney W. Grimes
15118fae3551SRodney W. Grimes if (sp->se_window) {
15128fae3551SRodney W. Grimes start_window_system(sp);
15138fae3551SRodney W. Grimes sleep(WINDOW_WAIT);
15148fae3551SRodney W. Grimes }
15158fae3551SRodney W. Grimes
15168fae3551SRodney W. Grimes sigemptyset(&mask);
1517d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL);
15188fae3551SRodney W. Grimes
15191ef60eb1SDavid Nugent #ifdef LOGIN_CAP
15201ef60eb1SDavid Nugent setprocresources(RESOURCE_GETTY);
15211ef60eb1SDavid Nugent #endif
1522b5df27e2SAndrey A. Chernov if (sp->se_type) {
1523b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */
1524b5df27e2SAndrey A. Chernov strcpy(term, "TERM=");
152595595f99SXin LI strlcat(term, sp->se_type, sizeof(term));
1526b5df27e2SAndrey A. Chernov env[0] = term;
1527be03cfddSEdward Tomasz Napierala env[1] = NULL;
1528091abe40SDavid E. O'Brien } else
1529be03cfddSEdward Tomasz Napierala env[0] = NULL;
1530b5df27e2SAndrey A. Chernov execve(sp->se_getty_argv[0], sp->se_getty_argv, env);
15318fae3551SRodney W. Grimes stall("can't exec getty '%s' for port %s: %m",
15328fae3551SRodney W. Grimes sp->se_getty_argv[0], sp->se_device);
15338fae3551SRodney W. Grimes _exit(1);
15348fae3551SRodney W. Grimes }
15358fae3551SRodney W. Grimes
15368fae3551SRodney W. Grimes /*
15371cde387cSEdward Tomasz Napierala * Return 1 if the session is defined as "onifexists"
15381cde387cSEdward Tomasz Napierala * or "onifconsole" and the device node does not exist.
15391cde387cSEdward Tomasz Napierala */
15401cde387cSEdward Tomasz Napierala static int
session_has_no_tty(session_t * sp)15411cde387cSEdward Tomasz Napierala session_has_no_tty(session_t *sp)
15421cde387cSEdward Tomasz Napierala {
15431cde387cSEdward Tomasz Napierala int fd;
15441cde387cSEdward Tomasz Napierala
15451cde387cSEdward Tomasz Napierala if ((sp->se_flags & SE_IFEXISTS) == 0 &&
15461cde387cSEdward Tomasz Napierala (sp->se_flags & SE_IFCONSOLE) == 0)
15471cde387cSEdward Tomasz Napierala return (0);
15481cde387cSEdward Tomasz Napierala
15491cde387cSEdward Tomasz Napierala fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0);
15501cde387cSEdward Tomasz Napierala if (fd < 0) {
15511cde387cSEdward Tomasz Napierala if (errno == ENOENT)
15521cde387cSEdward Tomasz Napierala return (1);
15531cde387cSEdward Tomasz Napierala return (0);
15541cde387cSEdward Tomasz Napierala }
15551cde387cSEdward Tomasz Napierala
15561cde387cSEdward Tomasz Napierala close(fd);
15571cde387cSEdward Tomasz Napierala return (0);
15581cde387cSEdward Tomasz Napierala }
15591cde387cSEdward Tomasz Napierala
15601cde387cSEdward Tomasz Napierala /*
15618fae3551SRodney W. Grimes * Collect exit status for a child.
15628fae3551SRodney W. Grimes * If an exiting login, start a new login running.
15638fae3551SRodney W. Grimes */
156445cfb1dcSXin LI static void
collect_child(pid_t pid)15658fae3551SRodney W. Grimes collect_child(pid_t pid)
15668fae3551SRodney W. Grimes {
15673d438ad6SDavid E. O'Brien session_t *sp, *sprev, *snext;
15688fae3551SRodney W. Grimes
15698fae3551SRodney W. Grimes if (! sessions)
15708fae3551SRodney W. Grimes return;
15718fae3551SRodney W. Grimes
15728fae3551SRodney W. Grimes if (! (sp = find_session(pid)))
15738fae3551SRodney W. Grimes return;
15748fae3551SRodney W. Grimes
15758fae3551SRodney W. Grimes del_session(sp);
15768fae3551SRodney W. Grimes sp->se_process = 0;
15778fae3551SRodney W. Grimes
15781cde387cSEdward Tomasz Napierala if (sp->se_flags & SE_SHUTDOWN ||
15791cde387cSEdward Tomasz Napierala session_has_no_tty(sp)) {
15808889c700SDavid Nugent if ((sprev = sp->se_prev) != NULL)
15818fae3551SRodney W. Grimes sprev->se_next = sp->se_next;
15828fae3551SRodney W. Grimes else
15838fae3551SRodney W. Grimes sessions = sp->se_next;
15848889c700SDavid Nugent if ((snext = sp->se_next) != NULL)
15858fae3551SRodney W. Grimes snext->se_prev = sp->se_prev;
15868fae3551SRodney W. Grimes free_session(sp);
15878fae3551SRodney W. Grimes return;
15888fae3551SRodney W. Grimes }
15898fae3551SRodney W. Grimes
15908fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) {
15918fae3551SRodney W. Grimes /* serious trouble */
15928fae3551SRodney W. Grimes requested_transition = clean_ttys;
15938fae3551SRodney W. Grimes return;
15948fae3551SRodney W. Grimes }
15958fae3551SRodney W. Grimes
15968fae3551SRodney W. Grimes sp->se_process = pid;
15978fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0);
15988fae3551SRodney W. Grimes add_session(sp);
15998fae3551SRodney W. Grimes }
16008fae3551SRodney W. Grimes
16017b0a665dSMitchell Horne static const char *
get_current_state(void)16027b0a665dSMitchell Horne get_current_state(void)
16037b0a665dSMitchell Horne {
16047b0a665dSMitchell Horne
16057b0a665dSMitchell Horne if (current_state == single_user)
16067b0a665dSMitchell Horne return ("single-user");
16077b0a665dSMitchell Horne if (current_state == runcom)
16087b0a665dSMitchell Horne return ("runcom");
16097b0a665dSMitchell Horne if (current_state == read_ttys)
16107b0a665dSMitchell Horne return ("read-ttys");
16117b0a665dSMitchell Horne if (current_state == multi_user)
16127b0a665dSMitchell Horne return ("multi-user");
16137b0a665dSMitchell Horne if (current_state == clean_ttys)
16147b0a665dSMitchell Horne return ("clean-ttys");
16157b0a665dSMitchell Horne if (current_state == catatonia)
16167b0a665dSMitchell Horne return ("catatonia");
16177b0a665dSMitchell Horne if (current_state == death)
16187b0a665dSMitchell Horne return ("death");
16197b0a665dSMitchell Horne if (current_state == death_single)
16207b0a665dSMitchell Horne return ("death-single");
16217b0a665dSMitchell Horne return ("unknown");
16227b0a665dSMitchell Horne }
16237b0a665dSMitchell Horne
16247b0a665dSMitchell Horne static void
boottrace_transition(int sig)16257b0a665dSMitchell Horne boottrace_transition(int sig)
16267b0a665dSMitchell Horne {
16277b0a665dSMitchell Horne const char *action;
16287b0a665dSMitchell Horne
16297b0a665dSMitchell Horne switch (sig) {
16307b0a665dSMitchell Horne case SIGUSR2:
16317b0a665dSMitchell Horne action = "halt & poweroff";
16327b0a665dSMitchell Horne break;
16337b0a665dSMitchell Horne case SIGUSR1:
16347b0a665dSMitchell Horne action = "halt";
16357b0a665dSMitchell Horne break;
16367b0a665dSMitchell Horne case SIGINT:
16377b0a665dSMitchell Horne action = "reboot";
16387b0a665dSMitchell Horne break;
16397b0a665dSMitchell Horne case SIGWINCH:
16407b0a665dSMitchell Horne action = "powercycle";
16417b0a665dSMitchell Horne break;
16427b0a665dSMitchell Horne case SIGTERM:
16437b0a665dSMitchell Horne action = Reboot ? "reboot" : "single-user";
16447b0a665dSMitchell Horne break;
16457b0a665dSMitchell Horne default:
16467b0a665dSMitchell Horne BOOTTRACE("signal %d from %s", sig, get_current_state());
16477b0a665dSMitchell Horne return;
16487b0a665dSMitchell Horne }
16497b0a665dSMitchell Horne
16507b0a665dSMitchell Horne /* Trace the shutdown reason. */
16517b0a665dSMitchell Horne SHUTTRACE("%s from %s", action, get_current_state());
16527b0a665dSMitchell Horne }
16537b0a665dSMitchell Horne
16548fae3551SRodney W. Grimes /*
16558fae3551SRodney W. Grimes * Catch a signal and request a state transition.
16568fae3551SRodney W. Grimes */
165745cfb1dcSXin LI static void
transition_handler(int sig)165873bf18edSWarner Losh transition_handler(int sig)
16598fae3551SRodney W. Grimes {
16608fae3551SRodney W. Grimes
16617b0a665dSMitchell Horne boottrace_transition(sig);
16628fae3551SRodney W. Grimes switch (sig) {
16638fae3551SRodney W. Grimes case SIGHUP:
1664acf0ab06SJilles Tjoelker if (current_state == read_ttys || current_state == multi_user ||
1665acf0ab06SJilles Tjoelker current_state == clean_ttys || current_state == catatonia)
16668fae3551SRodney W. Grimes requested_transition = clean_ttys;
16678fae3551SRodney W. Grimes break;
1668a0a549c7SRuslan Ermilov case SIGUSR2:
166973c6e0c3SWarner Losh howto = RB_POWEROFF;
1670a0a549c7SRuslan Ermilov case SIGUSR1:
1671a0a549c7SRuslan Ermilov howto |= RB_HALT;
167273c6e0c3SWarner Losh case SIGWINCH:
1673e460cfd3SNate Williams case SIGINT:
167473c6e0c3SWarner Losh if (sig == SIGWINCH)
167573c6e0c3SWarner Losh howto |= RB_POWERCYCLE;
1676de08b516SMitchell Horne Reboot = true;
16778fae3551SRodney W. Grimes case SIGTERM:
1678acf0ab06SJilles Tjoelker if (current_state == read_ttys || current_state == multi_user ||
1679acf0ab06SJilles Tjoelker current_state == clean_ttys || current_state == catatonia)
16808fae3551SRodney W. Grimes requested_transition = death;
1681acf0ab06SJilles Tjoelker else
1682acf0ab06SJilles Tjoelker requested_transition = death_single;
16838fae3551SRodney W. Grimes break;
16848fae3551SRodney W. Grimes case SIGTSTP:
1685acf0ab06SJilles Tjoelker if (current_state == runcom || current_state == read_ttys ||
1686acf0ab06SJilles Tjoelker current_state == clean_ttys ||
1687acf0ab06SJilles Tjoelker current_state == multi_user || current_state == catatonia)
16888fae3551SRodney W. Grimes requested_transition = catatonia;
16898fae3551SRodney W. Grimes break;
16903f5ac575SEdward Tomasz Napierala case SIGEMT:
16913f5ac575SEdward Tomasz Napierala requested_transition = reroot;
16923f5ac575SEdward Tomasz Napierala break;
16938fae3551SRodney W. Grimes default:
16948fae3551SRodney W. Grimes requested_transition = 0;
16958fae3551SRodney W. Grimes break;
16968fae3551SRodney W. Grimes }
16978fae3551SRodney W. Grimes }
16988fae3551SRodney W. Grimes
16998fae3551SRodney W. Grimes /*
17008fae3551SRodney W. Grimes * Take the system multiuser.
17018fae3551SRodney W. Grimes */
170245cfb1dcSXin LI static state_func_t
multi_user(void)170373bf18edSWarner Losh multi_user(void)
17048fae3551SRodney W. Grimes {
17057b0a665dSMitchell Horne static bool inmultiuser = false;
17068fae3551SRodney W. Grimes pid_t pid;
17073d438ad6SDavid E. O'Brien session_t *sp;
17088fae3551SRodney W. Grimes
17098fae3551SRodney W. Grimes requested_transition = 0;
17108fae3551SRodney W. Grimes
17118fae3551SRodney W. Grimes /*
17128fae3551SRodney W. Grimes * If the administrator has not set the security level to -1
17138fae3551SRodney W. Grimes * to indicate that the kernel should not run multiuser in secure
17148fae3551SRodney W. Grimes * mode, and the run script has not set a higher level of security
17158fae3551SRodney W. Grimes * than level 1, then put the kernel into secure mode.
17168fae3551SRodney W. Grimes */
17178fae3551SRodney W. Grimes if (getsecuritylevel() == 0)
17188fae3551SRodney W. Grimes setsecuritylevel(1);
17198fae3551SRodney W. Grimes
17208fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next) {
17218fae3551SRodney W. Grimes if (sp->se_process)
17228fae3551SRodney W. Grimes continue;
17231cde387cSEdward Tomasz Napierala if (session_has_no_tty(sp))
17241cde387cSEdward Tomasz Napierala continue;
17258fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) {
17268fae3551SRodney W. Grimes /* serious trouble */
17278fae3551SRodney W. Grimes requested_transition = clean_ttys;
17288fae3551SRodney W. Grimes break;
17298fae3551SRodney W. Grimes }
17308fae3551SRodney W. Grimes sp->se_process = pid;
17318fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0);
17328fae3551SRodney W. Grimes add_session(sp);
17338fae3551SRodney W. Grimes }
17348fae3551SRodney W. Grimes
17357b0a665dSMitchell Horne if (requested_transition == 0 && !inmultiuser) {
17367b0a665dSMitchell Horne inmultiuser = true;
17377b0a665dSMitchell Horne /* This marks the change from boot-time tracing to run-time. */
17387b0a665dSMitchell Horne RUNTRACE("multi-user start");
17397b0a665dSMitchell Horne }
17408fae3551SRodney W. Grimes while (!requested_transition)
17418fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *) 0, 0)) != -1)
17428fae3551SRodney W. Grimes collect_child(pid);
17438fae3551SRodney W. Grimes
17448fae3551SRodney W. Grimes return (state_func_t) requested_transition;
17458fae3551SRodney W. Grimes }
17468fae3551SRodney W. Grimes
17478fae3551SRodney W. Grimes /*
1748b0b670eeSAlfred Perlstein * This is an (n*2)+(n^2) algorithm. We hope it isn't run often...
17498fae3551SRodney W. Grimes */
175045cfb1dcSXin LI static state_func_t
clean_ttys(void)175173bf18edSWarner Losh clean_ttys(void)
17528fae3551SRodney W. Grimes {
17533d438ad6SDavid E. O'Brien session_t *sp, *sprev;
17543d438ad6SDavid E. O'Brien struct ttyent *typ;
17553d438ad6SDavid E. O'Brien int devlen;
1756b5df27e2SAndrey A. Chernov char *old_getty, *old_window, *old_type;
17578fae3551SRodney W. Grimes
1758b0b670eeSAlfred Perlstein /*
1759b0b670eeSAlfred Perlstein * mark all sessions for death, (!SE_PRESENT)
1760b0b670eeSAlfred Perlstein * as we find or create new ones they'll be marked as keepers,
1761b0b670eeSAlfred Perlstein * we'll later nuke all the ones not found in /etc/ttys
1762b0b670eeSAlfred Perlstein */
1763b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next)
1764b0b670eeSAlfred Perlstein sp->se_flags &= ~SE_PRESENT;
1765b0b670eeSAlfred Perlstein
17668fae3551SRodney W. Grimes devlen = sizeof(_PATH_DEV) - 1;
17678889c700SDavid Nugent while ((typ = getttyent()) != NULL) {
17688fae3551SRodney W. Grimes for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next)
17698fae3551SRodney W. Grimes if (strcmp(typ->ty_name, sp->se_device + devlen) == 0)
17708fae3551SRodney W. Grimes break;
17718fae3551SRodney W. Grimes
17728fae3551SRodney W. Grimes if (sp) {
1773b0b670eeSAlfred Perlstein /* we want this one to live */
1774b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT;
17758fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 ||
17768fae3551SRodney W. Grimes typ->ty_getty == 0) {
17778fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN;
17788fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP);
17798fae3551SRodney W. Grimes continue;
17808fae3551SRodney W. Grimes }
17818fae3551SRodney W. Grimes sp->se_flags &= ~SE_SHUTDOWN;
1782b5df27e2SAndrey A. Chernov old_getty = sp->se_getty ? strdup(sp->se_getty) : 0;
1783b5df27e2SAndrey A. Chernov old_window = sp->se_window ? strdup(sp->se_window) : 0;
1784b5df27e2SAndrey A. Chernov old_type = sp->se_type ? strdup(sp->se_type) : 0;
17858fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) {
17868fae3551SRodney W. Grimes warning("can't parse getty for port %s",
17878fae3551SRodney W. Grimes sp->se_device);
17888fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN;
17898fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP);
17908fae3551SRodney W. Grimes }
1791b5df27e2SAndrey A. Chernov else if ( !old_getty
17928889c700SDavid Nugent || (!old_type && sp->se_type)
17938889c700SDavid Nugent || (old_type && !sp->se_type)
17948889c700SDavid Nugent || (!old_window && sp->se_window)
17958889c700SDavid Nugent || (old_window && !sp->se_window)
17968889c700SDavid Nugent || (strcmp(old_getty, sp->se_getty) != 0)
17978889c700SDavid Nugent || (old_window && strcmp(old_window, sp->se_window) != 0)
17988889c700SDavid Nugent || (old_type && strcmp(old_type, sp->se_type) != 0)
1799b5df27e2SAndrey A. Chernov ) {
1800b5df27e2SAndrey A. Chernov /* Don't set SE_SHUTDOWN here */
1801b5df27e2SAndrey A. Chernov sp->se_nspace = 0;
1802b5df27e2SAndrey A. Chernov sp->se_started = 0;
1803b5df27e2SAndrey A. Chernov kill(sp->se_process, SIGHUP);
1804b5df27e2SAndrey A. Chernov }
1805b5df27e2SAndrey A. Chernov if (old_getty)
1806b5df27e2SAndrey A. Chernov free(old_getty);
18072d887af5SMike Heffner if (old_window)
1808b5df27e2SAndrey A. Chernov free(old_window);
1809b5df27e2SAndrey A. Chernov if (old_type)
1810b5df27e2SAndrey A. Chernov free(old_type);
18118fae3551SRodney W. Grimes continue;
18128fae3551SRodney W. Grimes }
18138fae3551SRodney W. Grimes
18140b57dd6bSJilles Tjoelker new_session(sprev, typ);
18158fae3551SRodney W. Grimes }
18168fae3551SRodney W. Grimes
18178fae3551SRodney W. Grimes endttyent();
18188fae3551SRodney W. Grimes
1819b0b670eeSAlfred Perlstein /*
1820b0b670eeSAlfred Perlstein * sweep through and kill all deleted sessions
1821b0b670eeSAlfred Perlstein * ones who's /etc/ttys line was deleted (SE_PRESENT unset)
1822b0b670eeSAlfred Perlstein */
1823b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next) {
1824b0b670eeSAlfred Perlstein if ((sp->se_flags & SE_PRESENT) == 0) {
1825b0b670eeSAlfred Perlstein sp->se_flags |= SE_SHUTDOWN;
1826b0b670eeSAlfred Perlstein kill(sp->se_process, SIGHUP);
1827b0b670eeSAlfred Perlstein }
1828b0b670eeSAlfred Perlstein }
1829b0b670eeSAlfred Perlstein
18308fae3551SRodney W. Grimes return (state_func_t) multi_user;
18318fae3551SRodney W. Grimes }
18328fae3551SRodney W. Grimes
18338fae3551SRodney W. Grimes /*
18348fae3551SRodney W. Grimes * Block further logins.
18358fae3551SRodney W. Grimes */
183645cfb1dcSXin LI static state_func_t
catatonia(void)183773bf18edSWarner Losh catatonia(void)
18388fae3551SRodney W. Grimes {
18393d438ad6SDavid E. O'Brien session_t *sp;
18408fae3551SRodney W. Grimes
18418fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next)
18428fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN;
18438fae3551SRodney W. Grimes
18448fae3551SRodney W. Grimes return (state_func_t) multi_user;
18458fae3551SRodney W. Grimes }
18468fae3551SRodney W. Grimes
18478fae3551SRodney W. Grimes /*
18488fae3551SRodney W. Grimes * Note SIGALRM.
18498fae3551SRodney W. Grimes */
185045cfb1dcSXin LI static void
alrm_handler(int sig)185173bf18edSWarner Losh alrm_handler(int sig)
18528fae3551SRodney W. Grimes {
1853091abe40SDavid E. O'Brien
18548889c700SDavid Nugent (void)sig;
1855de08b516SMitchell Horne clang = true;
18568fae3551SRodney W. Grimes }
18578fae3551SRodney W. Grimes
18588fae3551SRodney W. Grimes /*
18598fae3551SRodney W. Grimes * Bring the system down to single user.
18608fae3551SRodney W. Grimes */
186145cfb1dcSXin LI static state_func_t
death(void)186273bf18edSWarner Losh death(void)
18638fae3551SRodney W. Grimes {
18642eb0015aSColin Percival int block, blocked;
18652eb0015aSColin Percival size_t len;
18662eb0015aSColin Percival
18672eb0015aSColin Percival /* Temporarily block suspend. */
18682eb0015aSColin Percival len = sizeof(blocked);
18692eb0015aSColin Percival block = 1;
18702eb0015aSColin Percival if (sysctlbyname("kern.suspend_blocked", &blocked, &len,
18712eb0015aSColin Percival &block, sizeof(block)) == -1)
18722eb0015aSColin Percival blocked = 0;
18738fae3551SRodney W. Grimes
18744ae35b5dSEd Schouten /*
18754ae35b5dSEd Schouten * Also revoke the TTY here. Because runshutdown() may reopen
18764ae35b5dSEd Schouten * the TTY whose getty we're killing here, there is no guarantee
18774ae35b5dSEd Schouten * runshutdown() will perform the initial open() call, causing
18784ae35b5dSEd Schouten * the terminal attributes to be misconfigured.
18794ae35b5dSEd Schouten */
18803f5ac575SEdward Tomasz Napierala revoke_ttys();
18818fae3551SRodney W. Grimes
18828889c700SDavid Nugent /* Try to run the rc.shutdown script within a period of time */
1883091abe40SDavid E. O'Brien runshutdown();
18848889c700SDavid Nugent
18852eb0015aSColin Percival /* Unblock suspend if we blocked it. */
18862eb0015aSColin Percival if (!blocked)
18872eb0015aSColin Percival sysctlbyname("kern.suspend_blocked", NULL, NULL,
18882eb0015aSColin Percival &blocked, sizeof(blocked));
18892eb0015aSColin Percival
1890acf0ab06SJilles Tjoelker return (state_func_t) death_single;
1891acf0ab06SJilles Tjoelker }
1892acf0ab06SJilles Tjoelker
1893acf0ab06SJilles Tjoelker /*
1894acf0ab06SJilles Tjoelker * Do what is necessary to reinitialize single user mode or reboot
1895acf0ab06SJilles Tjoelker * from an incomplete state.
1896acf0ab06SJilles Tjoelker */
1897acf0ab06SJilles Tjoelker static state_func_t
death_single(void)1898acf0ab06SJilles Tjoelker death_single(void)
1899acf0ab06SJilles Tjoelker {
1900acf0ab06SJilles Tjoelker int i;
1901acf0ab06SJilles Tjoelker pid_t pid;
1902acf0ab06SJilles Tjoelker static const int death_sigs[2] = { SIGTERM, SIGKILL };
1903acf0ab06SJilles Tjoelker
1904acf0ab06SJilles Tjoelker revoke(_PATH_CONSOLE);
1905acf0ab06SJilles Tjoelker
19067b0a665dSMitchell Horne BOOTTRACE("start killing user processes");
1907c3d7c52eSAndrey A. Chernov for (i = 0; i < 2; ++i) {
19088fae3551SRodney W. Grimes if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH)
19098fae3551SRodney W. Grimes return (state_func_t) single_user;
19108fae3551SRodney W. Grimes
1911de08b516SMitchell Horne clang = false;
19128fae3551SRodney W. Grimes alarm(DEATH_WATCH);
19138fae3551SRodney W. Grimes do
19148fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *)0, 0)) != -1)
19158fae3551SRodney W. Grimes collect_child(pid);
1916de08b516SMitchell Horne while (!clang && errno != ECHILD);
19178fae3551SRodney W. Grimes
19188fae3551SRodney W. Grimes if (errno == ECHILD)
19198fae3551SRodney W. Grimes return (state_func_t) single_user;
19208fae3551SRodney W. Grimes }
19218fae3551SRodney W. Grimes
19228fae3551SRodney W. Grimes warning("some processes would not die; ps axl advised");
19238fae3551SRodney W. Grimes
19248fae3551SRodney W. Grimes return (state_func_t) single_user;
19258fae3551SRodney W. Grimes }
19268889c700SDavid Nugent
19273f5ac575SEdward Tomasz Napierala static void
revoke_ttys(void)19283f5ac575SEdward Tomasz Napierala revoke_ttys(void)
19293f5ac575SEdward Tomasz Napierala {
19303f5ac575SEdward Tomasz Napierala session_t *sp;
19313f5ac575SEdward Tomasz Napierala
19323f5ac575SEdward Tomasz Napierala for (sp = sessions; sp; sp = sp->se_next) {
19333f5ac575SEdward Tomasz Napierala sp->se_flags |= SE_SHUTDOWN;
19343f5ac575SEdward Tomasz Napierala kill(sp->se_process, SIGHUP);
19353f5ac575SEdward Tomasz Napierala revoke(sp->se_device);
19363f5ac575SEdward Tomasz Napierala }
19373f5ac575SEdward Tomasz Napierala }
19383f5ac575SEdward Tomasz Napierala
19398889c700SDavid Nugent /*
19408889c700SDavid Nugent * Run the system shutdown script.
19418889c700SDavid Nugent *
19428889c700SDavid Nugent * Exit codes: XXX I should document more
19438889c700SDavid Nugent * -2 shutdown script terminated abnormally
19448889c700SDavid Nugent * -1 fatal error - can't run script
19458889c700SDavid Nugent * 0 good.
19468889c700SDavid Nugent * >0 some error (exit code)
19478889c700SDavid Nugent */
194845cfb1dcSXin LI static int
runshutdown(void)194973bf18edSWarner Losh runshutdown(void)
19508889c700SDavid Nugent {
19518889c700SDavid Nugent pid_t pid, wpid;
1952f3c4a698SEdward Tomasz Napierala int status;
19538889c700SDavid Nugent int shutdowntimeout;
19548889c700SDavid Nugent size_t len;
1955*f3dba162SSebastien Bini char *argv[SCRIPT_ARGV_SIZE];
195686bf62dcSDavid Nugent struct stat sb;
195786bf62dcSDavid Nugent
19587b0a665dSMitchell Horne BOOTTRACE("init(8): start rc.shutdown");
19597b0a665dSMitchell Horne
196086bf62dcSDavid Nugent /*
196186bf62dcSDavid Nugent * rc.shutdown is optional, so to prevent any unnecessary
196286bf62dcSDavid Nugent * complaints from the shell we simply don't run it if the
196386bf62dcSDavid Nugent * file does not exist. If the stat() here fails for other
196486bf62dcSDavid Nugent * reasons, we'll let the shell complain.
196586bf62dcSDavid Nugent */
196686bf62dcSDavid Nugent if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT)
196786bf62dcSDavid Nugent return 0;
19688889c700SDavid Nugent
19698889c700SDavid Nugent if ((pid = fork()) == 0) {
1970ab03e6d5SXin LI char _reboot[] = "reboot";
1971ab03e6d5SXin LI char _single[] = "single";
1972ab03e6d5SXin LI char _path_rundown[] = _PATH_RUNDOWN;
1973ab03e6d5SXin LI
1974*f3dba162SSebastien Bini argv[0] = _path_rundown;
1975*f3dba162SSebastien Bini argv[1] = Reboot ? _reboot : _single;
1976*f3dba162SSebastien Bini argv[2] = NULL;
19778889c700SDavid Nugent
1978f3c4a698SEdward Tomasz Napierala execute_script(argv);
19798889c700SDavid Nugent _exit(1); /* force single user mode */
19808889c700SDavid Nugent }
19818889c700SDavid Nugent
19828889c700SDavid Nugent if (pid == -1) {
1983f3c4a698SEdward Tomasz Napierala emergency("can't fork for %s: %m", _PATH_RUNDOWN);
19848889c700SDavid Nugent while (waitpid(-1, (int *) 0, WNOHANG) > 0)
19858889c700SDavid Nugent continue;
19868889c700SDavid Nugent sleep(STALL_TIMEOUT);
19878889c700SDavid Nugent return -1;
19888889c700SDavid Nugent }
19898889c700SDavid Nugent
19908889c700SDavid Nugent len = sizeof(shutdowntimeout);
1991091abe40SDavid E. O'Brien if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len,
1992091abe40SDavid E. O'Brien NULL, 0) == -1 || shutdowntimeout < 2)
19938889c700SDavid Nugent shutdowntimeout = DEATH_SCRIPT;
19948889c700SDavid Nugent alarm(shutdowntimeout);
1995de08b516SMitchell Horne clang = false;
19968889c700SDavid Nugent /*
19978889c700SDavid Nugent * Copied from single_user(). This is a bit paranoid.
19988889c700SDavid Nugent * Use the same ALRM handler.
19998889c700SDavid Nugent */
20008889c700SDavid Nugent do {
20018889c700SDavid Nugent if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1)
20028889c700SDavid Nugent collect_child(wpid);
2003de08b516SMitchell Horne if (clang) {
20048889c700SDavid Nugent /* we were waiting for the sub-shell */
20058889c700SDavid Nugent kill(wpid, SIGTERM);
2006f3c4a698SEdward Tomasz Napierala warning("timeout expired for %s: %m; going to "
2007f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN);
20087b0a665dSMitchell Horne BOOTTRACE("rc.shutdown's %d sec timeout expired",
20097b0a665dSMitchell Horne shutdowntimeout);
20108889c700SDavid Nugent return -1;
20118889c700SDavid Nugent }
20128889c700SDavid Nugent if (wpid == -1) {
20138889c700SDavid Nugent if (errno == EINTR)
20148889c700SDavid Nugent continue;
2015f3c4a698SEdward Tomasz Napierala warning("wait for %s failed: %m; going to "
2016f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN);
20178889c700SDavid Nugent return -1;
20188889c700SDavid Nugent }
20198889c700SDavid Nugent if (wpid == pid && WIFSTOPPED(status)) {
2020f3c4a698SEdward Tomasz Napierala warning("init: %s stopped, restarting\n",
2021f3c4a698SEdward Tomasz Napierala _PATH_RUNDOWN);
20228889c700SDavid Nugent kill(pid, SIGCONT);
20238889c700SDavid Nugent wpid = -1;
20248889c700SDavid Nugent }
20258889c700SDavid Nugent } while (wpid != pid && !clang);
20268889c700SDavid Nugent
20278889c700SDavid Nugent /* Turn off the alarm */
20288889c700SDavid Nugent alarm(0);
20298889c700SDavid Nugent
20308889c700SDavid Nugent if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM &&
20318889c700SDavid Nugent requested_transition == catatonia) {
20328889c700SDavid Nugent /*
20338889c700SDavid Nugent * /etc/rc.shutdown executed /sbin/reboot;
20348889c700SDavid Nugent * wait for the end quietly
20358889c700SDavid Nugent */
20368889c700SDavid Nugent sigset_t s;
20378889c700SDavid Nugent
20388889c700SDavid Nugent sigfillset(&s);
20398889c700SDavid Nugent for (;;)
20408889c700SDavid Nugent sigsuspend(&s);
20418889c700SDavid Nugent }
20428889c700SDavid Nugent
20438889c700SDavid Nugent if (!WIFEXITED(status)) {
2044f3c4a698SEdward Tomasz Napierala warning("%s terminated abnormally, going to "
2045f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN);
20468889c700SDavid Nugent return -2;
20478889c700SDavid Nugent }
20488889c700SDavid Nugent
20498889c700SDavid Nugent if ((status = WEXITSTATUS(status)) != 0)
20508889c700SDavid Nugent warning("%s returned status %d", _PATH_RUNDOWN, status);
20518889c700SDavid Nugent
20528889c700SDavid Nugent return status;
20538889c700SDavid Nugent }
20548889c700SDavid Nugent
2055ab03e6d5SXin LI static char *
strk(char * p)205681ab7fb2SAndrey A. Chernov strk(char *p)
205781ab7fb2SAndrey A. Chernov {
205881ab7fb2SAndrey A. Chernov static char *t;
205981ab7fb2SAndrey A. Chernov char *q;
206081ab7fb2SAndrey A. Chernov int c;
206181ab7fb2SAndrey A. Chernov
206281ab7fb2SAndrey A. Chernov if (p)
206381ab7fb2SAndrey A. Chernov t = p;
206481ab7fb2SAndrey A. Chernov if (!t)
206581ab7fb2SAndrey A. Chernov return 0;
206681ab7fb2SAndrey A. Chernov
206781ab7fb2SAndrey A. Chernov c = *t;
206881ab7fb2SAndrey A. Chernov while (c == ' ' || c == '\t' )
206981ab7fb2SAndrey A. Chernov c = *++t;
207081ab7fb2SAndrey A. Chernov if (!c) {
207181ab7fb2SAndrey A. Chernov t = 0;
207281ab7fb2SAndrey A. Chernov return 0;
207381ab7fb2SAndrey A. Chernov }
207481ab7fb2SAndrey A. Chernov q = t;
207581ab7fb2SAndrey A. Chernov if (c == '\'') {
207681ab7fb2SAndrey A. Chernov c = *++t;
207781ab7fb2SAndrey A. Chernov q = t;
207881ab7fb2SAndrey A. Chernov while (c && c != '\'')
207981ab7fb2SAndrey A. Chernov c = *++t;
208081ab7fb2SAndrey A. Chernov if (!c) /* unterminated string */
208181ab7fb2SAndrey A. Chernov q = t = 0;
208281ab7fb2SAndrey A. Chernov else
208381ab7fb2SAndrey A. Chernov *t++ = 0;
208481ab7fb2SAndrey A. Chernov } else {
208581ab7fb2SAndrey A. Chernov while (c && c != ' ' && c != '\t' )
208681ab7fb2SAndrey A. Chernov c = *++t;
208781ab7fb2SAndrey A. Chernov *t++ = 0;
208881ab7fb2SAndrey A. Chernov if (!c)
208981ab7fb2SAndrey A. Chernov t = 0;
209081ab7fb2SAndrey A. Chernov }
209181ab7fb2SAndrey A. Chernov return q;
209281ab7fb2SAndrey A. Chernov }
20931ef60eb1SDavid Nugent
20941ef60eb1SDavid Nugent #ifdef LOGIN_CAP
209545cfb1dcSXin LI static void
setprocresources(const char * cname)209673bf18edSWarner Losh setprocresources(const char *cname)
20971ef60eb1SDavid Nugent {
2098e82d5545SDavid Nugent login_cap_t *lc;
2099a2ee73bcSAndrey A. Chernov if ((lc = login_getclassbyname(cname, NULL)) != NULL) {
2100091abe40SDavid E. O'Brien setusercontext(lc, (struct passwd*)NULL, 0,
210121c1a93cSKyle Evans LOGIN_SETENV |
2102595ab563SJilles Tjoelker LOGIN_SETPRIORITY | LOGIN_SETRESOURCES |
2103595ab563SJilles Tjoelker LOGIN_SETLOGINCLASS | LOGIN_SETCPUMASK);
21041ef60eb1SDavid Nugent login_close(lc);
21051ef60eb1SDavid Nugent }
21061ef60eb1SDavid Nugent }
21071ef60eb1SDavid Nugent #endif
21084d159769SKyle Evans
21094d159769SKyle Evans /*
21104d159769SKyle Evans * Run /etc/rc.final to execute scripts after all user processes have been
21114d159769SKyle Evans * terminated.
21124d159769SKyle Evans */
21134d159769SKyle Evans static void
runfinal(void)21144d159769SKyle Evans runfinal(void)
21154d159769SKyle Evans {
21164d159769SKyle Evans struct stat sb;
21174d159769SKyle Evans pid_t other_pid, pid;
21184d159769SKyle Evans sigset_t mask;
21194d159769SKyle Evans
21204d159769SKyle Evans /* Avoid any surprises. */
21214d159769SKyle Evans alarm(0);
21224d159769SKyle Evans
21234d159769SKyle Evans /* rc.final is optional. */
21244d159769SKyle Evans if (stat(_PATH_RUNFINAL, &sb) == -1 && errno == ENOENT)
21254d159769SKyle Evans return;
21264d159769SKyle Evans if (access(_PATH_RUNFINAL, X_OK) != 0) {
21274d159769SKyle Evans warning("%s exists, but not executable", _PATH_RUNFINAL);
21284d159769SKyle Evans return;
21294d159769SKyle Evans }
21304d159769SKyle Evans
21314d159769SKyle Evans pid = fork();
21324d159769SKyle Evans if (pid == 0) {
21334d159769SKyle Evans /*
21344d159769SKyle Evans * Reopen stdin/stdout/stderr so that scripts can write to
21354d159769SKyle Evans * console.
21364d159769SKyle Evans */
21374d159769SKyle Evans close(0);
21384d159769SKyle Evans open(_PATH_DEVNULL, O_RDONLY);
21394d159769SKyle Evans close(1);
21404d159769SKyle Evans close(2);
21414d159769SKyle Evans open_console();
21424d159769SKyle Evans dup2(1, 2);
21434d159769SKyle Evans sigemptyset(&mask);
21444d159769SKyle Evans sigprocmask(SIG_SETMASK, &mask, NULL);
21454d159769SKyle Evans signal(SIGCHLD, SIG_DFL);
21464d159769SKyle Evans execl(_PATH_RUNFINAL, _PATH_RUNFINAL, NULL);
21474d159769SKyle Evans perror("execl(" _PATH_RUNFINAL ") failed");
21484d159769SKyle Evans exit(1);
21494d159769SKyle Evans }
21504d159769SKyle Evans
21514d159769SKyle Evans /* Wait for rc.final script to exit */
21524d159769SKyle Evans while ((other_pid = waitpid(-1, NULL, 0)) != pid && other_pid > 0) {
21534d159769SKyle Evans continue;
21544d159769SKyle Evans }
21554d159769SKyle Evans }
2156