Skip to content

Commit

Permalink
[Startup][Refactor][Backport]
Browse files Browse the repository at this point in the history
* OS memory allocation fail handler.
* OS signal handler registration method created to remove code duplication.
* AppInitBasicSetup() method created, organizing better the setup step of the wallet initialization.
  • Loading branch information
furszy committed Sep 29, 2019
1 parent 968000b commit ad87023
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
64 changes: 45 additions & 19 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,25 @@ void Shutdown()
*/
void HandleSIGTERM(int)
{
fRequestShutdown = true;
StartShutdown();
}

void HandleSIGHUP(int)
{
fReopenDebugLog = true;
}

#ifndef WIN32
static void registerSignalHandler(int signal, void(*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(signal, &sa, nullptr);
}
#endif

bool static InitError(const std::string& str)
{
uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR);
Expand Down Expand Up @@ -748,10 +759,20 @@ bool AppInitServers()
return true;
}

/** Initialize pivx.
* @pre Parameters should be parsed and config file should be read.
*/
bool AppInit2()
[[noreturn]] static void new_handler_terminate()
{
// Rather than throwing std::bad-alloc if allocation fails, terminate
// immediately to (try to) avoid chain corruption.
// Since LogPrintf may itself allocate memory, set the handler directly
// to terminate first.
std::set_new_handler(std::terminate);
LogPrintf("Error: Out of memory. Terminating.\n");

// The log was successful, terminate now.
std::terminate();
};

bool AppInitBasicSetup()
{
// ********************************************************* Step 1: setup
#ifdef _MSC_VER
Expand All @@ -764,7 +785,7 @@ bool AppInit2()
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
#endif
#ifdef WIN32
// Enable Data Execution Prevention (DEP)
// Enable Data Execution Prevention (DEP)
// Minimum supported OS versions: WinXP SP3, WinVista >= SP1, Win Server 2008
// A failure is non-critical and needs no further attention!
#ifndef PROCESS_DEP_ENABLE
Expand All @@ -790,26 +811,31 @@ bool AppInit2()
umask(077);
}


// Clean shutdown on SIGTERM
struct sigaction sa;
sa.sa_handler = HandleSIGTERM;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
// Clean shutdown on SIGTERMx
registerSignalHandler(SIGTERM, HandleSIGTERM);
registerSignalHandler(SIGINT, HandleSIGTERM);

// Reopen debug.log on SIGHUP
struct sigaction sa_hup;
sa_hup.sa_handler = HandleSIGHUP;
sigemptyset(&sa_hup.sa_mask);
sa_hup.sa_flags = 0;
sigaction(SIGHUP, &sa_hup, NULL);
registerSignalHandler(SIGHUP, HandleSIGHUP);

// Ignore SIGPIPE, otherwise it will bring the daemon down if the client closes unexpectedly
signal(SIGPIPE, SIG_IGN);
#endif

std::set_new_handler(new_handler_terminate);

return true;
}

/** Initialize pivx.
* @pre Parameters should be parsed and config file should be read.
*/
bool AppInit2()
{
// ********************************************************* Step 1: setup
if (!AppInitBasicSetup())
return false;

// ********************************************************* Step 2: parameter interactions
// Set this early so that parameter interactions go to console
fPrintToConsole = GetBoolArg("-printtoconsole", false);
Expand Down
6 changes: 6 additions & 0 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void Shutdown();
void PrepareShutdown();
bool AppInit2();

/** Initialize PIVX core: Basic context setup.
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read.
*/
bool AppInitBasicSetup();

/** The help message mode determines what help message to show */
enum HelpMessageMode {
HMM_BITCOIND,
Expand Down

0 comments on commit ad87023

Please sign in to comment.