From d7a41d40abe24a277ececef82d305eceb4874b04 Mon Sep 17 00:00:00 2001 From: dongresource Date: Mon, 31 Aug 2020 00:30:15 +0200 Subject: [PATCH] Replace signal() with sigaction(). --- src/main.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6c36e2c..447414e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include #endif #include +#include CNShardServer *shardServer; std::thread *shardThread; @@ -26,14 +27,36 @@ void startShard(CNShardServer* server) { server->start(); } +#ifndef _WIN32 // terminate gracefully on SIGINT (for gprof) void terminate(int arg) { - std::cout << "OpenFusion: terminating" << std::endl; + std::cout << "OpenFusion: terminating." << std::endl; shardServer->kill(); shardThread->join(); exit(0); } +void initsignals() { + struct sigaction act; + + memset((void*)&act, 0, sizeof(act)); + sigemptyset(&act.sa_mask); + + // tell the OS to not kill us if you use a broken pipe, just let us know thru recv() or send() + act.sa_handler = SIG_IGN; + if (sigaction(SIGPIPE, &act, NULL) < 0) { + perror("sigaction"); + exit(1); + } + + act.sa_handler = terminate; + if (sigaction(SIGINT, &act, NULL) < 0) { + perror("sigaction"); + exit(1); + } +} +#endif + int main() { #ifdef _WIN32 WSADATA wsaData; @@ -42,9 +65,7 @@ int main() { exit(EXIT_FAILURE); } #else - // tell the OS to not kill us if you use a broken pipe, just let us know thru recv() or send() - signal(SIGPIPE, SIG_IGN); - signal(SIGINT, terminate); // TODO: use sigaction() instead + initsignals(); #endif settings::init(); std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl;