reboot.c revision 17801
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 351558Srgrimesstatic char copyright[] = 361558Srgrimes"@(#) Copyright (c) 1980, 1986, 1993\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 411558Srgrimesstatic char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes#include <sys/reboot.h> 451558Srgrimes#include <signal.h> 461558Srgrimes#include <pwd.h> 471558Srgrimes#include <errno.h> 481558Srgrimes#include <syslog.h> 491558Srgrimes#include <unistd.h> 501558Srgrimes#include <stdio.h> 511558Srgrimes#include <stdlib.h> 521558Srgrimes#include <string.h> 531558Srgrimes 541558Srgrimesvoid err __P((const char *fmt, ...)); 551558Srgrimesvoid usage __P((void)); 561558Srgrimes 571558Srgrimesint dohalt; 581558Srgrimes 591558Srgrimesint 601558Srgrimesmain(argc, argv) 611558Srgrimes int argc; 621558Srgrimes char *argv[]; 631558Srgrimes{ 641558Srgrimes register int i; 651558Srgrimes struct passwd *pw; 6617801Sjulian int ch, howto, lflag, nflag, qflag, pflag, sverrno; 671558Srgrimes char *p, *user; 681558Srgrimes 692171Sdg if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { 701558Srgrimes dohalt = 1; 711558Srgrimes howto = RB_HALT; 721558Srgrimes } else 731558Srgrimes howto = 0; 741558Srgrimes lflag = nflag = qflag = 0; 7517801Sjulian while ((ch = getopt(argc, argv, "lnpq")) != EOF) 761558Srgrimes switch(ch) { 771558Srgrimes case 'l': /* Undocumented; used by shutdown. */ 781558Srgrimes lflag = 1; 791558Srgrimes break; 801558Srgrimes case 'n': 811558Srgrimes nflag = 1; 821558Srgrimes howto |= RB_NOSYNC; 831558Srgrimes break; 8417801Sjulian case 'p': 8517801Sjulian pflag = 1; 8617801Sjulian howto |= (RB_POWEROFF | RB_HALT); 8717801Sjulian break; 881558Srgrimes case 'q': 891558Srgrimes qflag = 1; 901558Srgrimes break; 911558Srgrimes case '?': 921558Srgrimes default: 931558Srgrimes usage(); 941558Srgrimes } 951558Srgrimes argc -= optind; 961558Srgrimes argv += optind; 971558Srgrimes 981558Srgrimes if (geteuid()) 991558Srgrimes err("%s", strerror(EPERM)); 1001558Srgrimes 1011558Srgrimes if (qflag) { 1021558Srgrimes reboot(howto); 1031558Srgrimes err("%s", strerror(errno)); 1041558Srgrimes } 1051558Srgrimes 1061558Srgrimes /* Log the reboot. */ 1071558Srgrimes if (!lflag) { 1081558Srgrimes if ((user = getlogin()) == NULL) 1091558Srgrimes user = (pw = getpwuid(getuid())) ? 1101558Srgrimes pw->pw_name : "???"; 1111558Srgrimes if (dohalt) { 1121558Srgrimes openlog("halt", 0, LOG_AUTH | LOG_CONS); 1131558Srgrimes syslog(LOG_CRIT, "halted by %s", user); 1141558Srgrimes } else { 1151558Srgrimes openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1161558Srgrimes syslog(LOG_CRIT, "rebooted by %s", user); 1171558Srgrimes } 1181558Srgrimes } 1191558Srgrimes logwtmp("~", "shutdown", ""); 1201558Srgrimes 1211558Srgrimes /* 1221558Srgrimes * Do a sync early on, so disks start transfers while we're off 1231558Srgrimes * killing processes. Don't worry about writes done before the 1241558Srgrimes * processes die, the reboot system call syncs the disks. 1251558Srgrimes */ 1261558Srgrimes if (!nflag) 1271558Srgrimes sync(); 1281558Srgrimes 1291558Srgrimes /* Just stop init -- if we fail, we'll restart it. */ 1301558Srgrimes if (kill(1, SIGTSTP) == -1) 1311558Srgrimes err("SIGTSTP init: %s", strerror(errno)); 1321558Srgrimes 1331558Srgrimes /* Ignore the SIGHUP we get when our parent shell dies. */ 1341558Srgrimes (void)signal(SIGHUP, SIG_IGN); 1351558Srgrimes 1361558Srgrimes /* Send a SIGTERM first, a chance to save the buffers. */ 1371558Srgrimes if (kill(-1, SIGTERM) == -1) 1381558Srgrimes err("SIGTERM processes: %s", strerror(errno)); 1391558Srgrimes 1401558Srgrimes /* 1411558Srgrimes * After the processes receive the signal, start the rest of the 1421558Srgrimes * buffers on their way. Wait 5 seconds between the SIGTERM and 1431558Srgrimes * the SIGKILL to give everybody a chance. 1441558Srgrimes */ 1451558Srgrimes sleep(2); 1461558Srgrimes if (!nflag) 1471558Srgrimes sync(); 1481558Srgrimes sleep(3); 1491558Srgrimes 1501558Srgrimes for (i = 1;; ++i) { 1511558Srgrimes if (kill(-1, SIGKILL) == -1) { 1521558Srgrimes if (errno == ESRCH) 1531558Srgrimes break; 1541558Srgrimes goto restart; 1551558Srgrimes } 1561558Srgrimes if (i > 5) { 1571558Srgrimes (void)fprintf(stderr, 1581558Srgrimes "WARNING: some process(es) wouldn't die\n"); 1591558Srgrimes break; 1601558Srgrimes } 1611558Srgrimes (void)sleep(2 * i); 1621558Srgrimes } 1631558Srgrimes 1641558Srgrimes reboot(howto); 1651558Srgrimes /* FALLTHROUGH */ 1661558Srgrimes 1671558Srgrimesrestart: 1681558Srgrimes sverrno = errno; 1691558Srgrimes err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 1701558Srgrimes strerror(sverrno)); 1711558Srgrimes /* NOTREACHED */ 1721558Srgrimes} 1731558Srgrimes 1741558Srgrimesvoid 1751558Srgrimesusage() 1761558Srgrimes{ 1771558Srgrimes (void)fprintf(stderr, "usage: %s [-nq]\n", dohalt ? "halt" : "reboot"); 1781558Srgrimes exit(1); 1791558Srgrimes} 1801558Srgrimes 1811558Srgrimes#if __STDC__ 1821558Srgrimes#include <stdarg.h> 1831558Srgrimes#else 1841558Srgrimes#include <varargs.h> 1851558Srgrimes#endif 1861558Srgrimes 1871558Srgrimesvoid 1881558Srgrimes#if __STDC__ 1891558Srgrimeserr(const char *fmt, ...) 1901558Srgrimes#else 1911558Srgrimeserr(fmt, va_alist) 1921558Srgrimes char *fmt; 1931558Srgrimes va_dcl 1941558Srgrimes#endif 1951558Srgrimes{ 1961558Srgrimes va_list ap; 1971558Srgrimes#if __STDC__ 1981558Srgrimes va_start(ap, fmt); 1991558Srgrimes#else 2001558Srgrimes va_start(ap); 2011558Srgrimes#endif 2021558Srgrimes (void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot"); 2031558Srgrimes (void)vfprintf(stderr, fmt, ap); 2041558Srgrimes va_end(ap); 2051558Srgrimes (void)fprintf(stderr, "\n"); 2061558Srgrimes exit(1); 2071558Srgrimes /* NOTREACHED */ 2081558Srgrimes} 209