From 4bde24cb63313f636ac72b449f54d68515efe485 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 16 Feb 2021 11:14:46 -0500 Subject: fix process-posix.c --- main.c | 4 +++- process-posix.c | 18 ++++++++++++------ process.h | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index d879cb7..d2f7e2f 100644 --- a/main.c +++ b/main.c @@ -155,7 +155,7 @@ int main(int argc, char **argv) { printf("Error: %s\n", process_geterr(proc)); return EXIT_FAILURE; } - #if 1 + #if 0 { i64 bytes = 0; char buf[256]; @@ -172,6 +172,8 @@ int main(int argc, char **argv) { } } } + #endif + #if 1 { char message[256]; while (1) { diff --git a/process-posix.c b/process-posix.c index d683abe..da0448d 100644 --- a/process-posix.c +++ b/process-posix.c @@ -58,8 +58,10 @@ long long process_read(Process *proc, char *data, size_t size) { } -void process_stop(Process *proc) { +void process_kill(Process *proc) { kill(proc->pid, SIGKILL); + // get rid of zombie process + waitpid(proc->pid, NULL, 0); close(proc->pipe); proc->pid = 0; proc->pipe = 0; @@ -67,8 +69,13 @@ void process_stop(Process *proc) { int process_check_status(Process *proc, char *message, size_t message_size) { int wait_status = 0; - if (waitpid(proc->pid, &wait_status, WNOHANG) >= 0) { + int ret = waitpid(proc->pid, &wait_status, WNOHANG); + if (ret == 0) { + // process still running + return 0; + } else if (ret > 0) { if (WIFEXITED(wait_status)) { + close(proc->pipe); proc->pipe = 0; int code = WEXITSTATUS(wait_status); if (code == 0) { str_printf(message, message_size, "exited successfully"); @@ -78,16 +85,15 @@ int process_check_status(Process *proc, char *message, size_t message_size) { return -1; } } else if (WIFSIGNALED(wait_status)) { + close(proc->pipe); str_printf(message, message_size, "terminated by signal %d", WTERMSIG(wait_status)); return -1; } return 0; - } else if (errno == ECHILD) { + } else { // this process is gone or something? + close(proc->pipe); proc->pipe = 0; str_printf(message, message_size, "process ended unexpectedly"); return -1; - } else { - // probably shouldn't happen - return 0; } } diff --git a/process.h b/process.h index a21fd5b..023145c 100644 --- a/process.h +++ b/process.h @@ -20,7 +20,7 @@ long long process_read(Process *process, char *data, size_t size); // 0 if the process hasn't exited. // If message is not NULL, it will be set to a description of what happened (e.g. "exited successfully") int process_check_status(Process *process, char *message, size_t message_size); -// kills process if still running, and frees data associated with it. -void process_stop(Process *process); +// kills process if still running +void process_kill(Process *process); #endif -- cgit v1.2.3