From 99be43e366b5c4d9dbaeaad9d885ff6d7f3aec4d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 20 Jan 2017 00:34:09 +0100 Subject: Fix and improve blind-gauss-blur, and fix new bug in blind-from-image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/util.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index b87e9b5..c315010 100644 --- a/src/util.c +++ b/src/util.c @@ -167,35 +167,56 @@ enfork(int status) } +/* If <() is used in Bash (possibily other shells), that process becomes + * child of the process for each <() is used. Therefore, we cannot simply + * wait until the last child has been reaped, or even the expected number + * of children has been reaped, we must instead remember the PID of each + * child we created and wait for all of them to be reaped. { */ + int -enfork_jobs(int status, size_t *start, size_t *end, size_t jobs) +enfork_jobs(int status, size_t *start, size_t *end, size_t jobs, pid_t **pids) { size_t j, s = *start, n = *end - *start; - if (jobs < 2) + pid_t pid; + if (jobs < 2) { + *pids = NULL; return 1; + } *end = n / jobs + s; + *pids = enmalloc(status, jobs * sizeof(**pids)); for (j = 1; j < jobs; j++) { - if (!enfork(status)) { + pid = enfork(status); + if (!pid) { #if defined(HAVE_PRCTL) && defined(PR_SET_PDEATHSIG) prctl(PR_SET_PDEATHSIG, SIGKILL); #endif *start = n * (j + 0) / jobs + s; *end = n * (j + 1) / jobs + s; return 0; + } else { + (*pids)[j - 1] = pid; } } + (*pids)[jobs - 1] = -1; return 1; } void -enjoin_jobs(int status, int is_master) +enjoin_jobs(int status, int is_master, pid_t *pids) { int stat; + size_t i; if (!is_master) exit(0); - while (wait(&stat) != -1) - if (!stat) + if (!pids) + return; + for (i = 0; pids[i] != -1; i++) { + if (waitpid(pids[i], &stat, 0) == -1) + enprintf(status, "waitpid:"); + if (stat) exit(status); - if (errno != ECHILD) - enprintf(status, "wait:"); + } + free(pids); } + +/* } */ -- cgit v1.2.3-70-g09d2