18fae3551SRodney W. Grimes /*- 28fae3551SRodney W. Grimes * Copyright (c) 1991, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Donn Seeley at Berkeley Software Design, Inc. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 168fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 178fae3551SRodney W. Grimes * must display the following acknowledgement: 188fae3551SRodney W. Grimes * This product includes software developed by the University of 198fae3551SRodney W. Grimes * California, Berkeley and its contributors. 208fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 218fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 228fae3551SRodney W. Grimes * without specific prior written permission. 238fae3551SRodney W. Grimes * 248fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fae3551SRodney W. Grimes * SUCH DAMAGE. 358fae3551SRodney W. Grimes */ 368fae3551SRodney W. Grimes 378fae3551SRodney W. Grimes #ifndef lint 385df42cf4SPhilippe Charnier static const char copyright[] = 398fae3551SRodney W. Grimes "@(#) Copyright (c) 1991, 1993\n\ 408fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 418fae3551SRodney W. Grimes #endif /* not lint */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 445df42cf4SPhilippe Charnier #if 0 458fae3551SRodney W. Grimes static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 7/15/93"; 465df42cf4SPhilippe Charnier #endif 475df42cf4SPhilippe Charnier static const char rcsid[] = 487f3dea24SPeter Wemm "$FreeBSD$"; 498fae3551SRodney W. Grimes #endif /* not lint */ 508fae3551SRodney W. Grimes 518fae3551SRodney W. Grimes #include <sys/param.h> 528889c700SDavid Nugent #include <sys/ioctl.h> 5357622f22SPoul-Henning Kamp #include <sys/mount.h> 548fae3551SRodney W. Grimes #include <sys/sysctl.h> 558fae3551SRodney W. Grimes #include <sys/wait.h> 5686bf62dcSDavid Nugent #include <sys/stat.h> 578fae3551SRodney W. Grimes 588fae3551SRodney W. Grimes #include <db.h> 598fae3551SRodney W. Grimes #include <errno.h> 608fae3551SRodney W. Grimes #include <fcntl.h> 61423b6a39SAndrey A. Chernov #include <libutil.h> 621a37aa56SDavid E. O'Brien #include <paths.h> 638fae3551SRodney W. Grimes #include <signal.h> 648fae3551SRodney W. Grimes #include <stdio.h> 658fae3551SRodney W. Grimes #include <stdlib.h> 668fae3551SRodney W. Grimes #include <string.h> 678fae3551SRodney W. Grimes #include <syslog.h> 688fae3551SRodney W. Grimes #include <time.h> 698fae3551SRodney W. Grimes #include <ttyent.h> 708fae3551SRodney W. Grimes #include <unistd.h> 71e460cfd3SNate Williams #include <sys/reboot.h> 72c5842835SPhilippe Charnier #include <err.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #ifdef __STDC__ 758fae3551SRodney W. Grimes #include <stdarg.h> 768fae3551SRodney W. Grimes #else 778fae3551SRodney W. Grimes #include <varargs.h> 788fae3551SRodney W. Grimes #endif 798fae3551SRodney W. Grimes 808fae3551SRodney W. Grimes #ifdef SECURE 818fae3551SRodney W. Grimes #include <pwd.h> 828fae3551SRodney W. Grimes #endif 838fae3551SRodney W. Grimes 841ef60eb1SDavid Nugent #ifdef LOGIN_CAP 851ef60eb1SDavid Nugent #include <login_cap.h> 861ef60eb1SDavid Nugent #endif 871ef60eb1SDavid Nugent 888fae3551SRodney W. Grimes #include "pathnames.h" 898fae3551SRodney W. Grimes 908fae3551SRodney W. Grimes /* 918fae3551SRodney W. Grimes * Sleep times; used to prevent thrashing. 928fae3551SRodney W. Grimes */ 938fae3551SRodney W. Grimes #define GETTY_SPACING 5 /* N secs minimum getty spacing */ 948fae3551SRodney W. Grimes #define GETTY_SLEEP 30 /* sleep N secs after spacing problem */ 95b5df27e2SAndrey A. Chernov #define GETTY_NSPACE 3 /* max. spacing count to bring reaction */ 968fae3551SRodney W. Grimes #define WINDOW_WAIT 3 /* wait N secs after starting window */ 978fae3551SRodney W. Grimes #define STALL_TIMEOUT 30 /* wait N secs after warning */ 988fae3551SRodney W. Grimes #define DEATH_WATCH 10 /* wait N secs for procs to die */ 995df42cf4SPhilippe Charnier #define DEATH_SCRIPT 120 /* wait for 2min for /etc/rc.shutdown */ 100e82d5545SDavid Nugent #define RESOURCE_RC "daemon" 101e82d5545SDavid Nugent #define RESOURCE_WINDOW "default" 102e82d5545SDavid Nugent #define RESOURCE_GETTY "default" 1038fae3551SRodney W. Grimes 1048fae3551SRodney W. Grimes void handle __P((sig_t, ...)); 1058fae3551SRodney W. Grimes void delset __P((sigset_t *, ...)); 1068fae3551SRodney W. Grimes 1075979df34SKris Kennaway void stall __P((const char *, ...)) __printflike(1, 2); 1085979df34SKris Kennaway void warning __P((const char *, ...)) __printflike(1, 2); 1095979df34SKris Kennaway void emergency __P((const char *, ...)) __printflike(1, 2); 1108fae3551SRodney W. Grimes void disaster __P((int)); 1118fae3551SRodney W. Grimes void badsys __P((int)); 1128889c700SDavid Nugent int runshutdown __P((void)); 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 */ 1198fae3551SRodney W. Grimes typedef long (*state_func_t) __P((void)); 1208fae3551SRodney W. Grimes typedef state_func_t (*state_t) __P((void)); 1218fae3551SRodney W. Grimes 1228fae3551SRodney W. Grimes state_func_t single_user __P((void)); 1238fae3551SRodney W. Grimes state_func_t runcom __P((void)); 1248fae3551SRodney W. Grimes state_func_t read_ttys __P((void)); 1258fae3551SRodney W. Grimes state_func_t multi_user __P((void)); 1268fae3551SRodney W. Grimes state_func_t clean_ttys __P((void)); 1278fae3551SRodney W. Grimes state_func_t catatonia __P((void)); 1288fae3551SRodney W. Grimes state_func_t death __P((void)); 1298fae3551SRodney W. Grimes 1308fae3551SRodney W. Grimes enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT; 13177103ea3SPoul-Henning Kamp #define FALSE 0 13277103ea3SPoul-Henning Kamp #define TRUE 1 13377103ea3SPoul-Henning Kamp 134db8ad19dSJordan K. Hubbard int Reboot = FALSE; 135a0a549c7SRuslan Ermilov int howto = RB_AUTOBOOT; 1368fae3551SRodney W. Grimes 13757622f22SPoul-Henning Kamp int devfs; 13857622f22SPoul-Henning Kamp 1398fae3551SRodney W. Grimes void transition __P((state_t)); 1408fae3551SRodney W. Grimes state_t requested_transition = runcom; 1418fae3551SRodney W. Grimes 1428fae3551SRodney W. Grimes void setctty __P((char *)); 1438fae3551SRodney W. Grimes 1448fae3551SRodney W. Grimes typedef struct init_session { 1458fae3551SRodney W. Grimes int se_index; /* index of entry in ttys file */ 1468fae3551SRodney W. Grimes pid_t se_process; /* controlling process */ 1478fae3551SRodney W. Grimes time_t se_started; /* used to avoid thrashing */ 1488fae3551SRodney W. Grimes int se_flags; /* status of session */ 1498fae3551SRodney W. Grimes #define SE_SHUTDOWN 0x1 /* session won't be restarted */ 150b0b670eeSAlfred Perlstein #define SE_PRESENT 0x2 /* session is in /etc/ttys */ 151b5df27e2SAndrey A. Chernov int se_nspace; /* spacing count */ 1528fae3551SRodney W. Grimes char *se_device; /* filename of port */ 1538fae3551SRodney W. Grimes char *se_getty; /* what to run on that port */ 154b5df27e2SAndrey A. Chernov char *se_getty_argv_space; /* pre-parsed argument array space */ 1558fae3551SRodney W. Grimes char **se_getty_argv; /* pre-parsed argument array */ 1568fae3551SRodney W. Grimes char *se_window; /* window system (started only once) */ 157b5df27e2SAndrey A. Chernov char *se_window_argv_space; /* pre-parsed argument array space */ 1588fae3551SRodney W. Grimes char **se_window_argv; /* pre-parsed argument array */ 159b5df27e2SAndrey A. Chernov char *se_type; /* default terminal type */ 1608fae3551SRodney W. Grimes struct init_session *se_prev; 1618fae3551SRodney W. Grimes struct init_session *se_next; 1628fae3551SRodney W. Grimes } session_t; 1638fae3551SRodney W. Grimes 1648fae3551SRodney W. Grimes void free_session __P((session_t *)); 1658fae3551SRodney W. Grimes session_t *new_session __P((session_t *, int, struct ttyent *)); 1668fae3551SRodney W. Grimes session_t *sessions; 1678fae3551SRodney W. Grimes 1688fae3551SRodney W. Grimes char **construct_argv __P((char *)); 1698fae3551SRodney W. Grimes void start_window_system __P((session_t *)); 1708fae3551SRodney W. Grimes void collect_child __P((pid_t)); 1718fae3551SRodney W. Grimes pid_t start_getty __P((session_t *)); 1728fae3551SRodney W. Grimes void transition_handler __P((int)); 1738fae3551SRodney W. Grimes void alrm_handler __P((int)); 1748fae3551SRodney W. Grimes void setsecuritylevel __P((int)); 1758fae3551SRodney W. Grimes int getsecuritylevel __P((void)); 1768fae3551SRodney W. Grimes int setupargv __P((session_t *, struct ttyent *)); 177e82d5545SDavid Nugent #ifdef LOGIN_CAP 178e82d5545SDavid Nugent void setprocresources __P((const char *)); 179e82d5545SDavid Nugent #endif 1808fae3551SRodney W. Grimes int clang; 1818fae3551SRodney W. Grimes 1828fae3551SRodney W. Grimes void clear_session_logs __P((session_t *)); 1838fae3551SRodney W. Grimes 1848fae3551SRodney W. Grimes int start_session_db __P((void)); 1858fae3551SRodney W. Grimes void add_session __P((session_t *)); 1868fae3551SRodney W. Grimes void del_session __P((session_t *)); 1878fae3551SRodney W. Grimes session_t *find_session __P((pid_t)); 1888fae3551SRodney W. Grimes DB *session_db; 1898fae3551SRodney W. Grimes 1908fae3551SRodney W. Grimes /* 1918fae3551SRodney W. Grimes * The mother of all processes. 1928fae3551SRodney W. Grimes */ 1938fae3551SRodney W. Grimes int 1948fae3551SRodney W. Grimes main(argc, argv) 1958fae3551SRodney W. Grimes int argc; 1968fae3551SRodney W. Grimes char **argv; 1978fae3551SRodney W. Grimes { 1988fae3551SRodney W. Grimes int c; 1998fae3551SRodney W. Grimes struct sigaction sa; 2008fae3551SRodney W. Grimes sigset_t mask; 2018fae3551SRodney W. Grimes 2028fae3551SRodney W. Grimes 2038fae3551SRodney W. Grimes /* Dispose of random users. */ 204c5842835SPhilippe Charnier if (getuid() != 0) 205c5842835SPhilippe Charnier errx(1, "%s", strerror(EPERM)); 2068fae3551SRodney W. Grimes 2078fae3551SRodney W. Grimes /* System V users like to reexec init. */ 2081681d659SRuslan Ermilov if (getpid() != 1) { 2091681d659SRuslan Ermilov #ifdef COMPAT_SYSV_INIT 2101681d659SRuslan Ermilov /* So give them what they want */ 2111681d659SRuslan Ermilov if (argc > 1) { 2121681d659SRuslan Ermilov if (strlen(argv[1]) == 1) { 2131681d659SRuslan Ermilov register char runlevel = *argv[1]; 2141681d659SRuslan Ermilov register int sig; 2158fae3551SRodney W. Grimes 2161681d659SRuslan Ermilov switch (runlevel) { 2171681d659SRuslan Ermilov case '0': /* halt + poweroff */ 2181681d659SRuslan Ermilov sig = SIGUSR2; 2191681d659SRuslan Ermilov break; 2201681d659SRuslan Ermilov case '1': /* single-user */ 2211681d659SRuslan Ermilov sig = SIGTERM; 2221681d659SRuslan Ermilov break; 2231681d659SRuslan Ermilov case '6': /* reboot */ 2241681d659SRuslan Ermilov sig = SIGINT; 2251681d659SRuslan Ermilov break; 2261681d659SRuslan Ermilov case 'c': /* block further logins */ 2271681d659SRuslan Ermilov sig = SIGTSTP; 2281681d659SRuslan Ermilov break; 2291681d659SRuslan Ermilov case 'q': /* rescan /etc/ttys */ 2301681d659SRuslan Ermilov sig = SIGHUP; 2311681d659SRuslan Ermilov break; 2321681d659SRuslan Ermilov default: 2331681d659SRuslan Ermilov goto invalid; 2341681d659SRuslan Ermilov } 2351681d659SRuslan Ermilov kill(1, sig); 2361681d659SRuslan Ermilov _exit(0); 2371681d659SRuslan Ermilov } else 2381681d659SRuslan Ermilov invalid: 2391681d659SRuslan Ermilov errx(1, "invalid run-level ``%s''", argv[1]); 2401681d659SRuslan Ermilov } else 2411681d659SRuslan Ermilov #endif 2421681d659SRuslan Ermilov errx(1, "already running"); 2431681d659SRuslan Ermilov } 2448fae3551SRodney W. Grimes /* 2458fae3551SRodney W. Grimes * Note that this does NOT open a file... 2468fae3551SRodney W. Grimes * Does 'init' deserve its own facility number? 2478fae3551SRodney W. Grimes */ 2488fae3551SRodney W. Grimes openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH); 2498fae3551SRodney W. Grimes 2508fae3551SRodney W. Grimes /* 2518fae3551SRodney W. Grimes * Create an initial session. 2528fae3551SRodney W. Grimes */ 2538fae3551SRodney W. Grimes if (setsid() < 0) 2548fae3551SRodney W. Grimes warning("initial setsid() failed: %m"); 2558fae3551SRodney W. Grimes 2568fae3551SRodney W. Grimes /* 2578fae3551SRodney W. Grimes * Establish an initial user so that programs running 2588fae3551SRodney W. Grimes * single user do not freak out and die (like passwd). 2598fae3551SRodney W. Grimes */ 2608fae3551SRodney W. Grimes if (setlogin("root") < 0) 2618fae3551SRodney W. Grimes warning("setlogin() failed: %m"); 2628fae3551SRodney W. Grimes 2638fae3551SRodney W. Grimes /* 2648fae3551SRodney W. Grimes * This code assumes that we always get arguments through flags, 2658fae3551SRodney W. Grimes * never through bits set in some random machine register. 2668fae3551SRodney W. Grimes */ 26757622f22SPoul-Henning Kamp while ((c = getopt(argc, argv, "dsf")) != -1) 2688fae3551SRodney W. Grimes switch (c) { 26957622f22SPoul-Henning Kamp case 'd': 27057622f22SPoul-Henning Kamp devfs = 1; 27157622f22SPoul-Henning Kamp break; 2728fae3551SRodney W. Grimes case 's': 2738fae3551SRodney W. Grimes requested_transition = single_user; 2748fae3551SRodney W. Grimes break; 2758fae3551SRodney W. Grimes case 'f': 2768fae3551SRodney W. Grimes runcom_mode = FASTBOOT; 2778fae3551SRodney W. Grimes break; 2788fae3551SRodney W. Grimes default: 2798fae3551SRodney W. Grimes warning("unrecognized flag '-%c'", c); 2808fae3551SRodney W. Grimes break; 2818fae3551SRodney W. Grimes } 2828fae3551SRodney W. Grimes 2838fae3551SRodney W. Grimes if (optind != argc) 2848fae3551SRodney W. Grimes warning("ignoring excess arguments"); 2858fae3551SRodney W. Grimes 28657622f22SPoul-Henning Kamp if (devfs) { 287421b0201SPoul-Henning Kamp char *s; 288421b0201SPoul-Henning Kamp int i; 289421b0201SPoul-Henning Kamp 290421b0201SPoul-Henning Kamp /* 291421b0201SPoul-Henning Kamp * Try to avoid the trailing slash in _PATH_DEV. 292421b0201SPoul-Henning Kamp * Be *very* defensive. 293421b0201SPoul-Henning Kamp */ 294421b0201SPoul-Henning Kamp s = strdup(_PATH_DEV); 295421b0201SPoul-Henning Kamp if (s != NULL) { 296421b0201SPoul-Henning Kamp i = strlen(s); 297421b0201SPoul-Henning Kamp if (i > 0 && s[i - 1] == '/') 298421b0201SPoul-Henning Kamp s[i - 1] = '\0'; 299421b0201SPoul-Henning Kamp mount("devfs", s, 0, 0); 300421b0201SPoul-Henning Kamp free(s); 301421b0201SPoul-Henning Kamp } else { 3021a37aa56SDavid E. O'Brien mount("devfs", _PATH_DEV, 0, 0); 30357622f22SPoul-Henning Kamp } 304421b0201SPoul-Henning Kamp } 30557622f22SPoul-Henning Kamp 3068fae3551SRodney W. Grimes /* 3078fae3551SRodney W. Grimes * We catch or block signals rather than ignore them, 3088fae3551SRodney W. Grimes * so that they get reset on exec. 3098fae3551SRodney W. Grimes */ 3108fae3551SRodney W. Grimes handle(badsys, SIGSYS, 0); 3118fae3551SRodney W. Grimes handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, 3128fae3551SRodney W. Grimes SIGBUS, SIGXCPU, SIGXFSZ, 0); 313a0a549c7SRuslan Ermilov handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, 314a0a549c7SRuslan Ermilov SIGUSR1, SIGUSR2, 0); 3158fae3551SRodney W. Grimes handle(alrm_handler, SIGALRM, 0); 3168fae3551SRodney W. Grimes sigfillset(&mask); 3178fae3551SRodney W. Grimes delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS, 318a0a549c7SRuslan Ermilov SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM, 319a0a549c7SRuslan Ermilov SIGUSR1, SIGUSR2, 0); 3208fae3551SRodney W. Grimes sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 3218fae3551SRodney W. Grimes sigemptyset(&sa.sa_mask); 3228fae3551SRodney W. Grimes sa.sa_flags = 0; 3238fae3551SRodney W. Grimes sa.sa_handler = SIG_IGN; 3248fae3551SRodney W. Grimes (void) sigaction(SIGTTIN, &sa, (struct sigaction *)0); 3258fae3551SRodney W. Grimes (void) sigaction(SIGTTOU, &sa, (struct sigaction *)0); 3268fae3551SRodney W. Grimes 3278fae3551SRodney W. Grimes /* 3288fae3551SRodney W. Grimes * Paranoia. 3298fae3551SRodney W. Grimes */ 3308fae3551SRodney W. Grimes close(0); 3318fae3551SRodney W. Grimes close(1); 3328fae3551SRodney W. Grimes close(2); 3338fae3551SRodney W. Grimes 3348fae3551SRodney W. Grimes /* 3358fae3551SRodney W. Grimes * Start the state machine. 3368fae3551SRodney W. Grimes */ 3378fae3551SRodney W. Grimes transition(requested_transition); 3388fae3551SRodney W. Grimes 3398fae3551SRodney W. Grimes /* 3408fae3551SRodney W. Grimes * Should never reach here. 3418fae3551SRodney W. Grimes */ 3428fae3551SRodney W. Grimes return 1; 3438fae3551SRodney W. Grimes } 3448fae3551SRodney W. Grimes 3458fae3551SRodney W. Grimes /* 3468fae3551SRodney W. Grimes * Associate a function with a signal handler. 3478fae3551SRodney W. Grimes */ 3488fae3551SRodney W. Grimes void 3498fae3551SRodney W. Grimes #ifdef __STDC__ 3508fae3551SRodney W. Grimes handle(sig_t handler, ...) 3518fae3551SRodney W. Grimes #else 3528fae3551SRodney W. Grimes handle(va_alist) 3538fae3551SRodney W. Grimes va_dcl 3548fae3551SRodney W. Grimes #endif 3558fae3551SRodney W. Grimes { 3568fae3551SRodney W. Grimes int sig; 3578fae3551SRodney W. Grimes struct sigaction sa; 35839034633SJames Raynard sigset_t mask_everything; 3598fae3551SRodney W. Grimes va_list ap; 3608fae3551SRodney W. Grimes #ifndef __STDC__ 3618fae3551SRodney W. Grimes sig_t handler; 3628fae3551SRodney W. Grimes 3638fae3551SRodney W. Grimes va_start(ap); 3648fae3551SRodney W. Grimes handler = va_arg(ap, sig_t); 3658fae3551SRodney W. Grimes #else 3668fae3551SRodney W. Grimes va_start(ap, handler); 3678fae3551SRodney W. Grimes #endif 3688fae3551SRodney W. Grimes 3698fae3551SRodney W. Grimes sa.sa_handler = handler; 3708fae3551SRodney W. Grimes sigfillset(&mask_everything); 3718fae3551SRodney W. Grimes 3728889c700SDavid Nugent while ((sig = va_arg(ap, int)) != NULL) { 3738fae3551SRodney W. Grimes sa.sa_mask = mask_everything; 3748fae3551SRodney W. Grimes /* XXX SA_RESTART? */ 3758fae3551SRodney W. Grimes sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0; 3768fae3551SRodney W. Grimes sigaction(sig, &sa, (struct sigaction *) 0); 3778fae3551SRodney W. Grimes } 3788fae3551SRodney W. Grimes va_end(ap); 3798fae3551SRodney W. Grimes } 3808fae3551SRodney W. Grimes 3818fae3551SRodney W. Grimes /* 3828fae3551SRodney W. Grimes * Delete a set of signals from a mask. 3838fae3551SRodney W. Grimes */ 3848fae3551SRodney W. Grimes void 3858fae3551SRodney W. Grimes #ifdef __STDC__ 3868fae3551SRodney W. Grimes delset(sigset_t *maskp, ...) 3878fae3551SRodney W. Grimes #else 3888fae3551SRodney W. Grimes delset(va_alist) 3898fae3551SRodney W. Grimes va_dcl 3908fae3551SRodney W. Grimes #endif 3918fae3551SRodney W. Grimes { 3928fae3551SRodney W. Grimes int sig; 3938fae3551SRodney W. Grimes va_list ap; 3948fae3551SRodney W. Grimes #ifndef __STDC__ 3958fae3551SRodney W. Grimes sigset_t *maskp; 3968fae3551SRodney W. Grimes 3978fae3551SRodney W. Grimes va_start(ap); 3988fae3551SRodney W. Grimes maskp = va_arg(ap, sigset_t *); 3998fae3551SRodney W. Grimes #else 4008fae3551SRodney W. Grimes va_start(ap, maskp); 4018fae3551SRodney W. Grimes #endif 4028fae3551SRodney W. Grimes 4038889c700SDavid Nugent while ((sig = va_arg(ap, int)) != NULL) 4048fae3551SRodney W. Grimes sigdelset(maskp, sig); 4058fae3551SRodney W. Grimes va_end(ap); 4068fae3551SRodney W. Grimes } 4078fae3551SRodney W. Grimes 4088fae3551SRodney W. Grimes /* 4098fae3551SRodney W. Grimes * Log a message and sleep for a while (to give someone an opportunity 4108fae3551SRodney W. Grimes * to read it and to save log or hardcopy output if the problem is chronic). 4118fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4128fae3551SRodney W. Grimes */ 4138fae3551SRodney W. Grimes void 4148fae3551SRodney W. Grimes #ifdef __STDC__ 4155979df34SKris Kennaway stall(const char *message, ...) 4168fae3551SRodney W. Grimes #else 4178fae3551SRodney W. Grimes stall(va_alist) 4188fae3551SRodney W. Grimes va_dcl 4198fae3551SRodney W. Grimes #endif 4208fae3551SRodney W. Grimes { 4218fae3551SRodney W. Grimes va_list ap; 4228fae3551SRodney W. Grimes #ifndef __STDC__ 4235979df34SKris Kennaway const char *message; 4248fae3551SRodney W. Grimes 4258fae3551SRodney W. Grimes va_start(ap); 4268fae3551SRodney W. Grimes message = va_arg(ap, char *); 4278fae3551SRodney W. Grimes #else 4288fae3551SRodney W. Grimes va_start(ap, message); 4298fae3551SRodney W. Grimes #endif 4308fae3551SRodney W. Grimes 4318fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap); 4328fae3551SRodney W. Grimes va_end(ap); 4338fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 4348fae3551SRodney W. Grimes } 4358fae3551SRodney W. Grimes 4368fae3551SRodney W. Grimes /* 4378fae3551SRodney W. Grimes * Like stall(), but doesn't sleep. 4388fae3551SRodney W. Grimes * If cpp had variadic macros, the two functions could be #defines for another. 4398fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4408fae3551SRodney W. Grimes */ 4418fae3551SRodney W. Grimes void 4428fae3551SRodney W. Grimes #ifdef __STDC__ 4435979df34SKris Kennaway warning(const char *message, ...) 4448fae3551SRodney W. Grimes #else 4458fae3551SRodney W. Grimes warning(va_alist) 4468fae3551SRodney W. Grimes va_dcl 4478fae3551SRodney W. Grimes #endif 4488fae3551SRodney W. Grimes { 4498fae3551SRodney W. Grimes va_list ap; 4508fae3551SRodney W. Grimes #ifndef __STDC__ 4515979df34SKris Kennaway const char *message; 4528fae3551SRodney W. Grimes 4538fae3551SRodney W. Grimes va_start(ap); 4548fae3551SRodney W. Grimes message = va_arg(ap, char *); 4558fae3551SRodney W. Grimes #else 4568fae3551SRodney W. Grimes va_start(ap, message); 4578fae3551SRodney W. Grimes #endif 4588fae3551SRodney W. Grimes 4598fae3551SRodney W. Grimes vsyslog(LOG_ALERT, message, ap); 4608fae3551SRodney W. Grimes va_end(ap); 4618fae3551SRodney W. Grimes } 4628fae3551SRodney W. Grimes 4638fae3551SRodney W. Grimes /* 4648fae3551SRodney W. Grimes * Log an emergency message. 4658fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 4668fae3551SRodney W. Grimes */ 4678fae3551SRodney W. Grimes void 4688fae3551SRodney W. Grimes #ifdef __STDC__ 4695979df34SKris Kennaway emergency(const char *message, ...) 4708fae3551SRodney W. Grimes #else 4718fae3551SRodney W. Grimes emergency(va_alist) 4728fae3551SRodney W. Grimes va_dcl 4738fae3551SRodney W. Grimes #endif 4748fae3551SRodney W. Grimes { 4758fae3551SRodney W. Grimes va_list ap; 4768fae3551SRodney W. Grimes #ifndef __STDC__ 4775979df34SKris Kennaway const char *message; 4788fae3551SRodney W. Grimes 4798fae3551SRodney W. Grimes va_start(ap); 4808fae3551SRodney W. Grimes message = va_arg(ap, char *); 4818fae3551SRodney W. Grimes #else 4828fae3551SRodney W. Grimes va_start(ap, message); 4838fae3551SRodney W. Grimes #endif 4848fae3551SRodney W. Grimes 4858fae3551SRodney W. Grimes vsyslog(LOG_EMERG, message, ap); 4868fae3551SRodney W. Grimes va_end(ap); 4878fae3551SRodney W. Grimes } 4888fae3551SRodney W. Grimes 4898fae3551SRodney W. Grimes /* 4908fae3551SRodney W. Grimes * Catch a SIGSYS signal. 4918fae3551SRodney W. Grimes * 4928fae3551SRodney W. Grimes * These may arise if a system does not support sysctl. 4938fae3551SRodney W. Grimes * We tolerate up to 25 of these, then throw in the towel. 4948fae3551SRodney W. Grimes */ 4958fae3551SRodney W. Grimes void 4968fae3551SRodney W. Grimes badsys(sig) 4978fae3551SRodney W. Grimes int sig; 4988fae3551SRodney W. Grimes { 4998fae3551SRodney W. Grimes static int badcount = 0; 5008fae3551SRodney W. Grimes 5018fae3551SRodney W. Grimes if (badcount++ < 25) 5028fae3551SRodney W. Grimes return; 5038fae3551SRodney W. Grimes disaster(sig); 5048fae3551SRodney W. Grimes } 5058fae3551SRodney W. Grimes 5068fae3551SRodney W. Grimes /* 5078fae3551SRodney W. Grimes * Catch an unexpected signal. 5088fae3551SRodney W. Grimes */ 5098fae3551SRodney W. Grimes void 5108fae3551SRodney W. Grimes disaster(sig) 5118fae3551SRodney W. Grimes int sig; 5128fae3551SRodney W. Grimes { 5138fae3551SRodney W. Grimes emergency("fatal signal: %s", 5148889c700SDavid Nugent (unsigned)sig < NSIG ? sys_siglist[sig] : "unknown signal"); 5158fae3551SRodney W. Grimes 5168fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 5178fae3551SRodney W. Grimes _exit(sig); /* reboot */ 5188fae3551SRodney W. Grimes } 5198fae3551SRodney W. Grimes 5208fae3551SRodney W. Grimes /* 5218fae3551SRodney W. Grimes * Get the security level of the kernel. 5228fae3551SRodney W. Grimes */ 5238fae3551SRodney W. Grimes int 5248fae3551SRodney W. Grimes getsecuritylevel() 5258fae3551SRodney W. Grimes { 5268fae3551SRodney W. Grimes #ifdef KERN_SECURELVL 5278fae3551SRodney W. Grimes int name[2], curlevel; 5288fae3551SRodney W. Grimes size_t len; 5298fae3551SRodney W. Grimes 5308fae3551SRodney W. Grimes name[0] = CTL_KERN; 5318fae3551SRodney W. Grimes name[1] = KERN_SECURELVL; 5328fae3551SRodney W. Grimes len = sizeof curlevel; 5338fae3551SRodney W. Grimes if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) { 5348fae3551SRodney W. Grimes emergency("cannot get kernel security level: %s", 5358fae3551SRodney W. Grimes strerror(errno)); 5368fae3551SRodney W. Grimes return (-1); 5378fae3551SRodney W. Grimes } 5388fae3551SRodney W. Grimes return (curlevel); 5398fae3551SRodney W. Grimes #else 5408fae3551SRodney W. Grimes return (-1); 5418fae3551SRodney W. Grimes #endif 5428fae3551SRodney W. Grimes } 5438fae3551SRodney W. Grimes 5448fae3551SRodney W. Grimes /* 5458fae3551SRodney W. Grimes * Set the security level of the kernel. 5468fae3551SRodney W. Grimes */ 5478fae3551SRodney W. Grimes void 5488fae3551SRodney W. Grimes setsecuritylevel(newlevel) 5498fae3551SRodney W. Grimes int newlevel; 5508fae3551SRodney W. Grimes { 5518fae3551SRodney W. Grimes #ifdef KERN_SECURELVL 5528fae3551SRodney W. Grimes int name[2], curlevel; 5538fae3551SRodney W. Grimes 5548fae3551SRodney W. Grimes curlevel = getsecuritylevel(); 5558fae3551SRodney W. Grimes if (newlevel == curlevel) 5568fae3551SRodney W. Grimes return; 5578fae3551SRodney W. Grimes name[0] = CTL_KERN; 5588fae3551SRodney W. Grimes name[1] = KERN_SECURELVL; 5598fae3551SRodney W. Grimes if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) { 5608fae3551SRodney W. Grimes emergency( 5618fae3551SRodney W. Grimes "cannot change kernel security level from %d to %d: %s", 5628fae3551SRodney W. Grimes curlevel, newlevel, strerror(errno)); 5638fae3551SRodney W. Grimes return; 5648fae3551SRodney W. Grimes } 5658fae3551SRodney W. Grimes #ifdef SECURE 5668fae3551SRodney W. Grimes warning("kernel security level changed from %d to %d", 5678fae3551SRodney W. Grimes curlevel, newlevel); 5688fae3551SRodney W. Grimes #endif 5698fae3551SRodney W. Grimes #endif 5708fae3551SRodney W. Grimes } 5718fae3551SRodney W. Grimes 5728fae3551SRodney W. Grimes /* 5738fae3551SRodney W. Grimes * Change states in the finite state machine. 5748fae3551SRodney W. Grimes * The initial state is passed as an argument. 5758fae3551SRodney W. Grimes */ 5768fae3551SRodney W. Grimes void 5778fae3551SRodney W. Grimes transition(s) 5788fae3551SRodney W. Grimes state_t s; 5798fae3551SRodney W. Grimes { 5808fae3551SRodney W. Grimes for (;;) 5818fae3551SRodney W. Grimes s = (state_t) (*s)(); 5828fae3551SRodney W. Grimes } 5838fae3551SRodney W. Grimes 5848fae3551SRodney W. Grimes /* 5858fae3551SRodney W. Grimes * Close out the accounting files for a login session. 5868fae3551SRodney W. Grimes * NB: should send a message to the session logger to avoid blocking. 5878fae3551SRodney W. Grimes */ 5888fae3551SRodney W. Grimes void 5898fae3551SRodney W. Grimes clear_session_logs(sp) 5908fae3551SRodney W. Grimes session_t *sp; 5918fae3551SRodney W. Grimes { 5928fae3551SRodney W. Grimes char *line = sp->se_device + sizeof(_PATH_DEV) - 1; 5938fae3551SRodney W. Grimes 5948fae3551SRodney W. Grimes if (logout(line)) 5958fae3551SRodney W. Grimes logwtmp(line, "", ""); 5968fae3551SRodney W. Grimes } 5978fae3551SRodney W. Grimes 5988fae3551SRodney W. Grimes /* 5998fae3551SRodney W. Grimes * Start a session and allocate a controlling terminal. 6008fae3551SRodney W. Grimes * Only called by children of init after forking. 6018fae3551SRodney W. Grimes */ 6028fae3551SRodney W. Grimes void 6038fae3551SRodney W. Grimes setctty(name) 6048fae3551SRodney W. Grimes char *name; 6058fae3551SRodney W. Grimes { 6068fae3551SRodney W. Grimes int fd; 6078fae3551SRodney W. Grimes 6088fae3551SRodney W. Grimes (void) revoke(name); 6098fae3551SRodney W. Grimes if ((fd = open(name, O_RDWR)) == -1) { 6108fae3551SRodney W. Grimes stall("can't open %s: %m", name); 6118fae3551SRodney W. Grimes _exit(1); 6128fae3551SRodney W. Grimes } 6138fae3551SRodney W. Grimes if (login_tty(fd) == -1) { 6148fae3551SRodney W. Grimes stall("can't get %s for controlling terminal: %m", name); 6158fae3551SRodney W. Grimes _exit(1); 6168fae3551SRodney W. Grimes } 6178fae3551SRodney W. Grimes } 6188fae3551SRodney W. Grimes 6198fae3551SRodney W. Grimes /* 6208fae3551SRodney W. Grimes * Bring the system up single user. 6218fae3551SRodney W. Grimes */ 6228fae3551SRodney W. Grimes state_func_t 6238fae3551SRodney W. Grimes single_user() 6248fae3551SRodney W. Grimes { 6258fae3551SRodney W. Grimes pid_t pid, wpid; 6268fae3551SRodney W. Grimes int status; 6278fae3551SRodney W. Grimes sigset_t mask; 6288fae3551SRodney W. Grimes char *shell = _PATH_BSHELL; 6298fae3551SRodney W. Grimes char *argv[2]; 6308fae3551SRodney W. Grimes #ifdef SECURE 6318fae3551SRodney W. Grimes struct ttyent *typ; 6328fae3551SRodney W. Grimes struct passwd *pp; 6338fae3551SRodney W. Grimes static const char banner[] = 6348fae3551SRodney W. Grimes "Enter root password, or ^D to go multi-user\n"; 6358fae3551SRodney W. Grimes char *clear, *password; 6368fae3551SRodney W. Grimes #endif 63763322c28SPoul-Henning Kamp #ifdef DEBUGSHELL 63863322c28SPoul-Henning Kamp char altshell[128]; 63963322c28SPoul-Henning Kamp #endif 6408fae3551SRodney W. Grimes 641db8ad19dSJordan K. Hubbard if (Reboot) { 642a0a549c7SRuslan Ermilov /* Instead of going single user, let's reboot the machine */ 643e460cfd3SNate Williams sync(); 644e460cfd3SNate Williams alarm(2); 645e460cfd3SNate Williams pause(); 646a0a549c7SRuslan Ermilov reboot(howto); 647e460cfd3SNate Williams _exit(0); 648e460cfd3SNate Williams } 649e460cfd3SNate Williams 6508fae3551SRodney W. Grimes if ((pid = fork()) == 0) { 6518fae3551SRodney W. Grimes /* 6528fae3551SRodney W. Grimes * Start the single user session. 6538fae3551SRodney W. Grimes */ 6548fae3551SRodney W. Grimes setctty(_PATH_CONSOLE); 6558fae3551SRodney W. Grimes 6568fae3551SRodney W. Grimes #ifdef SECURE 6578fae3551SRodney W. Grimes /* 6588fae3551SRodney W. Grimes * Check the root password. 6598fae3551SRodney W. Grimes * We don't care if the console is 'on' by default; 6608fae3551SRodney W. Grimes * it's the only tty that can be 'off' and 'secure'. 6618fae3551SRodney W. Grimes */ 6628fae3551SRodney W. Grimes typ = getttynam("console"); 6638fae3551SRodney W. Grimes pp = getpwnam("root"); 664a69497d7SMatthew Dillon if (typ && (typ->ty_status & TTY_SECURE) == 0 && 665a69497d7SMatthew Dillon pp && *pp->pw_passwd) { 666e1b4d8d0SSheldon Hearn write(STDERR_FILENO, banner, sizeof banner - 1); 6678fae3551SRodney W. Grimes for (;;) { 6688fae3551SRodney W. Grimes clear = getpass("Password:"); 6698fae3551SRodney W. Grimes if (clear == 0 || *clear == '\0') 6708fae3551SRodney W. Grimes _exit(0); 6718fae3551SRodney W. Grimes password = crypt(clear, pp->pw_passwd); 6728fae3551SRodney W. Grimes bzero(clear, _PASSWORD_LEN); 6738fae3551SRodney W. Grimes if (strcmp(password, pp->pw_passwd) == 0) 6748fae3551SRodney W. Grimes break; 6758fae3551SRodney W. Grimes warning("single-user login failed\n"); 6768fae3551SRodney W. Grimes } 6778fae3551SRodney W. Grimes } 6788fae3551SRodney W. Grimes endttyent(); 6798fae3551SRodney W. Grimes endpwent(); 6808fae3551SRodney W. Grimes #endif /* SECURE */ 6818fae3551SRodney W. Grimes 6828fae3551SRodney W. Grimes #ifdef DEBUGSHELL 6838fae3551SRodney W. Grimes { 68463322c28SPoul-Henning Kamp char *cp = altshell; 6858fae3551SRodney W. Grimes int num; 6868fae3551SRodney W. Grimes 6878fae3551SRodney W. Grimes #define SHREQUEST \ 6881bc5fcd0SPeter Wemm "Enter full pathname of shell or RETURN for " _PATH_BSHELL ": " 6898fae3551SRodney W. Grimes (void)write(STDERR_FILENO, 6908fae3551SRodney W. Grimes SHREQUEST, sizeof(SHREQUEST) - 1); 6918fae3551SRodney W. Grimes while ((num = read(STDIN_FILENO, cp, 1)) != -1 && 6928fae3551SRodney W. Grimes num != 0 && *cp != '\n' && cp < &altshell[127]) 6938fae3551SRodney W. Grimes cp++; 6948fae3551SRodney W. Grimes *cp = '\0'; 6958fae3551SRodney W. Grimes if (altshell[0] != '\0') 6968fae3551SRodney W. Grimes shell = altshell; 6978fae3551SRodney W. Grimes } 6988fae3551SRodney W. Grimes #endif /* DEBUGSHELL */ 6998fae3551SRodney W. Grimes 7008fae3551SRodney W. Grimes /* 7018fae3551SRodney W. Grimes * Unblock signals. 7028fae3551SRodney W. Grimes * We catch all the interesting ones, 7038fae3551SRodney W. Grimes * and those are reset to SIG_DFL on exec. 7048fae3551SRodney W. Grimes */ 7058fae3551SRodney W. Grimes sigemptyset(&mask); 7068fae3551SRodney W. Grimes sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 7078fae3551SRodney W. Grimes 7088fae3551SRodney W. Grimes /* 7098fae3551SRodney W. Grimes * Fire off a shell. 7108fae3551SRodney W. Grimes * If the default one doesn't work, try the Bourne shell. 7118fae3551SRodney W. Grimes */ 7128fae3551SRodney W. Grimes argv[0] = "-sh"; 7138fae3551SRodney W. Grimes argv[1] = 0; 7148fae3551SRodney W. Grimes execv(shell, argv); 7158fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", shell); 7168fae3551SRodney W. Grimes execv(_PATH_BSHELL, argv); 7178fae3551SRodney W. Grimes emergency("can't exec %s for single user: %m", _PATH_BSHELL); 7188fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 7198fae3551SRodney W. Grimes _exit(1); 7208fae3551SRodney W. Grimes } 7218fae3551SRodney W. Grimes 7228fae3551SRodney W. Grimes if (pid == -1) { 7238fae3551SRodney W. Grimes /* 7248fae3551SRodney W. Grimes * We are seriously hosed. Do our best. 7258fae3551SRodney W. Grimes */ 7268fae3551SRodney W. Grimes emergency("can't fork single-user shell, trying again"); 7278fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0) 7288fae3551SRodney W. Grimes continue; 7298fae3551SRodney W. Grimes return (state_func_t) single_user; 7308fae3551SRodney W. Grimes } 7318fae3551SRodney W. Grimes 7328fae3551SRodney W. Grimes requested_transition = 0; 7338fae3551SRodney W. Grimes do { 7348fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 7358fae3551SRodney W. Grimes collect_child(wpid); 7368fae3551SRodney W. Grimes if (wpid == -1) { 7378fae3551SRodney W. Grimes if (errno == EINTR) 7388fae3551SRodney W. Grimes continue; 7398fae3551SRodney W. Grimes warning("wait for single-user shell failed: %m; restarting"); 7408fae3551SRodney W. Grimes return (state_func_t) single_user; 7418fae3551SRodney W. Grimes } 7428fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) { 7438fae3551SRodney W. Grimes warning("init: shell stopped, restarting\n"); 7448fae3551SRodney W. Grimes kill(pid, SIGCONT); 7458fae3551SRodney W. Grimes wpid = -1; 7468fae3551SRodney W. Grimes } 7478fae3551SRodney W. Grimes } while (wpid != pid && !requested_transition); 7488fae3551SRodney W. Grimes 7498fae3551SRodney W. Grimes if (requested_transition) 7508fae3551SRodney W. Grimes return (state_func_t) requested_transition; 7518fae3551SRodney W. Grimes 7528fae3551SRodney W. Grimes if (!WIFEXITED(status)) { 7538fae3551SRodney W. Grimes if (WTERMSIG(status) == SIGKILL) { 7548fae3551SRodney W. Grimes /* 7558fae3551SRodney W. Grimes * reboot(8) killed shell? 7568fae3551SRodney W. Grimes */ 7578fae3551SRodney W. Grimes warning("single user shell terminated."); 7588fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 7598fae3551SRodney W. Grimes _exit(0); 7608fae3551SRodney W. Grimes } else { 7618fae3551SRodney W. Grimes warning("single user shell terminated, restarting"); 7628fae3551SRodney W. Grimes return (state_func_t) single_user; 7638fae3551SRodney W. Grimes } 7648fae3551SRodney W. Grimes } 7658fae3551SRodney W. Grimes 7668fae3551SRodney W. Grimes runcom_mode = FASTBOOT; 7678fae3551SRodney W. Grimes return (state_func_t) runcom; 7688fae3551SRodney W. Grimes } 7698fae3551SRodney W. Grimes 7708fae3551SRodney W. Grimes /* 7718fae3551SRodney W. Grimes * Run the system startup script. 7728fae3551SRodney W. Grimes */ 7738fae3551SRodney W. Grimes state_func_t 7748fae3551SRodney W. Grimes runcom() 7758fae3551SRodney W. Grimes { 7768fae3551SRodney W. Grimes pid_t pid, wpid; 7778fae3551SRodney W. Grimes int status; 7788fae3551SRodney W. Grimes char *argv[4]; 7798fae3551SRodney W. Grimes struct sigaction sa; 7808fae3551SRodney W. Grimes 7818fae3551SRodney W. Grimes if ((pid = fork()) == 0) { 7828fae3551SRodney W. Grimes sigemptyset(&sa.sa_mask); 7838fae3551SRodney W. Grimes sa.sa_flags = 0; 7848fae3551SRodney W. Grimes sa.sa_handler = SIG_IGN; 7858fae3551SRodney W. Grimes (void) sigaction(SIGTSTP, &sa, (struct sigaction *)0); 7868fae3551SRodney W. Grimes (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); 7878fae3551SRodney W. Grimes 7888fae3551SRodney W. Grimes setctty(_PATH_CONSOLE); 7898fae3551SRodney W. Grimes 7908fae3551SRodney W. Grimes argv[0] = "sh"; 7918fae3551SRodney W. Grimes argv[1] = _PATH_RUNCOM; 7928fae3551SRodney W. Grimes argv[2] = runcom_mode == AUTOBOOT ? "autoboot" : 0; 7938fae3551SRodney W. Grimes argv[3] = 0; 7948fae3551SRodney W. Grimes 7958fae3551SRodney W. Grimes sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 7968fae3551SRodney W. Grimes 7971ef60eb1SDavid Nugent #ifdef LOGIN_CAP 7981ef60eb1SDavid Nugent setprocresources(RESOURCE_RC); 7991ef60eb1SDavid Nugent #endif 8008fae3551SRodney W. Grimes execv(_PATH_BSHELL, argv); 8018fae3551SRodney W. Grimes stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM); 8028fae3551SRodney W. Grimes _exit(1); /* force single user mode */ 8038fae3551SRodney W. Grimes } 8048fae3551SRodney W. Grimes 8058fae3551SRodney W. Grimes if (pid == -1) { 8068fae3551SRodney W. Grimes emergency("can't fork for %s on %s: %m", 8078fae3551SRodney W. Grimes _PATH_BSHELL, _PATH_RUNCOM); 8088fae3551SRodney W. Grimes while (waitpid(-1, (int *) 0, WNOHANG) > 0) 8098fae3551SRodney W. Grimes continue; 8108fae3551SRodney W. Grimes sleep(STALL_TIMEOUT); 8118fae3551SRodney W. Grimes return (state_func_t) single_user; 8128fae3551SRodney W. Grimes } 8138fae3551SRodney W. Grimes 8148fae3551SRodney W. Grimes /* 8158fae3551SRodney W. Grimes * Copied from single_user(). This is a bit paranoid. 8168fae3551SRodney W. Grimes */ 8178fae3551SRodney W. Grimes do { 8188fae3551SRodney W. Grimes if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 8198fae3551SRodney W. Grimes collect_child(wpid); 8208fae3551SRodney W. Grimes if (wpid == -1) { 8218fae3551SRodney W. Grimes if (errno == EINTR) 8228fae3551SRodney W. Grimes continue; 8238fae3551SRodney W. Grimes warning("wait for %s on %s failed: %m; going to single user mode", 8248fae3551SRodney W. Grimes _PATH_BSHELL, _PATH_RUNCOM); 8258fae3551SRodney W. Grimes return (state_func_t) single_user; 8268fae3551SRodney W. Grimes } 8278fae3551SRodney W. Grimes if (wpid == pid && WIFSTOPPED(status)) { 8288fae3551SRodney W. Grimes warning("init: %s on %s stopped, restarting\n", 8298fae3551SRodney W. Grimes _PATH_BSHELL, _PATH_RUNCOM); 8308fae3551SRodney W. Grimes kill(pid, SIGCONT); 8318fae3551SRodney W. Grimes wpid = -1; 8328fae3551SRodney W. Grimes } 8338fae3551SRodney W. Grimes } while (wpid != pid); 8348fae3551SRodney W. Grimes 8358fae3551SRodney W. Grimes if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 8368fae3551SRodney W. Grimes requested_transition == catatonia) { 8378fae3551SRodney W. Grimes /* /etc/rc executed /sbin/reboot; wait for the end quietly */ 8388fae3551SRodney W. Grimes sigset_t s; 8398fae3551SRodney W. Grimes 8408fae3551SRodney W. Grimes sigfillset(&s); 8418fae3551SRodney W. Grimes for (;;) 8428fae3551SRodney W. Grimes sigsuspend(&s); 8438fae3551SRodney W. Grimes } 8448fae3551SRodney W. Grimes 8458fae3551SRodney W. Grimes if (!WIFEXITED(status)) { 8468fae3551SRodney W. Grimes warning("%s on %s terminated abnormally, going to single user mode", 8478fae3551SRodney W. Grimes _PATH_BSHELL, _PATH_RUNCOM); 8488fae3551SRodney W. Grimes return (state_func_t) single_user; 8498fae3551SRodney W. Grimes } 8508fae3551SRodney W. Grimes 8518fae3551SRodney W. Grimes if (WEXITSTATUS(status)) 8528fae3551SRodney W. Grimes return (state_func_t) single_user; 8538fae3551SRodney W. Grimes 8548fae3551SRodney W. Grimes runcom_mode = AUTOBOOT; /* the default */ 8558fae3551SRodney W. Grimes /* NB: should send a message to the session logger to avoid blocking. */ 8568fae3551SRodney W. Grimes logwtmp("~", "reboot", ""); 8578fae3551SRodney W. Grimes return (state_func_t) read_ttys; 8588fae3551SRodney W. Grimes } 8598fae3551SRodney W. Grimes 8608fae3551SRodney W. Grimes /* 8618fae3551SRodney W. Grimes * Open the session database. 8628fae3551SRodney W. Grimes * 8638fae3551SRodney W. Grimes * NB: We could pass in the size here; is it necessary? 8648fae3551SRodney W. Grimes */ 8658fae3551SRodney W. Grimes int 8668fae3551SRodney W. Grimes start_session_db() 8678fae3551SRodney W. Grimes { 8688fae3551SRodney W. Grimes if (session_db && (*session_db->close)(session_db)) 8698fae3551SRodney W. Grimes emergency("session database close: %s", strerror(errno)); 8708fae3551SRodney W. Grimes if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) { 8718fae3551SRodney W. Grimes emergency("session database open: %s", strerror(errno)); 8728fae3551SRodney W. Grimes return (1); 8738fae3551SRodney W. Grimes } 8748fae3551SRodney W. Grimes return (0); 8758fae3551SRodney W. Grimes 8768fae3551SRodney W. Grimes } 8778fae3551SRodney W. Grimes 8788fae3551SRodney W. Grimes /* 8798fae3551SRodney W. Grimes * Add a new login session. 8808fae3551SRodney W. Grimes */ 8818fae3551SRodney W. Grimes void 8828fae3551SRodney W. Grimes add_session(sp) 8838fae3551SRodney W. Grimes session_t *sp; 8848fae3551SRodney W. Grimes { 8858fae3551SRodney W. Grimes DBT key; 8868fae3551SRodney W. Grimes DBT data; 8878fae3551SRodney W. Grimes 8888fae3551SRodney W. Grimes key.data = &sp->se_process; 8898fae3551SRodney W. Grimes key.size = sizeof sp->se_process; 8908fae3551SRodney W. Grimes data.data = &sp; 8918fae3551SRodney W. Grimes data.size = sizeof sp; 8928fae3551SRodney W. Grimes 8938fae3551SRodney W. Grimes if ((*session_db->put)(session_db, &key, &data, 0)) 8948fae3551SRodney W. Grimes emergency("insert %d: %s", sp->se_process, strerror(errno)); 8958fae3551SRodney W. Grimes } 8968fae3551SRodney W. Grimes 8978fae3551SRodney W. Grimes /* 8988fae3551SRodney W. Grimes * Delete an old login session. 8998fae3551SRodney W. Grimes */ 9008fae3551SRodney W. Grimes void 9018fae3551SRodney W. Grimes del_session(sp) 9028fae3551SRodney W. Grimes session_t *sp; 9038fae3551SRodney W. Grimes { 9048fae3551SRodney W. Grimes DBT key; 9058fae3551SRodney W. Grimes 9068fae3551SRodney W. Grimes key.data = &sp->se_process; 9078fae3551SRodney W. Grimes key.size = sizeof sp->se_process; 9088fae3551SRodney W. Grimes 9098fae3551SRodney W. Grimes if ((*session_db->del)(session_db, &key, 0)) 9108fae3551SRodney W. Grimes emergency("delete %d: %s", sp->se_process, strerror(errno)); 9118fae3551SRodney W. Grimes } 9128fae3551SRodney W. Grimes 9138fae3551SRodney W. Grimes /* 9148fae3551SRodney W. Grimes * Look up a login session by pid. 9158fae3551SRodney W. Grimes */ 9168fae3551SRodney W. Grimes session_t * 9178fae3551SRodney W. Grimes #ifdef __STDC__ 9188fae3551SRodney W. Grimes find_session(pid_t pid) 9198fae3551SRodney W. Grimes #else 9208fae3551SRodney W. Grimes find_session(pid) 9218fae3551SRodney W. Grimes pid_t pid; 9228fae3551SRodney W. Grimes #endif 9238fae3551SRodney W. Grimes { 9248fae3551SRodney W. Grimes DBT key; 9258fae3551SRodney W. Grimes DBT data; 9268fae3551SRodney W. Grimes session_t *ret; 9278fae3551SRodney W. Grimes 9288fae3551SRodney W. Grimes key.data = &pid; 9298fae3551SRodney W. Grimes key.size = sizeof pid; 9308fae3551SRodney W. Grimes if ((*session_db->get)(session_db, &key, &data, 0) != 0) 9318fae3551SRodney W. Grimes return 0; 9328fae3551SRodney W. Grimes bcopy(data.data, (char *)&ret, sizeof(ret)); 9338fae3551SRodney W. Grimes return ret; 9348fae3551SRodney W. Grimes } 9358fae3551SRodney W. Grimes 9368fae3551SRodney W. Grimes /* 9378fae3551SRodney W. Grimes * Construct an argument vector from a command line. 9388fae3551SRodney W. Grimes */ 9398fae3551SRodney W. Grimes char ** 9408fae3551SRodney W. Grimes construct_argv(command) 9418fae3551SRodney W. Grimes char *command; 9428fae3551SRodney W. Grimes { 94381ab7fb2SAndrey A. Chernov char *strk (char *); 9448fae3551SRodney W. Grimes register int argc = 0; 9458fae3551SRodney W. Grimes register char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1) 9468fae3551SRodney W. Grimes * sizeof (char *)); 9478fae3551SRodney W. Grimes 9486be40c95SRuslan Ermilov if ((argv[argc++] = strk(command)) == 0) { 9496be40c95SRuslan Ermilov free(argv); 9506be40c95SRuslan Ermilov return (NULL); 9516be40c95SRuslan Ermilov } 9528889c700SDavid Nugent while ((argv[argc++] = strk((char *) 0)) != NULL) 9538fae3551SRodney W. Grimes continue; 9548fae3551SRodney W. Grimes return argv; 9558fae3551SRodney W. Grimes } 9568fae3551SRodney W. Grimes 9578fae3551SRodney W. Grimes /* 9588fae3551SRodney W. Grimes * Deallocate a session descriptor. 9598fae3551SRodney W. Grimes */ 9608fae3551SRodney W. Grimes void 9618fae3551SRodney W. Grimes free_session(sp) 9628fae3551SRodney W. Grimes register session_t *sp; 9638fae3551SRodney W. Grimes { 9648fae3551SRodney W. Grimes free(sp->se_device); 9658fae3551SRodney W. Grimes if (sp->se_getty) { 9668fae3551SRodney W. Grimes free(sp->se_getty); 967b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 9688fae3551SRodney W. Grimes free(sp->se_getty_argv); 9698fae3551SRodney W. Grimes } 9708fae3551SRodney W. Grimes if (sp->se_window) { 9718fae3551SRodney W. Grimes free(sp->se_window); 972b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 9738fae3551SRodney W. Grimes free(sp->se_window_argv); 9748fae3551SRodney W. Grimes } 975b5df27e2SAndrey A. Chernov if (sp->se_type) 976b5df27e2SAndrey A. Chernov free(sp->se_type); 9778fae3551SRodney W. Grimes free(sp); 9788fae3551SRodney W. Grimes } 9798fae3551SRodney W. Grimes 9808fae3551SRodney W. Grimes /* 9818fae3551SRodney W. Grimes * Allocate a new session descriptor. 982b0b670eeSAlfred Perlstein * Mark it SE_PRESENT. 9838fae3551SRodney W. Grimes */ 9848fae3551SRodney W. Grimes session_t * 9858fae3551SRodney W. Grimes new_session(sprev, session_index, typ) 9868fae3551SRodney W. Grimes session_t *sprev; 9878fae3551SRodney W. Grimes int session_index; 9888fae3551SRodney W. Grimes register struct ttyent *typ; 9898fae3551SRodney W. Grimes { 9908fae3551SRodney W. Grimes register session_t *sp; 9914cbf8903SPaul Traina int fd; 9928fae3551SRodney W. Grimes 9938fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 || 9948fae3551SRodney W. Grimes typ->ty_name == 0 || 9958fae3551SRodney W. Grimes typ->ty_getty == 0) 9968fae3551SRodney W. Grimes return 0; 9978fae3551SRodney W. Grimes 9981054bb1eSAndrey A. Chernov sp = (session_t *) calloc(1, sizeof (session_t)); 9998fae3551SRodney W. Grimes 10008fae3551SRodney W. Grimes sp->se_index = session_index; 1001b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT; 10028fae3551SRodney W. Grimes 10038fae3551SRodney W. Grimes sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name)); 10048fae3551SRodney W. Grimes (void) sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name); 10058fae3551SRodney W. Grimes 10064cbf8903SPaul Traina /* 10074cbf8903SPaul Traina * Attempt to open the device, if we get "device not configured" 10084cbf8903SPaul Traina * then don't add the device to the session list. 10094cbf8903SPaul Traina */ 10104cbf8903SPaul Traina if ((fd = open(sp->se_device, O_RDONLY | O_NONBLOCK, 0)) < 0) { 10114cbf8903SPaul Traina if (errno == ENXIO) { 10124cbf8903SPaul Traina free_session(sp); 10134cbf8903SPaul Traina return (0); 10144cbf8903SPaul Traina } 10154cbf8903SPaul Traina } else 10164cbf8903SPaul Traina close(fd); 10174cbf8903SPaul Traina 10188fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) { 10198fae3551SRodney W. Grimes free_session(sp); 10208fae3551SRodney W. Grimes return (0); 10218fae3551SRodney W. Grimes } 10228fae3551SRodney W. Grimes 10238fae3551SRodney W. Grimes sp->se_next = 0; 10248fae3551SRodney W. Grimes if (sprev == 0) { 10258fae3551SRodney W. Grimes sessions = sp; 10268fae3551SRodney W. Grimes sp->se_prev = 0; 10278fae3551SRodney W. Grimes } else { 10288fae3551SRodney W. Grimes sprev->se_next = sp; 10298fae3551SRodney W. Grimes sp->se_prev = sprev; 10308fae3551SRodney W. Grimes } 10318fae3551SRodney W. Grimes 10328fae3551SRodney W. Grimes return sp; 10338fae3551SRodney W. Grimes } 10348fae3551SRodney W. Grimes 10358fae3551SRodney W. Grimes /* 10368fae3551SRodney W. Grimes * Calculate getty and if useful window argv vectors. 10378fae3551SRodney W. Grimes */ 10388fae3551SRodney W. Grimes int 10398fae3551SRodney W. Grimes setupargv(sp, typ) 10408fae3551SRodney W. Grimes session_t *sp; 10418fae3551SRodney W. Grimes struct ttyent *typ; 10428fae3551SRodney W. Grimes { 10438fae3551SRodney W. Grimes 10448fae3551SRodney W. Grimes if (sp->se_getty) { 10458fae3551SRodney W. Grimes free(sp->se_getty); 1046b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 10478fae3551SRodney W. Grimes free(sp->se_getty_argv); 10488fae3551SRodney W. Grimes } 10498fae3551SRodney W. Grimes sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2); 10508fae3551SRodney W. Grimes (void) sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name); 1051b5df27e2SAndrey A. Chernov sp->se_getty_argv_space = strdup(sp->se_getty); 1052b5df27e2SAndrey A. Chernov sp->se_getty_argv = construct_argv(sp->se_getty_argv_space); 10538fae3551SRodney W. Grimes if (sp->se_getty_argv == 0) { 10548fae3551SRodney W. Grimes warning("can't parse getty for port %s", sp->se_device); 10558fae3551SRodney W. Grimes free(sp->se_getty); 1056b5df27e2SAndrey A. Chernov free(sp->se_getty_argv_space); 1057b5df27e2SAndrey A. Chernov sp->se_getty = sp->se_getty_argv_space = 0; 10588fae3551SRodney W. Grimes return (0); 10598fae3551SRodney W. Grimes } 1060b5df27e2SAndrey A. Chernov if (sp->se_window) { 10618fae3551SRodney W. Grimes free(sp->se_window); 1062b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 1063b5df27e2SAndrey A. Chernov free(sp->se_window_argv); 1064b5df27e2SAndrey A. Chernov } 1065b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0; 1066b5df27e2SAndrey A. Chernov sp->se_window_argv = 0; 1067b5df27e2SAndrey A. Chernov if (typ->ty_window) { 10688fae3551SRodney W. Grimes sp->se_window = strdup(typ->ty_window); 1069b5df27e2SAndrey A. Chernov sp->se_window_argv_space = strdup(sp->se_window); 1070b5df27e2SAndrey A. Chernov sp->se_window_argv = construct_argv(sp->se_window_argv_space); 10718fae3551SRodney W. Grimes if (sp->se_window_argv == 0) { 10728fae3551SRodney W. Grimes warning("can't parse window for port %s", 10738fae3551SRodney W. Grimes sp->se_device); 1074b5df27e2SAndrey A. Chernov free(sp->se_window_argv_space); 10758fae3551SRodney W. Grimes free(sp->se_window); 1076b5df27e2SAndrey A. Chernov sp->se_window = sp->se_window_argv_space = 0; 10778fae3551SRodney W. Grimes return (0); 10788fae3551SRodney W. Grimes } 10798fae3551SRodney W. Grimes } 1080b5df27e2SAndrey A. Chernov if (sp->se_type) 1081b5df27e2SAndrey A. Chernov free(sp->se_type); 1082b5df27e2SAndrey A. Chernov sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0; 10838fae3551SRodney W. Grimes return (1); 10848fae3551SRodney W. Grimes } 10858fae3551SRodney W. Grimes 10868fae3551SRodney W. Grimes /* 10878fae3551SRodney W. Grimes * Walk the list of ttys and create sessions for each active line. 10888fae3551SRodney W. Grimes */ 10898fae3551SRodney W. Grimes state_func_t 10908fae3551SRodney W. Grimes read_ttys() 10918fae3551SRodney W. Grimes { 10928fae3551SRodney W. Grimes int session_index = 0; 10938fae3551SRodney W. Grimes register session_t *sp, *snext; 10948fae3551SRodney W. Grimes register struct ttyent *typ; 10958fae3551SRodney W. Grimes 10968fae3551SRodney W. Grimes /* 10978fae3551SRodney W. Grimes * Destroy any previous session state. 10988fae3551SRodney W. Grimes * There shouldn't be any, but just in case... 10998fae3551SRodney W. Grimes */ 11008fae3551SRodney W. Grimes for (sp = sessions; sp; sp = snext) { 11018fae3551SRodney W. Grimes if (sp->se_process) 11028fae3551SRodney W. Grimes clear_session_logs(sp); 11038fae3551SRodney W. Grimes snext = sp->se_next; 11048fae3551SRodney W. Grimes free_session(sp); 11058fae3551SRodney W. Grimes } 11068fae3551SRodney W. Grimes sessions = 0; 11078fae3551SRodney W. Grimes if (start_session_db()) 11088fae3551SRodney W. Grimes return (state_func_t) single_user; 11098fae3551SRodney W. Grimes 11108fae3551SRodney W. Grimes /* 11118fae3551SRodney W. Grimes * Allocate a session entry for each active port. 11128fae3551SRodney W. Grimes * Note that sp starts at 0. 11138fae3551SRodney W. Grimes */ 11148889c700SDavid Nugent while ((typ = getttyent()) != NULL) 11158889c700SDavid Nugent if ((snext = new_session(sp, ++session_index, typ)) != NULL) 11168fae3551SRodney W. Grimes sp = snext; 11178fae3551SRodney W. Grimes 11188fae3551SRodney W. Grimes endttyent(); 11198fae3551SRodney W. Grimes 11208fae3551SRodney W. Grimes return (state_func_t) multi_user; 11218fae3551SRodney W. Grimes } 11228fae3551SRodney W. Grimes 11238fae3551SRodney W. Grimes /* 11248fae3551SRodney W. Grimes * Start a window system running. 11258fae3551SRodney W. Grimes */ 11268fae3551SRodney W. Grimes void 11278fae3551SRodney W. Grimes start_window_system(sp) 11288fae3551SRodney W. Grimes session_t *sp; 11298fae3551SRodney W. Grimes { 11308fae3551SRodney W. Grimes pid_t pid; 11318fae3551SRodney W. Grimes sigset_t mask; 1132b5df27e2SAndrey A. Chernov char term[64], *env[2]; 11338fae3551SRodney W. Grimes 11348fae3551SRodney W. Grimes if ((pid = fork()) == -1) { 11358fae3551SRodney W. Grimes emergency("can't fork for window system on port %s: %m", 11368fae3551SRodney W. Grimes sp->se_device); 11378fae3551SRodney W. Grimes /* hope that getty fails and we can try again */ 11388fae3551SRodney W. Grimes return; 11398fae3551SRodney W. Grimes } 11408fae3551SRodney W. Grimes 11418fae3551SRodney W. Grimes if (pid) 11428fae3551SRodney W. Grimes return; 11438fae3551SRodney W. Grimes 11448fae3551SRodney W. Grimes sigemptyset(&mask); 11458fae3551SRodney W. Grimes sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 11468fae3551SRodney W. Grimes 11478fae3551SRodney W. Grimes if (setsid() < 0) 11488fae3551SRodney W. Grimes emergency("setsid failed (window) %m"); 11498fae3551SRodney W. Grimes 11501ef60eb1SDavid Nugent #ifdef LOGIN_CAP 11511ef60eb1SDavid Nugent setprocresources(RESOURCE_WINDOW); 11521ef60eb1SDavid Nugent #endif 1153b5df27e2SAndrey A. Chernov if (sp->se_type) { 1154b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */ 1155b5df27e2SAndrey A. Chernov strcpy(term, "TERM="); 115633a20f82SDavid Greenman strncat(term, sp->se_type, sizeof(term) - 6); 1157b5df27e2SAndrey A. Chernov env[0] = term; 1158b5df27e2SAndrey A. Chernov env[1] = 0; 1159b5df27e2SAndrey A. Chernov } 1160b5df27e2SAndrey A. Chernov else 1161b5df27e2SAndrey A. Chernov env[0] = 0; 1162b5df27e2SAndrey A. Chernov execve(sp->se_window_argv[0], sp->se_window_argv, env); 11638fae3551SRodney W. Grimes stall("can't exec window system '%s' for port %s: %m", 11648fae3551SRodney W. Grimes sp->se_window_argv[0], sp->se_device); 11658fae3551SRodney W. Grimes _exit(1); 11668fae3551SRodney W. Grimes } 11678fae3551SRodney W. Grimes 11688fae3551SRodney W. Grimes /* 11698fae3551SRodney W. Grimes * Start a login session running. 11708fae3551SRodney W. Grimes */ 11718fae3551SRodney W. Grimes pid_t 11728fae3551SRodney W. Grimes start_getty(sp) 11738fae3551SRodney W. Grimes session_t *sp; 11748fae3551SRodney W. Grimes { 11758fae3551SRodney W. Grimes pid_t pid; 11768fae3551SRodney W. Grimes sigset_t mask; 11778fae3551SRodney W. Grimes time_t current_time = time((time_t *) 0); 1178228d7ef2SAndrey A. Chernov int too_quick = 0; 1179b5df27e2SAndrey A. Chernov char term[64], *env[2]; 11808fae3551SRodney W. Grimes 1181bb2e87c4SMike Pritchard if (current_time >= sp->se_started && 1182228d7ef2SAndrey A. Chernov current_time - sp->se_started < GETTY_SPACING) { 1183228d7ef2SAndrey A. Chernov if (++sp->se_nspace > GETTY_NSPACE) { 1184228d7ef2SAndrey A. Chernov sp->se_nspace = 0; 1185228d7ef2SAndrey A. Chernov too_quick = 1; 1186228d7ef2SAndrey A. Chernov } 1187228d7ef2SAndrey A. Chernov } else 1188228d7ef2SAndrey A. Chernov sp->se_nspace = 0; 1189228d7ef2SAndrey A. Chernov 11908fae3551SRodney W. Grimes /* 11918fae3551SRodney W. Grimes * fork(), not vfork() -- we can't afford to block. 11928fae3551SRodney W. Grimes */ 11938fae3551SRodney W. Grimes if ((pid = fork()) == -1) { 11948fae3551SRodney W. Grimes emergency("can't fork for getty on port %s: %m", sp->se_device); 11958fae3551SRodney W. Grimes return -1; 11968fae3551SRodney W. Grimes } 11978fae3551SRodney W. Grimes 11988fae3551SRodney W. Grimes if (pid) 11998fae3551SRodney W. Grimes return pid; 12008fae3551SRodney W. Grimes 1201228d7ef2SAndrey A. Chernov if (too_quick) { 1202b5df27e2SAndrey A. Chernov warning("getty repeating too quickly on port %s, sleeping %d secs", 1203b5df27e2SAndrey A. Chernov sp->se_device, GETTY_SLEEP); 12048fae3551SRodney W. Grimes sleep((unsigned) GETTY_SLEEP); 12058fae3551SRodney W. Grimes } 12068fae3551SRodney W. Grimes 12078fae3551SRodney W. Grimes if (sp->se_window) { 12088fae3551SRodney W. Grimes start_window_system(sp); 12098fae3551SRodney W. Grimes sleep(WINDOW_WAIT); 12108fae3551SRodney W. Grimes } 12118fae3551SRodney W. Grimes 12128fae3551SRodney W. Grimes sigemptyset(&mask); 12138fae3551SRodney W. Grimes sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); 12148fae3551SRodney W. Grimes 12151ef60eb1SDavid Nugent #ifdef LOGIN_CAP 12161ef60eb1SDavid Nugent setprocresources(RESOURCE_GETTY); 12171ef60eb1SDavid Nugent #endif 1218b5df27e2SAndrey A. Chernov if (sp->se_type) { 1219b5df27e2SAndrey A. Chernov /* Don't use malloc after fork */ 1220b5df27e2SAndrey A. Chernov strcpy(term, "TERM="); 122133a20f82SDavid Greenman strncat(term, sp->se_type, sizeof(term) - 6); 1222b5df27e2SAndrey A. Chernov env[0] = term; 1223b5df27e2SAndrey A. Chernov env[1] = 0; 1224b5df27e2SAndrey A. Chernov } 1225b5df27e2SAndrey A. Chernov else 1226b5df27e2SAndrey A. Chernov env[0] = 0; 1227b5df27e2SAndrey A. Chernov execve(sp->se_getty_argv[0], sp->se_getty_argv, env); 12288fae3551SRodney W. Grimes stall("can't exec getty '%s' for port %s: %m", 12298fae3551SRodney W. Grimes sp->se_getty_argv[0], sp->se_device); 12308fae3551SRodney W. Grimes _exit(1); 12318fae3551SRodney W. Grimes } 12328fae3551SRodney W. Grimes 12338fae3551SRodney W. Grimes /* 12348fae3551SRodney W. Grimes * Collect exit status for a child. 12358fae3551SRodney W. Grimes * If an exiting login, start a new login running. 12368fae3551SRodney W. Grimes */ 12378fae3551SRodney W. Grimes void 12388fae3551SRodney W. Grimes #ifdef __STDC__ 12398fae3551SRodney W. Grimes collect_child(pid_t pid) 12408fae3551SRodney W. Grimes #else 12418fae3551SRodney W. Grimes collect_child(pid) 12428fae3551SRodney W. Grimes pid_t pid; 12438fae3551SRodney W. Grimes #endif 12448fae3551SRodney W. Grimes { 12458fae3551SRodney W. Grimes register session_t *sp, *sprev, *snext; 12468fae3551SRodney W. Grimes 12478fae3551SRodney W. Grimes if (! sessions) 12488fae3551SRodney W. Grimes return; 12498fae3551SRodney W. Grimes 12508fae3551SRodney W. Grimes if (! (sp = find_session(pid))) 12518fae3551SRodney W. Grimes return; 12528fae3551SRodney W. Grimes 12538fae3551SRodney W. Grimes clear_session_logs(sp); 12548fae3551SRodney W. Grimes del_session(sp); 12558fae3551SRodney W. Grimes sp->se_process = 0; 12568fae3551SRodney W. Grimes 12578fae3551SRodney W. Grimes if (sp->se_flags & SE_SHUTDOWN) { 12588889c700SDavid Nugent if ((sprev = sp->se_prev) != NULL) 12598fae3551SRodney W. Grimes sprev->se_next = sp->se_next; 12608fae3551SRodney W. Grimes else 12618fae3551SRodney W. Grimes sessions = sp->se_next; 12628889c700SDavid Nugent if ((snext = sp->se_next) != NULL) 12638fae3551SRodney W. Grimes snext->se_prev = sp->se_prev; 12648fae3551SRodney W. Grimes free_session(sp); 12658fae3551SRodney W. Grimes return; 12668fae3551SRodney W. Grimes } 12678fae3551SRodney W. Grimes 12688fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) { 12698fae3551SRodney W. Grimes /* serious trouble */ 12708fae3551SRodney W. Grimes requested_transition = clean_ttys; 12718fae3551SRodney W. Grimes return; 12728fae3551SRodney W. Grimes } 12738fae3551SRodney W. Grimes 12748fae3551SRodney W. Grimes sp->se_process = pid; 12758fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0); 12768fae3551SRodney W. Grimes add_session(sp); 12778fae3551SRodney W. Grimes } 12788fae3551SRodney W. Grimes 12798fae3551SRodney W. Grimes /* 12808fae3551SRodney W. Grimes * Catch a signal and request a state transition. 12818fae3551SRodney W. Grimes */ 12828fae3551SRodney W. Grimes void 12838fae3551SRodney W. Grimes transition_handler(sig) 12848fae3551SRodney W. Grimes int sig; 12858fae3551SRodney W. Grimes { 12868fae3551SRodney W. Grimes 12878fae3551SRodney W. Grimes switch (sig) { 12888fae3551SRodney W. Grimes case SIGHUP: 12898fae3551SRodney W. Grimes requested_transition = clean_ttys; 12908fae3551SRodney W. Grimes break; 1291a0a549c7SRuslan Ermilov case SIGUSR2: 1292a0a549c7SRuslan Ermilov howto = RB_POWEROFF; 1293a0a549c7SRuslan Ermilov case SIGUSR1: 1294a0a549c7SRuslan Ermilov howto |= RB_HALT; 1295e460cfd3SNate Williams case SIGINT: 1296db8ad19dSJordan K. Hubbard Reboot = TRUE; 12978fae3551SRodney W. Grimes case SIGTERM: 12988fae3551SRodney W. Grimes requested_transition = death; 12998fae3551SRodney W. Grimes break; 13008fae3551SRodney W. Grimes case SIGTSTP: 13018fae3551SRodney W. Grimes requested_transition = catatonia; 13028fae3551SRodney W. Grimes break; 13038fae3551SRodney W. Grimes default: 13048fae3551SRodney W. Grimes requested_transition = 0; 13058fae3551SRodney W. Grimes break; 13068fae3551SRodney W. Grimes } 13078fae3551SRodney W. Grimes } 13088fae3551SRodney W. Grimes 13098fae3551SRodney W. Grimes /* 13108fae3551SRodney W. Grimes * Take the system multiuser. 13118fae3551SRodney W. Grimes */ 13128fae3551SRodney W. Grimes state_func_t 13138fae3551SRodney W. Grimes multi_user() 13148fae3551SRodney W. Grimes { 13158fae3551SRodney W. Grimes pid_t pid; 13168fae3551SRodney W. Grimes register session_t *sp; 13178fae3551SRodney W. Grimes 13188fae3551SRodney W. Grimes requested_transition = 0; 13198fae3551SRodney W. Grimes 13208fae3551SRodney W. Grimes /* 13218fae3551SRodney W. Grimes * If the administrator has not set the security level to -1 13228fae3551SRodney W. Grimes * to indicate that the kernel should not run multiuser in secure 13238fae3551SRodney W. Grimes * mode, and the run script has not set a higher level of security 13248fae3551SRodney W. Grimes * than level 1, then put the kernel into secure mode. 13258fae3551SRodney W. Grimes */ 13268fae3551SRodney W. Grimes if (getsecuritylevel() == 0) 13278fae3551SRodney W. Grimes setsecuritylevel(1); 13288fae3551SRodney W. Grimes 13298fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next) { 13308fae3551SRodney W. Grimes if (sp->se_process) 13318fae3551SRodney W. Grimes continue; 13328fae3551SRodney W. Grimes if ((pid = start_getty(sp)) == -1) { 13338fae3551SRodney W. Grimes /* serious trouble */ 13348fae3551SRodney W. Grimes requested_transition = clean_ttys; 13358fae3551SRodney W. Grimes break; 13368fae3551SRodney W. Grimes } 13378fae3551SRodney W. Grimes sp->se_process = pid; 13388fae3551SRodney W. Grimes sp->se_started = time((time_t *) 0); 13398fae3551SRodney W. Grimes add_session(sp); 13408fae3551SRodney W. Grimes } 13418fae3551SRodney W. Grimes 13428fae3551SRodney W. Grimes while (!requested_transition) 13438fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *) 0, 0)) != -1) 13448fae3551SRodney W. Grimes collect_child(pid); 13458fae3551SRodney W. Grimes 13468fae3551SRodney W. Grimes return (state_func_t) requested_transition; 13478fae3551SRodney W. Grimes } 13488fae3551SRodney W. Grimes 13498fae3551SRodney W. Grimes /* 1350b0b670eeSAlfred Perlstein * This is an (n*2)+(n^2) algorithm. We hope it isn't run often... 13518fae3551SRodney W. Grimes */ 13528fae3551SRodney W. Grimes state_func_t 13538fae3551SRodney W. Grimes clean_ttys() 13548fae3551SRodney W. Grimes { 13558fae3551SRodney W. Grimes register session_t *sp, *sprev; 13568fae3551SRodney W. Grimes register struct ttyent *typ; 13578fae3551SRodney W. Grimes register int session_index = 0; 13588fae3551SRodney W. Grimes register int devlen; 1359b5df27e2SAndrey A. Chernov char *old_getty, *old_window, *old_type; 13608fae3551SRodney W. Grimes 13618fae3551SRodney W. Grimes if (! sessions) 13628fae3551SRodney W. Grimes return (state_func_t) multi_user; 13638fae3551SRodney W. Grimes 1364b0b670eeSAlfred Perlstein /* 1365b0b670eeSAlfred Perlstein * mark all sessions for death, (!SE_PRESENT) 1366b0b670eeSAlfred Perlstein * as we find or create new ones they'll be marked as keepers, 1367b0b670eeSAlfred Perlstein * we'll later nuke all the ones not found in /etc/ttys 1368b0b670eeSAlfred Perlstein */ 1369b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next) 1370b0b670eeSAlfred Perlstein sp->se_flags &= ~SE_PRESENT; 1371b0b670eeSAlfred Perlstein 13728fae3551SRodney W. Grimes devlen = sizeof(_PATH_DEV) - 1; 13738889c700SDavid Nugent while ((typ = getttyent()) != NULL) { 13748fae3551SRodney W. Grimes ++session_index; 13758fae3551SRodney W. Grimes 13768fae3551SRodney W. Grimes for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next) 13778fae3551SRodney W. Grimes if (strcmp(typ->ty_name, sp->se_device + devlen) == 0) 13788fae3551SRodney W. Grimes break; 13798fae3551SRodney W. Grimes 13808fae3551SRodney W. Grimes if (sp) { 1381b0b670eeSAlfred Perlstein /* we want this one to live */ 1382b0b670eeSAlfred Perlstein sp->se_flags |= SE_PRESENT; 13838fae3551SRodney W. Grimes if (sp->se_index != session_index) { 13848fae3551SRodney W. Grimes warning("port %s changed utmp index from %d to %d", 13858fae3551SRodney W. Grimes sp->se_device, sp->se_index, 13868fae3551SRodney W. Grimes session_index); 13878fae3551SRodney W. Grimes sp->se_index = session_index; 13888fae3551SRodney W. Grimes } 13898fae3551SRodney W. Grimes if ((typ->ty_status & TTY_ON) == 0 || 13908fae3551SRodney W. Grimes typ->ty_getty == 0) { 13918fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 13928fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP); 13938fae3551SRodney W. Grimes continue; 13948fae3551SRodney W. Grimes } 13958fae3551SRodney W. Grimes sp->se_flags &= ~SE_SHUTDOWN; 1396b5df27e2SAndrey A. Chernov old_getty = sp->se_getty ? strdup(sp->se_getty) : 0; 1397b5df27e2SAndrey A. Chernov old_window = sp->se_window ? strdup(sp->se_window) : 0; 1398b5df27e2SAndrey A. Chernov old_type = sp->se_type ? strdup(sp->se_type) : 0; 13998fae3551SRodney W. Grimes if (setupargv(sp, typ) == 0) { 14008fae3551SRodney W. Grimes warning("can't parse getty for port %s", 14018fae3551SRodney W. Grimes sp->se_device); 14028fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 14038fae3551SRodney W. Grimes kill(sp->se_process, SIGHUP); 14048fae3551SRodney W. Grimes } 1405b5df27e2SAndrey A. Chernov else if ( !old_getty 14068889c700SDavid Nugent || (!old_type && sp->se_type) 14078889c700SDavid Nugent || (old_type && !sp->se_type) 14088889c700SDavid Nugent || (!old_window && sp->se_window) 14098889c700SDavid Nugent || (old_window && !sp->se_window) 14108889c700SDavid Nugent || (strcmp(old_getty, sp->se_getty) != 0) 14118889c700SDavid Nugent || (old_window && strcmp(old_window, sp->se_window) != 0) 14128889c700SDavid Nugent || (old_type && strcmp(old_type, sp->se_type) != 0) 1413b5df27e2SAndrey A. Chernov ) { 1414b5df27e2SAndrey A. Chernov /* Don't set SE_SHUTDOWN here */ 1415b5df27e2SAndrey A. Chernov sp->se_nspace = 0; 1416b5df27e2SAndrey A. Chernov sp->se_started = 0; 1417b5df27e2SAndrey A. Chernov kill(sp->se_process, SIGHUP); 1418b5df27e2SAndrey A. Chernov } 1419b5df27e2SAndrey A. Chernov if (old_getty) 1420b5df27e2SAndrey A. Chernov free(old_getty); 14212d887af5SMike Heffner if (old_window) 1422b5df27e2SAndrey A. Chernov free(old_window); 1423b5df27e2SAndrey A. Chernov if (old_type) 1424b5df27e2SAndrey A. Chernov free(old_type); 14258fae3551SRodney W. Grimes continue; 14268fae3551SRodney W. Grimes } 14278fae3551SRodney W. Grimes 14288fae3551SRodney W. Grimes new_session(sprev, session_index, typ); 14298fae3551SRodney W. Grimes } 14308fae3551SRodney W. Grimes 14318fae3551SRodney W. Grimes endttyent(); 14328fae3551SRodney W. Grimes 1433b0b670eeSAlfred Perlstein /* 1434b0b670eeSAlfred Perlstein * sweep through and kill all deleted sessions 1435b0b670eeSAlfred Perlstein * ones who's /etc/ttys line was deleted (SE_PRESENT unset) 1436b0b670eeSAlfred Perlstein */ 1437b0b670eeSAlfred Perlstein for (sp = sessions; sp != NULL; sp = sp->se_next) { 1438b0b670eeSAlfred Perlstein if ((sp->se_flags & SE_PRESENT) == 0) { 1439b0b670eeSAlfred Perlstein sp->se_flags |= SE_SHUTDOWN; 1440b0b670eeSAlfred Perlstein kill(sp->se_process, SIGHUP); 1441b0b670eeSAlfred Perlstein } 1442b0b670eeSAlfred Perlstein } 1443b0b670eeSAlfred Perlstein 14448fae3551SRodney W. Grimes return (state_func_t) multi_user; 14458fae3551SRodney W. Grimes } 14468fae3551SRodney W. Grimes 14478fae3551SRodney W. Grimes /* 14488fae3551SRodney W. Grimes * Block further logins. 14498fae3551SRodney W. Grimes */ 14508fae3551SRodney W. Grimes state_func_t 14518fae3551SRodney W. Grimes catatonia() 14528fae3551SRodney W. Grimes { 14538fae3551SRodney W. Grimes register session_t *sp; 14548fae3551SRodney W. Grimes 14558fae3551SRodney W. Grimes for (sp = sessions; sp; sp = sp->se_next) 14568fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 14578fae3551SRodney W. Grimes 14588fae3551SRodney W. Grimes return (state_func_t) multi_user; 14598fae3551SRodney W. Grimes } 14608fae3551SRodney W. Grimes 14618fae3551SRodney W. Grimes /* 14628fae3551SRodney W. Grimes * Note SIGALRM. 14638fae3551SRodney W. Grimes */ 14648fae3551SRodney W. Grimes void 14658fae3551SRodney W. Grimes alrm_handler(sig) 14668fae3551SRodney W. Grimes int sig; 14678fae3551SRodney W. Grimes { 14688889c700SDavid Nugent (void)sig; 14698fae3551SRodney W. Grimes clang = 1; 14708fae3551SRodney W. Grimes } 14718fae3551SRodney W. Grimes 14728fae3551SRodney W. Grimes /* 14738fae3551SRodney W. Grimes * Bring the system down to single user. 14748fae3551SRodney W. Grimes */ 14758fae3551SRodney W. Grimes state_func_t 14768fae3551SRodney W. Grimes death() 14778fae3551SRodney W. Grimes { 14788fae3551SRodney W. Grimes register session_t *sp; 14798fae3551SRodney W. Grimes register int i; 14808fae3551SRodney W. Grimes pid_t pid; 1481c3d7c52eSAndrey A. Chernov static const int death_sigs[2] = { SIGTERM, SIGKILL }; 14828fae3551SRodney W. Grimes 14833f31fb33SAndrey A. Chernov /* NB: should send a message to the session logger to avoid blocking. */ 14843f31fb33SAndrey A. Chernov logwtmp("~", "shutdown", ""); 14853f31fb33SAndrey A. Chernov 14861054bb1eSAndrey A. Chernov for (sp = sessions; sp; sp = sp->se_next) { 14878fae3551SRodney W. Grimes sp->se_flags |= SE_SHUTDOWN; 148825cf4a54SAndrey A. Chernov kill(sp->se_process, SIGHUP); 14891054bb1eSAndrey A. Chernov } 14908fae3551SRodney W. Grimes 14918889c700SDavid Nugent /* Try to run the rc.shutdown script within a period of time */ 149225cf4a54SAndrey A. Chernov (void) runshutdown(); 14938889c700SDavid Nugent 1494c3d7c52eSAndrey A. Chernov for (i = 0; i < 2; ++i) { 14958fae3551SRodney W. Grimes if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH) 14968fae3551SRodney W. Grimes return (state_func_t) single_user; 14978fae3551SRodney W. Grimes 14988fae3551SRodney W. Grimes clang = 0; 14998fae3551SRodney W. Grimes alarm(DEATH_WATCH); 15008fae3551SRodney W. Grimes do 15018fae3551SRodney W. Grimes if ((pid = waitpid(-1, (int *)0, 0)) != -1) 15028fae3551SRodney W. Grimes collect_child(pid); 15038fae3551SRodney W. Grimes while (clang == 0 && errno != ECHILD); 15048fae3551SRodney W. Grimes 15058fae3551SRodney W. Grimes if (errno == ECHILD) 15068fae3551SRodney W. Grimes return (state_func_t) single_user; 15078fae3551SRodney W. Grimes } 15088fae3551SRodney W. Grimes 15098fae3551SRodney W. Grimes warning("some processes would not die; ps axl advised"); 15108fae3551SRodney W. Grimes 15118fae3551SRodney W. Grimes return (state_func_t) single_user; 15128fae3551SRodney W. Grimes } 15138889c700SDavid Nugent 15148889c700SDavid Nugent /* 15158889c700SDavid Nugent * Run the system shutdown script. 15168889c700SDavid Nugent * 15178889c700SDavid Nugent * Exit codes: XXX I should document more 15188889c700SDavid Nugent * -2 shutdown script terminated abnormally 15198889c700SDavid Nugent * -1 fatal error - can't run script 15208889c700SDavid Nugent * 0 good. 15218889c700SDavid Nugent * >0 some error (exit code) 15228889c700SDavid Nugent */ 15238889c700SDavid Nugent int 15248889c700SDavid Nugent runshutdown() 15258889c700SDavid Nugent { 15268889c700SDavid Nugent pid_t pid, wpid; 15278889c700SDavid Nugent int status; 15288889c700SDavid Nugent int shutdowntimeout; 15298889c700SDavid Nugent size_t len; 1530a69497d7SMatthew Dillon char *argv[4]; 15318889c700SDavid Nugent struct sigaction sa; 153286bf62dcSDavid Nugent struct stat sb; 153386bf62dcSDavid Nugent 153486bf62dcSDavid Nugent /* 153586bf62dcSDavid Nugent * rc.shutdown is optional, so to prevent any unnecessary 153686bf62dcSDavid Nugent * complaints from the shell we simply don't run it if the 153786bf62dcSDavid Nugent * file does not exist. If the stat() here fails for other 153886bf62dcSDavid Nugent * reasons, we'll let the shell complain. 153986bf62dcSDavid Nugent */ 154086bf62dcSDavid Nugent if (stat(_PATH_RUNDOWN, &sb) == -1 && errno == ENOENT) 154186bf62dcSDavid Nugent return 0; 15428889c700SDavid Nugent 15438889c700SDavid Nugent if ((pid = fork()) == 0) { 15448889c700SDavid Nugent int fd; 15458889c700SDavid Nugent 154625cf4a54SAndrey A. Chernov /* Assume that init already grab console as ctty before */ 154725cf4a54SAndrey A. Chernov 15488889c700SDavid Nugent sigemptyset(&sa.sa_mask); 15498889c700SDavid Nugent sa.sa_flags = 0; 15508889c700SDavid Nugent sa.sa_handler = SIG_IGN; 15518889c700SDavid Nugent (void) sigaction(SIGTSTP, &sa, (struct sigaction *)0); 15528889c700SDavid Nugent (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); 15538889c700SDavid Nugent 15548889c700SDavid Nugent if ((fd = open(_PATH_CONSOLE, O_RDWR)) == -1) 15558889c700SDavid Nugent warning("can't open %s: %m", _PATH_CONSOLE); 15568889c700SDavid Nugent else { 155725cf4a54SAndrey A. Chernov (void) dup2(fd, 0); 15588889c700SDavid Nugent (void) dup2(fd, 1); 15598889c700SDavid Nugent (void) dup2(fd, 2); 156025cf4a54SAndrey A. Chernov if (fd > 2) 156125cf4a54SAndrey A. Chernov close(fd); 15628889c700SDavid Nugent } 15638889c700SDavid Nugent 15648889c700SDavid Nugent /* 15658889c700SDavid Nugent * Run the shutdown script. 15668889c700SDavid Nugent */ 15678889c700SDavid Nugent argv[0] = "sh"; 15688889c700SDavid Nugent argv[1] = _PATH_RUNDOWN; 1569a69497d7SMatthew Dillon if (Reboot) 1570a69497d7SMatthew Dillon argv[2] = "reboot"; 1571a69497d7SMatthew Dillon else 1572a69497d7SMatthew Dillon argv[2] = "single"; 1573a69497d7SMatthew Dillon argv[3] = 0; 15748889c700SDavid Nugent 15758889c700SDavid Nugent sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); 15768889c700SDavid Nugent 157725cf4a54SAndrey A. Chernov #ifdef LOGIN_CAP 157825cf4a54SAndrey A. Chernov setprocresources(RESOURCE_RC); 157925cf4a54SAndrey A. Chernov #endif 15808889c700SDavid Nugent execv(_PATH_BSHELL, argv); 15818889c700SDavid Nugent warning("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNDOWN); 15828889c700SDavid Nugent _exit(1); /* force single user mode */ 15838889c700SDavid Nugent } 15848889c700SDavid Nugent 15858889c700SDavid Nugent if (pid == -1) { 15868889c700SDavid Nugent emergency("can't fork for %s on %s: %m", 15878889c700SDavid Nugent _PATH_BSHELL, _PATH_RUNDOWN); 15888889c700SDavid Nugent while (waitpid(-1, (int *) 0, WNOHANG) > 0) 15898889c700SDavid Nugent continue; 15908889c700SDavid Nugent sleep(STALL_TIMEOUT); 15918889c700SDavid Nugent return -1; 15928889c700SDavid Nugent } 15938889c700SDavid Nugent 15948889c700SDavid Nugent len = sizeof(shutdowntimeout); 15958889c700SDavid Nugent if (sysctlbyname("kern.shutdown_timeout", 15968889c700SDavid Nugent &shutdowntimeout, 15978889c700SDavid Nugent &len, NULL, 0) == -1 || shutdowntimeout < 2) 15988889c700SDavid Nugent shutdowntimeout = DEATH_SCRIPT; 15998889c700SDavid Nugent alarm(shutdowntimeout); 16008889c700SDavid Nugent clang = 0; 16018889c700SDavid Nugent /* 16028889c700SDavid Nugent * Copied from single_user(). This is a bit paranoid. 16038889c700SDavid Nugent * Use the same ALRM handler. 16048889c700SDavid Nugent */ 16058889c700SDavid Nugent do { 16068889c700SDavid Nugent if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) 16078889c700SDavid Nugent collect_child(wpid); 16088889c700SDavid Nugent if (clang == 1) { 16098889c700SDavid Nugent /* we were waiting for the sub-shell */ 16108889c700SDavid Nugent kill(wpid, SIGTERM); 16118a8d42eeSJeroen Ruigrok van der Werven warning("timeout expired for %s on %s: %m; going to single user mode", 16128889c700SDavid Nugent _PATH_BSHELL, _PATH_RUNDOWN); 16138889c700SDavid Nugent return -1; 16148889c700SDavid Nugent } 16158889c700SDavid Nugent if (wpid == -1) { 16168889c700SDavid Nugent if (errno == EINTR) 16178889c700SDavid Nugent continue; 16188889c700SDavid Nugent warning("wait for %s on %s failed: %m; going to single user mode", 16198889c700SDavid Nugent _PATH_BSHELL, _PATH_RUNDOWN); 16208889c700SDavid Nugent return -1; 16218889c700SDavid Nugent } 16228889c700SDavid Nugent if (wpid == pid && WIFSTOPPED(status)) { 16238889c700SDavid Nugent warning("init: %s on %s stopped, restarting\n", 16248889c700SDavid Nugent _PATH_BSHELL, _PATH_RUNDOWN); 16258889c700SDavid Nugent kill(pid, SIGCONT); 16268889c700SDavid Nugent wpid = -1; 16278889c700SDavid Nugent } 16288889c700SDavid Nugent } while (wpid != pid && !clang); 16298889c700SDavid Nugent 16308889c700SDavid Nugent /* Turn off the alarm */ 16318889c700SDavid Nugent alarm(0); 16328889c700SDavid Nugent 16338889c700SDavid Nugent if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && 16348889c700SDavid Nugent requested_transition == catatonia) { 16358889c700SDavid Nugent /* 16368889c700SDavid Nugent * /etc/rc.shutdown executed /sbin/reboot; 16378889c700SDavid Nugent * wait for the end quietly 16388889c700SDavid Nugent */ 16398889c700SDavid Nugent sigset_t s; 16408889c700SDavid Nugent 16418889c700SDavid Nugent sigfillset(&s); 16428889c700SDavid Nugent for (;;) 16438889c700SDavid Nugent sigsuspend(&s); 16448889c700SDavid Nugent } 16458889c700SDavid Nugent 16468889c700SDavid Nugent if (!WIFEXITED(status)) { 16478889c700SDavid Nugent warning("%s on %s terminated abnormally, going to single user mode", 16488889c700SDavid Nugent _PATH_BSHELL, _PATH_RUNDOWN); 16498889c700SDavid Nugent return -2; 16508889c700SDavid Nugent } 16518889c700SDavid Nugent 16528889c700SDavid Nugent if ((status = WEXITSTATUS(status)) != 0) 16538889c700SDavid Nugent warning("%s returned status %d", _PATH_RUNDOWN, status); 16548889c700SDavid Nugent 16558889c700SDavid Nugent return status; 16568889c700SDavid Nugent } 16578889c700SDavid Nugent 165881ab7fb2SAndrey A. Chernov char * 165981ab7fb2SAndrey A. Chernov strk (char *p) 166081ab7fb2SAndrey A. Chernov { 166181ab7fb2SAndrey A. Chernov static char *t; 166281ab7fb2SAndrey A. Chernov char *q; 166381ab7fb2SAndrey A. Chernov int c; 166481ab7fb2SAndrey A. Chernov 166581ab7fb2SAndrey A. Chernov if (p) 166681ab7fb2SAndrey A. Chernov t = p; 166781ab7fb2SAndrey A. Chernov if (!t) 166881ab7fb2SAndrey A. Chernov return 0; 166981ab7fb2SAndrey A. Chernov 167081ab7fb2SAndrey A. Chernov c = *t; 167181ab7fb2SAndrey A. Chernov while (c == ' ' || c == '\t' ) 167281ab7fb2SAndrey A. Chernov c = *++t; 167381ab7fb2SAndrey A. Chernov if (!c) { 167481ab7fb2SAndrey A. Chernov t = 0; 167581ab7fb2SAndrey A. Chernov return 0; 167681ab7fb2SAndrey A. Chernov } 167781ab7fb2SAndrey A. Chernov q = t; 167881ab7fb2SAndrey A. Chernov if (c == '\'') { 167981ab7fb2SAndrey A. Chernov c = *++t; 168081ab7fb2SAndrey A. Chernov q = t; 168181ab7fb2SAndrey A. Chernov while (c && c != '\'') 168281ab7fb2SAndrey A. Chernov c = *++t; 168381ab7fb2SAndrey A. Chernov if (!c) /* unterminated string */ 168481ab7fb2SAndrey A. Chernov q = t = 0; 168581ab7fb2SAndrey A. Chernov else 168681ab7fb2SAndrey A. Chernov *t++ = 0; 168781ab7fb2SAndrey A. Chernov } else { 168881ab7fb2SAndrey A. Chernov while (c && c != ' ' && c != '\t' ) 168981ab7fb2SAndrey A. Chernov c = *++t; 169081ab7fb2SAndrey A. Chernov *t++ = 0; 169181ab7fb2SAndrey A. Chernov if (!c) 169281ab7fb2SAndrey A. Chernov t = 0; 169381ab7fb2SAndrey A. Chernov } 169481ab7fb2SAndrey A. Chernov return q; 169581ab7fb2SAndrey A. Chernov } 16961ef60eb1SDavid Nugent 16971ef60eb1SDavid Nugent #ifdef LOGIN_CAP 1698e82d5545SDavid Nugent void 1699e82d5545SDavid Nugent setprocresources(cname) 1700e82d5545SDavid Nugent const char *cname; 17011ef60eb1SDavid Nugent { 1702e82d5545SDavid Nugent login_cap_t *lc; 1703a2ee73bcSAndrey A. Chernov if ((lc = login_getclassbyname(cname, NULL)) != NULL) { 1704e82d5545SDavid Nugent setusercontext(lc, (struct passwd*)NULL, 0, LOGIN_SETPRIORITY|LOGIN_SETRESOURCES); 17051ef60eb1SDavid Nugent login_close(lc); 17061ef60eb1SDavid Nugent } 17071ef60eb1SDavid Nugent } 17081ef60eb1SDavid Nugent #endif 1709