18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1980, 1986, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 88fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 98fae3551SRodney W. Grimes * are met: 108fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 128fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 138fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 148fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 168fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 178fae3551SRodney W. Grimes * without specific prior written permission. 188fae3551SRodney W. Grimes * 198fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 208fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 218fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 228fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 238fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 248fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 258fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 268fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 278fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 288fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 298fae3551SRodney W. Grimes * SUCH DAMAGE. 308fae3551SRodney W. Grimes */ 318fae3551SRodney W. Grimes 32c69284caSDavid E. O'Brien #if 0 338fae3551SRodney W. Grimes #ifndef lint 349448def9SPhilippe Charnier static const char copyright[] = 358fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\ 368fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 378fae3551SRodney W. Grimes #endif /* not lint */ 388fae3551SRodney W. Grimes 398fae3551SRodney W. Grimes #ifndef lint 408fae3551SRodney W. Grimes static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 418fae3551SRodney W. Grimes #endif /* not lint */ 42c69284caSDavid E. O'Brien #endif 43c69284caSDavid E. O'Brien #include <sys/cdefs.h> 44c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 458fae3551SRodney W. Grimes 469448def9SPhilippe Charnier #include <sys/types.h> 477b0a665dSMitchell Horne #include <sys/boottrace.h> 487b0a665dSMitchell Horne #include <sys/reboot.h> 4985ae580cSIan Dowse #include <sys/sysctl.h> 507b0a665dSMitchell Horne #include <sys/time.h> 517b0a665dSMitchell Horne 528fae3551SRodney W. Grimes #include <signal.h> 53098166edSPhilippe Charnier #include <err.h> 549448def9SPhilippe Charnier #include <errno.h> 551de372dcSWes Peters #include <fcntl.h> 569448def9SPhilippe Charnier #include <pwd.h> 579448def9SPhilippe Charnier #include <syslog.h> 589448def9SPhilippe Charnier #include <stdio.h> 59cafefe8cSDima Dorfman #include <stdlib.h> 609448def9SPhilippe Charnier #include <string.h> 619448def9SPhilippe Charnier #include <unistd.h> 62bf8959b0SEd Schouten #include <utmpx.h> 638fae3551SRodney W. Grimes 64*65f3be91SAlfonso Gregory static void usage(void) __dead2; 6536737a0bSXin LI static u_int get_pageins(void); 668fae3551SRodney W. Grimes 67e802a0b3SEd Schouten static int dohalt; 688fae3551SRodney W. Grimes 698fae3551SRodney W. Grimes int 7085ae580cSIan Dowse main(int argc, char *argv[]) 718fae3551SRodney W. Grimes { 7214c69f21SEd Schouten struct utmpx utx; 7336737a0bSXin LI const struct passwd *pw; 746237ce08SSteven Hartland int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag; 7585ae580cSIan Dowse u_int pageins; 76e802a0b3SEd Schouten const char *user, *kernel = NULL; 778fae3551SRodney W. Grimes 786760585aSEric van Gyzen if (strstr(getprogname(), "halt") != NULL) { 798fae3551SRodney W. Grimes dohalt = 1; 808fae3551SRodney W. Grimes howto = RB_HALT; 818fae3551SRodney W. Grimes } else 828fae3551SRodney W. Grimes howto = 0; 830f79b5d7SSteven Hartland lflag = nflag = qflag = Nflag = 0; 847d7d9013SWarner Losh while ((ch = getopt(argc, argv, "cdk:lNnpqr")) != -1) 858fae3551SRodney W. Grimes switch(ch) { 867d7d9013SWarner Losh case 'c': 877d7d9013SWarner Losh howto |= RB_POWERCYCLE; 887d7d9013SWarner Losh break; 8985c981ccSJohn Polstra case 'd': 9085c981ccSJohn Polstra howto |= RB_DUMP; 9185c981ccSJohn Polstra break; 921de372dcSWes Peters case 'k': 931de372dcSWes Peters kernel = optarg; 941de372dcSWes Peters break; 956714dac4SNik Clayton case 'l': 968fae3551SRodney W. Grimes lflag = 1; 978fae3551SRodney W. Grimes break; 988fae3551SRodney W. Grimes case 'n': 998fae3551SRodney W. Grimes nflag = 1; 1008fae3551SRodney W. Grimes howto |= RB_NOSYNC; 1018fae3551SRodney W. Grimes break; 1026237ce08SSteven Hartland case 'N': 1036237ce08SSteven Hartland nflag = 1; 1046237ce08SSteven Hartland Nflag = 1; 1056237ce08SSteven Hartland break; 10647be7466SJulian Elischer case 'p': 107cfde77fbSThomas Quinot howto |= RB_POWEROFF; 10847be7466SJulian Elischer break; 1098fae3551SRodney W. Grimes case 'q': 1108fae3551SRodney W. Grimes qflag = 1; 1118fae3551SRodney W. Grimes break; 1123f5ac575SEdward Tomasz Napierala case 'r': 1133f5ac575SEdward Tomasz Napierala howto |= RB_REROOT; 1143f5ac575SEdward Tomasz Napierala break; 1158fae3551SRodney W. Grimes case '?': 1168fae3551SRodney W. Grimes default: 1178fae3551SRodney W. Grimes usage(); 1188fae3551SRodney W. Grimes } 1198fae3551SRodney W. Grimes argc -= optind; 1208fae3551SRodney W. Grimes argv += optind; 1212e23ded5SRodney W. Grimes if (argc != 0) 1222e23ded5SRodney W. Grimes usage(); 1238fae3551SRodney W. Grimes 12485c981ccSJohn Polstra if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 12585c981ccSJohn Polstra errx(1, "cannot dump (-d) when halting; must reboot instead"); 1266237ce08SSteven Hartland if (Nflag && (howto & RB_NOSYNC) != 0) 1276237ce08SSteven Hartland errx(1, "-N cannot be used with -n"); 1287d7d9013SWarner Losh if ((howto & RB_POWEROFF) && (howto & RB_POWERCYCLE)) 1297d7d9013SWarner Losh errx(1, "-c and -p cannot be used together"); 1303f5ac575SEdward Tomasz Napierala if ((howto & RB_REROOT) != 0 && howto != RB_REROOT) 1317d7d9013SWarner Losh errx(1, "-r cannot be used with -c, -d, -n, or -p"); 132098166edSPhilippe Charnier if (geteuid()) { 133098166edSPhilippe Charnier errno = EPERM; 134098166edSPhilippe Charnier err(1, NULL); 135098166edSPhilippe Charnier } 1368fae3551SRodney W. Grimes 1378fae3551SRodney W. Grimes if (qflag) { 1388fae3551SRodney W. Grimes reboot(howto); 139098166edSPhilippe Charnier err(1, NULL); 1408fae3551SRodney W. Grimes } 1418fae3551SRodney W. Grimes 142faa0ecddSXin LI if (kernel != NULL) { 1433edf7a78SPawel Jakub Dawidek fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC, 1443edf7a78SPawel Jakub Dawidek 0444); 1451de372dcSWes Peters if (fd > -1) { 146a72b2ac6SGordon Tetlow (void)write(fd, "nextboot_enable=\"YES\"\n", 22); 1471de372dcSWes Peters (void)write(fd, "kernel=\"", 8L); 1481de372dcSWes Peters (void)write(fd, kernel, strlen(kernel)); 1491de372dcSWes Peters (void)write(fd, "\"\n", 2); 1501de372dcSWes Peters close(fd); 1511de372dcSWes Peters } 1521de372dcSWes Peters } 1531de372dcSWes Peters 1548fae3551SRodney W. Grimes /* Log the reboot. */ 1558fae3551SRodney W. Grimes if (!lflag) { 1568fae3551SRodney W. Grimes if ((user = getlogin()) == NULL) 1578fae3551SRodney W. Grimes user = (pw = getpwuid(getuid())) ? 1588fae3551SRodney W. Grimes pw->pw_name : "???"; 1598fae3551SRodney W. Grimes if (dohalt) { 1608fae3551SRodney W. Grimes openlog("halt", 0, LOG_AUTH | LOG_CONS); 1618fae3551SRodney W. Grimes syslog(LOG_CRIT, "halted by %s", user); 1623f5ac575SEdward Tomasz Napierala } else if (howto & RB_REROOT) { 1633f5ac575SEdward Tomasz Napierala openlog("reroot", 0, LOG_AUTH | LOG_CONS); 1643f5ac575SEdward Tomasz Napierala syslog(LOG_CRIT, "rerooted by %s", user); 1657d7d9013SWarner Losh } else if (howto & RB_POWEROFF) { 1667d7d9013SWarner Losh openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1677d7d9013SWarner Losh syslog(LOG_CRIT, "powered off by %s", user); 1687d7d9013SWarner Losh } else if (howto & RB_POWERCYCLE) { 1697d7d9013SWarner Losh openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1707d7d9013SWarner Losh syslog(LOG_CRIT, "power cycled by %s", user); 1718fae3551SRodney W. Grimes } else { 1728fae3551SRodney W. Grimes openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1738fae3551SRodney W. Grimes syslog(LOG_CRIT, "rebooted by %s", user); 1748fae3551SRodney W. Grimes } 1758fae3551SRodney W. Grimes } 17614c69f21SEd Schouten utx.ut_type = SHUTDOWN_TIME; 17714c69f21SEd Schouten gettimeofday(&utx.ut_tv, NULL); 17814c69f21SEd Schouten pututxline(&utx); 1798fae3551SRodney W. Grimes 1808fae3551SRodney W. Grimes /* 1818fae3551SRodney W. Grimes * Do a sync early on, so disks start transfers while we're off 1828fae3551SRodney W. Grimes * killing processes. Don't worry about writes done before the 1838fae3551SRodney W. Grimes * processes die, the reboot system call syncs the disks. 1848fae3551SRodney W. Grimes */ 1858fae3551SRodney W. Grimes if (!nflag) 1868fae3551SRodney W. Grimes sync(); 1878fae3551SRodney W. Grimes 188d8f70938SBruce M Simpson /* 189d8f70938SBruce M Simpson * Ignore signals that we can get as a result of killing 190d8f70938SBruce M Simpson * parents, group leaders, etc. 191d8f70938SBruce M Simpson */ 192b08d1553SBruce M Simpson (void)signal(SIGHUP, SIG_IGN); 193d8f70938SBruce M Simpson (void)signal(SIGINT, SIG_IGN); 194d8f70938SBruce M Simpson (void)signal(SIGQUIT, SIG_IGN); 195d8f70938SBruce M Simpson (void)signal(SIGTERM, SIG_IGN); 196d8f70938SBruce M Simpson (void)signal(SIGTSTP, SIG_IGN); 197d8f70938SBruce M Simpson 198d8f70938SBruce M Simpson /* 199d8f70938SBruce M Simpson * If we're running in a pipeline, we don't want to die 200d8f70938SBruce M Simpson * after killing whatever we're writing to. 201d8f70938SBruce M Simpson */ 202d8f70938SBruce M Simpson (void)signal(SIGPIPE, SIG_IGN); 203b08d1553SBruce M Simpson 2043f5ac575SEdward Tomasz Napierala /* 2053f5ac575SEdward Tomasz Napierala * Only init(8) can perform rerooting. 2063f5ac575SEdward Tomasz Napierala */ 2073f5ac575SEdward Tomasz Napierala if (howto & RB_REROOT) { 2083f5ac575SEdward Tomasz Napierala if (kill(1, SIGEMT) == -1) 2093f5ac575SEdward Tomasz Napierala err(1, "SIGEMT init"); 2103f5ac575SEdward Tomasz Napierala 2113f5ac575SEdward Tomasz Napierala return (0); 2123f5ac575SEdward Tomasz Napierala } 2133f5ac575SEdward Tomasz Napierala 2148fae3551SRodney W. Grimes /* Just stop init -- if we fail, we'll restart it. */ 2157b0a665dSMitchell Horne BOOTTRACE("SIGTSTP to init(8)..."); 2168fae3551SRodney W. Grimes if (kill(1, SIGTSTP) == -1) 217098166edSPhilippe Charnier err(1, "SIGTSTP init"); 2188fae3551SRodney W. Grimes 2198fae3551SRodney W. Grimes /* Send a SIGTERM first, a chance to save the buffers. */ 2207b0a665dSMitchell Horne BOOTTRACE("SIGTERM to all other processes..."); 2216ab0d6c2SRobert Watson if (kill(-1, SIGTERM) == -1 && errno != ESRCH) 222098166edSPhilippe Charnier err(1, "SIGTERM processes"); 2238fae3551SRodney W. Grimes 2248fae3551SRodney W. Grimes /* 2258fae3551SRodney W. Grimes * After the processes receive the signal, start the rest of the 2268fae3551SRodney W. Grimes * buffers on their way. Wait 5 seconds between the SIGTERM and 22785ae580cSIan Dowse * the SIGKILL to give everybody a chance. If there is a lot of 22885ae580cSIan Dowse * paging activity then wait longer, up to a maximum of approx 22985ae580cSIan Dowse * 60 seconds. 2308fae3551SRodney W. Grimes */ 2318fae3551SRodney W. Grimes sleep(2); 23285ae580cSIan Dowse for (i = 0; i < 20; i++) { 23385ae580cSIan Dowse pageins = get_pageins(); 2348fae3551SRodney W. Grimes if (!nflag) 2358fae3551SRodney W. Grimes sync(); 2368fae3551SRodney W. Grimes sleep(3); 23785ae580cSIan Dowse if (get_pageins() == pageins) 23885ae580cSIan Dowse break; 23985ae580cSIan Dowse } 2408fae3551SRodney W. Grimes 2418fae3551SRodney W. Grimes for (i = 1;; ++i) { 2427b0a665dSMitchell Horne BOOTTRACE("SIGKILL to all other processes(%d)...", i); 2438fae3551SRodney W. Grimes if (kill(-1, SIGKILL) == -1) { 2448fae3551SRodney W. Grimes if (errno == ESRCH) 2458fae3551SRodney W. Grimes break; 2468fae3551SRodney W. Grimes goto restart; 2478fae3551SRodney W. Grimes } 2488fae3551SRodney W. Grimes if (i > 5) { 2498fae3551SRodney W. Grimes (void)fprintf(stderr, 2508fae3551SRodney W. Grimes "WARNING: some process(es) wouldn't die\n"); 2518fae3551SRodney W. Grimes break; 2528fae3551SRodney W. Grimes } 2538fae3551SRodney W. Grimes (void)sleep(2 * i); 2548fae3551SRodney W. Grimes } 2558fae3551SRodney W. Grimes 2568fae3551SRodney W. Grimes reboot(howto); 2578fae3551SRodney W. Grimes /* FALLTHROUGH */ 2588fae3551SRodney W. Grimes 2598fae3551SRodney W. Grimes restart: 2607b0a665dSMitchell Horne BOOTTRACE("SIGHUP to init(8)..."); 2618fae3551SRodney W. Grimes sverrno = errno; 262098166edSPhilippe Charnier errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 2638fae3551SRodney W. Grimes strerror(sverrno)); 2648fae3551SRodney W. Grimes /* NOTREACHED */ 2658fae3551SRodney W. Grimes } 2668fae3551SRodney W. Grimes 26736737a0bSXin LI static void 268c574558fSDag-Erling Smørgrav usage(void) 2698fae3551SRodney W. Grimes { 270c574558fSDag-Erling Smørgrav 271c574558fSDag-Erling Smørgrav (void)fprintf(stderr, dohalt ? 27225fd0815SWarner Losh "usage: halt [-clNnpq] [-k kernel]\n" : 27325fd0815SWarner Losh "usage: reboot [-cdlNnpqr] [-k kernel]\n"); 2748fae3551SRodney W. Grimes exit(1); 2758fae3551SRodney W. Grimes } 27685ae580cSIan Dowse 27736737a0bSXin LI static u_int 27888a8f792SEd Schouten get_pageins(void) 27985ae580cSIan Dowse { 28085ae580cSIan Dowse u_int pageins; 28185ae580cSIan Dowse size_t len; 28285ae580cSIan Dowse 28385ae580cSIan Dowse len = sizeof(pageins); 28485ae580cSIan Dowse if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 28585ae580cSIan Dowse != 0) { 28685ae580cSIan Dowse warnx("v_swappgsin"); 28785ae580cSIan Dowse return (0); 28885ae580cSIan Dowse } 28985ae580cSIan Dowse return pageins; 29085ae580cSIan Dowse } 291