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 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/types.h> 46 #include <sys/sysctl.h> 47 #include <signal.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <libutil.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 59 static void usage(void); 60 static u_int get_pageins(void); 61 62 int dohalt; 63 64 int 65 main(int argc, char *argv[]) 66 { 67 const struct passwd *pw; 68 int ch, howto, i, fd, lflag, nflag, qflag, pflag, sverrno; 69 u_int pageins; 70 const char *p, *user, *kernel = NULL; 71 72 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { 73 dohalt = 1; 74 howto = RB_HALT; 75 } else 76 howto = 0; 77 lflag = nflag = qflag = 0; 78 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1) 79 switch(ch) { 80 case 'd': 81 howto |= RB_DUMP; 82 break; 83 case 'k': 84 kernel = optarg; 85 break; 86 case 'l': 87 lflag = 1; 88 break; 89 case 'n': 90 nflag = 1; 91 howto |= RB_NOSYNC; 92 break; 93 case 'p': 94 pflag = 1; 95 howto |= RB_POWEROFF; 96 break; 97 case 'q': 98 qflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) 108 errx(1, "cannot dump (-d) when halting; must reboot instead"); 109 if (geteuid()) { 110 errno = EPERM; 111 err(1, NULL); 112 } 113 114 if (qflag) { 115 reboot(howto); 116 err(1, NULL); 117 } 118 119 if (kernel != NULL) { 120 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444); 121 if (fd > -1) { 122 (void)write(fd, "nextboot_enable=\"YES\"\n", 22); 123 (void)write(fd, "kernel=\"", 8L); 124 (void)write(fd, kernel, strlen(kernel)); 125 (void)write(fd, "\"\n", 2); 126 close(fd); 127 } 128 } 129 130 /* Log the reboot. */ 131 if (!lflag) { 132 if ((user = getlogin()) == NULL) 133 user = (pw = getpwuid(getuid())) ? 134 pw->pw_name : "???"; 135 if (dohalt) { 136 openlog("halt", 0, LOG_AUTH | LOG_CONS); 137 syslog(LOG_CRIT, "halted by %s", user); 138 } else { 139 openlog("reboot", 0, LOG_AUTH | LOG_CONS); 140 syslog(LOG_CRIT, "rebooted by %s", user); 141 } 142 } 143 logwtmp("~", "shutdown", ""); 144 145 /* 146 * Do a sync early on, so disks start transfers while we're off 147 * killing processes. Don't worry about writes done before the 148 * processes die, the reboot system call syncs the disks. 149 */ 150 if (!nflag) 151 sync(); 152 153 /* Just stop init -- if we fail, we'll restart it. */ 154 if (kill(1, SIGTSTP) == -1) 155 err(1, "SIGTSTP init"); 156 157 /* Ignore the SIGHUP we get when our parent shell dies. */ 158 (void)signal(SIGHUP, SIG_IGN); 159 160 /* Send a SIGTERM first, a chance to save the buffers. */ 161 if (kill(-1, SIGTERM) == -1 && errno != ESRCH) 162 err(1, "SIGTERM processes"); 163 164 /* 165 * After the processes receive the signal, start the rest of the 166 * buffers on their way. Wait 5 seconds between the SIGTERM and 167 * the SIGKILL to give everybody a chance. If there is a lot of 168 * paging activity then wait longer, up to a maximum of approx 169 * 60 seconds. 170 */ 171 sleep(2); 172 for (i = 0; i < 20; i++) { 173 pageins = get_pageins(); 174 if (!nflag) 175 sync(); 176 sleep(3); 177 if (get_pageins() == pageins) 178 break; 179 } 180 181 for (i = 1;; ++i) { 182 if (kill(-1, SIGKILL) == -1) { 183 if (errno == ESRCH) 184 break; 185 goto restart; 186 } 187 if (i > 5) { 188 (void)fprintf(stderr, 189 "WARNING: some process(es) wouldn't die\n"); 190 break; 191 } 192 (void)sleep(2 * i); 193 } 194 195 reboot(howto); 196 /* FALLTHROUGH */ 197 198 restart: 199 sverrno = errno; 200 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", 201 strerror(sverrno)); 202 /* NOTREACHED */ 203 } 204 205 static void 206 usage() 207 { 208 (void)fprintf(stderr, "usage: %s [-%slnpq] [-k kernel]\n", 209 getprogname(), dohalt ? "" : "d"); 210 exit(1); 211 } 212 213 static u_int 214 get_pageins() 215 { 216 u_int pageins; 217 size_t len; 218 219 len = sizeof(pageins); 220 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) 221 != 0) { 222 warnx("v_swappgsin"); 223 return (0); 224 } 225 return pageins; 226 } 227