From f3fbb4c77f39cea4e38794e41b321326827a1ecf Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Tue, 29 Nov 2022 11:30:29 +0100 Subject: [PATCH] feat(feuille.c): handle children exit codes --- feuille.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/feuille.c b/feuille.c index 034f0cf..9c261db 100644 --- a/feuille.c +++ b/feuille.c @@ -408,10 +408,24 @@ int main(int argc, char *argv[]) verbose(1, "all workers have been initialized."); verbose(1, "beginning to accept incoming connections."); - /* wait for children to finish */ - // TODO: handle children exit codes properly - while (wait(0) > 0); - close(server); + /* fork again if a child dies */ + int status; + int child_pid; + while ((child_pid = wait(&status)) > 0) { + error("child %d unexpectedly died with exit code %d.", child_pid, WEXITSTATUS(status)); + + /* do not fork if child was KILL'ed */ + if (WTERMSIG(status) == 9) + continue; + + if ((pid = fork()) == 0) { + accept_loop(server); + + } else if (pid < 0) + error("could not fork killed child again: ", strerror(errno)); + } + + close(server); return 0; }