diff --git a/main.c b/main.c index 4033c19..fc79ba5 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,7 @@ void drop_root(void) } } +struct sigaction forward_signal_descriptor; void forward_signal(int sig) { if (kill(pid_child, sig) == -1) @@ -64,6 +65,10 @@ char** argdup(int argc, const char** argv) int main(int argc, const char** argv) { int err; + + forward_signal_descriptor.sa_flags = SA_RESTART; + forward_signal_descriptor.sa_handler = &forward_signal; + if (argc == 1) { printf("Usage: pidjail PROGRAM ARGUMENTS...\n" @@ -99,16 +104,15 @@ int main(int argc, const char** argv) // Setup signal handler to forward SIGTERM pid_child = pid; - if (signal(SIGTERM, forward_signal) == SIG_ERR) + if (sigaction(SIGTERM, &forward_signal_descriptor, NULL) == -1) { 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; - while (waitpid(pid, &status, 0) == -1) + if (waitpid(pid, &status, 0) == -1) { - if(errno == EINTR) continue; // On EINTR retry. err = errno; printf("Failed to wait (%d)\n", err); return err; @@ -143,7 +147,7 @@ int main(int argc, const char** argv) // Setup forward for SIGTERM pid_child = first_child; - if (signal(SIGTERM, forward_signal) == SIG_ERR) + if (sigaction(SIGTERM, &forward_signal_descriptor, NULL) == -1) { printf("Unable to setup signal forward in init. Aborting.\n"); return 1; @@ -153,7 +157,7 @@ int main(int argc, const char** argv) do { exited_child = wait(&child_status); err = errno; - } while (!(exited_child == first_child || (exited_child == -1 && err != EINTR))); + } while (!(exited_child == first_child || (exited_child == -1 && err == ECHILD))); if (exited_child == -1) {