X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/34d340d711a2b033f5da480ed7b5eb147679a588..b58caf92d598c70ddd398b3909b0a2b8b5a110e1:/shutdown.tproj/shutdown.c diff --git a/shutdown.tproj/shutdown.c b/shutdown.tproj/shutdown.c index e7a7928..d6e60d6 100644 --- a/shutdown.tproj/shutdown.c +++ b/shutdown.tproj/shutdown.c @@ -66,6 +66,8 @@ __FBSDID("$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.28 2005/01/25 08:40:51 delp #include #include #include +#include +#include #include "kextmanager.h" #include @@ -73,7 +75,10 @@ __FBSDID("$FreeBSD: src/sbin/shutdown/shutdown.c,v 1.28 2005/01/25 08:40:51 delp #include // allocate #include // task_self, etc #include // bootstrap +#include #include +#include +#include #include "pathnames.h" #endif /* __APPLE__ */ @@ -109,7 +114,8 @@ struct interval { static time_t offset, shuttime; #ifdef __APPLE__ -static int dohalt, doreboot, doups, killflg, mbuflen, oflag; +static int dohalt, doreboot, doups, killflg, oflag; +static size_t mbuflen; #else static int dohalt, dopower, doreboot, killflg, mbuflen, oflag; #endif @@ -130,7 +136,7 @@ void getoffset(char *); void loop(void); void nolog(void); void timeout(int); -void timewarn(int); +void timewarn(time_t); void usage(const char *); #ifdef __APPLE__ int audit_shutdown(int); @@ -144,7 +150,8 @@ main(int argc, char **argv) { char *p, *endp; struct passwd *pw; - int arglen, ch, len, readstdin; + size_t arglen; + int ch, len, readstdin; #ifndef DEBUG if (geteuid()) @@ -214,7 +221,7 @@ main(int argc, char **argv) if (!(dohalt || doreboot || dosleep || killflg)) usage("-h, -r, -s, or -k is required"); - + if (doups && !dohalt) usage("-u requires -h"); #endif /* !__APPLE__ */ @@ -239,7 +246,7 @@ main(int argc, char **argv) p = mbuf; endp = mbuf + sizeof(mbuf) - 2; for (;;) { - if (!fgets(p, endp - p + 1, stdin)) + if (!fgets(p, (int)(endp - p + 1), stdin)) break; for (; *p && p < endp; ++p); if (p == endp) { @@ -278,6 +285,11 @@ main(int argc, char **argv) } if (forkpid) errx(0, "[pid %d]", forkpid); +#ifdef __APPLE__ + /* 5863185: reboot2() needs to talk to launchd. */ + if (_vprocmgr_detach_from_console(0) != NULL) + warnx("can't detach from console"); +#endif /* __APPLE__ */ } audit_shutdown(0); setsid(); @@ -288,7 +300,7 @@ main(int argc, char **argv) } void -loop() +loop(void) { struct interval *tp; u_int sltime; @@ -310,8 +322,8 @@ loop() * Warn now, if going to sleep more than a fifth of * the next wait time. */ - if ((sltime = offset - tp->timeleft)) { - if (sltime > (u_int)(tp->timetowait / 5)) + if ((sltime = (u_int)(offset - tp->timeleft))) { + if (sltime > (tp->timetowait / 5)) timewarn(offset); (void)sleep(sltime); } @@ -341,7 +353,7 @@ static const char *restricted_environ[] = { }; void -timewarn(int timeleft) +timewarn(time_t timeleft) { static int first; static char hostname[MAXHOSTNAMELEN + 1]; @@ -370,7 +382,7 @@ timewarn(int timeleft) (void)fprintf(pf, "System going down at %5.5s\n\n", ctime(&shuttime) + 11); else if (timeleft > 59) - (void)fprintf(pf, "System going down in %d minute%s\n\n", + (void)fprintf(pf, "System going down in %ld minute%s\n\n", timeleft / 60, (timeleft > 60) ? "s" : ""); else if (timeleft) (void)fprintf(pf, "System going down in 30 seconds\n\n"); @@ -409,13 +421,15 @@ die_you_gravy_sucking_pig_dog() #ifndef __APPLE__ char *empty_environ[] = { NULL }; #else - if ((errno = reserve_reboot())) - err(1, "couldn't lock for reboot"); + if ((errno = reserve_reboot())) { + warn("couldn't lock for reboot"); + finish(0); + } #endif syslog(LOG_NOTICE, "%s%s by %s: %s", #ifndef __APPLE__ - doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : + doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : #else doreboot ? "reboot" : dohalt ? "halt" : dosleep ? "sleep" : #endif @@ -460,21 +474,33 @@ die_you_gravy_sucking_pig_dog() } } } - exit((kr == kIOReturnSuccess) ? 0 : 1); } else { int howto = 0; +#if defined(__APPLE__) + { + struct utmpx utx; + bzero(&utx, sizeof(utx)); + utx.ut_type = SHUTDOWN_TIME; + gettimeofday(&utx.ut_tv, NULL); + pututxline(&utx); + + int newvalue = 1; + sysctlbyname("kern.willshutdown", NULL, NULL, &newvalue, sizeof(newvalue)); + } +#else logwtmp("~", "shutdown", ""); +#endif if (dohalt) howto |= RB_HALT; if (doups) howto |= RB_UPSDELAY; if (nosync) howto |= RB_NOSYNC; // launchd(8) handles reboot. This call returns NULL on success. - exit(reboot2(howto) == NULL ? EXIT_SUCCESS : EXIT_FAILURE); + if (reboot3(howto)) { + syslog(LOG_ERR, "shutdown: launchd reboot failed."); + } } - /* NOT-REACHED */ - #else /* __APPLE__ */ if (!oflag) { (void)kill(1, doreboot ? SIGINT : /* reboot */ @@ -483,7 +509,7 @@ die_you_gravy_sucking_pig_dog() SIGTERM); /* single-user */ } else { if (doreboot) { - execle(_PATH_REBOOT, "reboot", "-l", nosync, + execle(_PATH_REBOOT, "reboot", "-l", nosync, (char *)NULL, empty_environ); syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT); @@ -596,9 +622,8 @@ getoffset(char *timearg) #define NOMSG "\n\nNO LOGINS: System going down at " void -nolog() +nolog(void) { -#ifndef __APPLE__ int logfd; char *ct; @@ -616,21 +641,18 @@ nolog() (void)write(logfd, mbuf, strlen(mbuf)); (void)close(logfd); } -#endif /* !__APPLE__ */ } void finish(int signo __unused) { -#ifndef __APPLE__ if (!killflg) (void)unlink(_PATH_NOLOGIN); -#endif exit(0); } void -badtime() +badtime(void) { errx(1, "bad time format"); } @@ -656,8 +678,9 @@ usage(const char *cp) * header * subject * return - */ -int audit_shutdown(int exitstatus) + */ +int +audit_shutdown(int exitstatus) { int aufd; token_t *tok; @@ -673,7 +696,7 @@ int audit_shutdown(int exitstatus) if((aufd = au_open()) == -1) { fprintf(stderr, "shutdown: Audit Error: au_open() failed\n"); - exit(1); + exit(1); } /* The subject that performed the operation */ @@ -705,7 +728,7 @@ int audit_shutdown(int exitstatus) * contact kextd to lock for reboot */ int -reserve_reboot() +reserve_reboot(void) { int rval = ELAST + 1; kern_return_t macherr = KERN_FAILURE; @@ -713,7 +736,7 @@ reserve_reboot() int busyStatus = ELAST + 1; mountpoint_t busyVol; - macherr = bootstrap_look_up(bootstrap_port, KEXTD_SERVER_NAME, &kxport); + macherr = bootstrap_look_up2(bootstrap_port, KEXTD_SERVER_NAME, &kxport, 0, BOOTSTRAP_PRIVILEGED_SERVER); if (macherr) goto finish; // allocate a port to pass to kextd (in case we die) @@ -756,4 +779,3 @@ finish: return rval; } #endif /* __APPLE__ */ -