Skip to content

Instantly share code, notes, and snippets.

@dlevi309
Created October 14, 2023 04:15
Show Gist options
  • Save dlevi309/b9a372ecd320965fa19f42d61df477c1 to your computer and use it in GitHub Desktop.
Save dlevi309/b9a372ecd320965fa19f42d61df477c1 to your computer and use it in GitHub Desktop.
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
static void sleepHandler(int sleepType)
{
io_connect_t connect = IOPMFindPowerManagement(kIOMasterPortDefault);
IOReturn ret = IOPMSleepSystem(connect);
if (ret != kIOReturnSuccess) {
if (ret == kIOReturnNotPrivileged) {
printf("Sleep error 0x%08x; You must run this as root.\n", ret);
exit(EX_NOPERM);
} else {
printf("Unable to sleep system: error 0x%08x\n", ret);
exit(EX_OSERR);
}
} else {
printf("Sleeping now...\n");
}
}
int main(int argc, char *argv[])
{
int sleepType = 0;
sleepHandler(sleepType);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment