]>
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 | |
34d340d7 | 69 | #include <reboot2.h> |
b180c247 A |
70 | #endif |
71 | ||
20e66415 A |
72 | void usage(void); |
73 | u_int get_pageins(void); | |
916eb79e | 74 | #if defined(__APPLE__) && !TARGET_OS_EMBEDDED |
b180c247 A |
75 | int reserve_reboot(void); |
76 | #endif | |
1815bff5 A |
77 | |
78 | int dohalt; | |
79 | ||
80 | int | |
20e66415 | 81 | main(int argc, char *argv[]) |
1815bff5 | 82 | { |
1815bff5 | 83 | struct passwd *pw; |
34d340d7 A |
84 | int ch, howto, kflag, lflag, nflag, qflag, uflag; |
85 | char *p; | |
20e66415 | 86 | const char *user; |
34d340d7 A |
87 | #ifndef __APPLE__ |
88 | int i, fd, pflag, sverrno; | |
89 | u_int pageins; | |
90 | char *kernel; | |
91 | #endif | |
1815bff5 | 92 | |
20e66415 | 93 | if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { |
1815bff5 A |
94 | dohalt = 1; |
95 | howto = RB_HALT; | |
96 | } else | |
97 | howto = 0; | |
20e66415 A |
98 | kflag = lflag = nflag = qflag = 0; |
99 | #ifndef __APPLE__ | |
100 | while ((ch = getopt(argc, argv, "dk:lnpq")) != -1) | |
101 | #else | |
b180c247 | 102 | while ((ch = getopt(argc, argv, "lnqu")) != -1) |
20e66415 | 103 | #endif |
1815bff5 | 104 | switch(ch) { |
20e66415 A |
105 | #ifndef __APPLE__ |
106 | case 'd': | |
107 | howto |= RB_DUMP; | |
108 | break; | |
20e66415 A |
109 | case 'k': |
110 | kflag = 1; | |
111 | kernel = optarg; | |
112 | break; | |
2fc1e207 | 113 | #endif |
20e66415 | 114 | case 'l': |
1815bff5 A |
115 | lflag = 1; |
116 | break; | |
117 | case 'n': | |
118 | nflag = 1; | |
119 | howto |= RB_NOSYNC; | |
120 | break; | |
20e66415 A |
121 | /* -p is irrelevant on OS X. It does that anyway. */ |
122 | #ifndef __APPLE__ | |
123 | case 'p': | |
124 | pflag = 1; | |
125 | howto |= RB_POWEROFF; | |
126 | break; | |
127 | #endif | |
b180c247 A |
128 | case 'u': |
129 | uflag = 1; | |
130 | howto |= RB_UPSDELAY; | |
131 | break; | |
1815bff5 A |
132 | case 'q': |
133 | qflag = 1; | |
916eb79e | 134 | howto |= RB_QUICK; |
1815bff5 A |
135 | break; |
136 | case '?': | |
137 | default: | |
138 | usage(); | |
139 | } | |
140 | argc -= optind; | |
141 | argv += optind; | |
142 | ||
20e66415 A |
143 | #ifndef __APPLE__ |
144 | if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT)) | |
145 | errx(1, "cannot dump (-d) when halting; must reboot instead"); | |
146 | #endif | |
147 | if (geteuid()) { | |
148 | errno = EPERM; | |
149 | err(1, NULL); | |
150 | } | |
1815bff5 | 151 | |
916eb79e | 152 | #if defined(__APPLE__) && !TARGET_OS_EMBEDDED |
b180c247 A |
153 | if (!lflag) { // shutdown(8) has already checked w/kextd |
154 | if ((errno = reserve_reboot()) && !qflag) | |
155 | err(1, "couldn't lock for reboot"); | |
156 | } | |
157 | #endif | |
158 | ||
1815bff5 | 159 | if (qflag) { |
916eb79e A |
160 | #ifdef __APPLE__ |
161 | // launchd(8) handles reboot. This call returns NULL on success. | |
162 | exit(reboot2(howto) == NULL ? EXIT_SUCCESS : EXIT_FAILURE); | |
163 | #else /* __APPLE__ */ | |
1815bff5 | 164 | reboot(howto); |
916eb79e | 165 | #endif /* __APPLE__ */ |
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 | } | |
196 | logwtmp("~", "shutdown", ""); | |
197 | ||
198 | /* | |
199 | * Do a sync early on, so disks start transfers while we're off | |
200 | * killing processes. Don't worry about writes done before the | |
201 | * processes die, the reboot system call syncs the disks. | |
202 | */ | |
203 | if (!nflag) | |
204 | sync(); | |
205 | ||
34d340d7 | 206 | #ifndef __APPLE__ |
1815bff5 A |
207 | /* Just stop init -- if we fail, we'll restart it. */ |
208 | if (kill(1, SIGTSTP) == -1) | |
20e66415 | 209 | err(1, "SIGTSTP init"); |
34d340d7 | 210 | #endif |
1815bff5 A |
211 | |
212 | /* Ignore the SIGHUP we get when our parent shell dies. */ | |
213 | (void)signal(SIGHUP, SIG_IGN); | |
214 | ||
20e66415 | 215 | #ifndef __APPLE__ |
1815bff5 A |
216 | /* Send a SIGTERM first, a chance to save the buffers. */ |
217 | if (kill(-1, SIGTERM) == -1) | |
20e66415 | 218 | err(1, "SIGTERM processes"); |
1815bff5 A |
219 | |
220 | /* | |
221 | * After the processes receive the signal, start the rest of the | |
222 | * buffers on their way. Wait 5 seconds between the SIGTERM and | |
20e66415 A |
223 | * the SIGKILL to give everybody a chance. If there is a lot of |
224 | * paging activity then wait longer, up to a maximum of approx | |
225 | * 60 seconds. | |
1815bff5 A |
226 | */ |
227 | sleep(2); | |
20e66415 A |
228 | for (i = 0; i < 20; i++) { |
229 | pageins = get_pageins(); | |
230 | if (!nflag) | |
231 | sync(); | |
232 | sleep(3); | |
233 | if (get_pageins() == pageins) | |
234 | break; | |
235 | } | |
1815bff5 A |
236 | |
237 | for (i = 1;; ++i) { | |
238 | if (kill(-1, SIGKILL) == -1) { | |
239 | if (errno == ESRCH) | |
240 | break; | |
241 | goto restart; | |
242 | } | |
243 | if (i > 5) { | |
244 | (void)fprintf(stderr, | |
245 | "WARNING: some process(es) wouldn't die\n"); | |
246 | break; | |
247 | } | |
248 | (void)sleep(2 * i); | |
249 | } | |
250 | #endif | |
20e66415 | 251 | |
34d340d7 A |
252 | #ifdef __APPLE__ |
253 | // launchd(8) handles reboot. This call returns NULL on success. | |
254 | exit(reboot2(howto) == NULL ? EXIT_SUCCESS : EXIT_FAILURE); | |
255 | #else /* __APPLE__ */ | |
1815bff5 A |
256 | reboot(howto); |
257 | /* FALLTHROUGH */ | |
258 | ||
259 | restart: | |
260 | sverrno = errno; | |
20e66415 | 261 | errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", |
1815bff5 A |
262 | strerror(sverrno)); |
263 | /* NOTREACHED */ | |
34d340d7 | 264 | #endif /* __APPLE__ */ |
1815bff5 A |
265 | } |
266 | ||
267 | void | |
268 | usage() | |
269 | { | |
2fc1e207 | 270 | #ifndef __APPLE__ |
20e66415 | 271 | (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n", |
2fc1e207 A |
272 | #else |
273 | (void)fprintf(stderr, "usage: %s [-lnq]\n", | |
274 | #endif | |
20e66415 | 275 | dohalt ? "halt" : "reboot"); |
1815bff5 A |
276 | exit(1); |
277 | } | |
278 | ||
20e66415 A |
279 | u_int |
280 | get_pageins() | |
1815bff5 | 281 | { |
20e66415 A |
282 | u_int pageins; |
283 | size_t len; | |
284 | ||
285 | len = sizeof(pageins); | |
286 | if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0) | |
287 | != 0) { | |
288 | warnx("v_swappgsin"); | |
289 | return (0); | |
290 | } | |
291 | return pageins; | |
1815bff5 | 292 | } |
b180c247 | 293 | |
916eb79e | 294 | #if defined(__APPLE__) && !TARGET_OS_EMBEDDED |
34d340d7 | 295 | // XX this routine is also in shutdown.tproj; it would be nice to share |
b180c247 | 296 | |
34d340d7 | 297 | #define WAITFORLOCK 1 |
b180c247 A |
298 | /* |
299 | * contact kextd to lock for reboot | |
300 | */ | |
301 | int | |
302 | reserve_reboot() | |
303 | { | |
34d340d7 | 304 | int rval = ELAST + 1; |
b180c247 | 305 | kern_return_t macherr = KERN_FAILURE; |
34d340d7 A |
306 | mach_port_t kxport, tport = MACH_PORT_NULL, myport = MACH_PORT_NULL; |
307 | int busyStatus = ELAST + 1; | |
308 | mountpoint_t busyVol; | |
b180c247 | 309 | |
34d340d7 | 310 | macherr = bootstrap_look_up(bootstrap_port, KEXTD_SERVER_NAME, &kxport); |
b180c247 A |
311 | if (macherr) goto finish; |
312 | ||
313 | // allocate a port to pass to kextd (in case we die) | |
34d340d7 A |
314 | tport = mach_task_self(); |
315 | if (tport == MACH_PORT_NULL) goto finish; | |
b180c247 A |
316 | macherr = mach_port_allocate(tport, MACH_PORT_RIGHT_RECEIVE, &myport); |
317 | if (macherr) goto finish; | |
318 | ||
34d340d7 A |
319 | // try to lock for reboot |
320 | macherr = kextmanager_lock_reboot(kxport, myport, !WAITFORLOCK, busyVol, | |
321 | &busyStatus); | |
322 | if (macherr) goto finish; | |
b180c247 | 323 | |
34d340d7 A |
324 | if (busyStatus == EBUSY) { |
325 | warnx("%s is busy updating; waiting for lock", busyVol); | |
326 | macherr = kextmanager_lock_reboot(kxport, myport, WAITFORLOCK, | |
327 | busyVol, &busyStatus); | |
328 | if (macherr) goto finish; | |
329 | } | |
b180c247 | 330 | |
34d340d7 A |
331 | if (busyStatus == EALREADY) { |
332 | // reboot already in progress | |
b180c247 | 333 | rval = 0; |
34d340d7 A |
334 | } else { |
335 | rval = busyStatus; | |
b180c247 | 336 | } |
34d340d7 A |
337 | |
338 | finish: | |
339 | // in general, we want to err on the side of allowing the reboot | |
340 | if (macherr) { | |
341 | if (macherr != BOOTSTRAP_UNKNOWN_SERVICE) | |
342 | warnx("WARNING: couldn't lock kext manager for reboot: %s", | |
343 | mach_error_string(macherr)); | |
344 | rval = 0; | |
345 | } | |
346 | // unless we got the lock, clean up our port | |
347 | if (busyStatus != 0 && myport != MACH_PORT_NULL) | |
b180c247 | 348 | mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1); |
b180c247 A |
349 | |
350 | return rval; | |
351 | } | |
352 | #endif |