diff --git a/impl/src/main.c b/impl/src/main.c index 55b4b62..18a0445 100644 --- a/impl/src/main.c +++ b/impl/src/main.c @@ -1,9 +1,7 @@ #include #include #include -#include #include -#include #include @@ -11,6 +9,7 @@ #include #include #include +#include #include "utils.h" #include "layer1/tx.h" @@ -42,6 +41,17 @@ static struct { } m_stats; +static void signal_handler(int signal, siginfo_t *info, void *ctx) +{ + (void)signal; + (void)info; + (void)ctx; + + fprintf(stderr, "\nGracefully shutting down on signal %d.\n", signal); + + m_running = false; +} + static void block_tx_for(unsigned offset_ms) { next_tx_switch_time = get_hires_time() + (double)offset_ms * 0.001; @@ -210,6 +220,21 @@ int main(void) RESULT_CHECK(layer1_tx_init(&tx)); RESULT_CHECK(layer1_rx_init(&rx, cb_rx)); + // ** Set up signal handling + + struct sigaction term_action = {0}; + term_action.sa_sigaction = signal_handler; + + if(sigaction(SIGTERM, &term_action, NULL) < 0) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + + if(sigaction(SIGINT, &term_action, NULL) < 0) { + perror("sigaction"); + exit(EXIT_FAILURE); + } + // ** Process packets ** struct pollfd pfd;