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