From 09b04d7aefc55112b3994c30236010750fbec369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20M=C3=BCnnich?= Date: Sun, 18 Feb 2018 14:32:55 +0100 Subject: [PATCH] Revise waiting for child processes --- main.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 427362f..a9c9c44 100644 --- a/main.c +++ b/main.c @@ -219,7 +219,6 @@ void close_info(void) kill(info.pid, SIGTERM); close(info.fd); info.fd = -1; - waitpid(info.pid, NULL, WNOHANG); } } @@ -468,7 +467,7 @@ void run_key_handler(const char *key, unsigned int mask) FILE *pfs; bool marked = mode == MODE_THUMB && markcnt > 0; bool changed = false; - int f, i, pfd[2], status; + int f, i, pfd[2]; int fcnt = marked ? markcnt : 1; char kstr[32]; struct stat *oldst, st; @@ -526,9 +525,7 @@ void run_key_handler(const char *key, unsigned int mask) } } fclose(pfs); - waitpid(pid, &status, 0); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) - error(0, 0, "%s: Exited abnormally", keyhandler.f.cmd); + while (waitpid(pid, NULL, 0) == -1 && errno == EINTR); for (f = i = 0; f < fcnt; i++) { if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { @@ -788,6 +785,22 @@ int fncmp(const void *a, const void *b) return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name); } +void sigchld(int sig) +{ + while (waitpid(-1, NULL, WNOHANG) > 0); +} + +void setup_signal(int sig, void (*handler)(int sig)) +{ + struct sigaction sa; + + sa.sa_handler = handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; + if (sigaction(sig, &sa, 0) == -1) + error(EXIT_FAILURE, errno, "signal %d", sig); +} + int main(int argc, char **argv) { int i, start; @@ -798,7 +811,8 @@ int main(int argc, char **argv) struct stat fstats; r_dir_t dir; - signal(SIGPIPE, SIG_IGN); + setup_signal(SIGCHLD, sigchld); + setup_signal(SIGPIPE, SIG_IGN); setlocale(LC_COLLATE, "");