Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect safe mode on OSX

Tags:

c

macos

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
}
like image 639
Alex Zielenski Avatar asked Jul 23 '14 17:07

Alex Zielenski


People also ask

How do I take my Mac out of Safe Mode?

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.


Video Answer


1 Answers

You can use sysctl like this:

sysctl -n kern.safeboot

It gives 1 when in safe boot mode and 0 when in normal mode.

like image 169
Mark Setchell Avatar answered Oct 22 '22 20:10

Mark Setchell