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>
#endif
#include <string>
#include <signal.h>
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;