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