diff --git a/src/App.cpp b/src/App.cpp index 008df0a7..5f59e1b5 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -24,7 +24,7 @@ */ -#include +#include #include @@ -42,18 +42,9 @@ #include "version.h" -xmrig::App::App(Process *process) : - m_console(nullptr), - m_signals(nullptr) +xmrig::App::App(Process *process) { m_controller = new Controller(process); - if (m_controller->init() != 0) { - return; - } - - if (!m_controller->config()->isBackground()) { - m_console = new Console(this); - } } @@ -68,12 +59,26 @@ xmrig::App::~App() int xmrig::App::exec() { if (!m_controller->isReady()) { + LOG_EMERG("no valid configuration found."); + return 2; } m_signals = new Signals(this); - background(); + int rc = 0; + if (background(rc)) { + return rc; + } + + rc = m_controller->init(); + if (rc != 0) { + return rc; + } + + if (!m_controller->isBackground()) { + m_console = new Console(this); + } VirtualMemory::init(m_controller->config()->cpu().isHugePages()); @@ -87,10 +92,10 @@ int xmrig::App::exec() m_controller->start(); - const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); + rc = uv_run(uv_default_loop(), UV_RUN_DEFAULT); uv_loop_close(uv_default_loop()); - return r; + return rc; } diff --git a/src/App.h b/src/App.h index aa534aad..b46dcefa 100644 --- a/src/App.h +++ b/src/App.h @@ -29,6 +29,7 @@ #include "base/kernel/interfaces/IConsoleListener.h" #include "base/kernel/interfaces/ISignalListener.h" +#include "base/tools/Object.h" namespace xmrig { @@ -44,6 +45,8 @@ class Signals; class App : public IConsoleListener, public ISignalListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(App) + App(Process *process); ~App() override; @@ -54,12 +57,12 @@ protected: void onSignal(int signum) override; private: - void background(); + bool background(int &rc); void close(); - Console *m_console; - Controller *m_controller; - Signals *m_signals; + Console *m_console = nullptr; + Controller *m_controller = nullptr; + Signals *m_signals = nullptr; }; diff --git a/src/App_unix.cpp b/src/App_unix.cpp index 5149513c..ae2905db 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -23,33 +23,36 @@ */ -#include -#include -#include +#include +#include +#include #include #include "App.h" #include "base/io/log/Log.h" -#include "core/config/Config.h" #include "core/Controller.h" -void xmrig::App::background() +bool xmrig::App::background(int &rc) { signal(SIGPIPE, SIG_IGN); - if (!m_controller->config()->isBackground()) { - return; + if (!m_controller->isBackground()) { + return false; } int i = fork(); if (i < 0) { - exit(1); + rc = 1; + + return true; } if (i > 0) { - exit(0); + rc = 0; + + return true; } i = setsid(); @@ -62,4 +65,6 @@ void xmrig::App::background() if (i < 0) { LOG_ERR("chdir() failed (errno = %d)", errno); } + + return false; } diff --git a/src/App_win.cpp b/src/App_win.cpp index a41ed505..e803080c 100644 --- a/src/App_win.cpp +++ b/src/App_win.cpp @@ -29,13 +29,12 @@ #include "App.h" #include "core/Controller.h" -#include "core/config/Config.h" -void xmrig::App::background() +bool xmrig::App::background(int &) { - if (!m_controller->config()->isBackground()) { - return; + if (!m_controller->isBackground()) { + return false; } HWND hcon = GetConsoleWindow(); @@ -46,4 +45,6 @@ void xmrig::App::background() CloseHandle(h); FreeConsole(); } + + return false; } diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 3740655d..90c7cb57 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -23,7 +23,7 @@ */ -#include +#include #include @@ -64,15 +64,16 @@ static const char *kConfigPathV2 = "/2/config"; #endif -class xmrig::BasePrivate +namespace xmrig { + + +class BasePrivate { public: - inline BasePrivate(Process *process) : - api(nullptr), - config(nullptr), - process(process), - watcher(nullptr) - {} + XMRIG_DISABLE_COPY_MOVE_DEFAULT(BasePrivate) + + + inline BasePrivate(Process *process) : config(load(process)) {} inline ~BasePrivate() @@ -94,13 +95,33 @@ public: } - inline Config *load() + inline void replace(Config *newConfig) + { + Config *previousConfig = config; + config = newConfig; + + for (IBaseListener *listener : listeners) { + listener->onConfigChanged(config, previousConfig); + } + + delete previousConfig; + } + + + Api *api = nullptr; + Config *config = nullptr; + std::vector listeners; + Watcher *watcher = nullptr; + + +private: + inline Config *load(Process *process) { JsonChain chain; ConfigTransform transform; std::unique_ptr config; - transform.load(chain, process, transform); + ConfigTransform::load(chain, process, transform); if (read(chain, config)) { return config.release(); @@ -122,29 +143,12 @@ public: return nullptr; } - - - inline void replace(Config *newConfig) - { - Config *previousConfig = config; - config = newConfig; - - for (IBaseListener *listener : listeners) { - listener->onConfigChanged(config, previousConfig); - } - - delete previousConfig; - } - - - Api *api; - Config *config; - Process *process; - std::vector listeners; - Watcher *watcher; }; +} // namespace xmrig + + xmrig::Base::Base(Process *process) : d_ptr(new BasePrivate(process)) { @@ -165,14 +169,6 @@ bool xmrig::Base::isReady() const int xmrig::Base::init() { - d_ptr->config = d_ptr->load(); - - if (!d_ptr->config) { - LOG_EMERG("No valid configuration found. Exiting."); - - return 1; - } - # ifdef XMRIG_FEATURE_API d_ptr->api = new Api(this); d_ptr->api->addListener(this); @@ -184,7 +180,7 @@ int xmrig::Base::init() Platform::setProcessPriority(config()->cpu().priority()); # endif - if (config()->isBackground()) { + if (isBackground()) { Log::background = true; } else { @@ -240,6 +236,12 @@ xmrig::Api *xmrig::Base::api() const } +bool xmrig::Base::isBackground() const +{ + return d_ptr->config && d_ptr->config->isBackground(); +} + + bool xmrig::Base::reload(const rapidjson::Value &json) { JsonReader reader(json); @@ -247,7 +249,7 @@ bool xmrig::Base::reload(const rapidjson::Value &json) return false; } - Config *config = new Config(); + auto config = new Config(); if (!config->read(reader, d_ptr->config->fileName())) { delete config; @@ -289,7 +291,7 @@ void xmrig::Base::onFileChanged(const String &fileName) JsonChain chain; chain.addFile(fileName); - Config *config = new Config(); + auto config = new Config(); if (!config->read(chain, chain.fileName())) { LOG_ERR("reloading failed"); diff --git a/src/base/kernel/Base.h b/src/base/kernel/Base.h index 8eb68866..ef850edd 100644 --- a/src/base/kernel/Base.h +++ b/src/base/kernel/Base.h @@ -29,6 +29,7 @@ #include "base/api/interfaces/IApiListener.h" #include "base/kernel/interfaces/IConfigListener.h" #include "base/kernel/interfaces/IWatcherListener.h" +#include "base/tools/Object.h" #include "rapidjson/fwd.h" @@ -45,6 +46,8 @@ class Process; class Base : public IWatcherListener, public IApiListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Base) + Base(Process *process); ~Base() override; @@ -54,6 +57,7 @@ public: virtual void stop(); Api *api() const; + bool isBackground() const; bool reload(const rapidjson::Value &json); Config *config() const; void addListener(IBaseListener *listener); diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 9193d378..fae0d679 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.cpp @@ -23,8 +23,8 @@ */ +#include #include -#include #include "base/kernel/Process.h" @@ -55,11 +55,6 @@ xmrig::Process::Process(int argc, char **argv) : } -xmrig::Process::~Process() -{ -} - - xmrig::String xmrig::Process::location(Location location, const char *fileName) const { constexpr const size_t max = 520; diff --git a/src/base/kernel/Process.h b/src/base/kernel/Process.h index 9b29eb57..22959044 100644 --- a/src/base/kernel/Process.h +++ b/src/base/kernel/Process.h @@ -47,7 +47,6 @@ public: # endif Process(int argc, char **argv); - ~Process(); String location(Location location, const char *fileName = nullptr) const; diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index 54c9ee34..848396a7 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -23,7 +23,7 @@ */ -#include +#include #include "backend/cpu/Cpu.h" @@ -44,20 +44,10 @@ xmrig::Controller::~Controller() } -bool xmrig::Controller::isReady() const -{ - return Base::isReady() && m_network; -} - - int xmrig::Controller::init() { Cpu::init(); - - const int rc = Base::init(); - if (rc != 0) { - return rc; - } + Base::init(); m_network = new Network(this); return 0; diff --git a/src/core/Controller.h b/src/core/Controller.h index da7ba368..b2b8c9cb 100644 --- a/src/core/Controller.h +++ b/src/core/Controller.h @@ -27,6 +27,7 @@ #include "base/kernel/Base.h" +#include "base/tools/Object.h" namespace xmrig { @@ -40,10 +41,11 @@ class Network; class Controller : public Base { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Controller) + Controller(Process *process); ~Controller() override; - bool isReady() const override; int init() override; void start() override; void stop() override;