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