I have some code that I only want to run if the user didn't boot in safe mode. Is there a way using the CoreFoundation or C standard APIs that I can detect that?
EDIT: here is my code thanks to my accepted answer:
#include <sys/sysctl.h>
...
int safeBoot;
int mib_name[2] = { CTL_KERN, KERN_SAFEBOOT };
size_t length = sizeof(safeBoot);
if (!sysctl(mib_name, 2, &safeBoot, &length, NULL, 0)) {
if (safeBoot == 1) {
// We are in safe mode
} else {
// Normal mode. Continue…
}
} else {
// Couldn't find safe boot information
}
To exit safe mode, restart your Mac like you normally would (choose Apple menu () > Shut Down), but don't hold down any keys during startup. You should be back on your desktop in normal mode. Keep in mind that leaving safe mode might take longer than it does to boot in normal mode.
You can use sysctl
like this:
sysctl -n kern.safeboot
It gives 1
when in safe boot
mode and 0
when in normal mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With