2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1986, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/reboot.h>
46 #include <sys/types.h>
47 #include <sys/sysctl.h>
60 static void usage(void);
61 static u_int get_pageins(void);
66 main(int argc, char *argv[])
69 const struct passwd *pw;
70 int ch, howto, i, fd, lflag, nflag, qflag, sverrno, Nflag;
72 const char *user, *kernel = NULL;
74 if (strcmp(getprogname(), "halt") == 0) {
79 lflag = nflag = qflag = Nflag = 0;
80 while ((ch = getopt(argc, argv, "dk:lNnpqr")) != -1)
100 howto |= RB_POWEROFF;
117 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
118 errx(1, "cannot dump (-d) when halting; must reboot instead");
119 if (Nflag && (howto & RB_NOSYNC) != 0)
120 errx(1, "-N cannot be used with -n");
121 if ((howto & RB_REROOT) != 0 && howto != RB_REROOT)
122 errx(1, "-r cannot be used with -d, -n, or -p");
133 if (kernel != NULL) {
134 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT | O_TRUNC,
137 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
138 (void)write(fd, "kernel=\"", 8L);
139 (void)write(fd, kernel, strlen(kernel));
140 (void)write(fd, "\"\n", 2);
145 /* Log the reboot. */
147 if ((user = getlogin()) == NULL)
148 user = (pw = getpwuid(getuid())) ?
151 openlog("halt", 0, LOG_AUTH | LOG_CONS);
152 syslog(LOG_CRIT, "halted by %s", user);
153 } else if (howto & RB_REROOT) {
154 openlog("reroot", 0, LOG_AUTH | LOG_CONS);
155 syslog(LOG_CRIT, "rerooted by %s", user);
157 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
158 syslog(LOG_CRIT, "rebooted by %s", user);
161 utx.ut_type = SHUTDOWN_TIME;
162 gettimeofday(&utx.ut_tv, NULL);
166 * Do a sync early on, so disks start transfers while we're off
167 * killing processes. Don't worry about writes done before the
168 * processes die, the reboot system call syncs the disks.
174 * Ignore signals that we can get as a result of killing
175 * parents, group leaders, etc.
177 (void)signal(SIGHUP, SIG_IGN);
178 (void)signal(SIGINT, SIG_IGN);
179 (void)signal(SIGQUIT, SIG_IGN);
180 (void)signal(SIGTERM, SIG_IGN);
181 (void)signal(SIGTSTP, SIG_IGN);
184 * If we're running in a pipeline, we don't want to die
185 * after killing whatever we're writing to.
187 (void)signal(SIGPIPE, SIG_IGN);
190 * Only init(8) can perform rerooting.
192 if (howto & RB_REROOT) {
193 if (kill(1, SIGEMT) == -1)
194 err(1, "SIGEMT init");
199 /* Just stop init -- if we fail, we'll restart it. */
200 if (kill(1, SIGTSTP) == -1)
201 err(1, "SIGTSTP init");
203 /* Send a SIGTERM first, a chance to save the buffers. */
204 if (kill(-1, SIGTERM) == -1 && errno != ESRCH)
205 err(1, "SIGTERM processes");
208 * After the processes receive the signal, start the rest of the
209 * buffers on their way. Wait 5 seconds between the SIGTERM and
210 * the SIGKILL to give everybody a chance. If there is a lot of
211 * paging activity then wait longer, up to a maximum of approx
215 for (i = 0; i < 20; i++) {
216 pageins = get_pageins();
220 if (get_pageins() == pageins)
225 if (kill(-1, SIGKILL) == -1) {
231 (void)fprintf(stderr,
232 "WARNING: some process(es) wouldn't die\n");
243 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
252 (void)fprintf(stderr, dohalt ?
253 "usage: halt [-lNnpq] [-k kernel]\n" :
254 "usage: reboot [-dlNnpqr] [-k kernel]\n");
264 len = sizeof(pageins);
265 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
267 warnx("v_swappgsin");