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