🐛 fix error return value after call waitpid
This commit is contained in:
parent
35c8afe56b
commit
456ed5c782
|
@ -169,6 +169,11 @@ var builtin_subprocess_active(context* ctx, gc* ngc) {
|
||||||
int status;
|
int status;
|
||||||
pid_t result = waitpid(pid, &status, WNOHANG);
|
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;
|
return result==0? one:zero;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -192,21 +197,31 @@ var builtin_subprocess_terminate(context* ctx, gc* ngc) {
|
||||||
|
|
||||||
CloseHandle(pi->hProcess);
|
CloseHandle(pi->hProcess);
|
||||||
CloseHandle(pi->hThread);
|
CloseHandle(pi->hThread);
|
||||||
|
|
||||||
|
return var::num(res);
|
||||||
#else
|
#else
|
||||||
auto pid = obj.ghost().get<subprocess>()->pid;
|
auto pid = obj.ghost().get<subprocess>()->pid;
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
pid_t result = waitpid(pid, &status, WNOHANG);
|
pid_t result = waitpid(pid, &status, WNOHANG);
|
||||||
|
|
||||||
// child process running
|
if (result<0) {
|
||||||
if (result==0) {
|
auto pstat = obj.ghost().get<subprocess>()->status;
|
||||||
kill(pid, SIGTERM);
|
// 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;
|
// child process is still running
|
||||||
#endif
|
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);
|
return var::num(res);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
nasal_builtin_table subprocess_native[] = {
|
nasal_builtin_table subprocess_native[] = {
|
||||||
|
|
|
@ -17,6 +17,7 @@ namespace nasal {
|
||||||
struct subprocess {
|
struct subprocess {
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
int status = 0;
|
||||||
#else
|
#else
|
||||||
STARTUPINFOW si;
|
STARTUPINFOW si;
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
|
|
|
@ -65,7 +65,7 @@ while(1) {
|
||||||
modified_time = latest_modified_time;
|
modified_time = latest_modified_time;
|
||||||
println(os_time(), modified_hd(), filename);
|
println(os_time(), modified_hd(), filename);
|
||||||
|
|
||||||
var cmd = (os.platform()=="windows"?"":"./") ~ "nasal " ~ filename;
|
var cmd = "nasal " ~ filename;
|
||||||
foreach(var i; args) {
|
foreach(var i; args) {
|
||||||
cmd ~= " " ~ i;
|
cmd ~= " " ~ i;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue