diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-12-25 16:13:53 +0100 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2025-01-02 06:28:51 -0700 |
| commit | 9e5917823209f217f65c9b4c9d3ae7b2c45c9d2e (patch) | |
| tree | 7194ddb9ef9dcf29a7b7327bb543175d9e24c763 /vis-subprocess.c | |
| parent | 36efd90ceed914496739479998d2849ff5e07d11 (diff) | |
| download | vis-9e5917823209f217f65c9b4c9d3ae7b2c45c9d2e.tar.gz vis-9e5917823209f217f65c9b4c9d3ae7b2c45c9d2e.tar.xz | |
move waiting and potentially killing a subprocess into a helper function
The separation between reading from a subprocess and handling its
life time will be useful for future changes.
Diffstat (limited to 'vis-subprocess.c')
| -rw-r--r-- | vis-subprocess.c | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/vis-subprocess.c b/vis-subprocess.c index 39282e4..31a1479 100644 --- a/vis-subprocess.c +++ b/vis-subprocess.c @@ -177,6 +177,36 @@ static void read_and_fire(Vis* vis, int fd, const char *name, ResponseType rtype } /** + * Checks if a subprocess is dead or needs to be killed then raises an event + * or kills it if necessary. + * @param current the process to wait for or kill + * @return true if the process is dead + */ +static bool wait_or_kill_process(Vis *vis, Process *current) { + int status; + pid_t wpid = waitpid(current->pid, &status, WNOHANG); + if (wpid == -1) { + vis_message_show(vis, strerror(errno)); + } else if (wpid == current->pid) { + goto just_destroy; + } else if (!*(current->invalidator)) { + goto kill_and_destroy; + } + return false; + +kill_and_destroy: + kill(current->pid, SIGTERM); + waitpid(current->pid, &status, 0); +just_destroy: + if (WIFSIGNALED(status)) { + vis_lua_process_response(vis, current->name, NULL, WTERMSIG(status), SIGNAL); + } else { + vis_lua_process_response(vis, current->name, NULL, WEXITSTATUS(status), EXIT); + } + return true; +} + +/** * Checks if `readfds` contains file descriptors of subprocesses from * the pool. If so, it reads their data and fires corresponding events. * Also checks if each subprocess from the pool is dead or needs to be @@ -192,27 +222,11 @@ void vis_process_tick(Vis *vis, fd_set *readfds) { if (current->errfd != -1 && FD_ISSET(current->errfd, readfds)) { read_and_fire(vis, current->errfd, current->name, STDERR); } - int status; - pid_t wpid = waitpid(current->pid, &status, WNOHANG); - if (wpid == -1) { - vis_message_show(vis, strerror(errno)); - } else if (wpid == current->pid) { - goto just_destroy; - } else if (!*(current->invalidator)) { - goto kill_and_destroy; - } - pointer = ¤t->next; - continue; -kill_and_destroy: - kill(current->pid, SIGTERM); - waitpid(current->pid, &status, 0); -just_destroy: - if (WIFSIGNALED(status)) { - vis_lua_process_response(vis, current->name, NULL, WTERMSIG(status), SIGNAL); + if (!wait_or_kill_process(vis, current)) { + pointer = ¤t->next; } else { - vis_lua_process_response(vis, current->name, NULL, WEXITSTATUS(status), EXIT); + /* update our iteration pointer */ + *pointer = destroy_process(current); } - /* update our iteration pointer */ - *pointer = destroy_process(current); } } |
