2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2014-08-13 10:51:37 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "SignalHandler.h"
|
|
|
|
|
|
|
|
#include <mutex>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <iostream>
|
2014-08-13 10:51:37 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <signal.h>
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <cstring>
|
2015-05-27 12:08:46 +00:00
|
|
|
#endif
|
2014-08-13 10:51:37 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::function<void(void)> m_handler;
|
|
|
|
|
|
|
|
void handleSignal() {
|
|
|
|
static std::mutex m_mutex;
|
2015-07-09 14:52:47 +00:00
|
|
|
std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
|
|
|
|
if (!lock.owns_lock()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
m_handler();
|
|
|
|
}
|
2014-08-13 10:51:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
2015-05-27 12:08:46 +00:00
|
|
|
BOOL WINAPI winHandler(DWORD type) {
|
|
|
|
if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) {
|
|
|
|
handleSignal();
|
2014-08-13 10:51:37 +00:00
|
|
|
return TRUE;
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "Got control signal " << type << ". Exiting without saving...";
|
|
|
|
return FALSE;
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2014-08-13 10:51:37 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void posixHandler(int /*type*/) {
|
|
|
|
handleSignal();
|
|
|
|
}
|
2014-08-13 10:51:37 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
namespace Tools {
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
bool SignalHandler::install(std::function<void(void)> t)
|
|
|
|
{
|
|
|
|
#if defined(WIN32)
|
|
|
|
bool r = TRUE == ::SetConsoleCtrlHandler(&winHandler, TRUE);
|
|
|
|
if (r) {
|
|
|
|
m_handler = t;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
#else
|
2015-07-30 15:22:07 +00:00
|
|
|
struct sigaction newMask;
|
|
|
|
std::memset(&newMask, 0, sizeof(struct sigaction));
|
|
|
|
newMask.sa_handler = posixHandler;
|
|
|
|
if (sigaction(SIGINT, &newMask, nullptr) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sigaction(SIGTERM, &newMask, nullptr) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memset(&newMask, 0, sizeof(struct sigaction));
|
|
|
|
newMask.sa_handler = SIG_IGN;
|
|
|
|
if (sigaction(SIGPIPE, &newMask, nullptr) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
m_handler = t;
|
|
|
|
return true;
|
|
|
|
#endif
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
|
2014-08-13 10:51:37 +00:00
|
|
|
}
|