Update Signals class.

This commit is contained in:
XMRig
2022-05-17 18:05:53 +07:00
parent 75283445fd
commit d6a1c98314
2 changed files with 134 additions and 57 deletions

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,25 +14,113 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining
* it with OpenSSL (or a modified version of that library), containing parts
* covered by the terms of OpenSSL License and SSLeay License, the licensors
* of this Program grant you additional permission to convey the resulting work.
*/
#include "base/kernel/interfaces/ISignalListener.h"
#include "base/io/log/Log.h"
#include "base/io/log/Tags.h"
#include "base/io/Signals.h"
#include "base/io/log/Log.h"
#include "base/kernel/interfaces/ISignalListener.h"
#include "base/kernel/Process.h"
#include "base/tools/Handle.h"
#ifdef SIGUSR1
static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM, SIGUSR1 };
#else
static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM };
#ifdef XMRIG_FEATURE_EVENTS
# include "base/kernel/Events.h"
# include "base/kernel/events/SignalEvent.h"
#endif
xmrig::Signals::Signals(ISignalListener *listener)
: m_listener(listener)
#include <csignal>
namespace xmrig {
#ifdef SIGUSR1
constexpr static const size_t kSignalsCount = 4;
constexpr static const int signums[kSignalsCount] = { SIGHUP, SIGINT, SIGTERM, SIGUSR1 };
#else
constexpr static const size_t kSignalsCount = 3;
constexpr static const int signums[kSignalsCount] = { SIGHUP, SIGINT, SIGTERM };
#endif
#ifdef XMRIG_FEATURE_EVENTS
static const char *signame(int signum)
{
switch (signum) {
case SIGHUP:
return "SIGHUP";
case SIGINT:
return "SIGINT";
case SIGTERM:
return "SIGTERM";
default:
break;
}
return nullptr;
}
#endif
class Signals::Private : public ISignalListener
{
public:
XMRIG_DISABLE_COPY_MOVE(Private)
Private();
~Private() override;
ISignalListener *listener = nullptr;
protected:
void onSignal(int signum) override;
private:
static void onSignal(uv_signal_t *handle, int signum);
uv_signal_t *signals[kSignalsCount]{};
};
} // namespace xmrig
xmrig::Signals::Signals() :
d(std::make_shared<Private>())
{
d->listener = d.get();
}
xmrig::Signals::Signals(ISignalListener *listener) :
d(std::make_shared<Private>())
{
d->listener = listener;
}
const char *xmrig::Signals::tag()
{
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " signal ");
return tag;
}
xmrig::Signals::Private::Private()
{
# ifndef XMRIG_OS_WIN
signal(SIGPIPE, SIG_IGN);
@ -42,47 +130,44 @@ xmrig::Signals::Signals(ISignalListener *listener)
auto signal = new uv_signal_t;
signal->data = this;
m_signals[i] = signal;
signals[i] = signal;
uv_signal_init(uv_default_loop(), signal);
uv_signal_start(signal, Signals::onSignal, signums[i]);
uv_signal_start(signal, onSignal, signums[i]);
}
}
xmrig::Signals::~Signals()
xmrig::Signals::Private::~Private()
{
for (auto signal : m_signals) {
for (auto signal : signals) {
Handle::close(signal);
}
}
void xmrig::Signals::onSignal(uv_signal_t *handle, int signum)
void xmrig::Signals::Private::onSignal(int signum)
{
# ifdef XMRIG_FEATURE_EVENTS
Process::events().send<SignalEvent>(signum);
switch (signum)
{
case SIGHUP:
LOG_WARN("%s " YELLOW("SIGHUP received, exiting"), Tags::signal());
break;
case SIGTERM:
LOG_WARN("%s " YELLOW("SIGTERM received, exiting"), Tags::signal());
break;
case SIGINT:
LOG_WARN("%s " YELLOW("SIGINT received, exiting"), Tags::signal());
LOG_WARN("%s " YELLOW_BOLD("%s ") YELLOW("received, exiting"), tag(), signame(signum));
Process::exit(128 + signum);
break;
# ifdef SIGUSR1
case SIGUSR1:
LOG_V5("%s " WHITE_BOLD("SIGUSR1 received"), Tags::signal());
break;
# endif
default:
break;
}
static_cast<Signals *>(handle->data)->m_listener->onSignal(signum);
# endif
}
void xmrig::Signals::Private::onSignal(uv_signal_t *handle, int signum)
{
static_cast<Private *>(handle->data)->listener->onSignal(signum);
}

View File

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,6 +14,13 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining
* it with OpenSSL (or a modified version of that library), containing parts
* covered by the terms of OpenSSL License and SSLeay License, the licensors
* of this Program grant you additional permission to convey the resulting work.
*/
#ifndef XMRIG_SIGNALS_H
@ -23,13 +30,6 @@
#include "base/tools/Object.h"
#include <csignal>
#include <cstddef>
using uv_signal_t = struct uv_signal_s;
namespace xmrig {
@ -39,28 +39,20 @@ class ISignalListener;
class Signals
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Signals)
XMRIG_DISABLE_COPY_MOVE(Signals)
# ifdef SIGUSR1
constexpr static const size_t kSignalsCount = 4;
# else
constexpr static const size_t kSignalsCount = 3;
# endif
Signals();
explicit Signals(ISignalListener *listener);
~Signals() = default;
Signals(ISignalListener *listener);
~Signals();
static const char *tag();
private:
void close(int signum);
static void onSignal(uv_signal_t *handle, int signum);
ISignalListener *m_listener;
uv_signal_t *m_signals[kSignalsCount]{};
XMRIG_DECL_PRIVATE()
};
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_SIGNALS_H */
#endif // XMRIG_SIGNALS_H