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