From 48445a573b0dfd6deaadac91bfd9f4b705dd8bc5 Mon Sep 17 00:00:00 2001
From: MadMaurice <madmaurice@zom.bi>
Date: Fri, 15 Jan 2021 20:22:22 +0100
Subject: [PATCH] Handle EINTR error code for wait and waitpid

---
 main.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c
index ab70d81..4033c19 100644
--- a/main.c
+++ b/main.c
@@ -104,9 +104,11 @@ int main(int argc, const char** argv)
           printf("Unable to setup signal handler in head\n");
         }
       // parent waits for child then exits
+      // Could be interrupt due to a signal. Retry in that case.
       int status;
-      if (waitpid(pid, &status, 0) == -1)
+      while (waitpid(pid, &status, 0) == -1)
         {
+          if(errno == EINTR) continue; // On EINTR retry.
           err = errno;
           printf("Failed to wait (%d)\n", err);
           return err;
@@ -147,10 +149,11 @@ int main(int argc, const char** argv)
               return 1;
             }
 
+          // wait could be interrupt due to a signal. In that case just call wait again.
           do {
             exited_child = wait(&child_status);
             err = errno;
-          } while (exited_child != first_child && exited_child != -1);
+          } while (!(exited_child == first_child || (exited_child == -1 && err != EINTR)));
 
           if (exited_child == -1)
             {