Use sigaction instead of signal

This way we don't have to handle EINTR.
This commit is contained in:
madmaurice 2021-01-15 20:22:22 +01:00
parent 48445a573b
commit 6655b38a47

14
main.c
View file

@ -41,6 +41,7 @@ void drop_root(void)
} }
} }
struct sigaction forward_signal_descriptor;
void forward_signal(int sig) void forward_signal(int sig)
{ {
if (kill(pid_child, sig) == -1) 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 main(int argc, const char** argv)
{ {
int err; int err;
forward_signal_descriptor.sa_flags = SA_RESTART;
forward_signal_descriptor.sa_handler = &forward_signal;
if (argc == 1) if (argc == 1)
{ {
printf("Usage: pidjail PROGRAM ARGUMENTS...\n" printf("Usage: pidjail PROGRAM ARGUMENTS...\n"
@ -99,16 +104,15 @@ int main(int argc, const char** argv)
// Setup signal handler to forward SIGTERM // Setup signal handler to forward SIGTERM
pid_child = pid; 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"); 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. // Could be interrupt due to a signal. Retry in that case.
int status; int status;
while (waitpid(pid, &status, 0) == -1) if (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;
@ -143,7 +147,7 @@ int main(int argc, const char** argv)
// Setup forward for SIGTERM // Setup forward for SIGTERM
pid_child = first_child; 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"); printf("Unable to setup signal forward in init. Aborting.\n");
return 1; return 1;
@ -153,7 +157,7 @@ int main(int argc, const char** argv)
do { do {
exited_child = wait(&child_status); exited_child = wait(&child_status);
err = errno; 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) if (exited_child == -1)
{ {