18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1980, 1986, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 68fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 78fae3551SRodney W. Grimes * are met: 88fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 98fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 108fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 128fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 138fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 148fae3551SRodney W. Grimes * must display the following acknowledgement: 158fae3551SRodney W. Grimes * This product includes software developed by the University of 168fae3551SRodney W. Grimes * California, Berkeley and its contributors. 178fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 188fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 198fae3551SRodney W. Grimes * without specific prior written permission. 208fae3551SRodney W. Grimes * 218fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 228fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 238fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 248fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 258fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 268fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 278fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 288fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 298fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 308fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318fae3551SRodney W. Grimes * SUCH DAMAGE. 328fae3551SRodney W. Grimes */ 338fae3551SRodney W. Grimes 348fae3551SRodney W. Grimes #ifndef lint 358fae3551SRodney W. Grimes static char copyright[] = 368fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\ 378fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 388fae3551SRodney W. Grimes #endif /* not lint */ 398fae3551SRodney W. Grimes 408fae3551SRodney W. Grimes #ifndef lint 418fae3551SRodney W. Grimes static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 428fae3551SRodney W. Grimes #endif /* not lint */ 438fae3551SRodney W. Grimes 448fae3551SRodney W. Grimes #include <sys/reboot.h> 458fae3551SRodney W. Grimes #include <signal.h> 468fae3551SRodney W. Grimes #include <pwd.h> 478fae3551SRodney W. Grimes #include <errno.h> 488fae3551SRodney W. Grimes #include <syslog.h> 498fae3551SRodney W. Grimes #include <unistd.h> 508fae3551SRodney W. Grimes #include <stdio.h> 518fae3551SRodney W. Grimes #include <stdlib.h> 528fae3551SRodney W. Grimes #include <string.h> 538fae3551SRodney W. Grimes 548fae3551SRodney W. Grimes void err __P((const char *fmt, ...)); 558fae3551SRodney W. Grimes void usage __P((void)); 568fae3551SRodney W. Grimes 578fae3551SRodney W. Grimes int dohalt; 588fae3551SRodney W. Grimes 598fae3551SRodney W. Grimes int 608fae3551SRodney W. Grimes main(argc, argv) 618fae3551SRodney W. Grimes int argc; 628fae3551SRodney W. Grimes char *argv[]; 638fae3551SRodney W. Grimes { 648fae3551SRodney W. Grimes register int i; 658fae3551SRodney W. Grimes struct passwd *pw; 6647be7466SJulian Elischer int ch, howto, lflag, nflag, qflag, pflag, sverrno; 678fae3551SRodney W. Grimes char *p, *user; 688fae3551SRodney W. Grimes 697f4b19d6SDavid Greenman if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { 708fae3551SRodney W. Grimes dohalt = 1; 718fae3551SRodney W. Grimes howto = RB_HALT; 728fae3551SRodney W. Grimes } else 738fae3551SRodney W. Grimes howto = 0; 748fae3551SRodney W. Grimes lflag = nflag = qflag = 0; 7547be7466SJulian Elischer while ((ch = getopt(argc, argv, "lnpq")) != EOF) 768fae3551SRodney W. Grimes switch(ch) { 778fae3551SRodney W. Grimes case 'l': /* Undocumented; used by shutdown. */ 788fae3551SRodney W. Grimes lflag = 1; 798fae3551SRodney W. Grimes break; 808fae3551SRodney W. Grimes case 'n': 818fae3551SRodney W. Grimes nflag = 1; 828fae3551SRodney W. Grimes howto |= RB_NOSYNC; 838fae3551SRodney W. Grimes break; 8447be7466SJulian Elischer case 'p': 8547be7466SJulian Elischer pflag = 1; 8647be7466SJulian Elischer howto |= (RB_POWEROFF | RB_HALT); 8747be7466SJulian Elischer break; 888fae3551SRodney W. Grimes case 'q': 898fae3551SRodney W. Grimes qflag = 1; 908fae3551SRodney W. Grimes break; 918fae3551SRodney W. Grimes case '?': 928fae3551SRodney W. Grimes default: 938fae3551SRodney W. Grimes usage(); 948fae3551SRodney W. Grimes } 958fae3551SRodney W. Grimes argc -= optind; 968fae3551SRodney W. Grimes argv += optind; 978fae3551SRodney W. Grimes 988fae3551SRodney W. Grimes if (geteuid()) 998fae3551SRodney W. Grimes err("%s", strerror(EPERM)); 1008fae3551SRodney W. Grimes 1018fae3551SRodney W. Grimes if (qflag) { 1028fae3551SRodney W. Grimes reboot(howto); 1038fae3551SRodney W. Grimes err("%s", strerror(errno)); 1048fae3551SRodney W. Grimes } 1058fae3551SRodney W. Grimes 1068fae3551SRodney W. Grimes /* Log the reboot. */ 1078fae3551SRodney W. Grimes if (!lflag) { 1088fae3551SRodney W. Grimes if ((user = getlogin()) == NULL) 1098fae3551SRodney W. Grimes user = (pw = getpwuid(getuid())) ? 1108fae3551SRodney W. Grimes pw->pw_name : "???"; 1118fae3551SRodney W. Grimes if (dohalt) { 1128fae3551SRodney W. Grimes openlog("halt", 0, LOG_AUTH | LOG_CONS); 1138fae3551SRodney W. Grimes syslog(LOG_CRIT, "halted by %s", user); 1148fae3551SRodney W. Grimes } else { 1158fae3551SRodney W. Grimes openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1168fae3551SRodney W. Grimes syslog(LOG_CRIT, "rebooted by %s", user); 1178fae3551SRodney W. Grimes } 1188fae3551SRodney W. Grimes } 1198fae3551SRodney W. Grimes logwtmp("~", "shutdown", ""); 1208fae3551SRodney W. Grimes 1218fae3551SRodney W. Grimes /* 1228fae3551SRodney W. Grimes * Do a sync early on, so disks start transfers while we're off 1238fae3551SRodney W. Grimes * killing processes. Don't worry about writes done before the 1248fae3551SRodney W. Grimes * processes die, the reboot system call syncs the disks. 1258fae3551SRodney W. Grimes */ 1268fae3551SRodney W. Grimes if (!nflag) 1278fae3551SRodney W. Grimes sync(); 1288fae3551SRodney W. Grimes 1298fae3551SRodney W. Grimes /* Just stop init -- if we fail, we'll restart it. */ 1308fae3551SRodney W. Grimes if (kill(1, SIGTSTP) == -1) 1318fae3551SRodney W. Grimes err("SIGTSTP init: %s", strerror(errno)); 1328fae3551SRodney W. Grimes 1338fae3551SRodney W. Grimes /* Ignore the SIGHUP we get when our parent shell dies. */ 1348fae3551SRodney W. Grimes (void)signal(SIGHUP, SIG_IGN); 1358fae3551SRodney W. Grimes 1368fae3551SRodney W. Grimes /* Send a SIGTERM first, a chance to save the buffers. */ 1378fae3551SRodney W. Grimes if (kill(-1, SIGTERM) == -1) 1388fae3551SRodney W. Grimes err("SIGTERM processes: %s", strerror(errno)); 1398fae3551SRodney W. Grimes 1408fae3551SRodney W. Grimes /* 1418fae3551SRodney W. Grimes * After the processes receive the signal, start the rest of the 1428fae3551SRodney W. Grimes * buffers on their way. Wait 5 seconds between the SIGTERM and 1438fae3551SRodney W. Grimes * the SIGKILL to give everybody a chance. 1448fae3551SRodney W. Grimes */ 1458fae3551SRodney W. Grimes sleep(2); 1468fae3551SRodney W. Grimes if (!nflag) 1478fae3551SRodney W. Grimes sync(); 1488fae3551SRodney W. Grimes sleep(3); 1498fae3551SRodney W. Grimes 1508fae3551SRodney W. Grimes for (i = 1;; ++i) { 1518fae3551SRodney W. Grimes if (kill(-1, SIGKILL) == -1) { 1528fae3551SRodney W. Grimes if (errno == ESRCH) 1538fae3551SRodney W. Grimes break; 1548fae3551SRodney W. Grimes goto restart; 1558fae3551SRodney W. Grimes } 1568fae3551SRodney W. Grimes if (i > 5) { 1578fae3551SRodney W. Grimes (void)fprintf(stderr, 1588fae3551SRodney W. Grimes "WARNING: some process(es) wouldn't die\n"); 1598fae3551SRodney W. Grimes break; 1608fae3551SRodney W. Grimes } 1618fae3551SRodney W. Grimes (void)sleep(2 * i); 1628fae3551SRodney W. Grimes } 1638fae3551SRodney W. Grimes 1648fae3551SRodney W. Grimes reboot(howto); 1658fae3551SRodney W. Grimes /* FALLTHROUGH */ 1668fae3551SRodney W. Grimes 1678fae3551SRodney W. Grimes restart: 1688fae3551SRodney W. Grimes sverrno = errno; 1698fae3551SRodney W. Grimes err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 1708fae3551SRodney W. Grimes strerror(sverrno)); 1718fae3551SRodney W. Grimes /* NOTREACHED */ 1728fae3551SRodney W. Grimes } 1738fae3551SRodney W. Grimes 1748fae3551SRodney W. Grimes void 1758fae3551SRodney W. Grimes usage() 1768fae3551SRodney W. Grimes { 1778fae3551SRodney W. Grimes (void)fprintf(stderr, "usage: %s [-nq]\n", dohalt ? "halt" : "reboot"); 1788fae3551SRodney W. Grimes exit(1); 1798fae3551SRodney W. Grimes } 1808fae3551SRodney W. Grimes 1818fae3551SRodney W. Grimes #if __STDC__ 1828fae3551SRodney W. Grimes #include <stdarg.h> 1838fae3551SRodney W. Grimes #else 1848fae3551SRodney W. Grimes #include <varargs.h> 1858fae3551SRodney W. Grimes #endif 1868fae3551SRodney W. Grimes 1878fae3551SRodney W. Grimes void 1888fae3551SRodney W. Grimes #if __STDC__ 1898fae3551SRodney W. Grimes err(const char *fmt, ...) 1908fae3551SRodney W. Grimes #else 1918fae3551SRodney W. Grimes err(fmt, va_alist) 1928fae3551SRodney W. Grimes char *fmt; 1938fae3551SRodney W. Grimes va_dcl 1948fae3551SRodney W. Grimes #endif 1958fae3551SRodney W. Grimes { 1968fae3551SRodney W. Grimes va_list ap; 1978fae3551SRodney W. Grimes #if __STDC__ 1988fae3551SRodney W. Grimes va_start(ap, fmt); 1998fae3551SRodney W. Grimes #else 2008fae3551SRodney W. Grimes va_start(ap); 2018fae3551SRodney W. Grimes #endif 2028fae3551SRodney W. Grimes (void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot"); 2038fae3551SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 2048fae3551SRodney W. Grimes va_end(ap); 2058fae3551SRodney W. Grimes (void)fprintf(stderr, "\n"); 2068fae3551SRodney W. Grimes exit(1); 2078fae3551SRodney W. Grimes /* NOTREACHED */ 2088fae3551SRodney W. Grimes } 209