]>
git.saurik.com Git - apple/system_cmds.git/blob - reboot.tproj/reboot.c
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
44 static const char rcsid
[] =
45 "$FreeBSD: src/sbin/reboot/reboot.c,v 1.17 2002/10/06 16:24:36 thomas Exp $";
48 #include <sys/reboot.h>
49 #include <sys/types.h>
50 #include <sys/sysctl.h>
64 u_int
get_pageins(void);
69 main(int argc
, char *argv
[])
72 int ch
, howto
, i
, fd
, kflag
, lflag
, nflag
, qflag
, pflag
, sverrno
;
77 if (strstr((p
= rindex(*argv
, '/')) ? p
+ 1 : *argv
, "halt")) {
82 kflag
= lflag
= nflag
= qflag
= 0;
84 while ((ch
= getopt(argc
, argv
, "dk:lnpq")) != -1)
86 while ((ch
= getopt(argc
, argv
, "lnq")) != -1)
105 /* -p is irrelevant on OS X. It does that anyway. */
109 howto
|= RB_POWEROFF
;
123 if ((howto
& (RB_DUMP
| RB_HALT
)) == (RB_DUMP
| RB_HALT
))
124 errx(1, "cannot dump (-d) when halting; must reboot instead");
138 fd
= open("/boot/nextboot.conf", O_WRONLY
| O_CREAT
, 0444);
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);
149 /* Log the reboot. */
151 if ((user
= getlogin()) == NULL
)
152 user
= (pw
= getpwuid(getuid())) ?
155 openlog("halt", 0, LOG_AUTH
| LOG_CONS
);
156 syslog(LOG_CRIT
, "halted by %s", user
);
158 openlog("reboot", 0, LOG_AUTH
| LOG_CONS
);
159 syslog(LOG_CRIT
, "rebooted by %s", user
);
162 logwtmp("~", "shutdown", "");
165 * Do a sync early on, so disks start transfers while we're off
166 * killing processes. Don't worry about writes done before the
167 * processes die, the reboot system call syncs the disks.
172 /* Just stop init -- if we fail, we'll restart it. */
173 if (kill(1, SIGTSTP
) == -1)
174 err(1, "SIGTSTP init");
176 /* Ignore the SIGHUP we get when our parent shell dies. */
177 (void)signal(SIGHUP
, SIG_IGN
);
180 /* Send a SIGTERM first, a chance to save the buffers. */
181 if (kill(-1, SIGTERM
) == -1)
182 err(1, "SIGTERM processes");
185 * After the processes receive the signal, start the rest of the
186 * buffers on their way. Wait 5 seconds between the SIGTERM and
187 * the SIGKILL to give everybody a chance. If there is a lot of
188 * paging activity then wait longer, up to a maximum of approx
192 for (i
= 0; i
< 20; i
++) {
193 pageins
= get_pageins();
197 if (get_pageins() == pageins
)
202 if (kill(-1, SIGKILL
) == -1) {
208 (void)fprintf(stderr
,
209 "WARNING: some process(es) wouldn't die\n");
221 errx(1, "%s%s", kill(1, SIGHUP
) == -1 ? "(can't restart init): " : "",
230 (void)fprintf(stderr
, "usage: %s [-dnpq] [-k kernel]\n",
232 (void)fprintf(stderr
, "usage: %s [-lnq]\n",
234 dohalt
? "halt" : "reboot");
244 len
= sizeof(pageins
);
245 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins
, &len
, NULL
, 0)
247 warnx("v_swappgsin");