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