fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

commit dde5bc1589dc5b5000e4eeea19fa88bfc0433f6a
parent d45d8b91076b184cf6409137149a300c1146d18a
Author: Friedel Schön <[email protected]>
Date:   Tue, 23 May 2023 11:42:31 +0200

fixing infinite waidpid again

Diffstat:
Msrc/stage.c | 65+++++++++++++++++++++--------------------------------------------
1 file changed, 21 insertions(+), 44 deletions(-)

diff --git a/src/stage.c b/src/stage.c @@ -1,5 +1,6 @@ #include "config.h" #include "service.h" +#include "util.h" #include <errno.h> #include <fcntl.h> @@ -56,56 +57,32 @@ void service_stage(int stage) { print_error("error: unable to exec stage %d: %s\n", stage); _exit(1); } - bool dont_wait = false; - for (;;) { - int child; - int sig = 0; - - if (!dont_wait) { - sigemptyset(&ss); - sigaddset(&ss, SIGCHLD); - sigaddset(&ss, SIGCONT); - sigaddset(&ss, SIGINT); - - sigwait(&ss, &sig); - } - dont_wait = false; - do { - child = waitpid(-1, &exitstat, WNOHANG); - } while (child > 0 && child != pid); + int child; + int sig = 0; - if (child == -1) { - print_error("warn: waitpid failed: %s"); - sleep(5); - } + sigemptyset(&ss); + sigaddset(&ss, SIGCHLD); + sigaddset(&ss, SIGCONT); + sigaddset(&ss, SIGINT); - reclaim_console(); + sigwait(&ss, &sig); - if (child == pid && stage == 0) { - if (!WIFEXITED(exitstat) || WEXITSTATUS(exitstat) != 0) { - if (WIFSIGNALED(exitstat)) { - /* this is stage 1 */ - fprintf(stderr, "stage 1 failed: skip stage 2\n"); - daemon_running = false; - break; - } - } - printf("leave stage 1\n"); - break; - } - if (child <= 0) { - /* collect terminated children */ + if (waitpid(pid, &exitstat, 0) == -1) { + print_error("warn: waitpid failed: %s"); + sleep(5); + } - dont_wait = true; - continue; - } + reclaim_console(); - /* sig? */ - if (sig != SIGCONT && sig != SIGINT) { - continue; + if (child == pid && stage == 0) { + if (!WIFEXITED(exitstat) || WEXITSTATUS(exitstat) != 0) { + if (WIFSIGNALED(exitstat)) { + /* this is stage 1 */ + fprintf(stderr, "stage 1 failed: skip stage 2\n"); + daemon_running = false; + } } - - fprintf(stderr, "warn: signals only work in stage 2, ignoring...\n"); + printf("leave stage 1\n"); } }