From b3f54e290ae82dd8ee948c472b7eae061adff2c7 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Tue, 1 Feb 2022 10:34:12 +0100 Subject: [PATCH] Change parent behaviour when calling waitpid(). Wait for any process in our process group only instead of WAIT_ANY. This allows the parent process to start subprocesses that end up in different process groups which are handled in user code instead completely (using signalfd for example). --- src/worker.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/worker.c b/src/worker.c index f14d95b..2b3dccf 100644 --- a/src/worker.c +++ b/src/worker.c @@ -61,10 +61,6 @@ #include "seccomp.h" #endif -#if !defined(WAIT_ANY) -#define WAIT_ANY (-1) -#endif - #define WORKER_SOLO_COUNT 3 #define WORKER(id) \ @@ -92,6 +88,7 @@ static void worker_accept_avail(struct kore_msg *, const void *); static void worker_entropy_recv(struct kore_msg *, const void *); static void worker_keymgr_response(struct kore_msg *, const void *); +static pid_t worker_pgrp; static int accept_avail; static struct kore_worker *kore_workers; static int worker_no_lock; @@ -153,6 +150,9 @@ kore_worker_init(void) if (!kore_quiet) kore_log(LOG_INFO, "starting worker processes"); + if ((worker_pgrp = getpgrp()) == -1) + fatal("%s: getpgrp(): %s", __func__, errno_s); + /* Now start all the workers. */ id = 1; cpu = 1; @@ -647,19 +647,22 @@ kore_worker_reap(void) pid_t pid; int status; - pid = waitpid(WAIT_ANY, &status, WNOHANG); + for (;;) { + pid = waitpid(-worker_pgrp, &status, WNOHANG); - if (pid == -1) { - if (errno == ECHILD || errno == EINTR) - return; - kore_log(LOG_ERR, "%s: waitpid(): %s", __func__, errno_s); - return; + if (pid == -1) { + if (errno != ECHILD && errno != EINTR) { + kore_log(LOG_ERR, + "%s: waitpid(): %s", __func__, errno_s); + } + break; + } + + if (pid == 0) + break; + + worker_reaper(pid, status); } - - if (pid == 0) - return; - - worker_reaper(pid, status); } void