Revise waiting for child processes

This commit is contained in:
Bert Münnich 2018-02-18 14:32:55 +01:00
parent 762420cdf1
commit 09b04d7aef
1 changed files with 20 additions and 6 deletions

26
main.c
View File

@ -219,7 +219,6 @@ void close_info(void)
kill(info.pid, SIGTERM); kill(info.pid, SIGTERM);
close(info.fd); close(info.fd);
info.fd = -1; info.fd = -1;
waitpid(info.pid, NULL, WNOHANG);
} }
} }
@ -468,7 +467,7 @@ void run_key_handler(const char *key, unsigned int mask)
FILE *pfs; FILE *pfs;
bool marked = mode == MODE_THUMB && markcnt > 0; bool marked = mode == MODE_THUMB && markcnt > 0;
bool changed = false; bool changed = false;
int f, i, pfd[2], status; int f, i, pfd[2];
int fcnt = marked ? markcnt : 1; int fcnt = marked ? markcnt : 1;
char kstr[32]; char kstr[32];
struct stat *oldst, st; struct stat *oldst, st;
@ -526,9 +525,7 @@ void run_key_handler(const char *key, unsigned int mask)
} }
} }
fclose(pfs); fclose(pfs);
waitpid(pid, &status, 0); while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
error(0, 0, "%s: Exited abnormally", keyhandler.f.cmd);
for (f = i = 0; f < fcnt; i++) { for (f = i = 0; f < fcnt; i++) {
if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { 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); 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 main(int argc, char **argv)
{ {
int i, start; int i, start;
@ -798,7 +811,8 @@ int main(int argc, char **argv)
struct stat fstats; struct stat fstats;
r_dir_t dir; r_dir_t dir;
signal(SIGPIPE, SIG_IGN); setup_signal(SIGCHLD, sigchld);
setup_signal(SIGPIPE, SIG_IGN);
setlocale(LC_COLLATE, ""); setlocale(LC_COLLATE, "");