Handle EINTR error code for wait and waitpid

This commit is contained in:
madmaurice 2021-01-15 20:22:22 +01:00
parent 12e4cd391a
commit 48445a573b

7
main.c
View file

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