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 #ifndef lint 365df42cf4SPhilippe Charnier static const char copyright[] = 378fae3551SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\ 388fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 398fae3551SRodney W. Grimes #endif /* not lint */ 408fae3551SRodney W. Grimes 418fae3551SRodney W. Grimes #ifndef lint 425df42cf4SPhilippe Charnier #if 0 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 7/15/93"; 445df42cf4SPhilippe Charnier #endif 455df42cf4SPhilippe Charnier static const char rcsid[] = 467f3dea24SPeter Wemm "$FreeBSD$"; 478fae3551SRodney W. Grimes #endif /* not lint */ 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes #include <sys/param.h> 508889c700SDavid Nugent #include <sys/ioctl.h> 513f5ac575SEdward Tomasz Napierala #include <sys/mman.h> 5257622f22SPoul-Henning Kamp #include <sys/mount.h> 538fae3551SRodney W. Grimes #include <sys/sysctl.h> 548fae3551SRodney W. Grimes #include <sys/wait.h> 5586bf62dcSDavid Nugent #include <sys/stat.h> 561f083b1eSMaxime Henrion #include <sys/uio.h> 578fae3551SRodney W. Grimes 588fae3551SRodney W. Grimes #include <db.h> 598fae3551SRodney W. Grimes #include <errno.h> 608fae3551SRodney W. Grimes #include <fcntl.h> 611a7bec91SWarner Losh #include <kenv.h> 62423b6a39SAndrey A. Chernov #include <libutil.h> 631a37aa56SDavid E. O'Brien #include <paths.h> 648fae3551SRodney W. Grimes #include <signal.h> 658fae3551SRodney W. Grimes #include <stdio.h> 668fae3551SRodney W. Grimes #include <stdlib.h> 678fae3551SRodney W. Grimes #include <string.h> 688fae3551SRodney W. Grimes #include <syslog.h> 698fae3551SRodney W. Grimes #include <time.h> 708fae3551SRodney W. Grimes #include <ttyent.h> 718fae3551SRodney W. Grimes #include <unistd.h> 72e460cfd3SNate Williams #include <sys/reboot.h> 73c5842835SPhilippe Charnier #include <err.h> 748fae3551SRodney W. Grimes 758fae3551SRodney W. Grimes #include <stdarg.h> 768fae3551SRodney W. Grimes 778fae3551SRodney W. Grimes #ifdef SECURE 788fae3551SRodney W. Grimes #include <pwd.h> 798fae3551SRodney W. Grimes #endif 808fae3551SRodney W. Grimes 811ef60eb1SDavid Nugent #ifdef LOGIN_CAP 821ef60eb1SDavid Nugent #include <login_cap.h> 831ef60eb1SDavid Nugent #endif 841ef60eb1SDavid Nugent 853f5ac575SEdward Tomasz Napierala #include "mntopts.h" 868fae3551SRodney W. Grimes #include "pathnames.h" 878fae3551SRodney W. Grimes 888fae3551SRodney W. Grimes /* 898fae3551SRodney W. Grimes * Sleep times; used to prevent thrashing. 908fae3551SRodney W. Grimes */ 918fae3551SRodney W. Grimes #define GETTY_SPACING 5 /* N secs minimum getty spacing */ 928fae3551SRodney W. Grimes #define GETTY_SLEEP 30 /* sleep N secs after spacing problem */ 93b5df27e2SAndrey A. Chernov #define GETTY_NSPACE 3 /* max. spacing count to bring reaction */ 948fae3551SRodney W. Grimes #define WINDOW_WAIT 3 /* wait N secs after starting window */ 958fae3551SRodney W. Grimes #define STALL_TIMEOUT 30 /* wait N secs after warning */ 968fae3551SRodney W. Grimes #define DEATH_WATCH 10 /* wait N secs for procs to die */ 975df42cf4SPhilippe Charnier #define DEATH_SCRIPT 120 /* wait for 2min for /etc/rc.shutdown */ 98e82d5545SDavid Nugent #define RESOURCE_RC "daemon" 99e82d5545SDavid Nugent #define RESOURCE_WINDOW "default" 100e82d5545SDavid Nugent #define RESOURCE_GETTY "default" 1018fae3551SRodney W. Grimes 10245cfb1dcSXin LI static void handle(sig_t, ...); 10345cfb1dcSXin LI static void delset(sigset_t *, ...); 1048fae3551SRodney W. Grimes 10545cfb1dcSXin LI static void stall(const char *, ...) __printflike(1, 2); 10645cfb1dcSXin LI static void warning(const char *, ...) __printflike(1, 2); 10745cfb1dcSXin LI static void emergency(const char *, ...) __printflike(1, 2); 10845cfb1dcSXin LI static void disaster(int); 10945cfb1dcSXin LI static void badsys(int); 1103f5ac575SEdward Tomasz Napierala static void revoke_ttys(void); 11145cfb1dcSXin LI static int runshutdown(void); 112ab03e6d5SXin LI static char *strk(char *); 1138fae3551SRodney W. Grimes 1148fae3551SRodney W. Grimes /* 1158fae3551SRodney W. Grimes * We really need a recursive typedef... 1168fae3551SRodney W. Grimes * The following at least guarantees that the return type of (*state_t)() 1178fae3551SRodney W. Grimes * is sufficiently wide to hold a function pointer. 1188fae3551SRodney W. Grimes */ 11973bf18edSWarner Losh typedef long (*state_func_t)(void); 12073bf18edSWarner Losh typedef state_func_t (*state_t)(void); 1218fae3551SRodney W. Grimes 12245cfb1dcSXin LI static state_func_t single_user(void); 12345cfb1dcSXin LI static state_func_t runcom(void); 12445cfb1dcSXin LI static state_func_t read_ttys(void); 12545cfb1dcSXin LI static state_func_t multi_user(void); 12645cfb1dcSXin LI static state_func_t clean_ttys(void); 12745cfb1dcSXin LI static state_func_t catatonia(void); 12845cfb1dcSXin LI static state_func_t death(void); 129acf0ab06SJilles Tjoelker static state_func_t death_single(void); 1303f5ac575SEdward Tomasz Napierala static state_func_t reroot(void); 1313f5ac575SEdward Tomasz Napierala static state_func_t reroot_phase_two(void); 1328fae3551SRodney W. Grimes 13345cfb1dcSXin LI static state_func_t run_script(const char *); 1341a7bec91SWarner Losh 1351efe3c6bSEd Schouten static enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT; 13677103ea3SPoul-Henning Kamp #define FALSE 0 13777103ea3SPoul-Henning Kamp #define TRUE 1 13877103ea3SPoul-Henning Kamp 1391efe3c6bSEd Schouten static int Reboot = FALSE; 1401efe3c6bSEd Schouten static int howto = RB_AUTOBOOT; 1418fae3551SRodney W. Grimes 1421efe3c6bSEd Schouten static int devfs; 143377b6d1eSEdward Tomasz Napierala static char *init_path_argv0; 14457622f22SPoul-Henning Kamp 14545cfb1dcSXin LI static void transition(state_t); 14645cfb1dcSXin LI static state_t requested_transition; 147acf0ab06SJilles Tjoelker static state_t current_state = death_single; 1488fae3551SRodney W. Grimes 149f3c4a698SEdward Tomasz Napierala static void execute_script(char *argv[]); 1504c2c7b2cSEd Schouten static void open_console(void); 15145cfb1dcSXin LI static const char *get_shell(void); 152335fe94fSEdward Tomasz Napierala static void replace_init(char *path); 15345cfb1dcSXin LI static void write_stderr(const char *message); 1548fae3551SRodney W. Grimes 1558fae3551SRodney W. Grimes typedef struct init_session { 1568fae3551SRodney W. Grimes pid_t se_process; /* controlling process */ 1578fae3551SRodney W. Grimes time_t se_started; /* used to avoid thrashing */ 1588fae3551SRodney W. Grimes int se_flags; /* status of session */ 1598fae3551SRodney W. Grimes #define SE_SHUTDOWN 0x1 /* session won't be restarted */ 160b0b670eeSAlfred Perlstein #define SE_PRESENT 0x2 /* session is in /etc/ttys */ 1611cde387cSEdward Tomasz Napierala #define SE_IFEXISTS 0x4 /* session defined as "onifexists" */ 1621cde387cSEdward Tomasz Napierala #define SE_IFCONSOLE 0x8 /* session defined as "onifconsole" */ 163b5df27e2SAndrey A. Chernov int se_nspace; /* spacing count */ 1648fae3551SRodney W. Grimes char *se_device; /* filename of port */ 1658fae3551SRodney W. Grimes char *se_getty; /* what to run on that port */ 166b5df27e2SAndrey A. Chernov char *se_getty_argv_space; /* pre-parsed argument array space */ 1678fae3551SRodney W. Grimes char **se_getty_argv; /* pre-parsed argument array */ 1688fae3551SRodney W. Grimes char *se_window; /* window system (started only once) */ 169b5df27e2SAndrey A. Chernov char *se_window_argv_space; /* pre-parsed argument array space */ 1708fae3551SRodney W. Grimes char **se_window_argv; /* pre-parsed argument array */ 171b5df27e2SAndrey A. Chernov char *se_type; /* default terminal type */ 1728fae3551SRodney W. Grimes struct init_session *se_prev; 1738fae3551SRodney W. Grimes struct init_session *se_next; 1748fae3551SRodney W. Grimes } session_t; 1758fae3551SRodney W. Grimes 17645cfb1dcSXin LI static void free_session(session_t *); 1770b57dd6bSJilles Tjoelker static session_t *new_session(session_t *, struct ttyent *); 17845cfb1dcSXin LI static session_t *sessions; 1798fae3551SRodney W. Grimes 18045cfb1dcSXin LI static char **construct_argv(char *); 18145cfb1dcSXin LI static void start_window_system(session_t *); 18245cfb1dcSXin LI static void collect_child(pid_t); 18345cfb1dcSXin LI static pid_t start_getty(session_t *); 18445cfb1dcSXin LI static void transition_handler(int); 18545cfb1dcSXin LI static void alrm_handler(int); 18645cfb1dcSXin LI static void setsecuritylevel(int); 18745cfb1dcSXin LI static int getsecuritylevel(void); 18845cfb1dcSXin LI static int setupargv(session_t *, struct ttyent *); 189e82d5545SDavid Nugent #ifdef LOGIN_CAP 19045cfb1dcSXin LI static void setprocresources(const char *); 191e82d5545SDavid Nugent #endif 19245cfb1dcSXin LI static int clang; 1938fae3551SRodney W. Grimes 19445cfb1dcSXin LI static int start_session_db(void); 19545cfb1dcSXin LI static void add_session(session_t *); 19645cfb1dcSXin LI static void del_session(session_t *); 19745cfb1dcSXin LI static session_t *find_session(pid_t); 19845cfb1dcSXin LI static DB *session_db; 1998fae3551SRodney W. Grimes 2008fae3551SRodney W. Grimes /* 2018fae3551SRodney W. Grimes * The mother of all processes. 2028fae3551SRodney W. Grimes */ 2038fae3551SRodney W. Grimes int 20473bf18edSWarner Losh main(int argc, char *argv[]) 2058fae3551SRodney W. Grimes { 2061a7bec91SWarner Losh state_t initial_transition = runcom; 2071a7bec91SWarner Losh char kenv_value[PATH_MAX]; 2083f5ac575SEdward Tomasz Napierala int c, error; 2098fae3551SRodney W. Grimes struct sigaction sa; 2108fae3551SRodney W. Grimes sigset_t mask; 2118fae3551SRodney W. Grimes 2128fae3551SRodney W. Grimes /* Dispose of random users. */ 213c5842835SPhilippe Charnier if (getuid() != 0) 214c5842835SPhilippe Charnier errx(1, "%s", strerror(EPERM)); 2158fae3551SRodney W. Grimes 2168fae3551SRodney W. Grimes /* System V users like to reexec init. */ 2171681d659SRuslan Ermilov if (getpid() != 1) { 2181681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT 2191681d659SRuslan Ermilov /* So give them what they want */ 2201681d659SRuslan Ermilov if (argc > 1) { 2211681d659SRuslan Ermilov if (strlen(argv[1]) == 1) { 2223d438ad6SDavid E. O'Brien char runlevel = *argv[1]; 2233d438ad6SDavid E. O'Brien int sig; 2248fae3551SRodney W. Grimes 2251681d659SRuslan Ermilov switch (runlevel) { 2261681d659SRuslan Ermilov case '0': /* halt + poweroff */ 2271681d659SRuslan Ermilov sig = SIGUSR2; 2281681d659SRuslan Ermilov break; 2291681d659SRuslan Ermilov case '1': /* single-user */ 2301681d659SRuslan Ermilov sig = SIGTERM; 2311681d659SRuslan Ermilov break; 2321681d659SRuslan Ermilov case '6': /* reboot */ 2331681d659SRuslan Ermilov sig = SIGINT; 2341681d659SRuslan Ermilov break; 2351681d659SRuslan Ermilov case 'c': /* block further logins */ 2361681d659SRuslan Ermilov sig = SIGTSTP; 2371681d659SRuslan Ermilov break; 2381681d659SRuslan Ermilov case 'q': /* rescan /etc/ttys */ 2391681d659SRuslan Ermilov sig = SIGHUP; 2401681d659SRuslan Ermilov break; 2413f5ac575SEdward Tomasz Napierala case 'r': /* remount root */ 2423f5ac575SEdward Tomasz Napierala sig = SIGEMT; 2433f5ac575SEdward Tomasz Napierala break; 2441681d659SRuslan Ermilov default: 2451681d659SRuslan Ermilov goto invalid; 2461681d659SRuslan Ermilov } 2471681d659SRuslan Ermilov kill(1, sig); 2481681d659SRuslan Ermilov _exit(0); 2491681d659SRuslan Ermilov } else 2501681d659SRuslan Ermilov invalid: 2511681d659SRuslan Ermilov errx(1, "invalid run-level ``%s''", argv[1]); 2521681d659SRuslan Ermilov } else 2531681d659SRuslan Ermilov #endif 2541681d659SRuslan Ermilov errx(1, "already running"); 2551681d659SRuslan Ermilov } 256377b6d1eSEdward Tomasz Napierala 257377b6d1eSEdward Tomasz Napierala init_path_argv0 = strdup(argv[0]); 258377b6d1eSEdward Tomasz Napierala if (init_path_argv0 == NULL) 259377b6d1eSEdward Tomasz Napierala err(1, "strdup"); 260377b6d1eSEdward Tomasz Napierala 2618fae3551SRodney W. Grimes /* 2628fae3551SRodney W. Grimes * Note that this does NOT open a file... 2638fae3551SRodney W. Grimes * Does 'init' deserve its own facility number? 2648fae3551SRodney W. Grimes */ 26506224a94SNeel Natu openlog("init", LOG_CONS, LOG_AUTH); 2668fae3551SRodney W. Grimes 2678fae3551SRodney W. Grimes /* 2688fae3551SRodney W. Grimes * Create an initial session. 2698fae3551SRodney W. Grimes */ 2703f5ac575SEdward Tomasz Napierala if (setsid() < 0 && (errno != EPERM || getsid(0) != 1)) 2718fae3551SRodney W. Grimes warning("initial setsid() failed: %m"); 2728fae3551SRodney W. Grimes 2738fae3551SRodney W. Grimes /* 2748fae3551SRodney W. Grimes * Establish an initial user so that programs running 2758fae3551SRodney W. Grimes * single user do not freak out and die (like passwd). 2768fae3551SRodney W. Grimes */ 2778fae3551SRodney W. Grimes if (setlogin("root") < 0) 2788fae3551SRodney W. Grimes warning("setlogin() failed: %m"); 2798fae3551SRodney W. Grimes 2808fae3551SRodney W. Grimes /* 2818fae3551SRodney W. Grimes * This code assumes that we always get arguments through flags, 2828fae3551SRodney W. Grimes * never through bits set in some random machine register. 2838fae3551SRodney W. Grimes */ 2843f5ac575SEdward Tomasz Napierala while ((c = getopt(argc, argv, "dsfr")) != -1) 2858fae3551SRodney W. Grimes switch (c) { 28657622f22SPoul-Henning Kamp case 'd': 28757622f22SPoul-Henning Kamp devfs = 1; 28857622f22SPoul-Henning Kamp break; 2898fae3551SRodney W. Grimes case 's': 2901a7bec91SWarner Losh initial_transition = single_user; 2918fae3551SRodney W. Grimes break; 2928fae3551SRodney W. Grimes case 'f': 2938fae3551SRodney W. Grimes runcom_mode = FASTBOOT; 2948fae3551SRodney W. Grimes break; 2953f5ac575SEdward Tomasz Napierala case 'r': 2963f5ac575SEdward Tomasz Napierala initial_transition = reroot_phase_two; 2973f5ac575SEdward Tomasz Napierala break; 2988fae3551SRodney W. Grimes default: 2998fae3551SRodney W. Grimes warning("unrecognized flag '-%c'", c); 3008fae3551SRodney W. Grimes break; 3018fae3551SRodney W. Grimes } 3028fae3551SRodney W. Grimes 3038fae3551SRodney W. Grimes if (optind != argc) 3048fae3551SRodney W. Grimes warning("ignoring excess arguments"); 3058fae3551SRodney W. Grimes 3061a7bec91SWarner Losh /* 3071a7bec91SWarner Losh * We catch or block signals rather than ignore them, 3081a7bec91SWarner Losh * so that they get reset on exec. 3091a7bec91SWarner Losh */ 3101a7bec91SWarner Losh handle(badsys, SIGSYS, 0); 311091abe40SDavid E. O'Brien handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGXCPU, 312091abe40SDavid E. O'Brien SIGXFSZ, 0); 3133f5ac575SEdward Tomasz Napierala handle(transition_handler, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP, 31435c1d16eSWarner Losh SIGUSR1, SIGUSR2, SIGWINCH, 0); 3151a7bec91SWarner Losh handle(alrm_handler, SIGALRM, 0); 3161a7bec91SWarner Losh sigfillset(&mask); 3171a7bec91SWarner Losh delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS, 3183f5ac575SEdward Tomasz Napierala SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGEMT, SIGTERM, SIGTSTP, 31935c1d16eSWarner Losh SIGALRM, SIGUSR1, SIGUSR2, SIGWINCH, 0); 320d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL); 3211a7bec91SWarner Losh sigemptyset(&sa.sa_mask); 3221a7bec91SWarner Losh sa.sa_flags = 0; 3231a7bec91SWarner Losh sa.sa_handler = SIG_IGN; 324d1b1fe3aSEdward Tomasz Napierala sigaction(SIGTTIN, &sa, NULL); 325d1b1fe3aSEdward Tomasz Napierala sigaction(SIGTTOU, &sa, NULL); 3261a7bec91SWarner Losh 3271a7bec91SWarner Losh /* 3281a7bec91SWarner Losh * Paranoia. 3291a7bec91SWarner Losh */ 3301a7bec91SWarner Losh close(0); 3311a7bec91SWarner Losh close(1); 3321a7bec91SWarner Losh close(2); 3331a7bec91SWarner Losh 334335fe94fSEdward Tomasz Napierala if (kenv(KENV_GET, "init_exec", kenv_value, sizeof(kenv_value)) > 0) { 335335fe94fSEdward Tomasz Napierala replace_init(kenv_value); 336335fe94fSEdward Tomasz Napierala _exit(0); /* reboot */ 337335fe94fSEdward Tomasz Napierala } 338335fe94fSEdward Tomasz Napierala 3391a7bec91SWarner Losh if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) { 3401a7bec91SWarner Losh state_func_t next_transition; 3411a7bec91SWarner Losh 3422ef6931aSMarcelo Araujo if ((next_transition = run_script(kenv_value)) != NULL) 3431a7bec91SWarner Losh initial_transition = (state_t) next_transition; 3441a7bec91SWarner Losh } 3451a7bec91SWarner Losh 3461a7bec91SWarner Losh if (kenv(KENV_GET, "init_chroot", kenv_value, sizeof(kenv_value)) > 0) { 3471a7bec91SWarner Losh if (chdir(kenv_value) != 0 || chroot(".") != 0) 3481a7bec91SWarner Losh warning("Can't chroot to %s: %m", kenv_value); 3491a7bec91SWarner Losh } 3501a7bec91SWarner Losh 3511a7bec91SWarner Losh /* 3521a7bec91SWarner Losh * Additional check if devfs needs to be mounted: 3531a7bec91SWarner Losh * If "/" and "/dev" have the same device number, 3541a7bec91SWarner Losh * then it hasn't been mounted yet. 3551a7bec91SWarner Losh */ 3561a7bec91SWarner Losh if (!devfs) { 3571a7bec91SWarner Losh struct stat stst; 3581a7bec91SWarner Losh dev_t root_devno; 3591a7bec91SWarner Losh 3601a7bec91SWarner Losh stat("/", &stst); 3611a7bec91SWarner Losh root_devno = stst.st_dev; 3621a7bec91SWarner Losh if (stat("/dev", &stst) != 0) 3631a7bec91SWarner Losh warning("Can't stat /dev: %m"); 3641a7bec91SWarner Losh else if (stst.st_dev == root_devno) 3651a7bec91SWarner Losh devfs++; 3661a7bec91SWarner Losh } 3671a7bec91SWarner Losh 36857622f22SPoul-Henning Kamp if (devfs) { 3691f083b1eSMaxime Henrion struct iovec iov[4]; 370421b0201SPoul-Henning Kamp char *s; 371421b0201SPoul-Henning Kamp int i; 372421b0201SPoul-Henning Kamp 373ab03e6d5SXin LI char _fstype[] = "fstype"; 374ab03e6d5SXin LI char _devfs[] = "devfs"; 375ab03e6d5SXin LI char _fspath[] = "fspath"; 376ab03e6d5SXin LI char _path_dev[]= _PATH_DEV; 377ab03e6d5SXin LI 378ab03e6d5SXin LI iov[0].iov_base = _fstype; 379ab03e6d5SXin LI iov[0].iov_len = sizeof(_fstype); 380ab03e6d5SXin LI iov[1].iov_base = _devfs; 381ab03e6d5SXin LI iov[1].iov_len = sizeof(_devfs); 382ab03e6d5SXin LI iov[2].iov_base = _fspath; 383ab03e6d5SXin LI iov[2].iov_len = sizeof(_fspath); 384421b0201SPoul-Henning Kamp /* 385421b0201SPoul-Henning Kamp * Try to avoid the trailing slash in _PATH_DEV. 386421b0201SPoul-Henning Kamp * Be *very* defensive. 387421b0201SPoul-Henning Kamp */ 388421b0201SPoul-Henning Kamp s = strdup(_PATH_DEV); 389421b0201SPoul-Henning Kamp if (s != NULL) { 390421b0201SPoul-Henning Kamp i = strlen(s); 391421b0201SPoul-Henning Kamp if (i > 0 && s[i - 1] == '/') 392421b0201SPoul-Henning Kamp s[i - 1] = '\0'; 3931f083b1eSMaxime Henrion iov[3].iov_base = s; 3941f083b1eSMaxime Henrion iov[3].iov_len = strlen(s) + 1; 395421b0201SPoul-Henning Kamp } else { 396ab03e6d5SXin LI iov[3].iov_base = _path_dev; 397ab03e6d5SXin LI iov[3].iov_len = sizeof(_path_dev); 39857622f22SPoul-Henning Kamp } 3991f083b1eSMaxime Henrion nmount(iov, 4, 0); 4001f083b1eSMaxime Henrion if (s != NULL) 4011f083b1eSMaxime Henrion free(s); 402421b0201SPoul-Henning Kamp } 40357622f22SPoul-Henning Kamp 4043f5ac575SEdward Tomasz Napierala if (initial_transition != reroot_phase_two) { 4053f5ac575SEdward Tomasz Napierala /* 4063f5ac575SEdward Tomasz Napierala * Unmount reroot leftovers. This runs after init(8) 4073f5ac575SEdward Tomasz Napierala * gets reexecuted after reroot_phase_two() is done. 4083f5ac575SEdward Tomasz Napierala */ 4093f5ac575SEdward Tomasz Napierala error = unmount(_PATH_REROOT, MNT_FORCE); 4103f5ac575SEdward Tomasz Napierala if (error != 0 && errno != EINVAL) 4113f5ac575SEdward Tomasz Napierala warning("Cannot unmount %s: %m", _PATH_REROOT); 4123f5ac575SEdward Tomasz Napierala } 4133f5ac575SEdward Tomasz Napierala 4148fae3551SRodney W. Grimes /* 4158fae3551SRodney W. Grimes * Start the state machine. 4168fae3551SRodney W. Grimes */ 4171a7bec91SWarner Losh transition(initial_transition); 4188fae3551SRodney W. Grimes 4198fae3551SRodney W. Grimes /* 4208fae3551SRodney W. Grimes * Should never reach here. 4218fae3551SRodney W. Grimes */ 4228fae3551SRodney W. Grimes return 1; 4238fae3551SRodney W. Grimes } 4248fae3551SRodney W. Grimes 4258fae3551SRodney W. Grimes /* 4268fae3551SRodney W. Grimes * Associate a function with a signal handler. 4278fae3551SRodney W. Grimes */ 42845cfb1dcSXin LI static void 4298fae3551SRodney W. Grimes handle(sig_t handler, ...) 4308fae3551SRodney W. Grimes { 4318fae3551SRodney W. Grimes int sig; 4328fae3551SRodney W. Grimes struct sigaction sa; 43339034633SJames Raynard sigset_t mask_everything; 4348fae3551SRodney W. Grimes va_list ap; 4358fae3551SRodney W. Grimes va_start(ap, handler); 4368fae3551SRodney W. Grimes 4378fae3551SRodney W. Grimes sa.sa_handler = handler; 4388fae3551SRodney W. Grimes sigfillset(&mask_everything); 4398fae3551SRodney W. Grimes 44030e8350cSBruce Evans while ((sig = va_arg(ap, int)) != 0) { 4418fae3551SRodney W. Grimes sa.sa_mask = mask_everything; 4428fae3551SRodney W. Grimes /* XXX SA_RESTART? */ 4438fae3551SRodney W. Grimes sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0; 444d1b1fe3aSEdward Tomasz Napierala sigaction(sig, &sa, NULL); 4458fae3551SRodney W. Grimes } 4468fae3551SRodney W. Grimes va_end(ap); 4478fae3551SRodney W. Grimes } 4488fae3551SRodney W. Grimes 4498fae3551SRodney W. Grimes /* 4508fae3551SRodney W. Grimes * Delete a set of signals from a mask. 4518fae3551SRodney W. Grimes */ 45245cfb1dcSXin LI static void 4538fae3551SRodney W. Grimes delset(sigset_t *maskp, ...) 4548fae3551SRodney W. Grimes { 4558fae3551SRodney W. Grimes int sig; 4568fae3551SRodney W. Grimes va_list ap; 4578fae3551SRodney W. Grimes va_start(ap, maskp); 4588fae3551SRodney W. Grimes 45930e8350cSBruce Evans while ((sig = va_arg(ap, int)) != 0) 4608fae3551SRodney W. Grimes sigdelset(maskp, sig); 4618fae3551SRodney W. Grimes va_end(ap); 4628fae3551SRodney W. Grimes } 4638fae3551SRodney W. Grimes 4648fae3551SRodney W. Grimes /* 4658fae3551SRodney W. Grimes * Log a message and sleep for a while (to give someone an opportunity 4668fae3551SRodney W. Grimes * to read it and to save log or hardcopy output if the problem is chronic). 4678fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4688fae3551SRodney W. Grimes */ 46945cfb1dcSXin LI static void 4705979df34SKris Kennaway stall(const char *message, ...) 4718fae3551SRodney W. Grimes { 4728fae3551SRodney W. Grimes va_list ap; 4738fae3551SRodney W. Grimes va_start(ap, message); 4748fae3551SRodney W. Grimes 4758fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap); 4768fae3551SRodney W. Grimes va_end(ap); 4778fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 4788fae3551SRodney W. Grimes } 4798fae3551SRodney W. Grimes 4808fae3551SRodney W. Grimes /* 4818fae3551SRodney W. Grimes * Like stall(), but doesn't sleep. 4828fae3551SRodney W. Grimes * If cpp had variadic macros, the two functions could be #defines for another. 4838fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4848fae3551SRodney W. Grimes */ 48545cfb1dcSXin LI static void 4865979df34SKris Kennaway warning(const char *message, ...) 4878fae3551SRodney W. Grimes { 4888fae3551SRodney W. Grimes va_list ap; 4898fae3551SRodney W. Grimes va_start(ap, message); 4908fae3551SRodney W. Grimes 4918fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap); 4928fae3551SRodney W. Grimes va_end(ap); 4938fae3551SRodney W. Grimes } 4948fae3551SRodney W. Grimes 4958fae3551SRodney W. Grimes /* 4968fae3551SRodney W. Grimes * Log an emergency message. 4978fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4988fae3551SRodney W. Grimes */ 49945cfb1dcSXin LI static void 5005979df34SKris Kennaway emergency(const char *message, ...) 5018fae3551SRodney W. Grimes { 5028fae3551SRodney W. Grimes va_list ap; 5038fae3551SRodney W. Grimes va_start(ap, message); 5048fae3551SRodney W. Grimes 5058fae3551SRodney W. Grimes vsyslog(LOG_EMERG, message, ap); 5068fae3551SRodney W. Grimes va_end(ap); 5078fae3551SRodney W. Grimes } 5088fae3551SRodney W. Grimes 5098fae3551SRodney W. Grimes /* 5108fae3551SRodney W. Grimes * Catch a SIGSYS signal. 5118fae3551SRodney W. Grimes * 5128fae3551SRodney W. Grimes * These may arise if a system does not support sysctl. 5138fae3551SRodney W. Grimes * We tolerate up to 25 of these, then throw in the towel. 5148fae3551SRodney W. Grimes */ 51545cfb1dcSXin LI static void 51673bf18edSWarner Losh badsys(int sig) 5178fae3551SRodney W. Grimes { 5188fae3551SRodney W. Grimes static int badcount = 0; 5198fae3551SRodney W. Grimes 5208fae3551SRodney W. Grimes if (badcount++ < 25) 5218fae3551SRodney W. Grimes return; 5228fae3551SRodney W. Grimes disaster(sig); 5238fae3551SRodney W. Grimes } 5248fae3551SRodney W. Grimes 5258fae3551SRodney W. Grimes /* 5268fae3551SRodney W. Grimes * Catch an unexpected signal. 5278fae3551SRodney W. Grimes */ 52845cfb1dcSXin LI static void 52973bf18edSWarner Losh disaster(int sig) 5308fae3551SRodney W. Grimes { 531091abe40SDavid E. O'Brien 5328fae3551SRodney W. Grimes emergency("fatal signal: %s", 5338889c700SDavid Nugent (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal"); 5348fae3551SRodney W. Grimes 5358fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 5368fae3551SRodney W. Grimes _exit(sig); /* reboot */ 5378fae3551SRodney W. Grimes } 5388fae3551SRodney W. Grimes 5398fae3551SRodney W. Grimes /* 5408fae3551SRodney W. Grimes * Get the security level of the kernel. 5418fae3551SRodney W. Grimes */ 54245cfb1dcSXin LI static int 54373bf18edSWarner Losh getsecuritylevel(void) 5448fae3551SRodney W. Grimes { 5458fae3551SRodney W. Grimes #ifdef KERN_SECURELVL 5468fae3551SRodney W. Grimes int name[2], curlevel; 5478fae3551SRodney W. Grimes size_t len; 5488fae3551SRodney W. Grimes 5498fae3551SRodney W. Grimes name[0] = CTL_KERN; 5508fae3551SRodney W. Grimes name[1] = KERN_SECURELVL; 5518fae3551SRodney W. Grimes len = sizeof curlevel; 5528fae3551SRodney W. Grimes if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) { 5538fae3551SRodney W. Grimes emergency("cannot get kernel security level: %s", 5548fae3551SRodney W. Grimes strerror(errno)); 5558fae3551SRodney W. Grimes return (-1); 5568fae3551SRodney W. Grimes } 5578fae3551SRodney W. Grimes return (curlevel); 5588fae3551SRodney W. Grimes #else 5598fae3551SRodney W. Grimes return (-1); 5608fae3551SRodney W. Grimes #endif 5618fae3551SRodney W. Grimes } 5628fae3551SRodney W. Grimes 5638fae3551SRodney W. Grimes /* 5648fae3551SRodney W. Grimes * Set the security level of the kernel. 5658fae3551SRodney W. Grimes */ 56645cfb1dcSXin LI static void 56773bf18edSWarner Losh setsecuritylevel(int newlevel) 5688fae3551SRodney W. Grimes { 5698fae3551SRodney W. Grimes #ifdef KERN_SECURELVL 5708fae3551SRodney W. Grimes int name[2], curlevel; 5718fae3551SRodney W. Grimes 5728fae3551SRodney W. Grimes curlevel = getsecuritylevel(); 5738fae3551SRodney W. Grimes if (newlevel == curlevel) 5748fae3551SRodney W. Grimes return; 5758fae3551SRodney W. Grimes name[0] = CTL_KERN; 5768fae3551SRodney W. Grimes name[1] = KERN_SECURELVL; 5778fae3551SRodney W. Grimes if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) { 5788fae3551SRodney W. Grimes emergency( 5798fae3551SRodney W. Grimes "cannot change kernel security level from %d to %d: %s", 5808fae3551SRodney W. Grimes curlevel, newlevel, strerror(errno)); 5818fae3551SRodney W. Grimes return; 5828fae3551SRodney W. Grimes } 5838fae3551SRodney W. Grimes #ifdef SECURE 5848fae3551SRodney W. Grimes warning("kernel security level changed from %d to %d", 5858fae3551SRodney W. Grimes curlevel, newlevel); 5868fae3551SRodney W. Grimes #endif 5878fae3551SRodney W. Grimes #endif 5888fae3551SRodney W. Grimes } 5898fae3551SRodney W. Grimes 5908fae3551SRodney W. Grimes /* 5918fae3551SRodney W. Grimes * Change states in the finite state machine. 5928fae3551SRodney W. Grimes * The initial state is passed as an argument. 5938fae3551SRodney W. Grimes */ 59445cfb1dcSXin LI static void 59573bf18edSWarner Losh transition(state_t s) 5968fae3551SRodney W. Grimes { 597091abe40SDavid E. O'Brien 598acf0ab06SJilles Tjoelker current_state = s; 5998fae3551SRodney W. Grimes for (;;) 600acf0ab06SJilles Tjoelker current_state = (state_t) (*current_state)(); 6018fae3551SRodney W. Grimes } 6028fae3551SRodney W. Grimes 6038fae3551SRodney W. Grimes /* 6048fae3551SRodney W. Grimes * Start a session and allocate a controlling terminal. 6058fae3551SRodney W. Grimes * Only called by children of init after forking. 6068fae3551SRodney W. Grimes */ 60745cfb1dcSXin LI static void 6084c2c7b2cSEd Schouten open_console(void) 6098fae3551SRodney W. Grimes { 6108fae3551SRodney W. Grimes int fd; 6118fae3551SRodney W. Grimes 6126ee5808bSEd Schouten /* 6136ee5808bSEd Schouten * Try to open /dev/console. Open the device with O_NONBLOCK to 6146ee5808bSEd Schouten * prevent potential blocking on a carrier. 6156ee5808bSEd Schouten */ 6164c2c7b2cSEd Schouten revoke(_PATH_CONSOLE); 6174c2c7b2cSEd Schouten if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) { 6186ee5808bSEd Schouten (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK); 6194c2c7b2cSEd Schouten if (login_tty(fd) == 0) 6204c2c7b2cSEd Schouten return; 6214c2c7b2cSEd Schouten close(fd); 6224c2c7b2cSEd Schouten } 6234c2c7b2cSEd Schouten 6244c2c7b2cSEd Schouten /* No luck. Log output to file if possible. */ 6254c2c7b2cSEd Schouten if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) { 6264c2c7b2cSEd Schouten stall("cannot open null device."); 6278fae3551SRodney W. Grimes _exit(1); 6288fae3551SRodney W. Grimes } 6294c2c7b2cSEd Schouten if (fd != STDIN_FILENO) { 6304c2c7b2cSEd Schouten dup2(fd, STDIN_FILENO); 6314c2c7b2cSEd Schouten close(fd); 6328fae3551SRodney W. Grimes } 6334c2c7b2cSEd Schouten fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644); 6344c2c7b2cSEd Schouten if (fd == -1) 6354c2c7b2cSEd Schouten dup2(STDIN_FILENO, STDOUT_FILENO); 6364c2c7b2cSEd Schouten else if (fd != STDOUT_FILENO) { 6374c2c7b2cSEd Schouten dup2(fd, STDOUT_FILENO); 6384c2c7b2cSEd Schouten close(fd); 6394c2c7b2cSEd Schouten } 6404c2c7b2cSEd Schouten dup2(STDOUT_FILENO, STDERR_FILENO); 6418fae3551SRodney W. Grimes } 6428fae3551SRodney W. Grimes 64345cfb1dcSXin LI static const char * 6441a7bec91SWarner Losh get_shell(void) 6451a7bec91SWarner Losh { 6461a7bec91SWarner Losh static char kenv_value[PATH_MAX]; 6471a7bec91SWarner Losh 6481a7bec91SWarner Losh if (kenv(KENV_GET, "init_shell", kenv_value, sizeof(kenv_value)) > 0) 6491a7bec91SWarner Losh return kenv_value; 6501a7bec91SWarner Losh else 6511a7bec91SWarner Losh return _PATH_BSHELL; 6521a7bec91SWarner Losh } 6531a7bec91SWarner Losh 65445cfb1dcSXin LI static void 6551a7bec91SWarner Losh write_stderr(const char *message) 6561a7bec91SWarner Losh { 657091abe40SDavid E. O'Brien 6581a7bec91SWarner Losh write(STDERR_FILENO, message, strlen(message)); 6591a7bec91SWarner Losh } 6601a7bec91SWarner Losh 6613f5ac575SEdward Tomasz Napierala static int 6623f5ac575SEdward Tomasz Napierala read_file(const char *path, void **bufp, size_t *bufsizep) 6633f5ac575SEdward Tomasz Napierala { 6643f5ac575SEdward Tomasz Napierala struct stat sb; 6653f5ac575SEdward Tomasz Napierala size_t bufsize; 6663f5ac575SEdward Tomasz Napierala void *buf; 6673f5ac575SEdward Tomasz Napierala ssize_t nbytes; 6683f5ac575SEdward Tomasz Napierala int error, fd; 6693f5ac575SEdward Tomasz Napierala 6703f5ac575SEdward Tomasz Napierala fd = open(path, O_RDONLY); 6713f5ac575SEdward Tomasz Napierala if (fd < 0) { 6723f5ac575SEdward Tomasz Napierala emergency("%s: %s", path, strerror(errno)); 6733f5ac575SEdward Tomasz Napierala return (-1); 6743f5ac575SEdward Tomasz Napierala } 6753f5ac575SEdward Tomasz Napierala 6763f5ac575SEdward Tomasz Napierala error = fstat(fd, &sb); 6773f5ac575SEdward Tomasz Napierala if (error != 0) { 6783f5ac575SEdward Tomasz Napierala emergency("fstat: %s", strerror(errno)); 679b9124fc3SEdward Tomasz Napierala close(fd); 6803f5ac575SEdward Tomasz Napierala return (error); 6813f5ac575SEdward Tomasz Napierala } 6823f5ac575SEdward Tomasz Napierala 6833f5ac575SEdward Tomasz Napierala bufsize = sb.st_size; 6843f5ac575SEdward Tomasz Napierala buf = malloc(bufsize); 6853f5ac575SEdward Tomasz Napierala if (buf == NULL) { 6863f5ac575SEdward Tomasz Napierala emergency("malloc: %s", strerror(errno)); 687b9124fc3SEdward Tomasz Napierala close(fd); 6883f5ac575SEdward Tomasz Napierala return (error); 6893f5ac575SEdward Tomasz Napierala } 6903f5ac575SEdward Tomasz Napierala 6913f5ac575SEdward Tomasz Napierala nbytes = read(fd, buf, bufsize); 6923f5ac575SEdward Tomasz Napierala if (nbytes != (ssize_t)bufsize) { 6933f5ac575SEdward Tomasz Napierala emergency("read: %s", strerror(errno)); 694b9124fc3SEdward Tomasz Napierala close(fd); 6953f5ac575SEdward Tomasz Napierala free(buf); 6963f5ac575SEdward Tomasz Napierala return (error); 6973f5ac575SEdward Tomasz Napierala } 6983f5ac575SEdward Tomasz Napierala 6993f5ac575SEdward Tomasz Napierala error = close(fd); 7003f5ac575SEdward Tomasz Napierala if (error != 0) { 7013f5ac575SEdward Tomasz Napierala emergency("close: %s", strerror(errno)); 7023f5ac575SEdward Tomasz Napierala free(buf); 7033f5ac575SEdward Tomasz Napierala return (error); 7043f5ac575SEdward Tomasz Napierala } 7053f5ac575SEdward Tomasz Napierala 7063f5ac575SEdward Tomasz Napierala *bufp = buf; 7073f5ac575SEdward Tomasz Napierala *bufsizep = bufsize; 7083f5ac575SEdward Tomasz Napierala 7093f5ac575SEdward Tomasz Napierala return (0); 7103f5ac575SEdward Tomasz Napierala } 7113f5ac575SEdward Tomasz Napierala 7123f5ac575SEdward Tomasz Napierala static int 713b9124fc3SEdward Tomasz Napierala create_file(const char *path, const void *buf, size_t bufsize) 7143f5ac575SEdward Tomasz Napierala { 7153f5ac575SEdward Tomasz Napierala ssize_t nbytes; 7163f5ac575SEdward Tomasz Napierala int error, fd; 7173f5ac575SEdward Tomasz Napierala 7183f5ac575SEdward Tomasz Napierala fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700); 7193f5ac575SEdward Tomasz Napierala if (fd < 0) { 7203f5ac575SEdward Tomasz Napierala emergency("%s: %s", path, strerror(errno)); 7213f5ac575SEdward Tomasz Napierala return (-1); 7223f5ac575SEdward Tomasz Napierala } 7233f5ac575SEdward Tomasz Napierala 7243f5ac575SEdward Tomasz Napierala nbytes = write(fd, buf, bufsize); 7253f5ac575SEdward Tomasz Napierala if (nbytes != (ssize_t)bufsize) { 7263f5ac575SEdward Tomasz Napierala emergency("write: %s", strerror(errno)); 727b9124fc3SEdward Tomasz Napierala close(fd); 7283f5ac575SEdward Tomasz Napierala return (-1); 7293f5ac575SEdward Tomasz Napierala } 7303f5ac575SEdward Tomasz Napierala 7313f5ac575SEdward Tomasz Napierala error = close(fd); 7323f5ac575SEdward Tomasz Napierala if (error != 0) { 7333f5ac575SEdward Tomasz Napierala emergency("close: %s", strerror(errno)); 7343f5ac575SEdward Tomasz Napierala return (-1); 7353f5ac575SEdward Tomasz Napierala } 7363f5ac575SEdward Tomasz Napierala 7373f5ac575SEdward Tomasz Napierala return (0); 7383f5ac575SEdward Tomasz Napierala } 7393f5ac575SEdward Tomasz Napierala 7403f5ac575SEdward Tomasz Napierala static int 7413f5ac575SEdward Tomasz Napierala mount_tmpfs(const char *fspath) 7423f5ac575SEdward Tomasz Napierala { 7433f5ac575SEdward Tomasz Napierala struct iovec *iov; 7443f5ac575SEdward Tomasz Napierala char errmsg[255]; 7453f5ac575SEdward Tomasz Napierala int error, iovlen; 7463f5ac575SEdward Tomasz Napierala 7473f5ac575SEdward Tomasz Napierala iov = NULL; 7483f5ac575SEdward Tomasz Napierala iovlen = 0; 7493f5ac575SEdward Tomasz Napierala memset(errmsg, 0, sizeof(errmsg)); 7503f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "fstype", 7513f5ac575SEdward Tomasz Napierala __DECONST(void *, "tmpfs"), (size_t)-1); 7523f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "fspath", 7533f5ac575SEdward Tomasz Napierala __DECONST(void *, fspath), (size_t)-1); 7543f5ac575SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "errmsg", 7553f5ac575SEdward Tomasz Napierala errmsg, sizeof(errmsg)); 7563f5ac575SEdward Tomasz Napierala 7573f5ac575SEdward Tomasz Napierala error = nmount(iov, iovlen, 0); 7583f5ac575SEdward Tomasz Napierala if (error != 0) { 7593f5ac575SEdward Tomasz Napierala if (*errmsg != '\0') { 7603f5ac575SEdward Tomasz Napierala emergency("cannot mount tmpfs on %s: %s: %s", 7613f5ac575SEdward Tomasz Napierala fspath, errmsg, strerror(errno)); 7623f5ac575SEdward Tomasz Napierala } else { 7633f5ac575SEdward Tomasz Napierala emergency("cannot mount tmpfs on %s: %s", 7643f5ac575SEdward Tomasz Napierala fspath, strerror(errno)); 7653f5ac575SEdward Tomasz Napierala } 7663f5ac575SEdward Tomasz Napierala return (error); 7673f5ac575SEdward Tomasz Napierala } 7683f5ac575SEdward Tomasz Napierala return (0); 7693f5ac575SEdward Tomasz Napierala } 7703f5ac575SEdward Tomasz Napierala 7713f5ac575SEdward Tomasz Napierala static state_func_t 7723f5ac575SEdward Tomasz Napierala reroot(void) 7733f5ac575SEdward Tomasz Napierala { 7743f5ac575SEdward Tomasz Napierala void *buf; 775377b6d1eSEdward Tomasz Napierala size_t bufsize; 776377b6d1eSEdward Tomasz Napierala int error; 7773f5ac575SEdward Tomasz Napierala 778b9124fc3SEdward Tomasz Napierala buf = NULL; 779b9124fc3SEdward Tomasz Napierala bufsize = 0; 780b9124fc3SEdward Tomasz Napierala 7813f5ac575SEdward Tomasz Napierala revoke_ttys(); 7823f5ac575SEdward Tomasz Napierala runshutdown(); 7833f5ac575SEdward Tomasz Napierala 7843f5ac575SEdward Tomasz Napierala /* 7853f5ac575SEdward Tomasz Napierala * Make sure nobody can interfere with our scheme. 786126ba219SEdward Tomasz Napierala * Ignore ESRCH, which can apparently happen when 787126ba219SEdward Tomasz Napierala * there are no processes to kill. 7883f5ac575SEdward Tomasz Napierala */ 7893f5ac575SEdward Tomasz Napierala error = kill(-1, SIGKILL); 790126ba219SEdward Tomasz Napierala if (error != 0 && errno != ESRCH) { 7913f5ac575SEdward Tomasz Napierala emergency("kill(2) failed: %s", strerror(errno)); 7923f5ac575SEdward Tomasz Napierala goto out; 7933f5ac575SEdward Tomasz Napierala } 7943f5ac575SEdward Tomasz Napierala 7953f5ac575SEdward Tomasz Napierala /* 7963f5ac575SEdward Tomasz Napierala * Copy the init binary into tmpfs, so that we can unmount 7973f5ac575SEdward Tomasz Napierala * the old rootfs without committing suicide. 7983f5ac575SEdward Tomasz Napierala */ 799377b6d1eSEdward Tomasz Napierala error = read_file(init_path_argv0, &buf, &bufsize); 8003f5ac575SEdward Tomasz Napierala if (error != 0) 8013f5ac575SEdward Tomasz Napierala goto out; 8023f5ac575SEdward Tomasz Napierala error = mount_tmpfs(_PATH_REROOT); 8033f5ac575SEdward Tomasz Napierala if (error != 0) 8043f5ac575SEdward Tomasz Napierala goto out; 8053f5ac575SEdward Tomasz Napierala error = create_file(_PATH_REROOT_INIT, buf, bufsize); 8063f5ac575SEdward Tomasz Napierala if (error != 0) 8073f5ac575SEdward Tomasz Napierala goto out; 8083f5ac575SEdward Tomasz Napierala 8093f5ac575SEdward Tomasz Napierala /* 8103f5ac575SEdward Tomasz Napierala * Execute the temporary init. 8113f5ac575SEdward Tomasz Napierala */ 8123f5ac575SEdward Tomasz Napierala execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL); 8133f5ac575SEdward Tomasz Napierala emergency("cannot exec %s: %s", _PATH_REROOT_INIT, strerror(errno)); 8143f5ac575SEdward Tomasz Napierala 8153f5ac575SEdward Tomasz Napierala out: 8163f5ac575SEdward Tomasz Napierala emergency("reroot failed; going to single user mode"); 817b9124fc3SEdward Tomasz Napierala free(buf); 8183f5ac575SEdward Tomasz Napierala return (state_func_t) single_user; 8193f5ac575SEdward Tomasz Napierala } 8203f5ac575SEdward Tomasz Napierala 8213f5ac575SEdward Tomasz Napierala static state_func_t 8223f5ac575SEdward Tomasz Napierala reroot_phase_two(void) 8233f5ac575SEdward Tomasz Napierala { 8243f5ac575SEdward Tomasz Napierala char init_path[PATH_MAX], *path, *path_component; 8253f5ac575SEdward Tomasz Napierala size_t init_path_len; 8263f5ac575SEdward Tomasz Napierala int nbytes, error; 8273f5ac575SEdward Tomasz Napierala 8283f5ac575SEdward Tomasz Napierala /* 8293f5ac575SEdward Tomasz Napierala * Ask the kernel to mount the new rootfs. 8303f5ac575SEdward Tomasz Napierala */ 8313f5ac575SEdward Tomasz Napierala error = reboot(RB_REROOT); 8323f5ac575SEdward Tomasz Napierala if (error != 0) { 8333f5ac575SEdward Tomasz Napierala emergency("RB_REBOOT failed: %s", strerror(errno)); 8343f5ac575SEdward Tomasz Napierala goto out; 8353f5ac575SEdward Tomasz Napierala } 8363f5ac575SEdward Tomasz Napierala 8373f5ac575SEdward Tomasz Napierala /* 8383f5ac575SEdward Tomasz Napierala * Figure out where the destination init(8) binary is. Note that 8393f5ac575SEdward Tomasz Napierala * the path could be different than what we've started with. Use 8403f5ac575SEdward Tomasz Napierala * the value from kenv, if set, or the one from sysctl otherwise. 8413f5ac575SEdward Tomasz Napierala * The latter defaults to a hardcoded value, but can be overridden 8423f5ac575SEdward Tomasz Napierala * by a build time option. 8433f5ac575SEdward Tomasz Napierala */ 8443f5ac575SEdward Tomasz Napierala nbytes = kenv(KENV_GET, "init_path", init_path, sizeof(init_path)); 8453f5ac575SEdward Tomasz Napierala if (nbytes <= 0) { 8463f5ac575SEdward Tomasz Napierala init_path_len = sizeof(init_path); 8473f5ac575SEdward Tomasz Napierala error = sysctlbyname("kern.init_path", 8483f5ac575SEdward Tomasz Napierala init_path, &init_path_len, NULL, 0); 8493f5ac575SEdward Tomasz Napierala if (error != 0) { 8503f5ac575SEdward Tomasz Napierala emergency("failed to retrieve kern.init_path: %s", 8513f5ac575SEdward Tomasz Napierala strerror(errno)); 8523f5ac575SEdward Tomasz Napierala goto out; 8533f5ac575SEdward Tomasz Napierala } 8543f5ac575SEdward Tomasz Napierala } 8553f5ac575SEdward Tomasz Napierala 8563f5ac575SEdward Tomasz Napierala /* 8573f5ac575SEdward Tomasz Napierala * Repeat the init search logic from sys/kern/init_path.c 8583f5ac575SEdward Tomasz Napierala */ 8593f5ac575SEdward Tomasz Napierala path_component = init_path; 8603f5ac575SEdward Tomasz Napierala while ((path = strsep(&path_component, ":")) != NULL) { 8613f5ac575SEdward Tomasz Napierala /* 8623f5ac575SEdward Tomasz Napierala * Execute init(8) from the new rootfs. 8633f5ac575SEdward Tomasz Napierala */ 8643f5ac575SEdward Tomasz Napierala execl(path, path, NULL); 8653f5ac575SEdward Tomasz Napierala } 8663f5ac575SEdward Tomasz Napierala emergency("cannot exec init from %s: %s", init_path, strerror(errno)); 8673f5ac575SEdward Tomasz Napierala 8683f5ac575SEdward Tomasz Napierala out: 8693f5ac575SEdward Tomasz Napierala emergency("reroot failed; going to single user mode"); 8703f5ac575SEdward Tomasz Napierala return (state_func_t) single_user; 8713f5ac575SEdward Tomasz Napierala } 8723f5ac575SEdward Tomasz Napierala 8738fae3551SRodney W. Grimes /* 8748fae3551SRodney W. Grimes * Bring the system up single user. 8758fae3551SRodney W. Grimes */ 87645cfb1dcSXin LI static state_func_t 87773bf18edSWarner Losh single_user(void) 8788fae3551SRodney W. Grimes { 8798fae3551SRodney W. Grimes pid_t pid, wpid; 8808fae3551SRodney W. Grimes int status; 8818fae3551SRodney W. Grimes sigset_t mask; 8821a7bec91SWarner Losh const char *shell; 8838fae3551SRodney W. Grimes char *argv[2]; 8848402d33aSKonstantin Belousov struct timeval tv, tn; 8858fae3551SRodney W. Grimes #ifdef SECURE 8868fae3551SRodney W. Grimes struct ttyent *typ; 8878fae3551SRodney W. Grimes struct passwd *pp; 8888fae3551SRodney W. Grimes static const char banner[] = 8898fae3551SRodney W. Grimes "Enter root password, or ^D to go multi-user\n"; 8908fae3551SRodney W. Grimes char *clear, *password; 8918fae3551SRodney W. Grimes #endif 89263322c28SPoul-Henning Kamp #ifdef DEBUGSHELL 89363322c28SPoul-Henning Kamp char altshell[128]; 89463322c28SPoul-Henning Kamp #endif 8958fae3551SRodney W. Grimes 896db8ad19dSJordan K. Hubbard if (Reboot) { 897a0a549c7SRuslan Ermilov /* Instead of going single user, let's reboot the machine */ 898e460cfd3SNate Williams sync(); 899e10037dfSKonstantin Belousov if (reboot(howto) == -1) { 900e10037dfSKonstantin Belousov emergency("reboot(%#x) failed, %s", howto, 901e10037dfSKonstantin Belousov strerror(errno)); 902e10037dfSKonstantin Belousov _exit(1); /* panic and reboot */ 903e10037dfSKonstantin Belousov } 904e10037dfSKonstantin Belousov warning("reboot(%#x) returned", howto); 905e10037dfSKonstantin Belousov _exit(0); /* panic as well */ 906e460cfd3SNate Williams } 907e460cfd3SNate Williams 9081a7bec91SWarner Losh shell = get_shell(); 9091a7bec91SWarner Losh 9108fae3551SRodney W. Grimes if ((pid = fork()) == 0) { 9118fae3551SRodney W. Grimes /* 9128fae3551SRodney W. Grimes * Start the single user session. 9138fae3551SRodney W. Grimes */ 9144c2c7b2cSEd Schouten open_console(); 9158fae3551SRodney W. Grimes 9168fae3551SRodney W. Grimes #ifdef SECURE 9178fae3551SRodney W. Grimes /* 9188fae3551SRodney W. Grimes * Check the root password. 9198fae3551SRodney W. Grimes * We don't care if the console is 'on' by default; 9208fae3551SRodney W. Grimes * it's the only tty that can be 'off' and 'secure'. 9218fae3551SRodney W. Grimes */ 9228fae3551SRodney W. Grimes typ = getttynam("console"); 9238fae3551SRodney W. Grimes pp = getpwnam("root"); 924a69497d7SMatthew Dillon if (typ && (typ->ty_status & TTY_SECURE) == 0 && 925a69497d7SMatthew Dillon pp && *pp->pw_passwd) { 9261a7bec91SWarner Losh write_stderr(banner); 9278fae3551SRodney W. Grimes for (;;) { 9288fae3551SRodney W. Grimes clear = getpass("Password:"); 9292ef6931aSMarcelo Araujo if (clear == NULL || *clear == '\0') 9308fae3551SRodney W. Grimes _exit(0); 9318fae3551SRodney W. Grimes password = crypt(clear, pp->pw_passwd); 9328fae3551SRodney W. Grimes bzero(clear, _PASSWORD_LEN); 9332c9a33f5SConrad Meyer if (password != NULL && 93429dcf726SKevin Lo strcmp(password, pp->pw_passwd) == 0) 9358fae3551SRodney W. Grimes break; 9368fae3551SRodney W. Grimes warning("single-user login failed\n"); 9378fae3551SRodney W. Grimes } 9388fae3551SRodney W. Grimes } 9398fae3551SRodney W. Grimes endttyent(); 9408fae3551SRodney W. Grimes endpwent(); 9418fae3551SRodney W. Grimes #endif /* SECURE */ 9428fae3551SRodney W. Grimes 9438fae3551SRodney W. Grimes #ifdef DEBUGSHELL 9448fae3551SRodney W. Grimes { 94563322c28SPoul-Henning Kamp char *cp = altshell; 9468fae3551SRodney W. Grimes int num; 9478fae3551SRodney W. Grimes 9481a7bec91SWarner Losh #define SHREQUEST "Enter full pathname of shell or RETURN for " 9491a7bec91SWarner Losh write_stderr(SHREQUEST); 9501a7bec91SWarner Losh write_stderr(shell); 9511a7bec91SWarner Losh write_stderr(": "); 9528fae3551SRodney W. Grimes while ((num = read(STDIN_FILENO, cp, 1)) != -1 && 9538fae3551SRodney W. Grimes num != 0 && *cp != '\n' && cp < &altshell[127]) 9548fae3551SRodney W. Grimes cp++; 9558fae3551SRodney W. Grimes *cp = '\0'; 9568fae3551SRodney W. Grimes if (altshell[0] != '\0') 9578fae3551SRodney W. Grimes shell = altshell; 9588fae3551SRodney W. Grimes } 9598fae3551SRodney W. Grimes #endif /* DEBUGSHELL */ 9608fae3551SRodney W. Grimes 9618fae3551SRodney W. Grimes /* 9628fae3551SRodney W. Grimes * Unblock signals. 9638fae3551SRodney W. Grimes * We catch all the interesting ones, 9648fae3551SRodney W. Grimes * and those are reset to SIG_DFL on exec. 9658fae3551SRodney W. Grimes */ 9668fae3551SRodney W. Grimes sigemptyset(&mask); 967d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL); 9688fae3551SRodney W. Grimes 9698fae3551SRodney W. Grimes /* 9708fae3551SRodney W. Grimes * Fire off a shell. 9718fae3551SRodney W. Grimes * If the default one doesn't work, try the Bourne shell. 9728fae3551SRodney W. Grimes */ 973ab03e6d5SXin LI 974ab03e6d5SXin LI char name[] = "-sh"; 975ab03e6d5SXin LI 976ab03e6d5SXin LI argv[0] = name; 977*be03cfddSEdward Tomasz Napierala argv[1] = NULL; 9788fae3551SRodney W. Grimes execv(shell, argv); 9798fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", shell); 9808fae3551SRodney W. Grimes execv(_PATH_BSHELL, argv); 9818fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", _PATH_BSHELL); 9828fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 9838fae3551SRodney W. Grimes _exit(1); 9848fae3551SRodney W. Grimes } 9858fae3551SRodney W. Grimes 9868fae3551SRodney W. Grimes if (pid == -1) { 9878fae3551SRodney W. Grimes /* 9888fae3551SRodney W. Grimes * We are seriously hosed. Do our best. 9898fae3551SRodney W. Grimes */ 9908fae3551SRodney W. Grimes emergency("can't fork single-user shell, trying again"); 9918fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0) 9928fae3551SRodney W. Grimes continue; 9938fae3551SRodney W. Grimes return (state_func_t) single_user; 9948fae3551SRodney W. Grimes } 9958fae3551SRodney W. Grimes 9968fae3551SRodney W. Grimes requested_transition = 0; 9978fae3551SRodney W. Grimes do { 9988fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 9998fae3551SRodney W. Grimes collect_child(wpid); 10008fae3551SRodney W. Grimes if (wpid == -1) { 10018fae3551SRodney W. Grimes if (errno == EINTR) 10028fae3551SRodney W. Grimes continue; 10038fae3551SRodney W. Grimes warning("wait for single-user shell failed: %m; restarting"); 10048fae3551SRodney W. Grimes return (state_func_t) single_user; 10058fae3551SRodney W. Grimes } 10068fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) { 10078fae3551SRodney W. Grimes warning("init: shell stopped, restarting\n"); 10088fae3551SRodney W. Grimes kill(pid, SIGCONT); 10098fae3551SRodney W. Grimes wpid = -1; 10108fae3551SRodney W. Grimes } 10118fae3551SRodney W. Grimes } while (wpid != pid && !requested_transition); 10128fae3551SRodney W. Grimes 10138fae3551SRodney W. Grimes if (requested_transition) 10148fae3551SRodney W. Grimes return (state_func_t) requested_transition; 10158fae3551SRodney W. Grimes 10168fae3551SRodney W. Grimes if (!WIFEXITED(status)) { 10178fae3551SRodney W. Grimes if (WTERMSIG(status) == SIGKILL) { 10188fae3551SRodney W. Grimes /* 10198fae3551SRodney W. Grimes * reboot(8) killed shell? 10208fae3551SRodney W. Grimes */ 10218fae3551SRodney W. Grimes warning("single user shell terminated."); 10228402d33aSKonstantin Belousov gettimeofday(&tv, NULL); 10238402d33aSKonstantin Belousov tn = tv; 10248402d33aSKonstantin Belousov tv.tv_sec += STALL_TIMEOUT; 10258402d33aSKonstantin Belousov while (tv.tv_sec > tn.tv_sec || (tv.tv_sec == 10268402d33aSKonstantin Belousov tn.tv_sec && tv.tv_usec > tn.tv_usec)) { 10278402d33aSKonstantin Belousov sleep(1); 10288402d33aSKonstantin Belousov gettimeofday(&tn, NULL); 10298402d33aSKonstantin Belousov } 10308fae3551SRodney W. Grimes _exit(0); 10318fae3551SRodney W. Grimes } else { 10328fae3551SRodney W. Grimes warning("single user shell terminated, restarting"); 10338fae3551SRodney W. Grimes return (state_func_t) single_user; 10348fae3551SRodney W. Grimes } 10358fae3551SRodney W. Grimes } 10368fae3551SRodney W. Grimes 10378fae3551SRodney W. Grimes runcom_mode = FASTBOOT; 10388fae3551SRodney W. Grimes return (state_func_t) runcom; 10398fae3551SRodney W. Grimes } 10408fae3551SRodney W. Grimes 10418fae3551SRodney W. Grimes /* 10428fae3551SRodney W. Grimes * Run the system startup script. 10438fae3551SRodney W. Grimes */ 104445cfb1dcSXin LI static state_func_t 104573bf18edSWarner Losh runcom(void) 10468fae3551SRodney W. Grimes { 10471a7bec91SWarner Losh state_func_t next_transition; 10481a7bec91SWarner Losh 10492ef6931aSMarcelo Araujo if ((next_transition = run_script(_PATH_RUNCOM)) != NULL) 10501a7bec91SWarner Losh return next_transition; 10511a7bec91SWarner Losh 10521a7bec91SWarner Losh runcom_mode = AUTOBOOT; /* the default */ 10531a7bec91SWarner Losh return (state_func_t) read_ttys; 10541a7bec91SWarner Losh } 10551a7bec91SWarner Losh 1056f3c4a698SEdward Tomasz Napierala static void 1057f3c4a698SEdward Tomasz Napierala execute_script(char *argv[]) 1058f3c4a698SEdward Tomasz Napierala { 1059f3c4a698SEdward Tomasz Napierala struct sigaction sa; 1060f3c4a698SEdward Tomasz Napierala const char *shell, *script; 1061f3c4a698SEdward Tomasz Napierala int error; 1062f3c4a698SEdward Tomasz Napierala 1063f3c4a698SEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 1064f3c4a698SEdward Tomasz Napierala sigemptyset(&sa.sa_mask); 1065f3c4a698SEdward Tomasz Napierala sa.sa_handler = SIG_IGN; 1066f3c4a698SEdward Tomasz Napierala sigaction(SIGTSTP, &sa, NULL); 1067f3c4a698SEdward Tomasz Napierala sigaction(SIGHUP, &sa, NULL); 1068f3c4a698SEdward Tomasz Napierala 1069f3c4a698SEdward Tomasz Napierala open_console(); 1070f3c4a698SEdward Tomasz Napierala 1071f3c4a698SEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL); 1072f3c4a698SEdward Tomasz Napierala #ifdef LOGIN_CAP 1073f3c4a698SEdward Tomasz Napierala setprocresources(RESOURCE_RC); 1074f3c4a698SEdward Tomasz Napierala #endif 1075f3c4a698SEdward Tomasz Napierala 1076f3c4a698SEdward Tomasz Napierala /* 1077f3c4a698SEdward Tomasz Napierala * Try to directly execute the script first. If it 1078f3c4a698SEdward Tomasz Napierala * fails, try the old method of passing the script path 1079f3c4a698SEdward Tomasz Napierala * to sh(1). Don't complain if it fails because of 1080f3c4a698SEdward Tomasz Napierala * the missing execute bit. 1081f3c4a698SEdward Tomasz Napierala */ 1082f3c4a698SEdward Tomasz Napierala script = argv[1]; 1083f3c4a698SEdward Tomasz Napierala error = access(script, X_OK); 1084f3c4a698SEdward Tomasz Napierala if (error == 0) { 1085f3c4a698SEdward Tomasz Napierala execv(script, argv + 1); 1086f3c4a698SEdward Tomasz Napierala warning("can't exec %s: %m", script); 1087f3c4a698SEdward Tomasz Napierala } else if (errno != EACCES) { 1088f3c4a698SEdward Tomasz Napierala warning("can't access %s: %m", script); 1089f3c4a698SEdward Tomasz Napierala } 1090f3c4a698SEdward Tomasz Napierala 1091f3c4a698SEdward Tomasz Napierala shell = get_shell(); 1092f3c4a698SEdward Tomasz Napierala execv(shell, argv); 1093f3c4a698SEdward Tomasz Napierala stall("can't exec %s for %s: %m", shell, script); 1094f3c4a698SEdward Tomasz Napierala } 1095f3c4a698SEdward Tomasz Napierala 10961a7bec91SWarner Losh /* 1097335fe94fSEdward Tomasz Napierala * Execute binary, replacing init(8) as PID 1. 1098335fe94fSEdward Tomasz Napierala */ 1099335fe94fSEdward Tomasz Napierala static void 1100335fe94fSEdward Tomasz Napierala replace_init(char *path) 1101335fe94fSEdward Tomasz Napierala { 1102335fe94fSEdward Tomasz Napierala char *argv[3]; 1103335fe94fSEdward Tomasz Napierala char sh[] = "sh"; 1104335fe94fSEdward Tomasz Napierala 1105335fe94fSEdward Tomasz Napierala argv[0] = sh; 1106335fe94fSEdward Tomasz Napierala argv[1] = path; 1107335fe94fSEdward Tomasz Napierala argv[2] = NULL; 1108335fe94fSEdward Tomasz Napierala 1109335fe94fSEdward Tomasz Napierala execute_script(argv); 1110335fe94fSEdward Tomasz Napierala } 1111335fe94fSEdward Tomasz Napierala 1112335fe94fSEdward Tomasz Napierala /* 11131a7bec91SWarner Losh * Run a shell script. 11141a7bec91SWarner Losh * Returns 0 on success, otherwise the next transition to enter: 11151a7bec91SWarner Losh * - single_user if fork/execv/waitpid failed, or if the script 11161a7bec91SWarner Losh * terminated with a signal or exit code != 0. 1117acf0ab06SJilles Tjoelker * - death_single if a SIGTERM was delivered to init(8). 11181a7bec91SWarner Losh */ 111945cfb1dcSXin LI static state_func_t 11201a7bec91SWarner Losh run_script(const char *script) 11211a7bec91SWarner Losh { 11228fae3551SRodney W. Grimes pid_t pid, wpid; 1123f3c4a698SEdward Tomasz Napierala int status; 11248fae3551SRodney W. Grimes char *argv[4]; 11251a7bec91SWarner Losh const char *shell; 11268fae3551SRodney W. Grimes 11271a7bec91SWarner Losh shell = get_shell(); 11281a7bec91SWarner Losh 11298fae3551SRodney W. Grimes if ((pid = fork()) == 0) { 11308fae3551SRodney W. Grimes 1131ab03e6d5SXin LI char _sh[] = "sh"; 1132ab03e6d5SXin LI char _autoboot[] = "autoboot"; 1133ab03e6d5SXin LI 1134ab03e6d5SXin LI argv[0] = _sh; 11351a7bec91SWarner Losh argv[1] = __DECONST(char *, script); 1136ab03e6d5SXin LI argv[2] = runcom_mode == AUTOBOOT ? _autoboot : 0; 1137*be03cfddSEdward Tomasz Napierala argv[3] = NULL; 11388fae3551SRodney W. Grimes 1139f3c4a698SEdward Tomasz Napierala execute_script(argv); 1140f3c4a698SEdward Tomasz Napierala sleep(STALL_TIMEOUT); 11418fae3551SRodney W. Grimes _exit(1); /* force single user mode */ 11428fae3551SRodney W. Grimes } 11438fae3551SRodney W. Grimes 11448fae3551SRodney W. Grimes if (pid == -1) { 11451a7bec91SWarner Losh emergency("can't fork for %s on %s: %m", shell, script); 11468fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0) 11478fae3551SRodney W. Grimes continue; 11488fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 11498fae3551SRodney W. Grimes return (state_func_t) single_user; 11508fae3551SRodney W. Grimes } 11518fae3551SRodney W. Grimes 11528fae3551SRodney W. Grimes /* 11538fae3551SRodney W. Grimes * Copied from single_user(). This is a bit paranoid. 11548fae3551SRodney W. Grimes */ 11556e8ff8b7SDag-Erling Smørgrav requested_transition = 0; 11568fae3551SRodney W. Grimes do { 11578fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 11588fae3551SRodney W. Grimes collect_child(wpid); 11598fae3551SRodney W. Grimes if (wpid == -1) { 11603f5ac575SEdward Tomasz Napierala if (requested_transition == death_single || 11613f5ac575SEdward Tomasz Napierala requested_transition == reroot) 11623f5ac575SEdward Tomasz Napierala return (state_func_t) requested_transition; 11638fae3551SRodney W. Grimes if (errno == EINTR) 11648fae3551SRodney W. Grimes continue; 11651a7bec91SWarner Losh warning("wait for %s on %s failed: %m; going to " 11661a7bec91SWarner Losh "single user mode", shell, script); 11678fae3551SRodney W. Grimes return (state_func_t) single_user; 11688fae3551SRodney W. Grimes } 11698fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) { 11708fae3551SRodney W. Grimes warning("init: %s on %s stopped, restarting\n", 11711a7bec91SWarner Losh shell, script); 11728fae3551SRodney W. Grimes kill(pid, SIGCONT); 11738fae3551SRodney W. Grimes wpid = -1; 11748fae3551SRodney W. Grimes } 11758fae3551SRodney W. Grimes } while (wpid != pid); 11768fae3551SRodney W. Grimes 11778fae3551SRodney W. Grimes if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 11788fae3551SRodney W. Grimes requested_transition == catatonia) { 11798fae3551SRodney W. Grimes /* /etc/rc executed /sbin/reboot; wait for the end quietly */ 11808fae3551SRodney W. Grimes sigset_t s; 11818fae3551SRodney W. Grimes 11828fae3551SRodney W. Grimes sigfillset(&s); 11838fae3551SRodney W. Grimes for (;;) 11848fae3551SRodney W. Grimes sigsuspend(&s); 11858fae3551SRodney W. Grimes } 11868fae3551SRodney W. Grimes 11878fae3551SRodney W. Grimes if (!WIFEXITED(status)) { 11881a7bec91SWarner Losh warning("%s on %s terminated abnormally, going to single " 11891a7bec91SWarner Losh "user mode", shell, script); 11908fae3551SRodney W. Grimes return (state_func_t) single_user; 11918fae3551SRodney W. Grimes } 11928fae3551SRodney W. Grimes 11938fae3551SRodney W. Grimes if (WEXITSTATUS(status)) 11948fae3551SRodney W. Grimes return (state_func_t) single_user; 11958fae3551SRodney W. Grimes 11961a7bec91SWarner Losh return (state_func_t) 0; 11978fae3551SRodney W. Grimes } 11988fae3551SRodney W. Grimes 11998fae3551SRodney W. Grimes /* 12008fae3551SRodney W. Grimes * Open the session database. 12018fae3551SRodney W. Grimes * 12028fae3551SRodney W. Grimes * NB: We could pass in the size here; is it necessary? 12038fae3551SRodney W. Grimes */ 120445cfb1dcSXin LI static int 120573bf18edSWarner Losh start_session_db(void) 12068fae3551SRodney W. Grimes { 12078fae3551SRodney W. Grimes if (session_db && (*session_db->close)(session_db)) 12088fae3551SRodney W. Grimes emergency("session database close: %s", strerror(errno)); 120914adaa14SMarcelo Araujo if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL) { 12108fae3551SRodney W. Grimes emergency("session database open: %s", strerror(errno)); 12118fae3551SRodney W. Grimes return (1); 12128fae3551SRodney W. Grimes } 12138fae3551SRodney W. Grimes return (0); 12148fae3551SRodney W. Grimes 12158fae3551SRodney W. Grimes } 12168fae3551SRodney W. Grimes 12178fae3551SRodney W. Grimes /* 12188fae3551SRodney W. Grimes * Add a new login session. 12198fae3551SRodney W. Grimes */ 122045cfb1dcSXin LI static void 122173bf18edSWarner Losh add_session(session_t *sp) 12228fae3551SRodney W. Grimes { 12238fae3551SRodney W. Grimes DBT key; 12248fae3551SRodney W. Grimes DBT data; 12258fae3551SRodney W. Grimes 12268fae3551SRodney W. Grimes key.data = &sp->se_process; 12278fae3551SRodney W. Grimes key.size = sizeof sp->se_process; 12288fae3551SRodney W. Grimes data.data = &sp; 12298fae3551SRodney W. Grimes data.size = sizeof sp; 12308fae3551SRodney W. Grimes 12318fae3551SRodney W. Grimes if ((*session_db->put)(session_db, &key, &data, 0)) 12328fae3551SRodney W. Grimes emergency("insert %d: %s", sp->se_process, strerror(errno)); 12338fae3551SRodney W. Grimes } 12348fae3551SRodney W. Grimes 12358fae3551SRodney W. Grimes /* 12368fae3551SRodney W. Grimes * Delete an old login session. 12378fae3551SRodney W. Grimes */ 123845cfb1dcSXin LI static void 123973bf18edSWarner Losh del_session(session_t *sp) 12408fae3551SRodney W. Grimes { 12418fae3551SRodney W. Grimes DBT key; 12428fae3551SRodney W. Grimes 12438fae3551SRodney W. Grimes key.data = &sp->se_process; 12448fae3551SRodney W. Grimes key.size = sizeof sp->se_process; 12458fae3551SRodney W. Grimes 12468fae3551SRodney W. Grimes if ((*session_db->del)(session_db, &key, 0)) 12478fae3551SRodney W. Grimes emergency("delete %d: %s", sp->se_process, strerror(errno)); 12488fae3551SRodney W. Grimes } 12498fae3551SRodney W. Grimes 12508fae3551SRodney W. Grimes /* 12518fae3551SRodney W. Grimes * Look up a login session by pid. 12528fae3551SRodney W. Grimes */ 125345cfb1dcSXin LI static session_t * 12548fae3551SRodney W. Grimes find_session(pid_t pid) 12558fae3551SRodney W. Grimes { 12568fae3551SRodney W. Grimes DBT key; 12578fae3551SRodney W. Grimes DBT data; 12588fae3551SRodney W. Grimes session_t *ret; 12598fae3551SRodney W. Grimes 12608fae3551SRodney W. Grimes key.data = &pid; 12618fae3551SRodney W. Grimes key.size = sizeof pid; 12628fae3551SRodney W. Grimes if ((*session_db->get)(session_db, &key, &data, 0) != 0) 12638fae3551SRodney W. Grimes return 0; 12648fae3551SRodney W. Grimes bcopy(data.data, (char *)&ret, sizeof(ret)); 12658fae3551SRodney W. Grimes return ret; 12668fae3551SRodney W. Grimes } 12678fae3551SRodney W. Grimes 12688fae3551SRodney W. Grimes /* 12698fae3551SRodney W. Grimes * Construct an argument vector from a command line. 12708fae3551SRodney W. Grimes */ 127145cfb1dcSXin LI static char ** 127273bf18edSWarner Losh construct_argv(char *command) 12738fae3551SRodney W. Grimes { 12743d438ad6SDavid E. O'Brien int argc = 0; 12753d438ad6SDavid E. O'Brien char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1) 12768fae3551SRodney W. Grimes * sizeof (char *)); 12778fae3551SRodney W. Grimes 12782ef6931aSMarcelo Araujo if ((argv[argc++] = strk(command)) == NULL) { 12796be40c95SRuslan Ermilov free(argv); 12806be40c95SRuslan Ermilov return (NULL); 12816be40c95SRuslan Ermilov } 12828889c700SDavid Nugent while ((argv[argc++] = strk((char *) 0)) != NULL) 12838fae3551SRodney W. Grimes continue; 12848fae3551SRodney W. Grimes return argv; 12858fae3551SRodney W. Grimes } 12868fae3551SRodney W. Grimes 12878fae3551SRodney W. Grimes /* 12888fae3551SRodney W. Grimes * Deallocate a session descriptor. 12898fae3551SRodney W. Grimes */ 129045cfb1dcSXin LI static void 129173bf18edSWarner Losh free_session(session_t *sp) 12928fae3551SRodney W. Grimes { 12938fae3551SRodney W. Grimes free(sp->se_device); 12948fae3551SRodney W. Grimes if (sp->se_getty) { 12958fae3551SRodney W. Grimes free(sp->se_getty); 1296b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 12978fae3551SRodney W. Grimes free(sp->se_getty_argv); 12988fae3551SRodney W. Grimes } 12998fae3551SRodney W. Grimes if (sp->se_window) { 13008fae3551SRodney W. Grimes free(sp->se_window); 1301b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 13028fae3551SRodney W. Grimes free(sp->se_window_argv); 13038fae3551SRodney W. Grimes } 1304b5df27e2SAndrey A. Chernov if (sp->se_type) 1305b5df27e2SAndrey A. Chernov free(sp->se_type); 13068fae3551SRodney W. Grimes free(sp); 13078fae3551SRodney W. Grimes } 13088fae3551SRodney W. Grimes 13098fae3551SRodney W. Grimes /* 13108fae3551SRodney W. Grimes * Allocate a new session descriptor. 1311b0b670eeSAlfred Perlstein * Mark it SE_PRESENT. 13128fae3551SRodney W. Grimes */ 131345cfb1dcSXin LI static session_t * 13140b57dd6bSJilles Tjoelker new_session(session_t *sprev, struct ttyent *typ) 13158fae3551SRodney W. Grimes { 13163d438ad6SDavid E. O'Brien session_t *sp; 13178fae3551SRodney W. Grimes 13188fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 || 13198fae3551SRodney W. Grimes typ->ty_name == 0 || 13208fae3551SRodney W. Grimes typ->ty_getty == 0) 13218fae3551SRodney W. Grimes return 0; 13228fae3551SRodney W. Grimes 13231054bb1eSAndrey A. Chernov sp = (session_t *) calloc(1, sizeof (session_t)); 13248fae3551SRodney W. Grimes 1325b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT; 13268fae3551SRodney W. Grimes 13271cde387cSEdward Tomasz Napierala if ((typ->ty_status & TTY_IFEXISTS) != 0) 13281cde387cSEdward Tomasz Napierala sp->se_flags |= SE_IFEXISTS; 13291cde387cSEdward Tomasz Napierala 13301cde387cSEdward Tomasz Napierala if ((typ->ty_status & TTY_IFCONSOLE) != 0) 13311cde387cSEdward Tomasz Napierala sp->se_flags |= SE_IFCONSOLE; 13321cde387cSEdward Tomasz Napierala 133395595f99SXin LI if (asprintf(&sp->se_device, "%s%s", _PATH_DEV, typ->ty_name) < 0) 133495595f99SXin LI err(1, "asprintf"); 13358fae3551SRodney W. Grimes 13368fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) { 13378fae3551SRodney W. Grimes free_session(sp); 13388fae3551SRodney W. Grimes return (0); 13398fae3551SRodney W. Grimes } 13408fae3551SRodney W. Grimes 13418fae3551SRodney W. Grimes sp->se_next = 0; 134214adaa14SMarcelo Araujo if (sprev == NULL) { 13438fae3551SRodney W. Grimes sessions = sp; 13448fae3551SRodney W. Grimes sp->se_prev = 0; 13458fae3551SRodney W. Grimes } else { 13468fae3551SRodney W. Grimes sprev->se_next = sp; 13478fae3551SRodney W. Grimes sp->se_prev = sprev; 13488fae3551SRodney W. Grimes } 13498fae3551SRodney W. Grimes 13508fae3551SRodney W. Grimes return sp; 13518fae3551SRodney W. Grimes } 13528fae3551SRodney W. Grimes 13538fae3551SRodney W. Grimes /* 13548fae3551SRodney W. Grimes * Calculate getty and if useful window argv vectors. 13558fae3551SRodney W. Grimes */ 135645cfb1dcSXin LI static int 135773bf18edSWarner Losh setupargv(session_t *sp, struct ttyent *typ) 13588fae3551SRodney W. Grimes { 13598fae3551SRodney W. Grimes 13608fae3551SRodney W. Grimes if (sp->se_getty) { 13618fae3551SRodney W. Grimes free(sp->se_getty); 1362b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 13638fae3551SRodney W. Grimes free(sp->se_getty_argv); 13648fae3551SRodney W. Grimes } 136595595f99SXin LI if (asprintf(&sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name) < 0) 136695595f99SXin LI err(1, "asprintf"); 1367b5df27e2SAndrey A. Chernov sp->se_getty_argv_space = strdup(sp->se_getty); 1368b5df27e2SAndrey A. Chernov sp->se_getty_argv = construct_argv(sp->se_getty_argv_space); 136914adaa14SMarcelo Araujo if (sp->se_getty_argv == NULL) { 13708fae3551SRodney W. Grimes warning("can't parse getty for port %s", sp->se_device); 13718fae3551SRodney W. Grimes free(sp->se_getty); 1372b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 1373b5df27e2SAndrey A. Chernov sp->se_getty = sp->se_getty_argv_space = 0; 13748fae3551SRodney W. Grimes return (0); 13758fae3551SRodney W. Grimes } 1376b5df27e2SAndrey A. Chernov if (sp->se_window) { 13778fae3551SRodney W. Grimes free(sp->se_window); 1378b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 1379b5df27e2SAndrey A. Chernov free(sp->se_window_argv); 1380b5df27e2SAndrey A. Chernov } 1381b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0; 1382b5df27e2SAndrey A. Chernov sp->se_window_argv = 0; 1383b5df27e2SAndrey A. Chernov if (typ->ty_window) { 13848fae3551SRodney W. Grimes sp->se_window = strdup(typ->ty_window); 1385b5df27e2SAndrey A. Chernov sp->se_window_argv_space = strdup(sp->se_window); 1386b5df27e2SAndrey A. Chernov sp->se_window_argv = construct_argv(sp->se_window_argv_space); 138714adaa14SMarcelo Araujo if (sp->se_window_argv == NULL) { 13888fae3551SRodney W. Grimes warning("can't parse window for port %s", 13898fae3551SRodney W. Grimes sp->se_device); 1390b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 13918fae3551SRodney W. Grimes free(sp->se_window); 1392b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0; 13938fae3551SRodney W. Grimes return (0); 13948fae3551SRodney W. Grimes } 13958fae3551SRodney W. Grimes } 1396b5df27e2SAndrey A. Chernov if (sp->se_type) 1397b5df27e2SAndrey A. Chernov free(sp->se_type); 1398b5df27e2SAndrey A. Chernov sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0; 13998fae3551SRodney W. Grimes return (1); 14008fae3551SRodney W. Grimes } 14018fae3551SRodney W. Grimes 14028fae3551SRodney W. Grimes /* 14038fae3551SRodney W. Grimes * Walk the list of ttys and create sessions for each active line. 14048fae3551SRodney W. Grimes */ 140545cfb1dcSXin LI static state_func_t 140673bf18edSWarner Losh read_ttys(void) 14078fae3551SRodney W. Grimes { 14083d438ad6SDavid E. O'Brien session_t *sp, *snext; 14093d438ad6SDavid E. O'Brien struct ttyent *typ; 14108fae3551SRodney W. Grimes 14118fae3551SRodney W. Grimes /* 14128fae3551SRodney W. Grimes * Destroy any previous session state. 14138fae3551SRodney W. Grimes * There shouldn't be any, but just in case... 14148fae3551SRodney W. Grimes */ 14158fae3551SRodney W. Grimes for (sp = sessions; sp; sp = snext) { 14168fae3551SRodney W. Grimes snext = sp->se_next; 14178fae3551SRodney W. Grimes free_session(sp); 14188fae3551SRodney W. Grimes } 14198fae3551SRodney W. Grimes sessions = 0; 14208fae3551SRodney W. Grimes if (start_session_db()) 14218fae3551SRodney W. Grimes return (state_func_t) single_user; 14228fae3551SRodney W. Grimes 14238fae3551SRodney W. Grimes /* 14248fae3551SRodney W. Grimes * Allocate a session entry for each active port. 14258fae3551SRodney W. Grimes * Note that sp starts at 0. 14268fae3551SRodney W. Grimes */ 14278889c700SDavid Nugent while ((typ = getttyent()) != NULL) 14280b57dd6bSJilles Tjoelker if ((snext = new_session(sp, typ)) != NULL) 14298fae3551SRodney W. Grimes sp = snext; 14308fae3551SRodney W. Grimes 14318fae3551SRodney W. Grimes endttyent(); 14328fae3551SRodney W. Grimes 14338fae3551SRodney W. Grimes return (state_func_t) multi_user; 14348fae3551SRodney W. Grimes } 14358fae3551SRodney W. Grimes 14368fae3551SRodney W. Grimes /* 14378fae3551SRodney W. Grimes * Start a window system running. 14388fae3551SRodney W. Grimes */ 143945cfb1dcSXin LI static void 144073bf18edSWarner Losh start_window_system(session_t *sp) 14418fae3551SRodney W. Grimes { 14428fae3551SRodney W. Grimes pid_t pid; 14438fae3551SRodney W. Grimes sigset_t mask; 1444b5df27e2SAndrey A. Chernov char term[64], *env[2]; 14455010c3b6SKonstantin Belousov int status; 14468fae3551SRodney W. Grimes 14478fae3551SRodney W. Grimes if ((pid = fork()) == -1) { 14488fae3551SRodney W. Grimes emergency("can't fork for window system on port %s: %m", 14498fae3551SRodney W. Grimes sp->se_device); 14508fae3551SRodney W. Grimes /* hope that getty fails and we can try again */ 14518fae3551SRodney W. Grimes return; 14528fae3551SRodney W. Grimes } 1453091abe40SDavid E. O'Brien if (pid) { 14545010c3b6SKonstantin Belousov waitpid(-1, &status, 0); 14558fae3551SRodney W. Grimes return; 14565010c3b6SKonstantin Belousov } 14575010c3b6SKonstantin Belousov 14585010c3b6SKonstantin Belousov /* reparent window process to the init to not make a zombie on exit */ 14595010c3b6SKonstantin Belousov if ((pid = fork()) == -1) { 14605010c3b6SKonstantin Belousov emergency("can't fork for window system on port %s: %m", 14615010c3b6SKonstantin Belousov sp->se_device); 14625010c3b6SKonstantin Belousov _exit(1); 14635010c3b6SKonstantin Belousov } 14645010c3b6SKonstantin Belousov if (pid) 14655010c3b6SKonstantin Belousov _exit(0); 14668fae3551SRodney W. Grimes 14678fae3551SRodney W. Grimes sigemptyset(&mask); 1468d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL); 14698fae3551SRodney W. Grimes 14708fae3551SRodney W. Grimes if (setsid() < 0) 14718fae3551SRodney W. Grimes emergency("setsid failed (window) %m"); 14728fae3551SRodney W. Grimes 14731ef60eb1SDavid Nugent #ifdef LOGIN_CAP 14741ef60eb1SDavid Nugent setprocresources(RESOURCE_WINDOW); 14751ef60eb1SDavid Nugent #endif 1476b5df27e2SAndrey A. Chernov if (sp->se_type) { 1477b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */ 1478b5df27e2SAndrey A. Chernov strcpy(term, "TERM="); 147995595f99SXin LI strlcat(term, sp->se_type, sizeof(term)); 1480b5df27e2SAndrey A. Chernov env[0] = term; 1481*be03cfddSEdward Tomasz Napierala env[1] = NULL; 1482b5df27e2SAndrey A. Chernov } 1483b5df27e2SAndrey A. Chernov else 1484*be03cfddSEdward Tomasz Napierala env[0] = NULL; 1485b5df27e2SAndrey A. Chernov execve(sp->se_window_argv[0], sp->se_window_argv, env); 14868fae3551SRodney W. Grimes stall("can't exec window system '%s' for port %s: %m", 14878fae3551SRodney W. Grimes sp->se_window_argv[0], sp->se_device); 14888fae3551SRodney W. Grimes _exit(1); 14898fae3551SRodney W. Grimes } 14908fae3551SRodney W. Grimes 14918fae3551SRodney W. Grimes /* 14928fae3551SRodney W. Grimes * Start a login session running. 14938fae3551SRodney W. Grimes */ 149445cfb1dcSXin LI static pid_t 149573bf18edSWarner Losh start_getty(session_t *sp) 14968fae3551SRodney W. Grimes { 14978fae3551SRodney W. Grimes pid_t pid; 14988fae3551SRodney W. Grimes sigset_t mask; 14998fae3551SRodney W. Grimes time_t current_time = time((time_t *) 0); 1500228d7ef2SAndrey A. Chernov int too_quick = 0; 1501b5df27e2SAndrey A. Chernov char term[64], *env[2]; 15028fae3551SRodney W. Grimes 1503bb2e87c4SMike Pritchard if (current_time >= sp->se_started && 1504228d7ef2SAndrey A. Chernov current_time - sp->se_started < GETTY_SPACING) { 1505228d7ef2SAndrey A. Chernov if (++sp->se_nspace > GETTY_NSPACE) { 1506228d7ef2SAndrey A. Chernov sp->se_nspace = 0; 1507228d7ef2SAndrey A. Chernov too_quick = 1; 1508228d7ef2SAndrey A. Chernov } 1509228d7ef2SAndrey A. Chernov } else 1510228d7ef2SAndrey A. Chernov sp->se_nspace = 0; 1511228d7ef2SAndrey A. Chernov 15128fae3551SRodney W. Grimes /* 15138fae3551SRodney W. Grimes * fork(), not vfork() -- we can't afford to block. 15148fae3551SRodney W. Grimes */ 15158fae3551SRodney W. Grimes if ((pid = fork()) == -1) { 15168fae3551SRodney W. Grimes emergency("can't fork for getty on port %s: %m", sp->se_device); 15178fae3551SRodney W. Grimes return -1; 15188fae3551SRodney W. Grimes } 15198fae3551SRodney W. Grimes 15208fae3551SRodney W. Grimes if (pid) 15218fae3551SRodney W. Grimes return pid; 15228fae3551SRodney W. Grimes 1523228d7ef2SAndrey A. Chernov if (too_quick) { 1524b5df27e2SAndrey A. Chernov warning("getty repeating too quickly on port %s, sleeping %d secs", 1525b5df27e2SAndrey A. Chernov sp->se_device, GETTY_SLEEP); 15268fae3551SRodney W. Grimes sleep((unsigned) GETTY_SLEEP); 15278fae3551SRodney W. Grimes } 15288fae3551SRodney W. Grimes 15298fae3551SRodney W. Grimes if (sp->se_window) { 15308fae3551SRodney W. Grimes start_window_system(sp); 15318fae3551SRodney W. Grimes sleep(WINDOW_WAIT); 15328fae3551SRodney W. Grimes } 15338fae3551SRodney W. Grimes 15348fae3551SRodney W. Grimes sigemptyset(&mask); 1535d1b1fe3aSEdward Tomasz Napierala sigprocmask(SIG_SETMASK, &mask, NULL); 15368fae3551SRodney W. Grimes 15371ef60eb1SDavid Nugent #ifdef LOGIN_CAP 15381ef60eb1SDavid Nugent setprocresources(RESOURCE_GETTY); 15391ef60eb1SDavid Nugent #endif 1540b5df27e2SAndrey A. Chernov if (sp->se_type) { 1541b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */ 1542b5df27e2SAndrey A. Chernov strcpy(term, "TERM="); 154395595f99SXin LI strlcat(term, sp->se_type, sizeof(term)); 1544b5df27e2SAndrey A. Chernov env[0] = term; 1545*be03cfddSEdward Tomasz Napierala env[1] = NULL; 1546091abe40SDavid E. O'Brien } else 1547*be03cfddSEdward Tomasz Napierala env[0] = NULL; 1548b5df27e2SAndrey A. Chernov execve(sp->se_getty_argv[0], sp->se_getty_argv, env); 15498fae3551SRodney W. Grimes stall("can't exec getty '%s' for port %s: %m", 15508fae3551SRodney W. Grimes sp->se_getty_argv[0], sp->se_device); 15518fae3551SRodney W. Grimes _exit(1); 15528fae3551SRodney W. Grimes } 15538fae3551SRodney W. Grimes 15548fae3551SRodney W. Grimes /* 15551cde387cSEdward Tomasz Napierala * Return 1 if the session is defined as "onifexists" 15561cde387cSEdward Tomasz Napierala * or "onifconsole" and the device node does not exist. 15571cde387cSEdward Tomasz Napierala */ 15581cde387cSEdward Tomasz Napierala static int 15591cde387cSEdward Tomasz Napierala session_has_no_tty(session_t *sp) 15601cde387cSEdward Tomasz Napierala { 15611cde387cSEdward Tomasz Napierala int fd; 15621cde387cSEdward Tomasz Napierala 15631cde387cSEdward Tomasz Napierala if ((sp->se_flags & SE_IFEXISTS) == 0 && 15641cde387cSEdward Tomasz Napierala (sp->se_flags & SE_IFCONSOLE) == 0) 15651cde387cSEdward Tomasz Napierala return (0); 15661cde387cSEdward Tomasz Napierala 15671cde387cSEdward Tomasz Napierala fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0); 15681cde387cSEdward Tomasz Napierala if (fd < 0) { 15691cde387cSEdward Tomasz Napierala if (errno == ENOENT) 15701cde387cSEdward Tomasz Napierala return (1); 15711cde387cSEdward Tomasz Napierala return (0); 15721cde387cSEdward Tomasz Napierala } 15731cde387cSEdward Tomasz Napierala 15741cde387cSEdward Tomasz Napierala close(fd); 15751cde387cSEdward Tomasz Napierala return (0); 15761cde387cSEdward Tomasz Napierala } 15771cde387cSEdward Tomasz Napierala 15781cde387cSEdward Tomasz Napierala /* 15798fae3551SRodney W. Grimes * Collect exit status for a child. 15808fae3551SRodney W. Grimes * If an exiting login, start a new login running. 15818fae3551SRodney W. Grimes */ 158245cfb1dcSXin LI static void 15838fae3551SRodney W. Grimes collect_child(pid_t pid) 15848fae3551SRodney W. Grimes { 15853d438ad6SDavid E. O'Brien session_t *sp, *sprev, *snext; 15868fae3551SRodney W. Grimes 15878fae3551SRodney W. Grimes if (! sessions) 15888fae3551SRodney W. Grimes return; 15898fae3551SRodney W. Grimes 15908fae3551SRodney W. Grimes if (! (sp = find_session(pid))) 15918fae3551SRodney W. Grimes return; 15928fae3551SRodney W. Grimes 15938fae3551SRodney W. Grimes del_session(sp); 15948fae3551SRodney W. Grimes sp->se_process = 0; 15958fae3551SRodney W. Grimes 15961cde387cSEdward Tomasz Napierala if (sp->se_flags & SE_SHUTDOWN || 15971cde387cSEdward Tomasz Napierala session_has_no_tty(sp)) { 15988889c700SDavid Nugent if ((sprev = sp->se_prev) != NULL) 15998fae3551SRodney W. Grimes sprev->se_next = sp->se_next; 16008fae3551SRodney W. Grimes else 16018fae3551SRodney W. Grimes sessions = sp->se_next; 16028889c700SDavid Nugent if ((snext = sp->se_next) != NULL) 16038fae3551SRodney W. Grimes snext->se_prev = sp->se_prev; 16048fae3551SRodney W. Grimes free_session(sp); 16058fae3551SRodney W. Grimes return; 16068fae3551SRodney W. Grimes } 16078fae3551SRodney W. Grimes 16088fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) { 16098fae3551SRodney W. Grimes /* serious trouble */ 16108fae3551SRodney W. Grimes requested_transition = clean_ttys; 16118fae3551SRodney W. Grimes return; 16128fae3551SRodney W. Grimes } 16138fae3551SRodney W. Grimes 16148fae3551SRodney W. Grimes sp->se_process = pid; 16158fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0); 16168fae3551SRodney W. Grimes add_session(sp); 16178fae3551SRodney W. Grimes } 16188fae3551SRodney W. Grimes 16198fae3551SRodney W. Grimes /* 16208fae3551SRodney W. Grimes * Catch a signal and request a state transition. 16218fae3551SRodney W. Grimes */ 162245cfb1dcSXin LI static void 162373bf18edSWarner Losh transition_handler(int sig) 16248fae3551SRodney W. Grimes { 16258fae3551SRodney W. Grimes 16268fae3551SRodney W. Grimes switch (sig) { 16278fae3551SRodney W. Grimes case SIGHUP: 1628acf0ab06SJilles Tjoelker if (current_state == read_ttys || current_state == multi_user || 1629acf0ab06SJilles Tjoelker current_state == clean_ttys || current_state == catatonia) 16308fae3551SRodney W. Grimes requested_transition = clean_ttys; 16318fae3551SRodney W. Grimes break; 163235c1d16eSWarner Losh case SIGWINCH: 1633a0a549c7SRuslan Ermilov case SIGUSR2: 163435c1d16eSWarner Losh howto = sig == SIGUSR2 ? RB_POWEROFF : RB_POWERCYCLE; 1635a0a549c7SRuslan Ermilov case SIGUSR1: 1636a0a549c7SRuslan Ermilov howto |= RB_HALT; 1637e460cfd3SNate Williams case SIGINT: 1638db8ad19dSJordan K. Hubbard Reboot = TRUE; 16398fae3551SRodney W. Grimes case SIGTERM: 1640acf0ab06SJilles Tjoelker if (current_state == read_ttys || current_state == multi_user || 1641acf0ab06SJilles Tjoelker current_state == clean_ttys || current_state == catatonia) 16428fae3551SRodney W. Grimes requested_transition = death; 1643acf0ab06SJilles Tjoelker else 1644acf0ab06SJilles Tjoelker requested_transition = death_single; 16458fae3551SRodney W. Grimes break; 16468fae3551SRodney W. Grimes case SIGTSTP: 1647acf0ab06SJilles Tjoelker if (current_state == runcom || current_state == read_ttys || 1648acf0ab06SJilles Tjoelker current_state == clean_ttys || 1649acf0ab06SJilles Tjoelker current_state == multi_user || current_state == catatonia) 16508fae3551SRodney W. Grimes requested_transition = catatonia; 16518fae3551SRodney W. Grimes break; 16523f5ac575SEdward Tomasz Napierala case SIGEMT: 16533f5ac575SEdward Tomasz Napierala requested_transition = reroot; 16543f5ac575SEdward Tomasz Napierala break; 16558fae3551SRodney W. Grimes default: 16568fae3551SRodney W. Grimes requested_transition = 0; 16578fae3551SRodney W. Grimes break; 16588fae3551SRodney W. Grimes } 16598fae3551SRodney W. Grimes } 16608fae3551SRodney W. Grimes 16618fae3551SRodney W. Grimes /* 16628fae3551SRodney W. Grimes * Take the system multiuser. 16638fae3551SRodney W. Grimes */ 166445cfb1dcSXin LI static state_func_t 166573bf18edSWarner Losh multi_user(void) 16668fae3551SRodney W. Grimes { 16678fae3551SRodney W. Grimes pid_t pid; 16683d438ad6SDavid E. O'Brien session_t *sp; 16698fae3551SRodney W. Grimes 16708fae3551SRodney W. Grimes requested_transition = 0; 16718fae3551SRodney W. Grimes 16728fae3551SRodney W. Grimes /* 16738fae3551SRodney W. Grimes * If the administrator has not set the security level to -1 16748fae3551SRodney W. Grimes * to indicate that the kernel should not run multiuser in secure 16758fae3551SRodney W. Grimes * mode, and the run script has not set a higher level of security 16768fae3551SRodney W. Grimes * than level 1, then put the kernel into secure mode. 16778fae3551SRodney W. Grimes */ 16788fae3551SRodney W. Grimes if (getsecuritylevel() == 0) 16798fae3551SRodney W. Grimes setsecuritylevel(1); 16808fae3551SRodney W. Grimes 16818fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next) { 16828fae3551SRodney W. Grimes if (sp->se_process) 16838fae3551SRodney W. Grimes continue; 16841cde387cSEdward Tomasz Napierala if (session_has_no_tty(sp)) 16851cde387cSEdward Tomasz Napierala continue; 16868fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) { 16878fae3551SRodney W. Grimes /* serious trouble */ 16888fae3551SRodney W. Grimes requested_transition = clean_ttys; 16898fae3551SRodney W. Grimes break; 16908fae3551SRodney W. Grimes } 16918fae3551SRodney W. Grimes sp->se_process = pid; 16928fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0); 16938fae3551SRodney W. Grimes add_session(sp); 16948fae3551SRodney W. Grimes } 16958fae3551SRodney W. Grimes 16968fae3551SRodney W. Grimes while (!requested_transition) 16978fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *) 0, 0)) != -1) 16988fae3551SRodney W. Grimes collect_child(pid); 16998fae3551SRodney W. Grimes 17008fae3551SRodney W. Grimes return (state_func_t) requested_transition; 17018fae3551SRodney W. Grimes } 17028fae3551SRodney W. Grimes 17038fae3551SRodney W. Grimes /* 1704b0b670eeSAlfred Perlstein * This is an (n*2)+(n^2) algorithm. We hope it isn't run often... 17058fae3551SRodney W. Grimes */ 170645cfb1dcSXin LI static state_func_t 170773bf18edSWarner Losh clean_ttys(void) 17088fae3551SRodney W. Grimes { 17093d438ad6SDavid E. O'Brien session_t *sp, *sprev; 17103d438ad6SDavid E. O'Brien struct ttyent *typ; 17113d438ad6SDavid E. O'Brien int devlen; 1712b5df27e2SAndrey A. Chernov char *old_getty, *old_window, *old_type; 17138fae3551SRodney W. Grimes 1714b0b670eeSAlfred Perlstein /* 1715b0b670eeSAlfred Perlstein * mark all sessions for death, (!SE_PRESENT) 1716b0b670eeSAlfred Perlstein * as we find or create new ones they'll be marked as keepers, 1717b0b670eeSAlfred Perlstein * we'll later nuke all the ones not found in /etc/ttys 1718b0b670eeSAlfred Perlstein */ 1719b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next) 1720b0b670eeSAlfred Perlstein sp->se_flags &= ~SE_PRESENT; 1721b0b670eeSAlfred Perlstein 17228fae3551SRodney W. Grimes devlen = sizeof(_PATH_DEV) - 1; 17238889c700SDavid Nugent while ((typ = getttyent()) != NULL) { 17248fae3551SRodney W. Grimes for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next) 17258fae3551SRodney W. Grimes if (strcmp(typ->ty_name, sp->se_device + devlen) == 0) 17268fae3551SRodney W. Grimes break; 17278fae3551SRodney W. Grimes 17288fae3551SRodney W. Grimes if (sp) { 1729b0b670eeSAlfred Perlstein /* we want this one to live */ 1730b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT; 17318fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 || 17328fae3551SRodney W. Grimes typ->ty_getty == 0) { 17338fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 17348fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP); 17358fae3551SRodney W. Grimes continue; 17368fae3551SRodney W. Grimes } 17378fae3551SRodney W. Grimes sp->se_flags &= ~SE_SHUTDOWN; 1738b5df27e2SAndrey A. Chernov old_getty = sp->se_getty ? strdup(sp->se_getty) : 0; 1739b5df27e2SAndrey A. Chernov old_window = sp->se_window ? strdup(sp->se_window) : 0; 1740b5df27e2SAndrey A. Chernov old_type = sp->se_type ? strdup(sp->se_type) : 0; 17418fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) { 17428fae3551SRodney W. Grimes warning("can't parse getty for port %s", 17438fae3551SRodney W. Grimes sp->se_device); 17448fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 17458fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP); 17468fae3551SRodney W. Grimes } 1747b5df27e2SAndrey A. Chernov else if ( !old_getty 17488889c700SDavid Nugent || (!old_type && sp->se_type) 17498889c700SDavid Nugent || (old_type && !sp->se_type) 17508889c700SDavid Nugent || (!old_window && sp->se_window) 17518889c700SDavid Nugent || (old_window && !sp->se_window) 17528889c700SDavid Nugent || (strcmp(old_getty, sp->se_getty) != 0) 17538889c700SDavid Nugent || (old_window && strcmp(old_window, sp->se_window) != 0) 17548889c700SDavid Nugent || (old_type && strcmp(old_type, sp->se_type) != 0) 1755b5df27e2SAndrey A. Chernov ) { 1756b5df27e2SAndrey A. Chernov /* Don't set SE_SHUTDOWN here */ 1757b5df27e2SAndrey A. Chernov sp->se_nspace = 0; 1758b5df27e2SAndrey A. Chernov sp->se_started = 0; 1759b5df27e2SAndrey A. Chernov kill(sp->se_process, SIGHUP); 1760b5df27e2SAndrey A. Chernov } 1761b5df27e2SAndrey A. Chernov if (old_getty) 1762b5df27e2SAndrey A. Chernov free(old_getty); 17632d887af5SMike Heffner if (old_window) 1764b5df27e2SAndrey A. Chernov free(old_window); 1765b5df27e2SAndrey A. Chernov if (old_type) 1766b5df27e2SAndrey A. Chernov free(old_type); 17678fae3551SRodney W. Grimes continue; 17688fae3551SRodney W. Grimes } 17698fae3551SRodney W. Grimes 17700b57dd6bSJilles Tjoelker new_session(sprev, typ); 17718fae3551SRodney W. Grimes } 17728fae3551SRodney W. Grimes 17738fae3551SRodney W. Grimes endttyent(); 17748fae3551SRodney W. Grimes 1775b0b670eeSAlfred Perlstein /* 1776b0b670eeSAlfred Perlstein * sweep through and kill all deleted sessions 1777b0b670eeSAlfred Perlstein * ones who's /etc/ttys line was deleted (SE_PRESENT unset) 1778b0b670eeSAlfred Perlstein */ 1779b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next) { 1780b0b670eeSAlfred Perlstein if ((sp->se_flags & SE_PRESENT) == 0) { 1781b0b670eeSAlfred Perlstein sp->se_flags |= SE_SHUTDOWN; 1782b0b670eeSAlfred Perlstein kill(sp->se_process, SIGHUP); 1783b0b670eeSAlfred Perlstein } 1784b0b670eeSAlfred Perlstein } 1785b0b670eeSAlfred Perlstein 17868fae3551SRodney W. Grimes return (state_func_t) multi_user; 17878fae3551SRodney W. Grimes } 17888fae3551SRodney W. Grimes 17898fae3551SRodney W. Grimes /* 17908fae3551SRodney W. Grimes * Block further logins. 17918fae3551SRodney W. Grimes */ 179245cfb1dcSXin LI static state_func_t 179373bf18edSWarner Losh catatonia(void) 17948fae3551SRodney W. Grimes { 17953d438ad6SDavid E. O'Brien session_t *sp; 17968fae3551SRodney W. Grimes 17978fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next) 17988fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 17998fae3551SRodney W. Grimes 18008fae3551SRodney W. Grimes return (state_func_t) multi_user; 18018fae3551SRodney W. Grimes } 18028fae3551SRodney W. Grimes 18038fae3551SRodney W. Grimes /* 18048fae3551SRodney W. Grimes * Note SIGALRM. 18058fae3551SRodney W. Grimes */ 180645cfb1dcSXin LI static void 180773bf18edSWarner Losh alrm_handler(int sig) 18088fae3551SRodney W. Grimes { 1809091abe40SDavid E. O'Brien 18108889c700SDavid Nugent (void)sig; 18118fae3551SRodney W. Grimes clang = 1; 18128fae3551SRodney W. Grimes } 18138fae3551SRodney W. Grimes 18148fae3551SRodney W. Grimes /* 18158fae3551SRodney W. Grimes * Bring the system down to single user. 18168fae3551SRodney W. Grimes */ 181745cfb1dcSXin LI static state_func_t 181873bf18edSWarner Losh death(void) 18198fae3551SRodney W. Grimes { 18202eb0015aSColin Percival int block, blocked; 18212eb0015aSColin Percival size_t len; 18222eb0015aSColin Percival 18232eb0015aSColin Percival /* Temporarily block suspend. */ 18242eb0015aSColin Percival len = sizeof(blocked); 18252eb0015aSColin Percival block = 1; 18262eb0015aSColin Percival if (sysctlbyname("kern.suspend_blocked", &blocked, &len, 18272eb0015aSColin Percival &block, sizeof(block)) == -1) 18282eb0015aSColin Percival blocked = 0; 18298fae3551SRodney W. Grimes 18304ae35b5dSEd Schouten /* 18314ae35b5dSEd Schouten * Also revoke the TTY here. Because runshutdown() may reopen 18324ae35b5dSEd Schouten * the TTY whose getty we're killing here, there is no guarantee 18334ae35b5dSEd Schouten * runshutdown() will perform the initial open() call, causing 18344ae35b5dSEd Schouten * the terminal attributes to be misconfigured. 18354ae35b5dSEd Schouten */ 18363f5ac575SEdward Tomasz Napierala revoke_ttys(); 18378fae3551SRodney W. Grimes 18388889c700SDavid Nugent /* Try to run the rc.shutdown script within a period of time */ 1839091abe40SDavid E. O'Brien runshutdown(); 18408889c700SDavid Nugent 18412eb0015aSColin Percival /* Unblock suspend if we blocked it. */ 18422eb0015aSColin Percival if (!blocked) 18432eb0015aSColin Percival sysctlbyname("kern.suspend_blocked", NULL, NULL, 18442eb0015aSColin Percival &blocked, sizeof(blocked)); 18452eb0015aSColin Percival 1846acf0ab06SJilles Tjoelker return (state_func_t) death_single; 1847acf0ab06SJilles Tjoelker } 1848acf0ab06SJilles Tjoelker 1849acf0ab06SJilles Tjoelker /* 1850acf0ab06SJilles Tjoelker * Do what is necessary to reinitialize single user mode or reboot 1851acf0ab06SJilles Tjoelker * from an incomplete state. 1852acf0ab06SJilles Tjoelker */ 1853acf0ab06SJilles Tjoelker static state_func_t 1854acf0ab06SJilles Tjoelker death_single(void) 1855acf0ab06SJilles Tjoelker { 1856acf0ab06SJilles Tjoelker int i; 1857acf0ab06SJilles Tjoelker pid_t pid; 1858acf0ab06SJilles Tjoelker static const int death_sigs[2] = { SIGTERM, SIGKILL }; 1859acf0ab06SJilles Tjoelker 1860acf0ab06SJilles Tjoelker revoke(_PATH_CONSOLE); 1861acf0ab06SJilles Tjoelker 1862c3d7c52eSAndrey A. Chernov for (i = 0; i < 2; ++i) { 18638fae3551SRodney W. Grimes if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH) 18648fae3551SRodney W. Grimes return (state_func_t) single_user; 18658fae3551SRodney W. Grimes 18668fae3551SRodney W. Grimes clang = 0; 18678fae3551SRodney W. Grimes alarm(DEATH_WATCH); 18688fae3551SRodney W. Grimes do 18698fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *)0, 0)) != -1) 18708fae3551SRodney W. Grimes collect_child(pid); 18718fae3551SRodney W. Grimes while (clang == 0 && errno != ECHILD); 18728fae3551SRodney W. Grimes 18738fae3551SRodney W. Grimes if (errno == ECHILD) 18748fae3551SRodney W. Grimes return (state_func_t) single_user; 18758fae3551SRodney W. Grimes } 18768fae3551SRodney W. Grimes 18778fae3551SRodney W. Grimes warning("some processes would not die; ps axl advised"); 18788fae3551SRodney W. Grimes 18798fae3551SRodney W. Grimes return (state_func_t) single_user; 18808fae3551SRodney W. Grimes } 18818889c700SDavid Nugent 18823f5ac575SEdward Tomasz Napierala static void 18833f5ac575SEdward Tomasz Napierala revoke_ttys(void) 18843f5ac575SEdward Tomasz Napierala { 18853f5ac575SEdward Tomasz Napierala session_t *sp; 18863f5ac575SEdward Tomasz Napierala 18873f5ac575SEdward Tomasz Napierala for (sp = sessions; sp; sp = sp->se_next) { 18883f5ac575SEdward Tomasz Napierala sp->se_flags |= SE_SHUTDOWN; 18893f5ac575SEdward Tomasz Napierala kill(sp->se_process, SIGHUP); 18903f5ac575SEdward Tomasz Napierala revoke(sp->se_device); 18913f5ac575SEdward Tomasz Napierala } 18923f5ac575SEdward Tomasz Napierala } 18933f5ac575SEdward Tomasz Napierala 18948889c700SDavid Nugent /* 18958889c700SDavid Nugent * Run the system shutdown script. 18968889c700SDavid Nugent * 18978889c700SDavid Nugent * Exit codes: XXX I should document more 18988889c700SDavid Nugent * -2 shutdown script terminated abnormally 18998889c700SDavid Nugent * -1 fatal error - can't run script 19008889c700SDavid Nugent * 0 good. 19018889c700SDavid Nugent * >0 some error (exit code) 19028889c700SDavid Nugent */ 190345cfb1dcSXin LI static int 190473bf18edSWarner Losh runshutdown(void) 19058889c700SDavid Nugent { 19068889c700SDavid Nugent pid_t pid, wpid; 1907f3c4a698SEdward Tomasz Napierala int status; 19088889c700SDavid Nugent int shutdowntimeout; 19098889c700SDavid Nugent size_t len; 1910a69497d7SMatthew Dillon char *argv[4]; 191186bf62dcSDavid Nugent struct stat sb; 191286bf62dcSDavid Nugent 191386bf62dcSDavid Nugent /* 191486bf62dcSDavid Nugent * rc.shutdown is optional, so to prevent any unnecessary 191586bf62dcSDavid Nugent * complaints from the shell we simply don't run it if the 191686bf62dcSDavid Nugent * file does not exist. If the stat() here fails for other 191786bf62dcSDavid Nugent * reasons, we'll let the shell complain. 191886bf62dcSDavid Nugent */ 191986bf62dcSDavid Nugent if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT) 192086bf62dcSDavid Nugent return 0; 19218889c700SDavid Nugent 19228889c700SDavid Nugent if ((pid = fork()) == 0) { 1923ab03e6d5SXin LI char _sh[] = "sh"; 1924ab03e6d5SXin LI char _reboot[] = "reboot"; 1925ab03e6d5SXin LI char _single[] = "single"; 1926ab03e6d5SXin LI char _path_rundown[] = _PATH_RUNDOWN; 1927ab03e6d5SXin LI 1928ab03e6d5SXin LI argv[0] = _sh; 1929ab03e6d5SXin LI argv[1] = _path_rundown; 1930ab03e6d5SXin LI argv[2] = Reboot ? _reboot : _single; 1931*be03cfddSEdward Tomasz Napierala argv[3] = NULL; 19328889c700SDavid Nugent 1933f3c4a698SEdward Tomasz Napierala execute_script(argv); 19348889c700SDavid Nugent _exit(1); /* force single user mode */ 19358889c700SDavid Nugent } 19368889c700SDavid Nugent 19378889c700SDavid Nugent if (pid == -1) { 1938f3c4a698SEdward Tomasz Napierala emergency("can't fork for %s: %m", _PATH_RUNDOWN); 19398889c700SDavid Nugent while (waitpid(-1, (int *) 0, WNOHANG) > 0) 19408889c700SDavid Nugent continue; 19418889c700SDavid Nugent sleep(STALL_TIMEOUT); 19428889c700SDavid Nugent return -1; 19438889c700SDavid Nugent } 19448889c700SDavid Nugent 19458889c700SDavid Nugent len = sizeof(shutdowntimeout); 1946091abe40SDavid E. O'Brien if (sysctlbyname("kern.init_shutdown_timeout", &shutdowntimeout, &len, 1947091abe40SDavid E. O'Brien NULL, 0) == -1 || shutdowntimeout < 2) 19488889c700SDavid Nugent shutdowntimeout = DEATH_SCRIPT; 19498889c700SDavid Nugent alarm(shutdowntimeout); 19508889c700SDavid Nugent clang = 0; 19518889c700SDavid Nugent /* 19528889c700SDavid Nugent * Copied from single_user(). This is a bit paranoid. 19538889c700SDavid Nugent * Use the same ALRM handler. 19548889c700SDavid Nugent */ 19558889c700SDavid Nugent do { 19568889c700SDavid Nugent if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 19578889c700SDavid Nugent collect_child(wpid); 19588889c700SDavid Nugent if (clang == 1) { 19598889c700SDavid Nugent /* we were waiting for the sub-shell */ 19608889c700SDavid Nugent kill(wpid, SIGTERM); 1961f3c4a698SEdward Tomasz Napierala warning("timeout expired for %s: %m; going to " 1962f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN); 19638889c700SDavid Nugent return -1; 19648889c700SDavid Nugent } 19658889c700SDavid Nugent if (wpid == -1) { 19668889c700SDavid Nugent if (errno == EINTR) 19678889c700SDavid Nugent continue; 1968f3c4a698SEdward Tomasz Napierala warning("wait for %s failed: %m; going to " 1969f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN); 19708889c700SDavid Nugent return -1; 19718889c700SDavid Nugent } 19728889c700SDavid Nugent if (wpid == pid && WIFSTOPPED(status)) { 1973f3c4a698SEdward Tomasz Napierala warning("init: %s stopped, restarting\n", 1974f3c4a698SEdward Tomasz Napierala _PATH_RUNDOWN); 19758889c700SDavid Nugent kill(pid, SIGCONT); 19768889c700SDavid Nugent wpid = -1; 19778889c700SDavid Nugent } 19788889c700SDavid Nugent } while (wpid != pid && !clang); 19798889c700SDavid Nugent 19808889c700SDavid Nugent /* Turn off the alarm */ 19818889c700SDavid Nugent alarm(0); 19828889c700SDavid Nugent 19838889c700SDavid Nugent if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 19848889c700SDavid Nugent requested_transition == catatonia) { 19858889c700SDavid Nugent /* 19868889c700SDavid Nugent * /etc/rc.shutdown executed /sbin/reboot; 19878889c700SDavid Nugent * wait for the end quietly 19888889c700SDavid Nugent */ 19898889c700SDavid Nugent sigset_t s; 19908889c700SDavid Nugent 19918889c700SDavid Nugent sigfillset(&s); 19928889c700SDavid Nugent for (;;) 19938889c700SDavid Nugent sigsuspend(&s); 19948889c700SDavid Nugent } 19958889c700SDavid Nugent 19968889c700SDavid Nugent if (!WIFEXITED(status)) { 1997f3c4a698SEdward Tomasz Napierala warning("%s terminated abnormally, going to " 1998f3c4a698SEdward Tomasz Napierala "single user mode", _PATH_RUNDOWN); 19998889c700SDavid Nugent return -2; 20008889c700SDavid Nugent } 20018889c700SDavid Nugent 20028889c700SDavid Nugent if ((status = WEXITSTATUS(status)) != 0) 20038889c700SDavid Nugent warning("%s returned status %d", _PATH_RUNDOWN, status); 20048889c700SDavid Nugent 20058889c700SDavid Nugent return status; 20068889c700SDavid Nugent } 20078889c700SDavid Nugent 2008ab03e6d5SXin LI static char * 200981ab7fb2SAndrey A. Chernov strk(char *p) 201081ab7fb2SAndrey A. Chernov { 201181ab7fb2SAndrey A. Chernov static char *t; 201281ab7fb2SAndrey A. Chernov char *q; 201381ab7fb2SAndrey A. Chernov int c; 201481ab7fb2SAndrey A. Chernov 201581ab7fb2SAndrey A. Chernov if (p) 201681ab7fb2SAndrey A. Chernov t = p; 201781ab7fb2SAndrey A. Chernov if (!t) 201881ab7fb2SAndrey A. Chernov return 0; 201981ab7fb2SAndrey A. Chernov 202081ab7fb2SAndrey A. Chernov c = *t; 202181ab7fb2SAndrey A. Chernov while (c == ' ' || c == '\t' ) 202281ab7fb2SAndrey A. Chernov c = *++t; 202381ab7fb2SAndrey A. Chernov if (!c) { 202481ab7fb2SAndrey A. Chernov t = 0; 202581ab7fb2SAndrey A. Chernov return 0; 202681ab7fb2SAndrey A. Chernov } 202781ab7fb2SAndrey A. Chernov q = t; 202881ab7fb2SAndrey A. Chernov if (c == '\'') { 202981ab7fb2SAndrey A. Chernov c = *++t; 203081ab7fb2SAndrey A. Chernov q = t; 203181ab7fb2SAndrey A. Chernov while (c && c != '\'') 203281ab7fb2SAndrey A. Chernov c = *++t; 203381ab7fb2SAndrey A. Chernov if (!c) /* unterminated string */ 203481ab7fb2SAndrey A. Chernov q = t = 0; 203581ab7fb2SAndrey A. Chernov else 203681ab7fb2SAndrey A. Chernov *t++ = 0; 203781ab7fb2SAndrey A. Chernov } else { 203881ab7fb2SAndrey A. Chernov while (c && c != ' ' && c != '\t' ) 203981ab7fb2SAndrey A. Chernov c = *++t; 204081ab7fb2SAndrey A. Chernov *t++ = 0; 204181ab7fb2SAndrey A. Chernov if (!c) 204281ab7fb2SAndrey A. Chernov t = 0; 204381ab7fb2SAndrey A. Chernov } 204481ab7fb2SAndrey A. Chernov return q; 204581ab7fb2SAndrey A. Chernov } 20461ef60eb1SDavid Nugent 20471ef60eb1SDavid Nugent #ifdef LOGIN_CAP 204845cfb1dcSXin LI static void 204973bf18edSWarner Losh setprocresources(const char *cname) 20501ef60eb1SDavid Nugent { 2051e82d5545SDavid Nugent login_cap_t *lc; 2052a2ee73bcSAndrey A. Chernov if ((lc = login_getclassbyname(cname, NULL)) != NULL) { 2053091abe40SDavid E. O'Brien setusercontext(lc, (struct passwd*)NULL, 0, 2054595ab563SJilles Tjoelker LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | 2055595ab563SJilles Tjoelker LOGIN_SETLOGINCLASS | LOGIN_SETCPUMASK); 20561ef60eb1SDavid Nugent login_close(lc); 20571ef60eb1SDavid Nugent } 20581ef60eb1SDavid Nugent } 20591ef60eb1SDavid Nugent #endif 2060