diff options
author | Bert Münnich <ber.t@posteo.de> | 2018-02-18 14:32:55 +0100 |
---|---|---|
committer | Bert Münnich <ber.t@posteo.de> | 2018-02-18 14:51:46 +0100 |
commit | 09b04d7aefc55112b3994c30236010750fbec369 (patch) | |
tree | 58a334f0a79eb5c8ef10ec7c3650a130e425f52a | |
parent | 762420cdf135a9a8381d43b074aa8f286bc4902e (diff) |
Revise waiting for child processes
-rw-r--r-- | main.c | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -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, ""); |