2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1986, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/reboot.h>
48 #include <sys/types.h>
49 #include <sys/sysctl.h>
62 static void usage(void);
63 static u_int get_pageins(void);
68 main(int argc, char *argv[])
71 const struct passwd *pw;
72 int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag;
74 const char *user, *kernel = NULL;
76 if (strcmp(getprogname(), "halt") == 0) {
81 lflag = nflag = qflag = Nflag = 0;
82 while ((ch = getopt(argc, argv, "cdk:lNnpqr")) != -1)
85 howto |= RB_POWERCYCLE;
105 howto |= RB_POWEROFF;
120 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
121 errx(1, "cannot dump (-d) when halting; must reboot instead");
122 if (Nflag && (howto & RB_NOSYNC) != 0)
123 errx(1, "-N cannot be used with -n");
124 if ((howto & RB_POWEROFF) && (howto & RB_POWERCYCLE))
125 errx(1, "-c and -p cannot be used together");
126 if ((howto & RB_REROOT) != 0 && howto != RB_REROOT)
127 errx(1, "-r cannot be used with -c, -d, -n, or -p");
138 if (kernel != NULL) {
139 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC,
142 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
143 (void)write(fd, "kernel=\"", 8L);
144 (void)write(fd, kernel, strlen(kernel));
145 (void)write(fd, "\"\n", 2);
150 /* Log the reboot. */
152 if ((user = getlogin()) == NULL)
153 user = (pw = getpwuid(getuid())) ?
156 openlog("halt", 0, LOG_AUTH | LOG_CONS);
157 syslog(LOG_CRIT, "halted by %s", user);
158 } else if (howto & RB_REROOT) {
159 openlog("reroot", 0, LOG_AUTH | LOG_CONS);
160 syslog(LOG_CRIT, "rerooted by %s", user);
161 } else if (howto & RB_POWEROFF) {
162 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
163 syslog(LOG_CRIT, "powered off by %s", user);
164 } else if (howto & RB_POWERCYCLE) {
165 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
166 syslog(LOG_CRIT, "power cycled by %s", user);
168 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
169 syslog(LOG_CRIT, "rebooted by %s", user);
172 utx.ut_type = SHUTDOWN_TIME;
173 gettimeofday(&utx.ut_tv, NULL);
177 * Do a sync early on, so disks start transfers while we're off
178 * killing processes. Don't worry about writes done before the
179 * processes die, the reboot system call syncs the disks.
185 * Ignore signals that we can get as a result of killing
186 * parents, group leaders, etc.
188 (void)signal(SIGHUP, SIG_IGN);
189 (void)signal(SIGINT, SIG_IGN);
190 (void)signal(SIGQUIT, SIG_IGN);
191 (void)signal(SIGTERM, SIG_IGN);
192 (void)signal(SIGTSTP, SIG_IGN);
195 * If we're running in a pipeline, we don't want to die
196 * after killing whatever we're writing to.
198 (void)signal(SIGPIPE, SIG_IGN);
201 * Only init(8) can perform rerooting.
203 if (howto & RB_REROOT) {
204 if (kill(1, SIGEMT) == -1)
205 err(1, "SIGEMT init");
210 /* Just stop init -- if we fail, we'll restart it. */
211 if (kill(1, SIGTSTP) == -1)
212 err(1, "SIGTSTP init");
214 /* Send a SIGTERM first, a chance to save the buffers. */
215 if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
216 err(1, "SIGTERM processes");
219 * After the processes receive the signal, start the rest of the
220 * buffers on their way. Wait 5 seconds between the SIGTERM and
221 * the SIGKILL to give everybody a chance. If there is a lot of
222 * paging activity then wait longer, up to a maximum of approx
226 for (i = 0; i < 20; i++) {
227 pageins = get_pageins();
231 if (get_pageins() == pageins)
236 if (kill(-1, SIGKILL) == -1) {
242 (void)fprintf(stderr,
243 "WARNING: some process(es) wouldn't die\n");
254 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
263 (void)fprintf(stderr, dohalt ?
264 "usage: halt [-clNnpq] [-k kernel]\n" :
265 "usage: reboot [-cdlNnpqr] [-k kernel]\n");
275 len = sizeof(pageins);
276 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
278 warnx("v_swappgsin");