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 468fae3551SRodney W. Grimes #include <sys/reboot.h> 4714c69f21SEd Schouten #include <sys/time.h> 489448def9SPhilippe Charnier #include <sys/types.h> 4985ae580cSIan Dowse #include <sys/sysctl.h> 508fae3551SRodney W. Grimes #include <signal.h> 51098166edSPhilippe Charnier #include <err.h> 529448def9SPhilippe Charnier #include <errno.h> 531de372dcSWes Peters #include <fcntl.h> 549448def9SPhilippe Charnier #include <pwd.h> 559448def9SPhilippe Charnier #include <syslog.h> 569448def9SPhilippe Charnier #include <stdio.h> 57cafefe8cSDima Dorfman #include <stdlib.h> 589448def9SPhilippe Charnier #include <string.h> 599448def9SPhilippe Charnier #include <unistd.h> 60bf8959b0SEd Schouten #include <utmpx.h> 618fae3551SRodney W. Grimes 6236737a0bSXin LI static void usage(void); 6336737a0bSXin LI static u_int get_pageins(void); 648fae3551SRodney W. Grimes 65e802a0b3SEd Schouten static int dohalt; 668fae3551SRodney W. Grimes 678fae3551SRodney W. Grimes int 6885ae580cSIan Dowse main(int argc, char *argv[]) 698fae3551SRodney W. Grimes { 7014c69f21SEd Schouten struct utmpx utx; 7136737a0bSXin LI const struct passwd *pw; 726237ce08SSteven Hartland int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag; 7385ae580cSIan Dowse u_int pageins; 74e802a0b3SEd Schouten const char *user, *kernel = NULL; 758fae3551SRodney W. Grimes 76e802a0b3SEd Schouten if (strcmp(getprogname(), "halt") == 0) { 778fae3551SRodney W. Grimes dohalt = 1; 788fae3551SRodney W. Grimes howto = RB_HALT; 798fae3551SRodney W. Grimes } else 808fae3551SRodney W. Grimes howto = 0; 810f79b5d7SSteven Hartland lflag = nflag = qflag = Nflag = 0; 827d7d9013SWarner Losh while ((ch = getopt(argc, argv, "cdk:lNnpqr")) != -1) 838fae3551SRodney W. Grimes switch(ch) { 847d7d9013SWarner Losh case 'c': 857d7d9013SWarner Losh howto |= RB_POWERCYCLE; 867d7d9013SWarner Losh break; 8785c981ccSJohn Polstra case 'd': 8885c981ccSJohn Polstra howto |= RB_DUMP; 8985c981ccSJohn Polstra break; 901de372dcSWes Peters case 'k': 911de372dcSWes Peters kernel = optarg; 921de372dcSWes Peters break; 936714dac4SNik Clayton case 'l': 948fae3551SRodney W. Grimes lflag = 1; 958fae3551SRodney W. Grimes break; 968fae3551SRodney W. Grimes case 'n': 978fae3551SRodney W. Grimes nflag = 1; 988fae3551SRodney W. Grimes howto |= RB_NOSYNC; 998fae3551SRodney W. Grimes break; 1006237ce08SSteven Hartland case 'N': 1016237ce08SSteven Hartland nflag = 1; 1026237ce08SSteven Hartland Nflag = 1; 1036237ce08SSteven Hartland break; 10447be7466SJulian Elischer case 'p': 105cfde77fbSThomas Quinot howto |= RB_POWEROFF; 10647be7466SJulian Elischer break; 1078fae3551SRodney W. Grimes case 'q': 1088fae3551SRodney W. Grimes qflag = 1; 1098fae3551SRodney W. Grimes break; 1103f5ac575SEdward Tomasz Napierala case 'r': 1113f5ac575SEdward Tomasz Napierala howto |= RB_REROOT; 1123f5ac575SEdward Tomasz Napierala break; 1138fae3551SRodney W. Grimes case '?': 1148fae3551SRodney W. Grimes default: 1158fae3551SRodney W. Grimes usage(); 1168fae3551SRodney W. Grimes } 1178fae3551SRodney W. Grimes argc -= optind; 1188fae3551SRodney W. Grimes argv += optind; 119*2e23ded5SRodney W. Grimes if (argc != 0) 120*2e23ded5SRodney W. Grimes usage(); 1218fae3551SRodney W. Grimes 12285c981ccSJohn Polstra if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 12385c981ccSJohn Polstra errx(1, "cannot dump (-d) when halting; must reboot instead"); 1246237ce08SSteven Hartland if (Nflag && (howto & RB_NOSYNC) != 0) 1256237ce08SSteven Hartland errx(1, "-N cannot be used with -n"); 1267d7d9013SWarner Losh if ((howto & RB_POWEROFF) && (howto & RB_POWERCYCLE)) 1277d7d9013SWarner Losh errx(1, "-c and -p cannot be used together"); 1283f5ac575SEdward Tomasz Napierala if ((howto & RB_REROOT) != 0 && howto != RB_REROOT) 1297d7d9013SWarner Losh errx(1, "-r cannot be used with -c, -d, -n, or -p"); 130098166edSPhilippe Charnier if (geteuid()) { 131098166edSPhilippe Charnier errno = EPERM; 132098166edSPhilippe Charnier err(1, NULL); 133098166edSPhilippe Charnier } 1348fae3551SRodney W. Grimes 1358fae3551SRodney W. Grimes if (qflag) { 1368fae3551SRodney W. Grimes reboot(howto); 137098166edSPhilippe Charnier err(1, NULL); 1388fae3551SRodney W. Grimes } 1398fae3551SRodney W. Grimes 140faa0ecddSXin LI if (kernel != NULL) { 1413edf7a78SPawel Jakub Dawidek fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC, 1423edf7a78SPawel Jakub Dawidek 0444); 1431de372dcSWes Peters if (fd > -1) { 144a72b2ac6SGordon Tetlow (void)write(fd, "nextboot_enable=\"YES\"\n", 22); 1451de372dcSWes Peters (void)write(fd, "kernel=\"", 8L); 1461de372dcSWes Peters (void)write(fd, kernel, strlen(kernel)); 1471de372dcSWes Peters (void)write(fd, "\"\n", 2); 1481de372dcSWes Peters close(fd); 1491de372dcSWes Peters } 1501de372dcSWes Peters } 1511de372dcSWes Peters 1528fae3551SRodney W. Grimes /* Log the reboot. */ 1538fae3551SRodney W. Grimes if (!lflag) { 1548fae3551SRodney W. Grimes if ((user = getlogin()) == NULL) 1558fae3551SRodney W. Grimes user = (pw = getpwuid(getuid())) ? 1568fae3551SRodney W. Grimes pw->pw_name : "???"; 1578fae3551SRodney W. Grimes if (dohalt) { 1588fae3551SRodney W. Grimes openlog("halt", 0, LOG_AUTH | LOG_CONS); 1598fae3551SRodney W. Grimes syslog(LOG_CRIT, "halted by %s", user); 1603f5ac575SEdward Tomasz Napierala } else if (howto & RB_REROOT) { 1613f5ac575SEdward Tomasz Napierala openlog("reroot", 0, LOG_AUTH | LOG_CONS); 1623f5ac575SEdward Tomasz Napierala syslog(LOG_CRIT, "rerooted by %s", user); 1637d7d9013SWarner Losh } else if (howto & RB_POWEROFF) { 1647d7d9013SWarner Losh openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1657d7d9013SWarner Losh syslog(LOG_CRIT, "powered off by %s", user); 1667d7d9013SWarner Losh } else if (howto & RB_POWERCYCLE) { 1677d7d9013SWarner Losh openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1687d7d9013SWarner Losh syslog(LOG_CRIT, "power cycled by %s", user); 1698fae3551SRodney W. Grimes } else { 1708fae3551SRodney W. Grimes openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1718fae3551SRodney W. Grimes syslog(LOG_CRIT, "rebooted by %s", user); 1728fae3551SRodney W. Grimes } 1738fae3551SRodney W. Grimes } 17414c69f21SEd Schouten utx.ut_type = SHUTDOWN_TIME; 17514c69f21SEd Schouten gettimeofday(&utx.ut_tv, NULL); 17614c69f21SEd Schouten pututxline(&utx); 1778fae3551SRodney W. Grimes 1788fae3551SRodney W. Grimes /* 1798fae3551SRodney W. Grimes * Do a sync early on, so disks start transfers while we're off 1808fae3551SRodney W. Grimes * killing processes. Don't worry about writes done before the 1818fae3551SRodney W. Grimes * processes die, the reboot system call syncs the disks. 1828fae3551SRodney W. Grimes */ 1838fae3551SRodney W. Grimes if (!nflag) 1848fae3551SRodney W. Grimes sync(); 1858fae3551SRodney W. Grimes 186d8f70938SBruce M Simpson /* 187d8f70938SBruce M Simpson * Ignore signals that we can get as a result of killing 188d8f70938SBruce M Simpson * parents, group leaders, etc. 189d8f70938SBruce M Simpson */ 190b08d1553SBruce M Simpson (void)signal(SIGHUP, SIG_IGN); 191d8f70938SBruce M Simpson (void)signal(SIGINT, SIG_IGN); 192d8f70938SBruce M Simpson (void)signal(SIGQUIT, SIG_IGN); 193d8f70938SBruce M Simpson (void)signal(SIGTERM, SIG_IGN); 194d8f70938SBruce M Simpson (void)signal(SIGTSTP, SIG_IGN); 195d8f70938SBruce M Simpson 196d8f70938SBruce M Simpson /* 197d8f70938SBruce M Simpson * If we're running in a pipeline, we don't want to die 198d8f70938SBruce M Simpson * after killing whatever we're writing to. 199d8f70938SBruce M Simpson */ 200d8f70938SBruce M Simpson (void)signal(SIGPIPE, SIG_IGN); 201b08d1553SBruce M Simpson 2023f5ac575SEdward Tomasz Napierala /* 2033f5ac575SEdward Tomasz Napierala * Only init(8) can perform rerooting. 2043f5ac575SEdward Tomasz Napierala */ 2053f5ac575SEdward Tomasz Napierala if (howto & RB_REROOT) { 2063f5ac575SEdward Tomasz Napierala if (kill(1, SIGEMT) == -1) 2073f5ac575SEdward Tomasz Napierala err(1, "SIGEMT init"); 2083f5ac575SEdward Tomasz Napierala 2093f5ac575SEdward Tomasz Napierala return (0); 2103f5ac575SEdward Tomasz Napierala } 2113f5ac575SEdward Tomasz Napierala 2128fae3551SRodney W. Grimes /* Just stop init -- if we fail, we'll restart it. */ 2138fae3551SRodney W. Grimes if (kill(1, SIGTSTP) == -1) 214098166edSPhilippe Charnier err(1, "SIGTSTP init"); 2158fae3551SRodney W. Grimes 2168fae3551SRodney W. Grimes /* Send a SIGTERM first, a chance to save the buffers. */ 2176ab0d6c2SRobert Watson if (kill(-1, SIGTERM) == -1 && errno != ESRCH) 218098166edSPhilippe Charnier err(1, "SIGTERM processes"); 2198fae3551SRodney W. Grimes 2208fae3551SRodney W. Grimes /* 2218fae3551SRodney W. Grimes * After the processes receive the signal, start the rest of the 2228fae3551SRodney W. Grimes * buffers on their way. Wait 5 seconds between the SIGTERM and 22385ae580cSIan Dowse * the SIGKILL to give everybody a chance. If there is a lot of 22485ae580cSIan Dowse * paging activity then wait longer, up to a maximum of approx 22585ae580cSIan Dowse * 60 seconds. 2268fae3551SRodney W. Grimes */ 2278fae3551SRodney W. Grimes sleep(2); 22885ae580cSIan Dowse for (i = 0; i < 20; i++) { 22985ae580cSIan Dowse pageins = get_pageins(); 2308fae3551SRodney W. Grimes if (!nflag) 2318fae3551SRodney W. Grimes sync(); 2328fae3551SRodney W. Grimes sleep(3); 23385ae580cSIan Dowse if (get_pageins() == pageins) 23485ae580cSIan Dowse break; 23585ae580cSIan Dowse } 2368fae3551SRodney W. Grimes 2378fae3551SRodney W. Grimes for (i = 1;; ++i) { 2388fae3551SRodney W. Grimes if (kill(-1, SIGKILL) == -1) { 2398fae3551SRodney W. Grimes if (errno == ESRCH) 2408fae3551SRodney W. Grimes break; 2418fae3551SRodney W. Grimes goto restart; 2428fae3551SRodney W. Grimes } 2438fae3551SRodney W. Grimes if (i > 5) { 2448fae3551SRodney W. Grimes (void)fprintf(stderr, 2458fae3551SRodney W. Grimes "WARNING: some process(es) wouldn't die\n"); 2468fae3551SRodney W. Grimes break; 2478fae3551SRodney W. Grimes } 2488fae3551SRodney W. Grimes (void)sleep(2 * i); 2498fae3551SRodney W. Grimes } 2508fae3551SRodney W. Grimes 2518fae3551SRodney W. Grimes reboot(howto); 2528fae3551SRodney W. Grimes /* FALLTHROUGH */ 2538fae3551SRodney W. Grimes 2548fae3551SRodney W. Grimes restart: 2558fae3551SRodney W. Grimes sverrno = errno; 256098166edSPhilippe Charnier errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 2578fae3551SRodney W. Grimes strerror(sverrno)); 2588fae3551SRodney W. Grimes /* NOTREACHED */ 2598fae3551SRodney W. Grimes } 2608fae3551SRodney W. Grimes 26136737a0bSXin LI static void 262c574558fSDag-Erling Smørgrav usage(void) 2638fae3551SRodney W. Grimes { 264c574558fSDag-Erling Smørgrav 265c574558fSDag-Erling Smørgrav (void)fprintf(stderr, dohalt ? 26625fd0815SWarner Losh "usage: halt [-clNnpq] [-k kernel]\n" : 26725fd0815SWarner Losh "usage: reboot [-cdlNnpqr] [-k kernel]\n"); 2688fae3551SRodney W. Grimes exit(1); 2698fae3551SRodney W. Grimes } 27085ae580cSIan Dowse 27136737a0bSXin LI static u_int 27288a8f792SEd Schouten get_pageins(void) 27385ae580cSIan Dowse { 27485ae580cSIan Dowse u_int pageins; 27585ae580cSIan Dowse size_t len; 27685ae580cSIan Dowse 27785ae580cSIan Dowse len = sizeof(pageins); 27885ae580cSIan Dowse if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 27985ae580cSIan Dowse != 0) { 28085ae580cSIan Dowse warnx("v_swappgsin"); 28185ae580cSIan Dowse return (0); 28285ae580cSIan Dowse } 28385ae580cSIan Dowse return pageins; 28485ae580cSIan Dowse } 285