Replace signal() with sigaction().

This commit is contained in:
dongresource 2020-08-31 00:30:15 +02:00
parent 0aeac0f6f3
commit d7a41d40ab

View File

@ -18,6 +18,7 @@
#include <thread> #include <thread>
#endif #endif
#include <string> #include <string>
#include <signal.h>
CNShardServer *shardServer; CNShardServer *shardServer;
std::thread *shardThread; std::thread *shardThread;
@ -26,14 +27,36 @@ void startShard(CNShardServer* server) {
server->start(); server->start();
} }
#ifndef _WIN32
// terminate gracefully on SIGINT (for gprof) // terminate gracefully on SIGINT (for gprof)
void terminate(int arg) { void terminate(int arg) {
std::cout << "OpenFusion: terminating" << std::endl; std::cout << "OpenFusion: terminating." << std::endl;
shardServer->kill(); shardServer->kill();
shardThread->join(); shardThread->join();
exit(0); 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() { int main() {
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsaData; WSADATA wsaData;
@ -42,9 +65,7 @@ int main() {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#else #else
// tell the OS to not kill us if you use a broken pipe, just let us know thru recv() or send() initsignals();
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, terminate); // TODO: use sigaction() instead
#endif #endif
settings::init(); settings::init();
std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl; std::cout << "[INFO] Protocol version: " << PROTOCOL_VERSION << std::endl;