]> git.saurik.com Git - apple/system_cmds.git/blame - reboot.tproj/reboot.c
system_cmds-735.tar.gz
[apple/system_cmds.git] / reboot.tproj / reboot.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1980, 1986, 1993
20e66415 3 * The Regents of the University of California. All rights reserved.
34d340d7 4 * Portions copyright (c) 2007 Apple Inc. All rights reserved.
1815bff5
A
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
34d340d7 14 * 3. Neither the name of the University nor the names of its contributors
1815bff5
A
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
cf37c299
A
31#include <sys/cdefs.h>
32
20e66415 33#ifndef lint
cf37c299 34__unused static const char copyright[] =
20e66415
A
35"@(#) Copyright (c) 1980, 1986, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37#endif /* not lint */
38
39#ifndef lint
40#if 0
41static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
42#endif
cf37c299 43__unused static const char rcsid[] =
20e66415
A
44 "$FreeBSD: src/sbin/reboot/reboot.c,v 1.17 2002/10/06 16:24:36 thomas Exp $";
45#endif /* not lint */
46
1815bff5 47#include <sys/reboot.h>
20e66415
A
48#include <sys/types.h>
49#include <sys/sysctl.h>
1815bff5 50#include <signal.h>
20e66415 51#include <err.h>
1815bff5 52#include <errno.h>
20e66415
A
53#include <fcntl.h>
54#include <util.h>
55#include <pwd.h>
1815bff5 56#include <syslog.h>
1815bff5
A
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
20e66415 60#include <unistd.h>
1815bff5 61
b180c247 62#ifdef __APPLE__
916eb79e
A
63#include <TargetConditionals.h>
64#if !TARGET_OS_EMBEDDED
b180c247
A
65#include "kextmanager.h"
66#include <IOKit/kext/kextmanager_types.h>
916eb79e 67#endif
b180c247
A
68#include <mach/mach_port.h> // allocate
69#include <mach/mach.h> // task_self, etc
70#include <servers/bootstrap.h> // bootstrap
fc6d9e4b 71#include <bootstrap_priv.h>
34d340d7 72#include <reboot2.h>
ef8ad44b
A
73#include <utmpx.h>
74#include <sys/time.h>
b180c247
A
75#endif
76
20e66415
A
77void usage(void);
78u_int get_pageins(void);
916eb79e 79#if defined(__APPLE__) && !TARGET_OS_EMBEDDED
b180c247
A
80int reserve_reboot(void);
81#endif
1815bff5
A
82
83int dohalt;
84
85int
20e66415 86main(int argc, char *argv[])
1815bff5 87{
1815bff5 88 struct passwd *pw;
34d340d7
A
89 int ch, howto, kflag, lflag, nflag, qflag, uflag;
90 char *p;
20e66415 91 const char *user;
34d340d7
A
92#ifndef __APPLE__
93 int i, fd, pflag, sverrno;
94 u_int pageins;
95 char *kernel;
96#endif
1815bff5 97
20e66415 98 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
1815bff5
A
99 dohalt = 1;
100 howto = RB_HALT;
101 } else
102 howto = 0;
20e66415
A
103 kflag = lflag = nflag = qflag = 0;
104#ifndef __APPLE__
105 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
106#else
b180c247 107 while ((ch = getopt(argc, argv, "lnqu")) != -1)
20e66415 108#endif
1815bff5 109 switch(ch) {
20e66415
A
110#ifndef __APPLE__
111 case 'd':
112 howto |= RB_DUMP;
113 break;
20e66415
A
114 case 'k':
115 kflag = 1;
116 kernel = optarg;
117 break;
2fc1e207 118#endif
20e66415 119 case 'l':
1815bff5
A
120 lflag = 1;
121 break;
122 case 'n':
123 nflag = 1;
124 howto |= RB_NOSYNC;
125 break;
20e66415
A
126/* -p is irrelevant on OS X. It does that anyway. */
127#ifndef __APPLE__
128 case 'p':
129 pflag = 1;
130 howto |= RB_POWEROFF;
131 break;
132#endif
b180c247
A
133 case 'u':
134 uflag = 1;
135 howto |= RB_UPSDELAY;
136 break;
1815bff5
A
137 case 'q':
138 qflag = 1;
916eb79e 139 howto |= RB_QUICK;
1815bff5
A
140 break;
141 case '?':
142 default:
143 usage();
144 }
145 argc -= optind;
146 argv += optind;
147
20e66415
A
148#ifndef __APPLE__
149 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
150 errx(1, "cannot dump (-d) when halting; must reboot instead");
151#endif
152 if (geteuid()) {
153 errno = EPERM;
154 err(1, NULL);
155 }
1815bff5 156
916eb79e 157#if defined(__APPLE__) && !TARGET_OS_EMBEDDED
ef8ad44b
A
158 if (!qflag && !lflag) { // shutdown(8) has already checked w/kextd
159 if ((errno = reserve_reboot()))
b180c247
A
160 err(1, "couldn't lock for reboot");
161 }
162#endif
163
1815bff5
A
164 if (qflag) {
165 reboot(howto);
20e66415
A
166 err(1, NULL);
167 }
168
2fc1e207 169#ifndef __APPLE__
20e66415
A
170 if (kflag) {
171 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
172 if (fd > -1) {
173 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
174 (void)write(fd, "kernel=\"", 8L);
175 (void)write(fd, kernel, strlen(kernel));
176 (void)write(fd, "\"\n", 2);
177 close(fd);
178 }
1815bff5 179 }
2fc1e207 180#endif
1815bff5
A
181
182 /* Log the reboot. */
183 if (!lflag) {
184 if ((user = getlogin()) == NULL)
185 user = (pw = getpwuid(getuid())) ?
186 pw->pw_name : "???";
187 if (dohalt) {
188 openlog("halt", 0, LOG_AUTH | LOG_CONS);
b180c247
A
189 syslog(LOG_CRIT, "halted by %s%s", user,
190 (howto & RB_UPSDELAY) ? " with UPS delay":"");
1815bff5
A
191 } else {
192 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
193 syslog(LOG_CRIT, "rebooted by %s", user);
194 }
195 }
ef8ad44b
A
196#if defined(__APPLE__)
197 {
198 struct utmpx utx;
199 bzero(&utx, sizeof(utx));
fc6d9e4b 200 utx.ut_type = SHUTDOWN_TIME;
ef8ad44b
A
201 gettimeofday(&utx.ut_tv, NULL);
202 pututxline(&utx);
203
204 int newvalue = 1;
205 sysctlbyname("kern.willshutdown", NULL, NULL, &newvalue, sizeof(newvalue));
206 }
207#else
1815bff5 208 logwtmp("~", "shutdown", "");
ef8ad44b 209#endif
1815bff5
A
210
211 /*
212 * Do a sync early on, so disks start transfers while we're off
213 * killing processes. Don't worry about writes done before the
214 * processes die, the reboot system call syncs the disks.
215 */
216 if (!nflag)
217 sync();
218
34d340d7 219#ifndef __APPLE__
1815bff5
A
220 /* Just stop init -- if we fail, we'll restart it. */
221 if (kill(1, SIGTSTP) == -1)
20e66415 222 err(1, "SIGTSTP init");
34d340d7 223#endif
1815bff5
A
224
225 /* Ignore the SIGHUP we get when our parent shell dies. */
226 (void)signal(SIGHUP, SIG_IGN);
227
20e66415 228#ifndef __APPLE__
1815bff5
A
229 /* Send a SIGTERM first, a chance to save the buffers. */
230 if (kill(-1, SIGTERM) == -1)
20e66415 231 err(1, "SIGTERM processes");
1815bff5
A
232
233 /*
234 * After the processes receive the signal, start the rest of the
235 * buffers on their way. Wait 5 seconds between the SIGTERM and
20e66415
A
236 * the SIGKILL to give everybody a chance. If there is a lot of
237 * paging activity then wait longer, up to a maximum of approx
238 * 60 seconds.
1815bff5
A
239 */
240 sleep(2);
20e66415
A
241 for (i = 0; i < 20; i++) {
242 pageins = get_pageins();
243 if (!nflag)
244 sync();
245 sleep(3);
246 if (get_pageins() == pageins)
247 break;
248 }
1815bff5
A
249
250 for (i = 1;; ++i) {
251 if (kill(-1, SIGKILL) == -1) {
252 if (errno == ESRCH)
253 break;
254 goto restart;
255 }
256 if (i > 5) {
257 (void)fprintf(stderr,
258 "WARNING: some process(es) wouldn't die\n");
259 break;
260 }
261 (void)sleep(2 * i);
262 }
263#endif
20e66415 264
34d340d7
A
265#ifdef __APPLE__
266 // launchd(8) handles reboot. This call returns NULL on success.
cf37c299 267 exit(reboot3(howto) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
34d340d7 268#else /* __APPLE__ */
1815bff5
A
269 reboot(howto);
270 /* FALLTHROUGH */
271
272restart:
273 sverrno = errno;
20e66415 274 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
1815bff5
A
275 strerror(sverrno));
276 /* NOTREACHED */
34d340d7 277#endif /* __APPLE__ */
1815bff5
A
278}
279
280void
cf37c299 281usage(void)
1815bff5 282{
2fc1e207 283#ifndef __APPLE__
20e66415 284 (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
2fc1e207
A
285#else
286 (void)fprintf(stderr, "usage: %s [-lnq]\n",
287#endif
20e66415 288 dohalt ? "halt" : "reboot");
1815bff5
A
289 exit(1);
290}
291
20e66415 292u_int
cf37c299 293get_pageins(void)
1815bff5 294{
20e66415
A
295 u_int pageins;
296 size_t len;
297
298 len = sizeof(pageins);
299 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
300 != 0) {
301 warnx("v_swappgsin");
302 return (0);
303 }
304 return pageins;
1815bff5 305}
b180c247 306
916eb79e 307#if defined(__APPLE__) && !TARGET_OS_EMBEDDED
34d340d7 308// XX this routine is also in shutdown.tproj; it would be nice to share
b180c247 309
34d340d7 310#define WAITFORLOCK 1
b180c247
A
311/*
312 * contact kextd to lock for reboot
313 */
314int
cf37c299 315reserve_reboot(void)
b180c247 316{
34d340d7 317 int rval = ELAST + 1;
b180c247 318 kern_return_t macherr = KERN_FAILURE;
34d340d7
A
319 mach_port_t kxport, tport = MACH_PORT_NULL, myport = MACH_PORT_NULL;
320 int busyStatus = ELAST + 1;
321 mountpoint_t busyVol;
b180c247 322
fc6d9e4b 323 macherr = bootstrap_look_up2(bootstrap_port, KEXTD_SERVER_NAME, &kxport, 0, BOOTSTRAP_PRIVILEGED_SERVER);
b180c247
A
324 if (macherr) goto finish;
325
326 // allocate a port to pass to kextd (in case we die)
34d340d7
A
327 tport = mach_task_self();
328 if (tport == MACH_PORT_NULL) goto finish;
b180c247
A
329 macherr = mach_port_allocate(tport, MACH_PORT_RIGHT_RECEIVE, &myport);
330 if (macherr) goto finish;
331
34d340d7
A
332 // try to lock for reboot
333 macherr = kextmanager_lock_reboot(kxport, myport, !WAITFORLOCK, busyVol,
334 &busyStatus);
335 if (macherr) goto finish;
b180c247 336
34d340d7
A
337 if (busyStatus == EBUSY) {
338 warnx("%s is busy updating; waiting for lock", busyVol);
339 macherr = kextmanager_lock_reboot(kxport, myport, WAITFORLOCK,
340 busyVol, &busyStatus);
341 if (macherr) goto finish;
342 }
b180c247 343
34d340d7
A
344 if (busyStatus == EALREADY) {
345 // reboot already in progress
b180c247 346 rval = 0;
34d340d7
A
347 } else {
348 rval = busyStatus;
b180c247 349 }
34d340d7
A
350
351finish:
352 // in general, we want to err on the side of allowing the reboot
353 if (macherr) {
354 if (macherr != BOOTSTRAP_UNKNOWN_SERVICE)
355 warnx("WARNING: couldn't lock kext manager for reboot: %s",
356 mach_error_string(macherr));
357 rval = 0;
cf37c299 358 }
34d340d7
A
359 // unless we got the lock, clean up our port
360 if (busyStatus != 0 && myport != MACH_PORT_NULL)
b180c247 361 mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1);
b180c247
A
362
363 return rval;
364}
365#endif