6 Replies
      Latest reply: Oct 28, 2016 11:01 AM by JZdziarski RSS
      JZdziarski Level 1 Level 1 (0 points)

            size_t len = sizeof(int);

            int safeboot = 0;

            int ret;

          

            ret = sysctlbyname("kern.safeboot", &safeboot, &len, NULL, 0);

         

        This code always seems to fail; I've tried UInt32 as well, it always returns an error code of 1. Is there a way to figure out if my kext is running in safeboot? I need it to behave differently.

        • Re: can't sysctlbyname(kern.safeboot)
          JZdziarski Level 1 Level 1 (0 points)

          it looks like that err is EPERM; is there any other way to tell if I'm in safe boot from kpi?

            • Re: can't sysctlbyname(kern.safeboot)
              JZdziarski Level 1 Level 1 (0 points)

              So it's looking like there must be no way to do this with the KPI, as nobody's responded with anything... normally this is something I'd do from userland, but in safe boot mode, no launch daemons or launch agents starts for me to connect to. Is there any good, safe code example somewhere to demonstrate how to spawn a new process from kernel space? I know some applications like VirtualBox do this, but I've been trying to avoid it if necessary. I suspect the only way to tell (reliably) if I'm in safe boot is to spawn a user process from the kernel, then have it pull this var for me. Open to any other suggestions.

                • Re: can't sysctlbyname(kern.safeboot)
                  eskimo Apple Staff Apple Staff (6,015 points)

                  Is there any good, safe code example somewhere to demonstrate how to spawn a new process from kernel space?

                  There is no supported way to do that.

                  Getting back to the safe boot issue, I took a quick look into this earlier and couldn’t find any good options.  If you want someone to dig deeper into this, my recommendation is that you open a DTS tech support incident.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

              • Re: can't sysctlbyname(kern.safeboot)
                JZdziarski Level 1 Level 1 (0 points)

                For anyone else dealing with this, the workaround was to parse the boot args for -x:

                 

                char namep[16];

                if (PE_parse_boot_argn("-x", namep, sizeof (namep)))

                {

                     // safe boot

                }

                 

                Thanks, DTS!