]> git.saurik.com Git - apple/system_cmds.git/blob - shutdown.tproj/shutdown.c
904110c88e531349e2649e2b3b55fbf783cf3f9b
[apple/system_cmds.git] / shutdown.tproj / shutdown.c
1 /*
2 * Copyright (c) 1988, 1990, 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 #if 0
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1990, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
40 #endif /* not lint */
41 #endif
42 #include <sys/cdefs.h>
43 #ifndef __APPLE__
44 __FBSDID("$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.28 2005/01/25 08:40:51 delphij Exp $");
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 #include <sys/syslog.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <pwd.h>
57 #include <setjmp.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #ifdef __APPLE__
65 #include <errno.h>
66 #include <util.h>
67 #include <bsm/libbsm.h>
68 #include <bsm/audit_uevents.h>
69 #include <vproc.h>
70 #include <vproc_priv.h>
71
72 #include "kextmanager.h"
73 #include <IOKit/kext/kextmanager_types.h>
74 #include <IOKit/pwr_mgt/IOPMLib.h>
75 #include <mach/mach_port.h> // allocate
76 #include <mach/mach.h> // task_self, etc
77 #include <servers/bootstrap.h> // bootstrap
78 #include <bootstrap_priv.h>
79 #include <reboot2.h>
80 #include <utmpx.h>
81
82 #include "pathnames.h"
83 #endif /* __APPLE__ */
84
85 #ifdef DEBUG
86 #undef _PATH_NOLOGIN
87 #define _PATH_NOLOGIN "./nologin"
88 #endif
89
90 #define H *60*60
91 #define M *60
92 #define S *1
93 #define NOLOG_TIME 5*60
94 struct interval {
95 int timeleft, timetowait;
96 } tlist[] = {
97 { 10 H, 5 H },
98 { 5 H, 3 H },
99 { 2 H, 1 H },
100 { 1 H, 30 M },
101 { 30 M, 10 M },
102 { 20 M, 10 M },
103 { 10 M, 5 M },
104 { 5 M, 3 M },
105 { 2 M, 1 M },
106 { 1 M, 30 S },
107 { 30 S, 30 S },
108 { 0 , 0 }
109 };
110 #undef H
111 #undef M
112 #undef S
113
114 static time_t offset, shuttime;
115 #ifdef __APPLE__
116 static int dohalt, doreboot, doups, killflg, oflag;
117 static size_t mbuflen;
118 #else
119 static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
120 #endif
121 static char mbuf[BUFSIZ];
122 static const char *nosync, *whom;
123 #ifdef __APPLE__
124 static int dosleep;
125 #endif
126
127 void badtime(void);
128 #ifdef __APPLE__
129 void log_and_exec_reboot_or_halt(void);
130 #else
131 void die_you_gravy_sucking_pig_dog(void);
132 #endif
133 void finish(int);
134 void getoffset(char *);
135 void loop(void);
136 void nolog(void);
137 void timeout(int);
138 void timewarn(time_t);
139 void usage(const char *);
140 #ifdef __APPLE__
141 int audit_shutdown(int);
142 int reserve_reboot(void);
143 #endif
144
145 extern const char **environ;
146
147 int
148 main(int argc, char **argv)
149 {
150 char *p, *endp;
151 struct passwd *pw;
152 size_t arglen;
153 int ch, len, readstdin;
154
155 #ifndef DEBUG
156 if (geteuid())
157 errx(1, "NOT super-user");
158 #endif
159 nosync = NULL;
160 readstdin = 0;
161 #ifndef __APPLE__
162 while ((ch = getopt(argc, argv, "-hknopr")) != -1)
163 #else
164 while ((ch = getopt(argc, argv, "-hknorsu")) != -1)
165 #endif
166 switch (ch) {
167 case '-':
168 readstdin = 1;
169 break;
170 case 'h':
171 dohalt = 1;
172 break;
173 case 'k':
174 killflg = 1;
175 break;
176 case 'n':
177 nosync = "-n";
178 break;
179 case 'o':
180 oflag = 1;
181 break;
182 #ifndef __APPLE__
183 case 'p':
184 dopower = 1;
185 break;
186 #endif
187 case 'u':
188 doups = 1;
189 break;
190 case 'r':
191 doreboot = 1;
192 break;
193 #ifdef __APPLE__
194 case 's':
195 dosleep = 1;
196 break;
197 #endif
198 case '?':
199 default:
200 usage((char *)NULL);
201 }
202 argc -= optind;
203 argv += optind;
204
205 if (argc < 1)
206 usage((char *)NULL);
207
208 #ifndef __APPLE__
209 if (killflg + doreboot + dohalt + dopower > 1)
210 usage("incompatible switches -h, -k, -p and -r");
211
212 if (oflag && !(dohalt || dopower || doreboot))
213 usage("-o requires -h, -p or -r");
214
215 if (nosync != NULL && !oflag)
216 usage("-n requires -o");
217 #else /* !__APPLE__ */
218 if (killflg + doreboot + dohalt + dosleep > 1)
219 usage("incompatible switches -h, -k, -r, and -s");
220
221 if (!(dohalt || doreboot || dosleep || killflg))
222 usage("-h, -r, -s, or -k is required");
223
224 if (doups && !dohalt)
225 usage("-u requires -h");
226 #endif /* !__APPLE__ */
227
228 getoffset(*argv++);
229
230 if (*argv) {
231 for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
232 arglen = strlen(*argv);
233 if ((len -= arglen) <= 2)
234 break;
235 if (p != mbuf)
236 *p++ = ' ';
237 memmove(p, *argv, arglen);
238 p += arglen;
239 }
240 *p = '\n';
241 *++p = '\0';
242 }
243
244 if (readstdin) {
245 p = mbuf;
246 endp = mbuf + sizeof(mbuf) - 2;
247 for (;;) {
248 if (!fgets(p, (int)(endp - p + 1), stdin))
249 break;
250 for (; *p && p < endp; ++p);
251 if (p == endp) {
252 *p = '\n';
253 *++p = '\0';
254 break;
255 }
256 }
257 }
258 mbuflen = strlen(mbuf);
259
260 if (offset)
261 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
262 else
263 (void)printf("Shutdown NOW!\n");
264
265 if (!(whom = getlogin()))
266 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
267
268 #ifdef DEBUG
269 audit_shutdown(0);
270 (void)putc('\n', stdout);
271 #else
272 (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
273 #ifdef __APPLE__
274 if (offset) {
275 #else
276 {
277 #endif
278 int forkpid;
279
280 forkpid = fork();
281 if (forkpid == -1) {
282 audit_shutdown(1);
283 err(1, "fork");
284 }
285 if (forkpid)
286 errx(0, "[pid %d]", forkpid);
287 #ifdef __APPLE__
288 /* 5863185: reboot2() needs to talk to launchd. */
289 if (_vprocmgr_detach_from_console(0) != NULL)
290 warnx("can't detach from console");
291 #endif /* __APPLE__ */
292 }
293 audit_shutdown(0);
294 setsid();
295 #endif
296 openlog("shutdown", LOG_CONS, LOG_AUTH);
297 loop();
298 return(0);
299 }
300
301 void
302 loop(void)
303 {
304 struct interval *tp;
305 u_int sltime;
306 int logged;
307
308 if (offset <= NOLOG_TIME) {
309 logged = 1;
310 nolog();
311 }
312 else
313 logged = 0;
314 tp = tlist;
315 if (tp->timeleft < offset)
316 (void)sleep((u_int)(offset - tp->timeleft));
317 else {
318 while (tp->timeleft && offset < tp->timeleft)
319 ++tp;
320 /*
321 * Warn now, if going to sleep more than a fifth of
322 * the next wait time.
323 */
324 if ((sltime = (u_int)(offset - tp->timeleft))) {
325 if (sltime > (tp->timetowait / 5))
326 timewarn(offset);
327 (void)sleep(sltime);
328 }
329 }
330 for (;; ++tp) {
331 timewarn(tp->timeleft);
332 if (!logged && tp->timeleft <= NOLOG_TIME) {
333 logged = 1;
334 nolog();
335 }
336 (void)sleep((u_int)tp->timetowait);
337 if (!tp->timeleft)
338 break;
339 }
340 #ifdef __APPLE__
341 log_and_exec_reboot_or_halt();
342 #else
343 die_you_gravy_sucking_pig_dog();
344 #endif
345 }
346
347 static jmp_buf alarmbuf;
348
349 static const char *restricted_environ[] = {
350 "PATH=" _PATH_STDPATH,
351 NULL
352 };
353
354 void
355 timewarn(time_t timeleft)
356 {
357 static int first;
358 static char hostname[MAXHOSTNAMELEN + 1];
359 FILE *pf;
360 char wcmd[MAXPATHLEN + 4];
361
362 /* wall is sometimes missing, e.g. on install media */
363 if (access(_PATH_WALL, X_OK) == -1) return;
364
365 if (!first++)
366 (void)gethostname(hostname, sizeof(hostname));
367
368 /* undoc -n option to wall suppresses normal wall banner */
369 (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
370 environ = restricted_environ;
371 if (!(pf = popen(wcmd, "w"))) {
372 syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
373 return;
374 }
375
376 (void)fprintf(pf,
377 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
378 timeleft ? "": "FINAL ", whom, hostname);
379
380 if (timeleft > 10*60)
381 (void)fprintf(pf, "System going down at %5.5s\n\n",
382 ctime(&shuttime) + 11);
383 else if (timeleft > 59)
384 (void)fprintf(pf, "System going down in %ld minute%s\n\n",
385 timeleft / 60, (timeleft > 60) ? "s" : "");
386 else if (timeleft)
387 (void)fprintf(pf, "System going down in 30 seconds\n\n");
388 else
389 (void)fprintf(pf, "System going down IMMEDIATELY\n\n");
390
391 if (mbuflen)
392 (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf);
393
394 /*
395 * play some games, just in case wall doesn't come back
396 * probably unnecessary, given that wall is careful.
397 */
398 if (!setjmp(alarmbuf)) {
399 (void)signal(SIGALRM, timeout);
400 (void)alarm((u_int)30);
401 (void)pclose(pf);
402 (void)alarm((u_int)0);
403 (void)signal(SIGALRM, SIG_DFL);
404 }
405 }
406
407 void
408 timeout(int signo __unused)
409 {
410 longjmp(alarmbuf, 1);
411 }
412
413 void
414 #ifdef __APPLE__
415 log_and_exec_reboot_or_halt()
416 #else
417 die_you_gravy_sucking_pig_dog()
418 #endif
419 {
420 #ifndef __APPLE__
421 char *empty_environ[] = { NULL };
422 #else
423 if ((errno = reserve_reboot())) {
424 warn("couldn't lock for reboot");
425 finish(0);
426 }
427 #endif
428
429 syslog(LOG_NOTICE, "%s%s by %s: %s",
430 #ifndef __APPLE__
431 doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
432 #else
433 doreboot ? "reboot" : dohalt ? "halt" : dosleep ? "sleep" :
434 #endif
435 "shutdown", doups?" with UPS delay":"", whom, mbuf);
436 #ifndef __APPLE__
437 (void)sleep(2);
438 #endif
439
440 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
441 if (killflg) {
442 (void)printf("\rbut you'll have to do it yourself\r\n");
443 exit(0);
444 }
445 #ifdef DEBUG
446 if (doreboot)
447 (void)printf("reboot");
448 else if (dohalt)
449 (void)printf("halt");
450 #ifndef __APPLE__
451 else if (dopower)
452 (void)printf("power-down");
453 if (nosync != NULL)
454 (void)printf(" no sync");
455 #else
456 else if (dosleep)
457 (void)printf("sleep");
458 #endif
459 (void)printf("\nkill -HUP 1\n");
460 #else
461 #ifdef __APPLE__
462 if (dosleep) {
463 mach_port_t mp;
464 io_connect_t fb;
465 kern_return_t kr = IOMasterPort(bootstrap_port, &mp);
466 if (kr == kIOReturnSuccess) {
467 fb = IOPMFindPowerManagement(mp);
468 if (fb != IO_OBJECT_NULL) {
469 IOReturn err = IOPMSleepSystem(fb);
470 if (err != kIOReturnSuccess) {
471 fprintf(stderr, "shutdown: sleep failed (0x%08x)\n", err);
472 kr = -1;
473 }
474 }
475 }
476 } else {
477 int howto = 0;
478
479 #if defined(__APPLE__)
480 {
481 struct utmpx utx;
482 bzero(&utx, sizeof(utx));
483 utx.ut_type = SHUTDOWN_TIME;
484 gettimeofday(&utx.ut_tv, NULL);
485 pututxline(&utx);
486 }
487 #else
488 logwtmp("~", "shutdown", "");
489 #endif
490
491 if (dohalt) howto |= RB_HALT;
492 if (doups) howto |= RB_UPSDELAY;
493 if (nosync) howto |= RB_NOSYNC;
494
495 // launchd(8) handles reboot. This call returns NULL on success.
496 if (reboot3(howto)) {
497 syslog(LOG_ERR, "shutdown: launchd reboot failed.");
498 }
499 }
500 #else /* __APPLE__ */
501 if (!oflag) {
502 (void)kill(1, doreboot ? SIGINT : /* reboot */
503 dohalt ? SIGUSR1 : /* halt */
504 dopower ? SIGUSR2 : /* power-down */
505 SIGTERM); /* single-user */
506 } else {
507 if (doreboot) {
508 execle(_PATH_REBOOT, "reboot", "-l", nosync,
509 (char *)NULL, empty_environ);
510 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
511 _PATH_REBOOT);
512 warn(_PATH_REBOOT);
513 }
514 else if (dohalt) {
515 execle(_PATH_HALT, "halt", "-l", nosync,
516 (char *)NULL, empty_environ);
517 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
518 _PATH_HALT);
519 warn(_PATH_HALT);
520 }
521 else if (dopower) {
522 execle(_PATH_HALT, "halt", "-l", "-p", nosync,
523 (char *)NULL, empty_environ);
524 syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
525 _PATH_HALT);
526 warn(_PATH_HALT);
527 }
528 (void)kill(1, SIGTERM); /* to single-user */
529 }
530 #endif /* __APPLE__ */
531 #endif
532 finish(0);
533 }
534
535 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
536
537 void
538 getoffset(char *timearg)
539 {
540 struct tm *lt;
541 char *p;
542 time_t now;
543 int this_year;
544
545 (void)time(&now);
546
547 if (!strcasecmp(timearg, "now")) { /* now */
548 offset = 0;
549 shuttime = now;
550 return;
551 }
552
553 if (*timearg == '+') { /* +minutes */
554 if (!isdigit(*++timearg))
555 badtime();
556 if ((offset = atoi(timearg) * 60) < 0)
557 badtime();
558 shuttime = now + offset;
559 return;
560 }
561
562 /* handle hh:mm by getting rid of the colon */
563 for (p = timearg; *p; ++p)
564 if (!isascii(*p) || !isdigit(*p)) {
565 if (*p == ':' && strlen(p) == 3) {
566 p[0] = p[1];
567 p[1] = p[2];
568 p[2] = '\0';
569 }
570 else
571 badtime();
572 }
573
574 unsetenv("TZ"); /* OUR timezone */
575 lt = localtime(&now); /* current time val */
576
577 switch(strlen(timearg)) {
578 case 10:
579 this_year = lt->tm_year;
580 lt->tm_year = ATOI2(timearg);
581 /*
582 * check if the specified year is in the next century.
583 * allow for one year of user error as many people will
584 * enter n - 1 at the start of year n.
585 */
586 if (lt->tm_year < (this_year % 100) - 1)
587 lt->tm_year += 100;
588 /* adjust for the year 2000 and beyond */
589 lt->tm_year += (this_year - (this_year % 100));
590 /* FALLTHROUGH */
591 case 8:
592 lt->tm_mon = ATOI2(timearg);
593 if (--lt->tm_mon < 0 || lt->tm_mon > 11)
594 badtime();
595 /* FALLTHROUGH */
596 case 6:
597 lt->tm_mday = ATOI2(timearg);
598 if (lt->tm_mday < 1 || lt->tm_mday > 31)
599 badtime();
600 /* FALLTHROUGH */
601 case 4:
602 lt->tm_hour = ATOI2(timearg);
603 if (lt->tm_hour < 0 || lt->tm_hour > 23)
604 badtime();
605 lt->tm_min = ATOI2(timearg);
606 if (lt->tm_min < 0 || lt->tm_min > 59)
607 badtime();
608 lt->tm_sec = 0;
609 if ((shuttime = mktime(lt)) == -1)
610 badtime();
611 if ((offset = shuttime - now) < 0)
612 errx(1, "that time is already past.");
613 break;
614 default:
615 badtime();
616 }
617 }
618
619 #define NOMSG "\n\nNO LOGINS: System going down at "
620 void
621 nolog(void)
622 {
623 int logfd;
624 char *ct;
625
626 (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */
627 (void)signal(SIGINT, finish);
628 (void)signal(SIGHUP, finish);
629 (void)signal(SIGQUIT, finish);
630 (void)signal(SIGTERM, finish);
631 if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC,
632 0664)) >= 0) {
633 (void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
634 ct = ctime(&shuttime);
635 (void)write(logfd, ct + 11, 5);
636 (void)write(logfd, "\n\n", 2);
637 (void)write(logfd, mbuf, strlen(mbuf));
638 (void)close(logfd);
639 }
640 }
641
642 void
643 finish(int signo __unused)
644 {
645 if (!killflg)
646 (void)unlink(_PATH_NOLOGIN);
647 exit(0);
648 }
649
650 void
651 badtime(void)
652 {
653 errx(1, "bad time format");
654 }
655
656 void
657 usage(const char *cp)
658 {
659 if (cp != NULL)
660 warnx("%s", cp);
661 (void)fprintf(stderr,
662 #ifdef __APPLE__
663 "usage: shutdown [-] [-h [-u] [-n] | -r [-n] | -s | -k]"
664 #else
665 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
666 #endif
667 " time [warning-message ...]\n");
668 exit(1);
669 }
670
671 #ifdef __APPLE__
672 /*
673 * The following tokens are included in the audit record for shutdown
674 * header
675 * subject
676 * return
677 */
678 int
679 audit_shutdown(int exitstatus)
680 {
681 int aufd;
682 token_t *tok;
683 long au_cond;
684
685 /* If we are not auditing, don't cut an audit record; just return */
686 if (auditon(A_GETCOND, &au_cond, sizeof(long)) < 0) {
687 fprintf(stderr, "shutdown: Could not determine audit condition\n");
688 return 0;
689 }
690 if (au_cond == AUC_NOAUDIT)
691 return 0;
692
693 if((aufd = au_open()) == -1) {
694 fprintf(stderr, "shutdown: Audit Error: au_open() failed\n");
695 exit(1);
696 }
697
698 /* The subject that performed the operation */
699 if((tok = au_to_me()) == NULL) {
700 fprintf(stderr, "shutdown: Audit Error: au_to_me() failed\n");
701 exit(1);
702 }
703 au_write(aufd, tok);
704
705 /* success and failure status */
706 if((tok = au_to_return32(exitstatus, errno)) == NULL) {
707 fprintf(stderr, "shutdown: Audit Error: au_to_return32() failed\n");
708 exit(1);
709 }
710 au_write(aufd, tok);
711
712 if(au_close(aufd, 1, AUE_shutdown) == -1) {
713 fprintf(stderr, "shutdown: Audit Error: au_close() failed\n");
714 exit(1);
715 }
716 return 1;
717 }
718
719
720 // XX copied from reboot.tproj/reboot.c; it would be nice to share the code
721
722 #define WAITFORLOCK 1
723 /*
724 * contact kextd to lock for reboot
725 */
726 int
727 reserve_reboot(void)
728 {
729 int rval = ELAST + 1;
730 kern_return_t macherr = KERN_FAILURE;
731 mach_port_t kxport, tport = MACH_PORT_NULL, myport = MACH_PORT_NULL;
732 int busyStatus = ELAST + 1;
733 mountpoint_t busyVol;
734
735 macherr = bootstrap_look_up2(bootstrap_port, KEXTD_SERVER_NAME, &kxport, 0, BOOTSTRAP_PRIVILEGED_SERVER);
736 if (macherr) goto finish;
737
738 // allocate a port to pass to kextd (in case we die)
739 tport = mach_task_self();
740 if (tport == MACH_PORT_NULL) goto finish;
741 macherr = mach_port_allocate(tport, MACH_PORT_RIGHT_RECEIVE, &myport);
742 if (macherr) goto finish;
743
744 // try to lock for reboot
745 macherr = kextmanager_lock_reboot(kxport, myport, !WAITFORLOCK, busyVol,
746 &busyStatus);
747 if (macherr) goto finish;
748
749 if (busyStatus == EBUSY) {
750 warnx("%s is busy updating; waiting for lock", busyVol);
751 macherr = kextmanager_lock_reboot(kxport, myport, WAITFORLOCK,
752 busyVol, &busyStatus);
753 if (macherr) goto finish;
754 }
755
756 if (busyStatus == EALREADY) {
757 // reboot already in progress
758 rval = 0;
759 } else {
760 rval = busyStatus;
761 }
762
763 finish:
764 // in general, we want to err on the side of allowing the reboot
765 if (macherr) {
766 if (macherr != BOOTSTRAP_UNKNOWN_SERVICE)
767 warnx("WARNING: couldn't lock kext manager for reboot: %s",
768 mach_error_string(macherr));
769 rval = 0;
770 }
771 // unless we got the lock, clean up our port
772 if (busyStatus != 0 && myport != MACH_PORT_NULL)
773 mach_port_mod_refs(tport, myport, MACH_PORT_RIGHT_RECEIVE, -1);
774
775 return rval;
776 }
777 #endif /* __APPLE__ */