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