Search code examples
iosobjective-cforkjailbreak

Using fork objc


When detecting jailbroken device, why do most examples terminate app if fork process fails? Doesn't that mean device is not jailbroken, so everything is ok, without any child process?

int pid = fork();
if (!pid){
    exit(0);
}
if (pid >= 0) {
    return YES;
}

Solution

  • A PID of 0 doesn't mean the fork failed. It means the fork succeeded and that the current process is the child. PID > 0 means the fork succeeded and the current process is the parent (the returned value is the child's PID). PID -1 means that it failed.

    Since app processes are usually forbidden from forking, a -1 is the expected result. If fork is allowed, then there are two processes. The parent returns YES, and the child is terminated since it isn't actually needed for anything but testing whether the fork was allowed.