reboot.c revision 77122
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 3537674Scharnierstatic const 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 4137674Scharnier#if 0 421558Srgrimesstatic char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 4337674Scharnier#endif 4437674Scharnierstatic const char rcsid[] = 4550476Speter "$FreeBSD: head/sbin/reboot/reboot.c 77122 2001-05-24 09:28:59Z nik $"; 461558Srgrimes#endif /* not lint */ 471558Srgrimes 481558Srgrimes#include <sys/reboot.h> 4937674Scharnier#include <sys/types.h> 5074525Siedowse#include <sys/sysctl.h> 511558Srgrimes#include <signal.h> 5237674Scharnier#include <err.h> 5337674Scharnier#include <errno.h> 5437674Scharnier#include <libutil.h> 551558Srgrimes#include <pwd.h> 561558Srgrimes#include <syslog.h> 571558Srgrimes#include <stdio.h> 581558Srgrimes#include <string.h> 5937674Scharnier#include <unistd.h> 601558Srgrimes 6174525Siedowsevoid usage(void); 6274525Siedowseu_int get_pageins(void); 631558Srgrimes 641558Srgrimesint dohalt; 651558Srgrimes 661558Srgrimesint 6774525Siedowsemain(int argc, char *argv[]) 681558Srgrimes{ 691558Srgrimes struct passwd *pw; 7074525Siedowse int ch, howto, i, lflag, nflag, qflag, pflag, sverrno; 7174525Siedowse u_int pageins; 721558Srgrimes char *p, *user; 731558Srgrimes 742171Sdg if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { 751558Srgrimes dohalt = 1; 761558Srgrimes howto = RB_HALT; 771558Srgrimes } else 781558Srgrimes howto = 0; 791558Srgrimes lflag = nflag = qflag = 0; 8053523Sjdp while ((ch = getopt(argc, argv, "dlnpq")) != -1) 811558Srgrimes switch(ch) { 8253523Sjdp case 'd': 8353523Sjdp howto |= RB_DUMP; 8453523Sjdp break; 8577122Snik case 'l': 861558Srgrimes lflag = 1; 871558Srgrimes break; 881558Srgrimes case 'n': 891558Srgrimes nflag = 1; 901558Srgrimes howto |= RB_NOSYNC; 911558Srgrimes break; 9217801Sjulian case 'p': 9317801Sjulian pflag = 1; 9417801Sjulian howto |= (RB_POWEROFF | RB_HALT); 9517801Sjulian break; 961558Srgrimes case 'q': 971558Srgrimes qflag = 1; 981558Srgrimes break; 991558Srgrimes case '?': 1001558Srgrimes default: 1011558Srgrimes usage(); 1021558Srgrimes } 1031558Srgrimes argc -= optind; 1041558Srgrimes argv += optind; 1051558Srgrimes 10653523Sjdp if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 10753523Sjdp errx(1, "cannot dump (-d) when halting; must reboot instead"); 10826676Scharnier if (geteuid()) { 10926676Scharnier errno = EPERM; 11026676Scharnier err(1, NULL); 11126676Scharnier } 1121558Srgrimes 1131558Srgrimes if (qflag) { 1141558Srgrimes reboot(howto); 11526676Scharnier err(1, NULL); 1161558Srgrimes } 1171558Srgrimes 1181558Srgrimes /* Log the reboot. */ 1191558Srgrimes if (!lflag) { 1201558Srgrimes if ((user = getlogin()) == NULL) 1211558Srgrimes user = (pw = getpwuid(getuid())) ? 1221558Srgrimes pw->pw_name : "???"; 1231558Srgrimes if (dohalt) { 1241558Srgrimes openlog("halt", 0, LOG_AUTH | LOG_CONS); 1251558Srgrimes syslog(LOG_CRIT, "halted by %s", user); 1261558Srgrimes } else { 1271558Srgrimes openlog("reboot", 0, LOG_AUTH | LOG_CONS); 1281558Srgrimes syslog(LOG_CRIT, "rebooted by %s", user); 1291558Srgrimes } 1301558Srgrimes } 1311558Srgrimes logwtmp("~", "shutdown", ""); 1321558Srgrimes 1331558Srgrimes /* 1341558Srgrimes * Do a sync early on, so disks start transfers while we're off 1351558Srgrimes * killing processes. Don't worry about writes done before the 1361558Srgrimes * processes die, the reboot system call syncs the disks. 1371558Srgrimes */ 1381558Srgrimes if (!nflag) 1391558Srgrimes sync(); 1401558Srgrimes 1411558Srgrimes /* Just stop init -- if we fail, we'll restart it. */ 1421558Srgrimes if (kill(1, SIGTSTP) == -1) 14326676Scharnier err(1, "SIGTSTP init"); 1441558Srgrimes 1451558Srgrimes /* Ignore the SIGHUP we get when our parent shell dies. */ 1461558Srgrimes (void)signal(SIGHUP, SIG_IGN); 1471558Srgrimes 1481558Srgrimes /* Send a SIGTERM first, a chance to save the buffers. */ 1491558Srgrimes if (kill(-1, SIGTERM) == -1) 15026676Scharnier err(1, "SIGTERM processes"); 1511558Srgrimes 1521558Srgrimes /* 1531558Srgrimes * After the processes receive the signal, start the rest of the 1541558Srgrimes * buffers on their way. Wait 5 seconds between the SIGTERM and 15574525Siedowse * the SIGKILL to give everybody a chance. If there is a lot of 15674525Siedowse * paging activity then wait longer, up to a maximum of approx 15774525Siedowse * 60 seconds. 1581558Srgrimes */ 1591558Srgrimes sleep(2); 16074525Siedowse for (i = 0; i < 20; i++) { 16174525Siedowse pageins = get_pageins(); 16274525Siedowse if (!nflag) 16374525Siedowse sync(); 16474525Siedowse sleep(3); 16574525Siedowse if (get_pageins() == pageins) 16674525Siedowse break; 16774525Siedowse } 1681558Srgrimes 1691558Srgrimes for (i = 1;; ++i) { 1701558Srgrimes if (kill(-1, SIGKILL) == -1) { 1711558Srgrimes if (errno == ESRCH) 1721558Srgrimes break; 1731558Srgrimes goto restart; 1741558Srgrimes } 1751558Srgrimes if (i > 5) { 1761558Srgrimes (void)fprintf(stderr, 1771558Srgrimes "WARNING: some process(es) wouldn't die\n"); 1781558Srgrimes break; 1791558Srgrimes } 1801558Srgrimes (void)sleep(2 * i); 1811558Srgrimes } 1821558Srgrimes 1831558Srgrimes reboot(howto); 1841558Srgrimes /* FALLTHROUGH */ 1851558Srgrimes 1861558Srgrimesrestart: 1871558Srgrimes sverrno = errno; 18826676Scharnier errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 1891558Srgrimes strerror(sverrno)); 1901558Srgrimes /* NOTREACHED */ 1911558Srgrimes} 1921558Srgrimes 1931558Srgrimesvoid 1941558Srgrimesusage() 1951558Srgrimes{ 19653523Sjdp (void)fprintf(stderr, "usage: %s [-dnpq]\n", 19753523Sjdp dohalt ? "halt" : "reboot"); 1981558Srgrimes exit(1); 1991558Srgrimes} 20074525Siedowse 20174525Siedowseu_int 20274525Siedowseget_pageins() 20374525Siedowse{ 20474525Siedowse u_int pageins; 20574525Siedowse size_t len; 20674525Siedowse 20774525Siedowse len = sizeof(pageins); 20874525Siedowse if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 20974525Siedowse != 0) { 21074525Siedowse warnx("v_swappgsin"); 21174525Siedowse return (0); 21274525Siedowse } 21374525Siedowse return pageins; 21474525Siedowse} 215