]> git.saurik.com Git - apple/system_cmds.git/blob - reboot.tproj/reboot.c
system_cmds-336.23.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 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93";
43 #endif
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/reboot/reboot.c,v 1.17 2002/10/06 16:24:36 thomas Exp $";
46 #endif /* not lint */
47
48 #include <sys/reboot.h>
49 #include <sys/types.h>
50 #include <sys/sysctl.h>
51 #include <signal.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <util.h>
56 #include <pwd.h>
57 #include <syslog.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #ifdef __APPLE__
64 #include "kextmanager.h"
65 #include <IOKit/kext/kextmanager_types.h>
66 #include <mach/mach_port.h> // allocate
67 #include <mach/mach.h> // task_self, etc
68 #include <servers/bootstrap.h> // bootstrap
69 #endif
70
71 void usage(void);
72 u_int get_pageins(void);
73 #ifdef __APPLE__
74 int reserve_reboot(void);
75 #endif
76
77 int dohalt;
78
79 int
80 main(int argc, char *argv[])
81 {
82 struct passwd *pw;
83 int ch, howto, i, fd, kflag, lflag, nflag, qflag, pflag, uflag, sverrno;
84 u_int pageins;
85 char *kernel, *p;
86 const char *user;
87
88 if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
89 dohalt = 1;
90 howto = RB_HALT;
91 } else
92 howto = 0;
93 kflag = lflag = nflag = qflag = 0;
94 #ifndef __APPLE__
95 while ((ch = getopt(argc, argv, "dk:lnpq")) != -1)
96 #else
97 while ((ch = getopt(argc, argv, "lnqu")) != -1)
98 #endif
99 switch(ch) {
100 #ifndef __APPLE__
101 case 'd':
102 howto |= RB_DUMP;
103 break;
104 case 'k':
105 kflag = 1;
106 kernel = optarg;
107 break;
108 #endif
109 case 'l':
110 lflag = 1;
111 break;
112 case 'n':
113 nflag = 1;
114 howto |= RB_NOSYNC;
115 break;
116 /* -p is irrelevant on OS X. It does that anyway. */
117 #ifndef __APPLE__
118 case 'p':
119 pflag = 1;
120 howto |= RB_POWEROFF;
121 break;
122 #endif
123 case 'u':
124 uflag = 1;
125 howto |= RB_UPSDELAY;
126 break;
127 case 'q':
128 qflag = 1;
129 break;
130 case '?':
131 default:
132 usage();
133 }
134 argc -= optind;
135 argv += optind;
136
137 #ifndef __APPLE__
138 if ((howto & (RB_DUMP | RB_HALT)) == (RB_DUMP | RB_HALT))
139 errx(1, "cannot dump (-d) when halting; must reboot instead");
140 #endif
141 if (geteuid()) {
142 errno = EPERM;
143 err(1, NULL);
144 }
145
146 #ifdef __APPLE__
147 if (!lflag) { // shutdown(8) has already checked w/kextd
148 if ((errno = reserve_reboot()) && !qflag)
149 err(1, "couldn't lock for reboot");
150 }
151 #endif
152
153 if (qflag) {
154 reboot(howto);
155 err(1, NULL);
156 }
157
158 #ifndef __APPLE__
159 if (kflag) {
160 fd = open("/boot/nextboot.conf", O_WRONLY | O_CREAT, 0444);
161 if (fd > -1) {
162 (void)write(fd, "nextboot_enable=\"YES\"\n", 22);
163 (void)write(fd, "kernel=\"", 8L);
164 (void)write(fd, kernel, strlen(kernel));
165 (void)write(fd, "\"\n", 2);
166 close(fd);
167 }
168 }
169 #endif
170
171 /* Log the reboot. */
172 if (!lflag) {
173 if ((user = getlogin()) == NULL)
174 user = (pw = getpwuid(getuid())) ?
175 pw->pw_name : "???";
176 if (dohalt) {
177 openlog("halt", 0, LOG_AUTH | LOG_CONS);
178 syslog(LOG_CRIT, "halted by %s%s", user,
179 (howto & RB_UPSDELAY) ? " with UPS delay":"");
180 } else {
181 openlog("reboot", 0, LOG_AUTH | LOG_CONS);
182 syslog(LOG_CRIT, "rebooted by %s", user);
183 }
184 }
185 logwtmp("~", "shutdown", "");
186
187 /*
188 * Do a sync early on, so disks start transfers while we're off
189 * killing processes. Don't worry about writes done before the
190 * processes die, the reboot system call syncs the disks.
191 */
192 if (!nflag)
193 sync();
194
195 /* Just stop init -- if we fail, we'll restart it. */
196 if (kill(1, SIGTSTP) == -1)
197 err(1, "SIGTSTP init");
198
199 /* Ignore the SIGHUP we get when our parent shell dies. */
200 (void)signal(SIGHUP, SIG_IGN);
201
202 #ifndef __APPLE__
203 /* Send a SIGTERM first, a chance to save the buffers. */
204 if (kill(-1, SIGTERM) == -1)
205 err(1, "SIGTERM processes");
206
207 /*
208 * After the processes receive the signal, start the rest of the
209 * buffers on their way. Wait 5 seconds between the SIGTERM and
210 * the SIGKILL to give everybody a chance. If there is a lot of
211 * paging activity then wait longer, up to a maximum of approx
212 * 60 seconds.
213 */
214 sleep(2);
215 for (i = 0; i < 20; i++) {
216 pageins = get_pageins();
217 if (!nflag)
218 sync();
219 sleep(3);
220 if (get_pageins() == pageins)
221 break;
222 }
223
224 for (i = 1;; ++i) {
225 if (kill(-1, SIGKILL) == -1) {
226 if (errno == ESRCH)
227 break;
228 goto restart;
229 }
230 if (i > 5) {
231 (void)fprintf(stderr,
232 "WARNING: some process(es) wouldn't die\n");
233 break;
234 }
235 (void)sleep(2 * i);
236 }
237 #endif
238
239 reboot(howto);
240 /* FALLTHROUGH */
241
242 #ifndef __APPLE__
243 restart:
244 #endif
245 sverrno = errno;
246 errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
247 strerror(sverrno));
248 /* NOTREACHED */
249 }
250
251 void
252 usage()
253 {
254 #ifndef __APPLE__
255 (void)fprintf(stderr, "usage: %s [-dnpq] [-k kernel]\n",
256 #else
257 (void)fprintf(stderr, "usage: %s [-lnq]\n",
258 #endif
259 dohalt ? "halt" : "reboot");
260 exit(1);
261 }
262
263 u_int
264 get_pageins()
265 {
266 u_int pageins;
267 size_t len;
268
269 len = sizeof(pageins);
270 if (sysctlbyname("vm.stats.vm.v_swappgsin", &pageins, &len, NULL, 0)
271 != 0) {
272 warnx("v_swappgsin");
273 return (0);
274 }
275 return pageins;
276 }
277
278 #ifdef __APPLE__
279 // XX another copy of this routine is in shutdown.c; it would be nice to share
280
281 #define LCK_MAXTRIES 10
282 #define LCK_DELAY 30
283 /*
284 * contact kextd to lock for reboot
285 */
286 int
287 reserve_reboot()
288 {
289 int rval = ELAST+1;
290 kern_return_t macherr = KERN_FAILURE;
291 mach_port_t tport, bsport, kxport, myport = MACH_PORT_NULL;
292 int busyStatus, nretries = LCK_MAXTRIES;
293 dev_path_t busyDev = "<unknown>";
294
295 // find kextd
296 tport = mach_task_self();
297 if (tport == MACH_PORT_NULL) goto finish;
298 macherr = task_get_bootstrap_port(tport, &bsport);
299 if (macherr) goto finish;
300 macherr = bootstrap_look_up(bsport, KEXTD_SERVER_NAME, &kxport);
301 if (macherr) goto finish;
302
303 // allocate a port to pass to kextd (in case we die)
304 macherr = mach_port_allocate(tport, MACH_PORT_RIGHT_RECEIVE, &myport);
305 if (macherr) goto finish;
306
307 // loop trying to lock for reboot (i.e. no volumes are busy)
308 do {
309 nretries--;
310 macherr = kextmanager_lock_reboot(kxport, myport, busyDev, &busyStatus);
311 if (macherr) goto finish;
312
313 if (busyStatus == EBUSY) {
314 if (*busyDev) {
315 warnx("%s is busy updating; delaying reboot (%d retries left)",
316 busyDev, nretries);
317 } else
318 warnx("kextd still starting up");
319 if (nretries) sleep(LCK_DELAY); // don't sleep the last time
320 }
321 } while (busyStatus == EBUSY && nretries > 0);
322
323 rval = busyStatus;
324
325 finish:
326 if (macherr == BOOTSTRAP_UNKNOWN_SERVICE) {
327 mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1);
328 rval = 0;
329 } else if (macherr) {
330 warnx("couldn't lock kext manager for reboot: %s",
331 mach_error_string(macherr));
332 rval = ELAST + 1;
333 }
334 if (rval && myport != MACH_PORT_NULL) {
335 mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1);
336 }
337
338 return rval;
339 }
340 #endif