diff --git a/src/App.cpp b/src/App.cpp index 1d1ec3e92..39ec0d62e 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -43,17 +43,13 @@ xmrig::App::App(Process *process) { - m_controller = new Controller(process); + m_controller = std::make_shared(process); } xmrig::App::~App() { Cpu::release(); - - delete m_signals; - delete m_console; - delete m_controller; } @@ -65,7 +61,7 @@ int xmrig::App::exec() return 2; } - m_signals = new Signals(this); + m_signals = std::make_shared(this); int rc = 0; if (background(rc)) { @@ -78,10 +74,10 @@ int xmrig::App::exec() } if (!m_controller->isBackground()) { - m_console = new Console(this); + m_console = std::make_shared(this); } - Summary::print(m_controller); + Summary::print(m_controller.get()); if (m_controller->config()->isDryRun()) { LOG_NOTICE("%s " WHITE_BOLD("OK"), Tags::config()); @@ -115,32 +111,20 @@ void xmrig::App::onSignal(int 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()); - break; + return close(); default: - return; + break; } - - close(); } void xmrig::App::close() { - m_signals->stop(); - - if (m_console) { - m_console->stop(); - } + m_signals.reset(); + m_console.reset(); m_controller->stop(); diff --git a/src/App.h b/src/App.h index 122e8fb63..962baead8 100644 --- a/src/App.h +++ b/src/App.h @@ -32,6 +32,9 @@ #include "base/tools/Object.h" +#include + + namespace xmrig { @@ -60,9 +63,9 @@ private: bool background(int &rc); void close(); - Console *m_console = nullptr; - Controller *m_controller = nullptr; - Signals *m_signals = nullptr; + std::shared_ptr m_console; + std::shared_ptr m_controller; + std::shared_ptr m_signals; }; diff --git a/src/App_unix.cpp b/src/App_unix.cpp index ae2905db9..b0b800792 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * 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 @@ -36,8 +36,6 @@ bool xmrig::App::background(int &rc) { - signal(SIGPIPE, SIG_IGN); - if (!m_controller->isBackground()) { return false; } diff --git a/src/base/io/Console.cpp b/src/base/io/Console.cpp index bba73035a..5af7e4a43 100644 --- a/src/base/io/Console.cpp +++ b/src/base/io/Console.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * 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 @@ -50,20 +44,9 @@ xmrig::Console::Console(IConsoleListener *listener) xmrig::Console::~Console() { - stop(); -} - - -void xmrig::Console::stop() -{ - if (!m_tty) { - return; - } - uv_tty_reset_mode(); Handle::close(m_tty); - m_tty = nullptr; } diff --git a/src/base/io/Console.h b/src/base/io/Console.h index 0a075348b..ecd07bdb7 100644 --- a/src/base/io/Console.h +++ b/src/base/io/Console.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * 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 @@ -29,7 +23,10 @@ #include "base/tools/Object.h" -#include +using uv_buf_t = struct uv_buf_t; +using uv_handle_t = struct uv_handle_s; +using uv_stream_t = struct uv_stream_s; +using uv_tty_t = struct uv_tty_s; namespace xmrig { @@ -46,8 +43,6 @@ public: Console(IConsoleListener *listener); ~Console(); - void stop(); - private: bool isSupported() const; diff --git a/src/base/io/Signals.cpp b/src/base/io/Signals.cpp index 00ec8c174..dfe4a89b3 100644 --- a/src/base/io/Signals.cpp +++ b/src/base/io/Signals.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * 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 @@ -23,23 +17,30 @@ */ -#include - - -#include "base/io/Signals.h" #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/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 }; +#endif xmrig::Signals::Signals(ISignalListener *listener) : m_listener(listener) { +# ifndef XMRIG_OS_WIN + signal(SIGPIPE, SIG_IGN); +# endif + for (size_t i = 0; i < kSignalsCount; ++i) { - uv_signal_t *signal = new uv_signal_t; - signal->data = this; + auto signal = new uv_signal_t; + signal->data = this; m_signals[i] = signal; @@ -51,24 +52,37 @@ xmrig::Signals::Signals(ISignalListener *listener) xmrig::Signals::~Signals() { - stop(); -} - - -void xmrig::Signals::stop() -{ - if (!m_signals[0]) { - return; - } - - for (size_t i = 0; i < kSignalsCount; ++i) { - Handle::close(m_signals[i]); - m_signals[i] = nullptr; + for (auto signal : m_signals) { + Handle::close(signal); } } void xmrig::Signals::onSignal(uv_signal_t *handle, int 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()); + break; + +# ifdef SIGUSR1 + case SIGUSR1: + LOG_V5("%s " WHITE_BOLD("SIGUSR1 received"), Tags::signal()); + break; +# endif + + default: + break; + } + static_cast(handle->data)->m_listener->onSignal(signum); } diff --git a/src/base/io/Signals.h b/src/base/io/Signals.h index 9b4a870a4..56be5889d 100644 --- a/src/base/io/Signals.h +++ b/src/base/io/Signals.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2020 SChernykh + * Copyright (c) 2016-2020 XMRig , * * 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 @@ -26,10 +20,14 @@ #define XMRIG_SIGNALS_H -#include +#include "base/tools/Object.h" -typedef struct uv_signal_s uv_signal_t; +#include +#include + + +using uv_signal_t = struct uv_signal_s; namespace xmrig { @@ -41,20 +39,24 @@ class ISignalListener; class Signals { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Signals) + +# ifdef SIGUSR1 + constexpr static const size_t kSignalsCount = 4; +# else constexpr static const size_t kSignalsCount = 3; +# endif Signals(ISignalListener *listener); ~Signals(); - void stop(); - private: void close(int signum); static void onSignal(uv_signal_t *handle, int signum); ISignalListener *m_listener; - uv_signal_t *m_signals[kSignalsCount]; + uv_signal_t *m_signals[kSignalsCount]{}; };