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 * 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.
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
29 * @(#) Copyright (c) 1980, 1986, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)reboot.c 8.1 (Berkeley) 6/5/93
31 * $FreeBSD: src/sbin/reboot/reboot.c,v 1.9.2.4 2002/04/28 22:50:00 wes Exp $
34 #include <sys/reboot.h>
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
50 static void usage(void) __dead2;
51 static u_int get_pageins(void);
56 main(int argc, char *argv[])
59 int ch, howto, i, lflag, nflag, qflag, sverrno;
64 if (strstr((p = strrchr(*argv, '/')) ? p + 1 : *argv, "halt")) {
69 lflag = nflag = qflag = 0;
70 while ((ch = getopt(argc, argv, "dlnpq")) != -1)
83 howto |= (RB_POWEROFF | RB_HALT);
95 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
96 errx(1, "cannot dump (-d) when halting; must reboot instead");
107 /* Log the reboot. */
109 if ((user = getlogin()) == NULL)
110 user = (pw = getpwuid(getuid())) ?
113 openlog("halt", 0, LOG_AUTH | LOG_CONS);
114 syslog(LOG_CRIT, "halted by %s", user);
116 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
117 syslog(LOG_CRIT, "rebooted by %s", user);
120 logwtmpx("~", "shutdown", "", 0, INIT_PROCESS);
123 * Do a sync early on, so disks start transfers while we're off
124 * killing processes. Don't worry about writes done before the
125 * processes die, the reboot system call syncs the disks.
130 /* Just stop init -- if we fail, we'll restart it. */
131 if (kill(1, SIGTSTP) == -1)
132 err(1, "SIGTSTP init");
134 /* Ignore the SIGHUP we get when our parent shell dies. */
135 signal(SIGHUP, SIG_IGN);
136 /* parent shell might also send a SIGTERM? Best to ignore as well */
137 signal(SIGTERM, SIG_IGN);
139 /* Send a SIGTERM first, a chance to save the buffers. */
140 if (kill(-1, SIGTERM) == -1)
141 err(1, "SIGTERM processes");
144 * After the processes receive the signal, start the rest of the
145 * buffers on their way. Wait 5 seconds between the SIGTERM and
146 * the SIGKILL to give everybody a chance. If there is a lot of
147 * paging activity then wait longer, up to a maximum of approx
151 for (i = 0; i < 20; i++) {
152 pageins = get_pageins();
156 if (get_pageins() == pageins)
161 if (kill(-1, SIGKILL) == -1) {
168 "WARNING: some process(es) wouldn't die\n");
179 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
187 fprintf(stderr, "usage: %s [-dnpq]\n", dohalt ? "halt" : "reboot");
197 len = sizeof(pageins);
198 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
200 warnx("v_swappgsin");