reboot.c revision 293232
1/* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if 0 31#ifndef lint 32static const char copyright[] = 33"@(#) Copyright (c) 1980, 1986, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35#endif /* not lint */ 36 37#ifndef lint 38static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 39#endif /* not lint */ 40#endif 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: stable/10/sbin/reboot/reboot.c 293232 2016-01-06 09:56:06Z smh $"); 43 44#include <sys/reboot.h> 45#include <sys/time.h> 46#include <sys/types.h> 47#include <sys/sysctl.h> 48#include <signal.h> 49#include <err.h> 50#include <errno.h> 51#include <fcntl.h> 52#include <pwd.h> 53#include <syslog.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57#include <unistd.h> 58#include <utmpx.h> 59 60static void usage(void); 61static u_int get_pageins(void); 62 63static int dohalt; 64 65int 66main(int argc, char *argv[]) 67{ 68 struct utmpx utx; 69 const struct passwd *pw; 70 int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag; 71 u_int pageins; 72 const char *user, *kernel = NULL; 73 74 if (strcmp(getprogname(), "halt") == 0) { 75 dohalt = 1; 76 howto = RB_HALT; 77 } else 78 howto = 0; 79 lflag = nflag = qflag = Nflag = 0; 80 while ((ch = getopt(argc, argv, "dk:lNnpq")) != -1) 81 switch(ch) { 82 case 'd': 83 howto |= RB_DUMP; 84 break; 85 case 'k': 86 kernel = optarg; 87 break; 88 case 'l': 89 lflag = 1; 90 break; 91 case 'n': 92 nflag = 1; 93 howto |= RB_NOSYNC; 94 break; 95 case 'N': 96 nflag = 1; 97 Nflag = 1; 98 break; 99 case 'p': 100 howto |= RB_POWEROFF; 101 break; 102 case 'q': 103 qflag = 1; 104 break; 105 case '?': 106 default: 107 usage(); 108 } 109 argc -= optind; 110 argv += optind; 111 112 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 113 errx(1, "cannot dump (-d) when halting; must reboot instead"); 114 if (Nflag && (howto & RB_NOSYNC) != 0) 115 errx(1, "-N cannot be used with -n"); 116 if (geteuid()) { 117 errno = EPERM; 118 err(1, NULL); 119 } 120 121 if (qflag) { 122 reboot(howto); 123 err(1, NULL); 124 } 125 126 if (kernel != NULL) { 127 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC, 128 0444); 129 if (fd > -1) { 130 (void)write(fd, "nextboot_enable=\"YES\"\n", 22); 131 (void)write(fd, "kernel=\"", 8L); 132 (void)write(fd, kernel, strlen(kernel)); 133 (void)write(fd, "\"\n", 2); 134 close(fd); 135 } 136 } 137 138 /* Log the reboot. */ 139 if (!lflag) { 140 if ((user = getlogin()) == NULL) 141 user = (pw = getpwuid(getuid())) ? 142 pw->pw_name : "???"; 143 if (dohalt) { 144 openlog("halt", 0, LOG_AUTH | LOG_CONS); 145 syslog(LOG_CRIT, "halted by %s", user); 146 } else { 147 openlog("reboot", 0, LOG_AUTH | LOG_CONS); 148 syslog(LOG_CRIT, "rebooted by %s", user); 149 } 150 } 151 utx.ut_type = SHUTDOWN_TIME; 152 gettimeofday(&utx.ut_tv, NULL); 153 pututxline(&utx); 154 155 /* 156 * Do a sync early on, so disks start transfers while we're off 157 * killing processes. Don't worry about writes done before the 158 * processes die, the reboot system call syncs the disks. 159 */ 160 if (!nflag) 161 sync(); 162 163 /* 164 * Ignore signals that we can get as a result of killing 165 * parents, group leaders, etc. 166 */ 167 (void)signal(SIGHUP, SIG_IGN); 168 (void)signal(SIGINT, SIG_IGN); 169 (void)signal(SIGQUIT, SIG_IGN); 170 (void)signal(SIGTERM, SIG_IGN); 171 (void)signal(SIGTSTP, SIG_IGN); 172 173 /* 174 * If we're running in a pipeline, we don't want to die 175 * after killing whatever we're writing to. 176 */ 177 (void)signal(SIGPIPE, SIG_IGN); 178 179 /* Just stop init -- if we fail, we'll restart it. */ 180 if (kill(1, SIGTSTP) == -1) 181 err(1, "SIGTSTP init"); 182 183 /* Send a SIGTERM first, a chance to save the buffers. */ 184 if (kill(-1, SIGTERM) == -1 && errno != ESRCH) 185 err(1, "SIGTERM processes"); 186 187 /* 188 * After the processes receive the signal, start the rest of the 189 * buffers on their way. Wait 5 seconds between the SIGTERM and 190 * the SIGKILL to give everybody a chance. If there is a lot of 191 * paging activity then wait longer, up to a maximum of approx 192 * 60 seconds. 193 */ 194 sleep(2); 195 for (i = 0; i < 20; i++) { 196 pageins = get_pageins(); 197 if (!nflag) 198 sync(); 199 sleep(3); 200 if (get_pageins() == pageins) 201 break; 202 } 203 204 for (i = 1;; ++i) { 205 if (kill(-1, SIGKILL) == -1) { 206 if (errno == ESRCH) 207 break; 208 goto restart; 209 } 210 if (i > 5) { 211 (void)fprintf(stderr, 212 "WARNING: some process(es) wouldn't die\n"); 213 break; 214 } 215 (void)sleep(2 * i); 216 } 217 218 reboot(howto); 219 /* FALLTHROUGH */ 220 221restart: 222 sverrno = errno; 223 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 224 strerror(sverrno)); 225 /* NOTREACHED */ 226} 227 228static void 229usage(void) 230{ 231 232 (void)fprintf(stderr, dohalt ? 233 "usage: halt [-lnpq] [-k kernel]\n" : 234 "usage: reboot [-dlnpq] [-k kernel]\n"); 235 exit(1); 236} 237 238static u_int 239get_pageins(void) 240{ 241 u_int pageins; 242 size_t len; 243 244 len = sizeof(pageins); 245 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 246 != 0) { 247 warnx("v_swappgsin"); 248 return (0); 249 } 250 return pageins; 251} 252