Add signal handling

This commit is contained in:
2025-08-16 17:21:16 -04:00
parent aef2d050f5
commit e111d4ab5c
2 changed files with 79 additions and 15 deletions

View File

@@ -4,6 +4,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <csignal>
#include <arpa/inet.h>
#include <thread>
#include <vector>
@@ -15,22 +16,27 @@
#include <sstp/socketserver.hpp>
#include <sstp/clientsocketsender.hpp>
// https://www.man7.org/linux/man-pages/man2/accept.2.html
void handleServer(Block<String> &commands);
void handleClient(Block<String> &commands);
void handleServer(Block<String> &commands); // handler for server mode
void handleClient(Block<String> &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()