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