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 * 3. 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 32 static 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 38 static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 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 60 static void usage(void); 61 static u_int get_pageins(void); 62 63 static int dohalt; 64 65 int 66 main(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, "cdk:lNnpqr")) != -1) 81 switch(ch) { 82 case 'c': 83 howto |= RB_POWERCYCLE; 84 break; 85 case 'd': 86 howto |= RB_DUMP; 87 break; 88 case 'k': 89 kernel = optarg; 90 break; 91 case 'l': 92 lflag = 1; 93 break; 94 case 'n': 95 nflag = 1; 96 howto |= RB_NOSYNC; 97 break; 98 case 'N': 99 nflag = 1; 100 Nflag = 1; 101 break; 102 case 'p': 103 howto |= RB_POWEROFF; 104 break; 105 case 'q': 106 qflag = 1; 107 break; 108 case 'r': 109 howto |= RB_REROOT; 110 break; 111 case '?': 112 default: 113 usage(); 114 } 115 argc -= optind; 116 argv += optind; 117 118 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 119 errx(1, "cannot dump (-d) when halting; must reboot instead"); 120 if (Nflag && (howto & RB_NOSYNC) != 0) 121 errx(1, "-N cannot be used with -n"); 122 if ((howto & RB_POWEROFF) && (howto & RB_POWERCYCLE)) 123 errx(1, "-c and -p cannot be used together"); 124 if ((howto & RB_REROOT) != 0 && howto != RB_REROOT) 125 errx(1, "-r cannot be used with -c, -d, -n, or -p"); 126 if (geteuid()) { 127 errno = EPERM; 128 err(1, NULL); 129 } 130 131 if (qflag) { 132 reboot(howto); 133 err(1, NULL); 134 } 135 136 if (kernel != NULL) { 137 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC, 138 0444); 139 if (fd > -1) { 140 (void)write(fd, "nextboot_enable=\"YES\"\n", 22); 141 (void)write(fd, "kernel=\"", 8L); 142 (void)write(fd, kernel, strlen(kernel)); 143 (void)write(fd, "\"\n", 2); 144 close(fd); 145 } 146 } 147 148 /* Log the reboot. */ 149 if (!lflag) { 150 if ((user = getlogin()) == NULL) 151 user = (pw = getpwuid(getuid())) ? 152 pw->pw_name : "???"; 153 if (dohalt) { 154 openlog("halt", 0, LOG_AUTH | LOG_CONS); 155 syslog(LOG_CRIT, "halted by %s", user); 156 } else if (howto & RB_REROOT) { 157 openlog("reroot", 0, LOG_AUTH | LOG_CONS); 158 syslog(LOG_CRIT, "rerooted by %s", user); 159 } else if (howto & RB_POWEROFF) { 160 openlog("reboot", 0, LOG_AUTH | LOG_CONS); 161 syslog(LOG_CRIT, "powered off by %s", user); 162 } else if (howto & RB_POWERCYCLE) { 163 openlog("reboot", 0, LOG_AUTH | LOG_CONS); 164 syslog(LOG_CRIT, "power cycled by %s", user); 165 } else { 166 openlog("reboot", 0, LOG_AUTH | LOG_CONS); 167 syslog(LOG_CRIT, "rebooted by %s", user); 168 } 169 } 170 utx.ut_type = SHUTDOWN_TIME; 171 gettimeofday(&utx.ut_tv, NULL); 172 pututxline(&utx); 173 174 /* 175 * Do a sync early on, so disks start transfers while we're off 176 * killing processes. Don't worry about writes done before the 177 * processes die, the reboot system call syncs the disks. 178 */ 179 if (!nflag) 180 sync(); 181 182 /* 183 * Ignore signals that we can get as a result of killing 184 * parents, group leaders, etc. 185 */ 186 (void)signal(SIGHUP, SIG_IGN); 187 (void)signal(SIGINT, SIG_IGN); 188 (void)signal(SIGQUIT, SIG_IGN); 189 (void)signal(SIGTERM, SIG_IGN); 190 (void)signal(SIGTSTP, SIG_IGN); 191 192 /* 193 * If we're running in a pipeline, we don't want to die 194 * after killing whatever we're writing to. 195 */ 196 (void)signal(SIGPIPE, SIG_IGN); 197 198 /* 199 * Only init(8) can perform rerooting. 200 */ 201 if (howto & RB_REROOT) { 202 if (kill(1, SIGEMT) == -1) 203 err(1, "SIGEMT init"); 204 205 return (0); 206 } 207 208 /* Just stop init -- if we fail, we'll restart it. */ 209 if (kill(1, SIGTSTP) == -1) 210 err(1, "SIGTSTP init"); 211 212 /* Send a SIGTERM first, a chance to save the buffers. */ 213 if (kill(-1, SIGTERM) == -1 && errno != ESRCH) 214 err(1, "SIGTERM processes"); 215 216 /* 217 * After the processes receive the signal, start the rest of the 218 * buffers on their way. Wait 5 seconds between the SIGTERM and 219 * the SIGKILL to give everybody a chance. If there is a lot of 220 * paging activity then wait longer, up to a maximum of approx 221 * 60 seconds. 222 */ 223 sleep(2); 224 for (i = 0; i < 20; i++) { 225 pageins = get_pageins(); 226 if (!nflag) 227 sync(); 228 sleep(3); 229 if (get_pageins() == pageins) 230 break; 231 } 232 233 for (i = 1;; ++i) { 234 if (kill(-1, SIGKILL) == -1) { 235 if (errno == ESRCH) 236 break; 237 goto restart; 238 } 239 if (i > 5) { 240 (void)fprintf(stderr, 241 "WARNING: some process(es) wouldn't die\n"); 242 break; 243 } 244 (void)sleep(2 * i); 245 } 246 247 reboot(howto); 248 /* FALLTHROUGH */ 249 250 restart: 251 sverrno = errno; 252 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 253 strerror(sverrno)); 254 /* NOTREACHED */ 255 } 256 257 static void 258 usage(void) 259 { 260 261 (void)fprintf(stderr, dohalt ? 262 "usage: halt [-lNnpq] [-k kernel]\n" : 263 "usage: reboot [-dlNnpqr] [-k kernel]\n"); 264 exit(1); 265 } 266 267 static u_int 268 get_pageins(void) 269 { 270 u_int pageins; 271 size_t len; 272 273 len = sizeof(pageins); 274 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 275 != 0) { 276 warnx("v_swappgsin"); 277 return (0); 278 } 279 return pageins; 280 } 281