From e111d4ab5c21f710d030db71a524c7a9cc2e89af Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 16 Aug 2025 17:21:16 -0400 Subject: [PATCH] Add signal handling --- ctest/main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++--------- sstp/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/ctest/main.cpp b/ctest/main.cpp index 80f65f6..3011d59 100644 --- a/ctest/main.cpp +++ b/ctest/main.cpp @@ -12,8 +12,13 @@ #include #include #include +#include #include + +bool registerSignalHandler(void); // registers a Control-c handler +void signalHandler(int signal); // The Control-C handler + class Info { public: @@ -104,6 +109,11 @@ void Monitor::doCallback(CallbackData &callbackData) int main(int argc, char ** argv) { + registerSignalHandler(); + while(true) + { + // do stuff + } // std::vector myBigVec(10000000, 2011); // std::vector s1; // std::vector s2; @@ -114,21 +124,42 @@ int main(int argc, char ** argv) // s2 = std::move(s1); - Monitor monitor; -// SmartPointer b(::new B(),PointerDisposition::Delete); - SmartPointer b(true); - Callback callback(&(*b),&B::callback); - monitor.setCallback(callback); + // Monitor monitor; + // SmartPointer b(true); + // Callback callback(&(*b),&B::callback); + // monitor.setCallback(callback); - Info info; - info.setMessage("Hello"); + // Info info; + // info.setMessage("Hello"); - CallbackData callbackData(info); - monitor.doCallback(callbackData); + // CallbackData callbackData(info); + // monitor.doCallback(callbackData); } +/// @brief The signal handler +void signalHandler(int signal) +{ + std::cout << "Received signal " << signal << std::endl; + exit(signal); +} +/// @brief Method that registers the signal handler +/// @param +bool registerSignalHandler(void) +{ + struct sigaction sa; + sa.sa_handler = signalHandler; + sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals + sa.sa_flags = 0; // No special flags + + if (-1==sigaction(SIGINT, &sa, nullptr)) + { + std::cerr << "Error registering SIGINT handler." << std::endl; + return false; + } + return true; +} diff --git a/sstp/main.cpp b/sstp/main.cpp index 0ae95dd..d003c3d 100644 --- a/sstp/main.cpp +++ b/sstp/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -15,22 +16,27 @@ #include #include -// https://www.man7.org/linux/man-pages/man2/accept.2.html - -void handleServer(Block &commands); -void handleClient(Block &commands); +void handleServer(Block &commands); // handler for server mode +void handleClient(Block &commands); // handler for client mode +bool registerSignalHandler(void); // registers a Control-c handler +void signalHandler(int signal); // The Control-C handler /// @brief /// @param argc /// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName} -/// @return +/// @return -1:Error 1:Success int main(int argc, char **argv) { int returnCode(0); Profiler profiler; - String version = "0.1.0.1"; // major version, minor version, patch, build + String version = "0.1.0.3"; // major version, minor version, patch, build std::cout << "sstp version " << version.str() << std::endl; + if(!registerSignalHandler()) + { + std::cout << "Unable to register CTRL-C handler" << std::endl; + return -1; + } try { if(argc<2) @@ -77,6 +83,33 @@ int main(int argc, char **argv) return returnCode; } +/// @brief The signal handler +/// TODO: If we are sending then we need to destroy the sender, if we are listening then we need to destroy the listener +/// Currently we exit the app without cleaning anything up. +void signalHandler(int signal) +{ + std::cout << "SSTP Received signal " << signal << std::endl; + exit(signal); +} + +/// @brief Method that registers the signal handler +/// @param +bool registerSignalHandler(void) +{ + struct sigaction sa; + sa.sa_handler = signalHandler; + sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals + sa.sa_flags = 0; // No special flags + + if (-1==sigaction(SIGINT, &sa, nullptr)) + { + std::cerr << "Error registering SIGINT handler." << std::endl; + return false; + } + return true; +} + + /// @brief [0]=program [1]=SERVERMODE [2]={port} /// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion /// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer()