Handle SIGTERM and SIGINT for graceful shutdown

This commit is contained in:
Thomas Kolb 2024-01-03 22:03:19 +01:00
parent ee63483b8f
commit be5fa06950

View file

@ -1,9 +1,7 @@
#include <linux/if.h> #include <linux/if.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <time.h>
#include <liquid/liquid.h> #include <liquid/liquid.h>
@ -11,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <signal.h>
#include "utils.h" #include "utils.h"
#include "layer1/tx.h" #include "layer1/tx.h"
@ -42,6 +41,17 @@ static struct {
} m_stats; } 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) static void block_tx_for(unsigned offset_ms)
{ {
next_tx_switch_time = get_hires_time() + (double)offset_ms * 0.001; 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_tx_init(&tx));
RESULT_CHECK(layer1_rx_init(&rx, cb_rx)); 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 ** // ** Process packets **
struct pollfd pfd; struct pollfd pfd;