From: Brendan Hansen Date: Thu, 2 Dec 2021 14:08:14 +0000 (-0600) Subject: defined the return value of __spawn_process X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4d7ad9504fa16063fabec1e9cec6e709642da602;p=onyx.git defined the return value of __spawn_process --- diff --git a/core/runtime/onyx_run.onyx b/core/runtime/onyx_run.onyx index 4b2fd6d4..a6015624 100644 --- a/core/runtime/onyx_run.onyx +++ b/core/runtime/onyx_run.onyx @@ -17,4 +17,10 @@ use package wasi #export "_thread_exit" _thread_exit } -__spawn_process :: (path: str, args: [] str) -> i32 #foreign "env" "spawn_process" --- \ No newline at end of file +#local SpawnProcessResult :: enum { + Success :: 0x00; + FailedToRun :: 0x01; + Error :: 0x02; +} + +__spawn_process :: (path: str, args: [] str) -> SpawnProcessResult #foreign "env" "spawn_process" --- \ No newline at end of file diff --git a/include/small_windows.h b/include/small_windows.h index 9aa075f5..4f3114dd 100644 --- a/include/small_windows.h +++ b/include/small_windows.h @@ -306,6 +306,7 @@ GB_DLL_IMPORT BOOL WINAPI CreateProcessA (char const * lpApplicationName, BOOL bInheritHandles, DWORD dwCreationFlags, void* lpEnvironment, char const * lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation); +GB_DLL_IMPORT BOOL WINAPI GetExitCodeProcess (HANDLE hProcess, DWORD *lpExitCode); GB_DLL_IMPORT BOOL WINAPI GetLogicalProcessorInformation(SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buffer, DWORD *return_length); GB_DLL_IMPORT DWORD_PTR WINAPI SetThreadAffinityMask(HANDLE thread, DWORD_PTR check_mask); diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index b3d8d4c2..862904f8 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -201,13 +201,25 @@ static wasm_trap_t* onyx_spawn_process_impl(const wasm_val_vec_t* params, wasm_v process_args[0] = process_path; process_args[args_len + 1] = NULL; - if (fork() == 0) { - execv(process_path, process_args); + switch (fork()) { + case -1: // Bad fork + results->data[0] = WASM_I32_VAL(1); // Failed to run + break; + + case 0: // Child process + execv(process_path, process_args); + + default: { + i32 status; + wait(&status); + + i32 exit_status = WEXITSTATUS(status); + + results->data[0] = WASM_I32_VAL(exit_status != 0 ? 2 : 0); // Error if non-zero exit, Success if zero. + break; + } } - i32 status; - wait(&status); - results->data[0] = WASM_I32_VAL(WEXITSTATUS(status)); #endif #ifdef _BH_WINDOWS @@ -232,8 +244,15 @@ static wasm_trap_t* onyx_spawn_process_impl(const wasm_val_vec_t* params, wasm_v PROCESS_INFORMATION proc_info; BOOL success = CreateProcessA(process_path, cmdLine, NULL, NULL, 1, 0, NULL, NULL, &startup, &proc_info); + if (!success) { + results->data[0] = WASM_I32_VAL(1); // Failed to run + return NULL; + } + DWORD result = WaitForSingleObject(proc_info.hProcess, INFINITE); - results->data[0] = WASM_I32_VAL(result); + DWORD exitCode; + GetExitCodeProcess(proc_info.hProcess, &exitCode); + results->data[0] = WASM_I32_VAL(exitCode != 0 ? 2 : 0); #endif return NULL;