diff --git a/src/natives/subprocess.cpp b/src/natives/subprocess.cpp index 7acbc33..53c6bf6 100644 --- a/src/natives/subprocess.cpp +++ b/src/natives/subprocess.cpp @@ -169,6 +169,11 @@ var builtin_subprocess_active(context* ctx, gc* ngc) { int status; pid_t result = waitpid(pid, &status, WNOHANG); + // this means the child process is returned + if (result==pid) { + obj.ghost().get()->status = status; + } + return result==0? one:zero; #endif } @@ -192,21 +197,31 @@ var builtin_subprocess_terminate(context* ctx, gc* ngc) { CloseHandle(pi->hProcess); CloseHandle(pi->hThread); + + return var::num(res); #else auto pid = obj.ghost().get()->pid; int status; pid_t result = waitpid(pid, &status, WNOHANG); - // child process running - if (result==0) { - kill(pid, SIGTERM); + if (result<0) { + auto pstat = obj.ghost().get()->status; + // if pstat is not 0, means child process already exited + auto res = WIFEXITED(pstat)? WEXITSTATUS(pstat):-1; + return var::num(res); } - auto res = WIFEXITED(status)? WEXITSTATUS(status):-1; -#endif + // child process is still running + if (result==0) { + kill(pid, SIGTERM); + return var::num(-1); + } + // child process exited when calling the waitpid above + auto res = WIFEXITED(status)? WEXITSTATUS(status):-1; return var::num(res); +#endif } nasal_builtin_table subprocess_native[] = { diff --git a/src/natives/subprocess.h b/src/natives/subprocess.h index 58db648..1af161c 100644 --- a/src/natives/subprocess.h +++ b/src/natives/subprocess.h @@ -17,6 +17,7 @@ namespace nasal { struct subprocess { #ifndef WIN32 pid_t pid; + int status = 0; #else STARTUPINFOW si; PROCESS_INFORMATION pi; diff --git a/test/watchdog.nas b/test/watchdog.nas index 1e4360f..9f1e3fa 100644 --- a/test/watchdog.nas +++ b/test/watchdog.nas @@ -65,7 +65,7 @@ while(1) { modified_time = latest_modified_time; println(os_time(), modified_hd(), filename); - var cmd = (os.platform()=="windows"?"":"./") ~ "nasal " ~ filename; + var cmd = "nasal " ~ filename; foreach(var i; args) { cmd ~= " " ~ i; }