#include "fork_program.h"
#include "kext_tools_util.h"
#include <spawn.h>
#include <libc.h>
int fork_program(const char * argv0, char * const argv[], Boolean wait)
{
int result = -2;
int spawn_result = 0;
pid_t child_pid = -1;
int child_status = 0;
int normal_iopolicy = getiopolicy_np(IOPOL_TYPE_DISK,
IOPOL_SCOPE_PROCESS);
extern char ** environ;
if (!wait) {
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_THROTTLE);
}
spawn_result = posix_spawn(&child_pid, argv0, NULL,
NULL, argv, environ);
if (spawn_result < 0) {
OSKextLog( NULL, kOSKextLogErrorLevel,
"posix_spawn failed for %s.", argv0);
goto finish;
}
OSKextLog( NULL, kOSKextLogDetailLevel,
"started child process %s[%d] (%ssynchronous).",
argv0, child_pid, wait ? "" : "a");
if (wait) {
OSKextLogSpec logSpec = kOSKextLogDetailLevel;
waitpid(child_pid, &child_status, 0);
result = WEXITSTATUS(child_status);
if (result) {
logSpec = kOSKextLogErrorLevel;
}
OSKextLog( NULL, logSpec,
"Child process %s[%d] exited with status %d.",
argv0, child_pid, WEXITSTATUS(child_status));
} else {
result = child_pid;
}
finish:
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, normal_iopolicy);
return result;
}