🐛 fix error return value after call waitpid

This commit is contained in:
ValKmjolnir 2024-06-18 01:00:22 +08:00
parent 35c8afe56b
commit 456ed5c782
3 changed files with 22 additions and 6 deletions

View File

@ -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<subprocess>()->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<subprocess>()->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<subprocess>()->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[] = {

View File

@ -17,6 +17,7 @@ namespace nasal {
struct subprocess {
#ifndef WIN32
pid_t pid;
int status = 0;
#else
STARTUPINFOW si;
PROCESS_INFORMATION pi;

View File

@ -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;
}