mirror of
https://github.com/liyunfan1223/azerothcore-wotlk.git
synced 2026-08-08 07:48:02 +00:00
feat(Core/SignalHandler): Replace ACE signal handling with std (#4877)
This commit is contained in:
@@ -4,26 +4,41 @@
|
|||||||
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __SIGNAL_HANDLER_H__
|
#ifndef _SIGNAL_HANDLER_H_
|
||||||
#define __SIGNAL_HANDLER_H__
|
#define _SIGNAL_HANDLER_H_
|
||||||
|
|
||||||
#include <ace/Event_Handler.h>
|
#include <csignal>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace acore
|
namespace acore
|
||||||
{
|
{
|
||||||
|
|
||||||
/// Handle termination signals
|
/// Handle termination signals
|
||||||
class SignalHandler : public ACE_Event_Handler
|
class SignalHandler
|
||||||
{
|
{
|
||||||
public:
|
private:
|
||||||
int handle_signal(int SigNum, siginfo_t* = nullptr, ucontext_t* = nullptr) override
|
std::unordered_set<int> _handled;
|
||||||
{
|
mutable std::mutex _mutex;
|
||||||
HandleSignal(SigNum);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
virtual void HandleSignal(int /*SigNum*/) { };
|
|
||||||
};
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool handle_signal(int sig, void (*func)(int))
|
||||||
|
{
|
||||||
|
std::lock_guard lock(_mutex);
|
||||||
|
|
||||||
|
if (_handled.find(sig) != _handled.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_handled.insert(sig);
|
||||||
|
signal(sig, func);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
~SignalHandler()
|
||||||
|
{
|
||||||
|
for (auto const& sig : _handled)
|
||||||
|
signal(sig, nullptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* __SIGNAL_HANDLER_H__ */
|
#endif // _SIGNAL_HANDLER_H_
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
#include <ace/Dev_Poll_Reactor.h>
|
#include <ace/Dev_Poll_Reactor.h>
|
||||||
#include <ace/TP_Reactor.h>
|
#include <ace/TP_Reactor.h>
|
||||||
#include <ace/ACE.h>
|
#include <ace/ACE.h>
|
||||||
#include <ace/Sig_Handler.h>
|
|
||||||
#include <openssl/opensslv.h>
|
#include <openssl/opensslv.h>
|
||||||
#include <openssl/crypto.h>
|
#include <openssl/crypto.h>
|
||||||
|
|
||||||
@@ -46,22 +45,6 @@ bool stopEvent = false; // Setting it to tru
|
|||||||
|
|
||||||
LoginDatabaseWorkerPool LoginDatabase; // Accessor to the authserver database
|
LoginDatabaseWorkerPool LoginDatabase; // Accessor to the authserver database
|
||||||
|
|
||||||
/// Handle authserver's termination signals
|
|
||||||
class AuthServerSignalHandler : public acore::SignalHandler
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void HandleSignal(int sigNum) override
|
|
||||||
{
|
|
||||||
switch (sigNum)
|
|
||||||
{
|
|
||||||
case SIGINT:
|
|
||||||
case SIGTERM:
|
|
||||||
stopEvent = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Print out the usage string for this program on the console.
|
/// Print out the usage string for this program on the console.
|
||||||
void usage(const char* prog)
|
void usage(const char* prog)
|
||||||
{
|
{
|
||||||
@@ -180,12 +163,15 @@ extern int main(int argc, char** argv)
|
|||||||
sLog->outString("Authserver listening to %s:%d", bind_ip.c_str(), rmport);
|
sLog->outString("Authserver listening to %s:%d", bind_ip.c_str(), rmport);
|
||||||
|
|
||||||
// Initialize the signal handlers
|
// Initialize the signal handlers
|
||||||
AuthServerSignalHandler SignalINT, SignalTERM;
|
acore::SignalHandler signalHandler;
|
||||||
|
auto const _handler = [](int) { stopEvent = true; };
|
||||||
|
|
||||||
// Register authservers's signal handlers
|
// Register authservers's signal handlers
|
||||||
ACE_Sig_Handler Handler;
|
signalHandler.handle_signal(SIGINT, _handler);
|
||||||
Handler.register_handler(SIGINT, &SignalINT);
|
signalHandler.handle_signal(SIGTERM, _handler);
|
||||||
Handler.register_handler(SIGTERM, &SignalTERM);
|
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
|
||||||
|
signalHandler.handle_signal(SIGBREAK, _handler);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(__linux__)
|
#if defined(_WIN32) || defined(__linux__)
|
||||||
|
|
||||||
|
|||||||
@@ -43,30 +43,28 @@ extern int m_ServiceStatus;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Handle worldservers's termination signals
|
/// Handle worldservers's termination signals
|
||||||
class WorldServerSignalHandler : public acore::SignalHandler
|
void HandleSignal(int sigNum)
|
||||||
{
|
{
|
||||||
public:
|
switch (sigNum)
|
||||||
void HandleSignal(int sigNum) override
|
|
||||||
{
|
{
|
||||||
switch (sigNum)
|
case SIGINT:
|
||||||
{
|
World::StopNow(RESTART_EXIT_CODE);
|
||||||
case SIGINT:
|
break;
|
||||||
World::StopNow(RESTART_EXIT_CODE);
|
case SIGTERM:
|
||||||
break;
|
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
|
||||||
case SIGTERM:
|
case SIGBREAK:
|
||||||
#ifdef _WIN32
|
if (m_ServiceStatus != 1)
|
||||||
case SIGBREAK:
|
|
||||||
if (m_ServiceStatus != 1)
|
|
||||||
#endif
|
#endif
|
||||||
World::StopNow(SHUTDOWN_EXIT_CODE);
|
World::StopNow(SHUTDOWN_EXIT_CODE);
|
||||||
break;
|
break;
|
||||||
/*case SIGSEGV:
|
/*case SIGSEGV:
|
||||||
sLog->outString("ZOMG! SIGSEGV handled!");
|
sLog->outString("ZOMG! SIGSEGV handled!");
|
||||||
World::StopNow(SHUTDOWN_EXIT_CODE);
|
World::StopNow(SHUTDOWN_EXIT_CODE);
|
||||||
break;*/
|
break;*/
|
||||||
}
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
class FreezeDetectorRunnable : public acore::Runnable
|
class FreezeDetectorRunnable : public acore::Runnable
|
||||||
{
|
{
|
||||||
@@ -164,19 +162,14 @@ int Master::Run()
|
|||||||
sScriptMgr->OnStartup();
|
sScriptMgr->OnStartup();
|
||||||
|
|
||||||
///- Initialize the signal handlers
|
///- Initialize the signal handlers
|
||||||
WorldServerSignalHandler signalINT, signalTERM; //, signalSEGV
|
acore::SignalHandler signalHandler;
|
||||||
#ifdef _WIN32
|
|
||||||
WorldServerSignalHandler signalBREAK;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
|
|
||||||
///- Register worldserver's signal handlers
|
signalHandler.handle_signal(SIGINT, &HandleSignal);
|
||||||
ACE_Sig_Handler handle;
|
signalHandler.handle_signal(SIGTERM, &HandleSignal);
|
||||||
handle.register_handler(SIGINT, &signalINT);
|
|
||||||
handle.register_handler(SIGTERM, &signalTERM);
|
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
|
||||||
#ifdef _WIN32
|
signalHandler.handle_signal(SIGBREAK, &HandleSignal);
|
||||||
handle.register_handler(SIGBREAK, &signalBREAK);
|
|
||||||
#endif
|
#endif
|
||||||
//handle.register_handler(SIGSEGV, &signalSEGV);
|
|
||||||
|
|
||||||
///- Launch WorldRunnable thread
|
///- Launch WorldRunnable thread
|
||||||
acore::Thread worldThread(new WorldRunnable);
|
acore::Thread worldThread(new WorldRunnable);
|
||||||
|
|||||||
Reference in New Issue
Block a user