From ee76a2e854aefe2b505a246f442fce1dfa87068e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Dahlstr=C3=B6m?= Date: Sun, 29 Mar 2020 20:23:56 +0200 Subject: [PATCH] Setup a SIGINT handler --- main.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index f345cf2..715b9ae 100644 --- a/main.cpp +++ b/main.cpp @@ -9,13 +9,18 @@ #include #include +#include #include "main.hpp" +namespace { + +std::weak_ptr g_ioService; + /* * Code from http://stackoverflow.com/a/77336/5419223 */ -static void sigsegv_handler(int sig) { +void sigsegv_handler(int sig) { constexpr int STACK_DEPTH = 10; void *array[STACK_DEPTH]; @@ -26,8 +31,36 @@ static void sigsegv_handler(int sig) { exit(1); } +void sigint_handler(int sig) +{ + fprintf(stderr, "Caught SIGINT, trying to stop\n"); + if (auto io = g_ioService.lock()) { + io->stop(); + } +} + +void SetupSignalHandlers() +{ + struct sigaction sigIntHandler; + + sigIntHandler.sa_handler = sigsegv_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGSEGV, &sigIntHandler, NULL); + + memset(&sigIntHandler, 0, sizeof(sigIntHandler)); + sigIntHandler.sa_handler = sigint_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + + sigaction(SIGINT, &sigIntHandler, NULL); +} + +} // anonymous namespace + int main(int argc, char *argv[]) { - signal(SIGSEGV, sigsegv_handler); + SetupSignalHandlers(); int max_calls; log4cpp::OstreamAppender appender("console", &std::cout); @@ -50,7 +83,8 @@ int main(int argc, char *argv[]) { sip::IncomingConnectionValidator connectionValidator(conf.getString("sip.validUriExpression")); - boost::asio::io_service ioService; + auto ioService = std::make_shared(); + g_ioService = ioService; try { max_calls = conf.getInt("sip.max_calls"); @@ -136,7 +170,7 @@ int main(int argc, char *argv[]) { for (int i = 0; icallId = i; using namespace std::placeholders; @@ -247,7 +281,7 @@ int main(int argc, char *argv[]) { logger.info("Application started."); - ioService.run(); + ioService->run(); return 0; }