diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f7d034a..88bbd2dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,14 +157,12 @@ endif() if (XMRIG_OS_WIN) list(APPEND SOURCES_OS res/app.rc - src/App_win.cpp src/crypto/common/VirtualMemory_win.cpp ) set(EXTRA_LIBS ws2_32 psapi iphlpapi userenv) elseif (XMRIG_OS_APPLE) list(APPEND SOURCES_OS - src/App_unix.cpp src/crypto/common/VirtualMemory_unix.cpp ) @@ -173,7 +171,6 @@ elseif (XMRIG_OS_APPLE) set(EXTRA_LIBS ${IOKIT_LIBRARY} ${CORESERVICES_LIBRARY}) else() list(APPEND SOURCES_OS - src/App_unix.cpp src/crypto/common/VirtualMemory_unix.cpp ) diff --git a/src/App.cpp b/src/App.cpp index f21d0276..2fe23e4e 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -1,13 +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 Lee Clagett - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 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 @@ -21,9 +14,15 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * 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 #include @@ -62,12 +61,7 @@ int xmrig::App::exec() m_signals = std::make_shared(this); - int rc = 0; - if (background(rc)) { - return rc; - } - - rc = m_controller->init(); + int rc = m_controller->init(); if (rc != 0) { return rc; } diff --git a/src/App.h b/src/App.h index 962baead..81598922 100644 --- a/src/App.h +++ b/src/App.h @@ -1,13 +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 Lee Clagett - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 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 @@ -21,6 +14,13 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * 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_APP_H @@ -60,7 +60,6 @@ protected: void onSignal(int signum) override; private: - bool background(int &rc); void close(); std::shared_ptr m_console; diff --git a/src/App_unix.cpp b/src/App_unix.cpp deleted file mode 100644 index b0b80079..00000000 --- a/src/App_unix.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* 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-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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include -#include -#include - - -#include "App.h" -#include "base/io/log/Log.h" -#include "core/Controller.h" - - -bool xmrig::App::background(int &rc) -{ - if (!m_controller->isBackground()) { - return false; - } - - int i = fork(); - if (i < 0) { - rc = 1; - - return true; - } - - if (i > 0) { - rc = 0; - - return true; - } - - i = setsid(); - - if (i < 0) { - LOG_ERR("setsid() failed (errno = %d)", errno); - } - - i = chdir("/"); - if (i < 0) { - LOG_ERR("chdir() failed (errno = %d)", errno); - } - - return false; -} diff --git a/src/base/base.cmake b/src/base/base.cmake index 29d03d01..7a64c587 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -152,18 +152,21 @@ set(SOURCES_BASE if (WIN32) set(SOURCES_OS src/base/io/json/Json_win.cpp + src/base/kernel/Entry_win.cpp src/base/kernel/OS_win.cpp src/base/kernel/Process_win.cpp ) elseif (APPLE) set(SOURCES_OS src/base/io/json/Json_unix.cpp + src/base/kernel/Entry_unix.cpp src/base/kernel/OS_mac.cpp src/base/kernel/Process_unix.cpp ) else() set(SOURCES_OS src/base/io/json/Json_unix.cpp + src/base/kernel/Entry_unix.cpp src/base/kernel/OS_unix.cpp src/base/kernel/Process_unix.cpp ) diff --git a/src/base/kernel/Entry.cpp b/src/base/kernel/Entry.cpp index d435e28c..603e7b38 100644 --- a/src/base/kernel/Entry.cpp +++ b/src/base/kernel/Entry.cpp @@ -16,92 +16,93 @@ * along with this program. If not, see . */ -#include -#include +#include "base/kernel/Entry.h" +#include "base/io/log/Log.h" +#include "base/kernel/OS.h" +#include "base/kernel/Process.h" +#include "base/kernel/Versions.h" +#include "base/tools/Arguments.h" +#include "version.h" -#ifdef XMRIG_FEATURE_TLS -# include -#endif +#include -#ifdef XMRIG_FEATURE_HWLOC -# include -#endif #ifdef XMRIG_FEATURE_OPENCL # include "backend/opencl/wrappers/OclLib.h" # include "backend/opencl/wrappers/OclPlatform.h" #endif -#include "base/kernel/Entry.h" -#include "base/kernel/Process.h" -#include "base/tools/Arguments.h" -#include "core/config/usage.h" -#include "version.h" +#ifdef XMRIG_FEATURE_HWLOC +# include +#endif namespace xmrig { -static int showVersion() +static bool showVersion(int &/*rc*/) { - printf(APP_NAME " " APP_VERSION "\n built on " __DATE__ - -# if defined(__clang__) - " with clang " __clang_version__); -# elif defined(__GNUC__) - " with GCC"); - printf(" %d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); -# elif defined(_MSC_VER) - " with MSVC"); - printf(" %d", MSVC_VERSION); -# else - ); -# endif - - printf("\n features:" -# if defined(__i386__) || defined(_M_IX86) - " 32-bit" -# elif defined(__x86_64__) || defined(_M_AMD64) - " 64-bit" -# endif - -# if defined(__AES__) || defined(_MSC_VER) - " AES" -# endif - "\n"); - - printf("\nlibuv/%s\n", uv_version_string()); - -# if defined(XMRIG_FEATURE_TLS) - { -# if defined(LIBRESSL_VERSION_TEXT) - printf("LibreSSL/%s\n", LIBRESSL_VERSION_TEXT + 9); -# elif defined(OPENSSL_VERSION_TEXT) - constexpr const char *v = &OPENSSL_VERSION_TEXT[8]; - printf("OpenSSL/%.*s\n", static_cast(strchr(v, ' ') - v), v); -# endif + if (!Process::arguments().contains("-V", "--version")) { + return false; } + + std::cout << APP_NAME " v" << Process::version() << std::endl + << " built on " __DATE__ " with " << Versions::kCompiler << "/" << Process::versions().get(Versions::kCompiler) + << " (" << OS::arch << ")" << std::endl; + +# ifdef XMRIG_LEGACY + std::cout << std::endl << "uv/" << Process::versions().get(Versions::kUv) << std::endl; + +# ifdef XMRIG_FEATURE_TLS + std::cout << Versions::kTls << "/" << Process::versions().get(Versions::kTls) << std::endl; # endif -# if defined(XMRIG_FEATURE_HWLOC) -# if defined(HWLOC_VERSION) - printf("hwloc/%s\n", HWLOC_VERSION); -# elif HWLOC_API_VERSION >= 0x20000 - printf("hwloc/2\n"); -# else - printf("hwloc/1\n"); +# ifdef XMRIG_FEATURE_HWLOC + std::cout << "hwloc/" << Process::versions().get(Versions::kHwloc) << std::endl; # endif # endif - return 0; + return true; +} + + +static bool showVersions(int &/*rc*/) +{ + if (Process::arguments().contains("--versions")) { + for (const auto &kv : Process::versions().get()) { + std::cout << kv.first << "/" << kv.second << std::endl;; + } + + return true; + } + + return false; +} + + +static bool userAgent(int &/*rc*/) +{ + Process::setUserAgent(Process::arguments().value("--user-agent")); + + if (Process::arguments().contains("--print-user-agent")) { + std::cout << Process::userAgent() << std::endl; + + return true; + } + + return false; } #ifdef XMRIG_FEATURE_HWLOC -static int exportTopology(const Process &) +static bool exportTopology(int &rc) { - const String path = Process::locate(Process::ExeLocation, "topology.xml"); + if (!Process::arguments().contains("--export-topology")) { + return false; + } + + const auto path = Process::locate(Process::DataLocation, "topology.xml"); hwloc_topology_t topology = nullptr; hwloc_topology_init(&topology); @@ -112,15 +113,16 @@ static int exportTopology(const Process &) # else if (hwloc_topology_export_xml(topology, path) == -1) { # endif - printf("failed to export hwloc topology.\n"); + rc = 1; + std::cout << "failed to export hwloc topology" << std::endl; } else { - printf("hwloc topology successfully exported to \"%s\"\n", path.data()); + std::cout << "hwloc topology successfully exported to \"" << path << '"' << std::endl; } hwloc_topology_destroy(topology); - return 0; + return true; } #endif @@ -128,59 +130,97 @@ static int exportTopology(const Process &) } // namespace xmrig -xmrig::Entry::Id xmrig::Entry::get(const Process &process) +xmrig::Entry::Entry(const Usage &usage) { - const Arguments &args = process.arguments(); - if (args.contains("-h", "--help")) { - return Usage; - } - - if (args.contains("-V", "--version", "--versions")) { - return Version; - } + add(showVersion); + add(showVersions); + add(userAgent); # ifdef XMRIG_FEATURE_HWLOC - if (args.contains("--export-topology")) { - return Topo; - } + add(exportTopology); # endif -# ifdef XMRIG_FEATURE_OPENCL - if (args.contains("--print-platforms")) { - return Platforms; - } -# endif - - return Default; -} - - -int xmrig::Entry::exec(const Process &process, Id id) -{ - switch (id) { - case Usage: - printf("%s\n", usage().c_str()); - return 0; - - case Version: - return showVersion(); - -# ifdef XMRIG_FEATURE_HWLOC - case Topo: - return exportTopology(process); -# endif - -# ifdef XMRIG_FEATURE_OPENCL - case Platforms: - if (OclLib::init()) { - OclPlatform::print(); + add([usage](int &/*rc*/) { + if (!Process::arguments().contains("-h", "--help")) { + return false; } - return 0; + + std::cout << "Usage: " APP_ID " [OPTIONS]\n"; + std::cout << usage(); + +# ifndef XMRIG_LEGACY + std::cout << "\nBase:\n"; + std::cout << " -h, --help print this help and exit\n"; + std::cout << " -V, --version print " APP_ID " version and exit\n"; + std::cout << " --versions print versions and exit\n"; + std::cout << " -d, --data-dir= alternative working directory\n"; + std::cout << " -c, --config= load a JSON-format configuration file\n"; + std::cout << " -B, --background run " APP_ID " in the background\n"; + std::cout << " --no-color disable colored output\n"; + std::cout << " --verbose=[LEVEL] verbose level (0-5)\n"; + std::cout << " --print-time= print report every N seconds\n"; + std::cout << " -l, --log-file= log all output to a file\n"; + +# ifdef HAVE_SYSLOG_H + std::cout << " -S, --syslog use system log for output messages\n"; +# endif + +# ifdef XMRIG_OS_WIN + std::cout << " --title=[TITLE] set custom console window title\n"; +# endif + + std::cout << " --user-agent= set custom user agent string\n"; + std::cout << " --print-user-agent print current user agent and exit\n"; + std::cout << " --dns-ipv6 prefer IPv6 records from DNS responses\n"; + std::cout << " --dns-ttl= N seconds (default: 30) TTL for internal DNS cache\n"; + +# ifdef XMRIG_FEATURE_HWLOC + std::cout << " --export-topology export hwloc topology to a XML file and exit\n"; +# endif +# endif + + return true; + }); + +# ifdef XMRIG_FEATURE_OPENCL + add([](int &/*rc*/) { + if (Process::arguments().contains("--print-platforms")) { + if (OclLib::init()) { + OclPlatform::print(); + } + + return true; + } + + return false; + }); # endif - default: - break; + add([](int &rc) { + if (Process::arguments().contains("-B", "--background")) { + Log::setBackground(true); + + return background(rc); + } + + return false; + }); +} + + +bool xmrig::Entry::exec(int &rc) const +{ + for (const auto &fn : m_entries) { + if (fn(rc)) { + return true; + } } - return 1; + return false; +} + + +void xmrig::Entry::add(Fn &&fn) +{ + m_entries.emplace_back(std::move(fn)); } diff --git a/src/base/kernel/Entry.h b/src/base/kernel/Entry.h index 63b86cf5..d0263244 100644 --- a/src/base/kernel/Entry.h +++ b/src/base/kernel/Entry.h @@ -20,29 +20,38 @@ #define XMRIG_ENTRY_H +#include "base/tools/Object.h" + + +#include +#include +#include + + namespace xmrig { -class Process; - - class Entry { public: - enum Id { - Default, - Usage, - Version, - Topo, - Platforms - }; + XMRIG_DISABLE_COPY_MOVE(Entry) - static Id get(const Process &process); - static int exec(const Process &process, Id id); + using Fn = std::function; + using Usage = std::function; + + Entry(const Usage &usage); + + bool exec(int &rc) const; + void add(Fn &&fn); + +private: + static bool background(int &rc); + + std::vector m_entries; }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_ENTRY_H */ +#endif // XMRIG_ENTRY_H diff --git a/src/base/kernel/Entry_unix.cpp b/src/base/kernel/Entry_unix.cpp new file mode 100644 index 00000000..99691941 --- /dev/null +++ b/src/base/kernel/Entry_unix.cpp @@ -0,0 +1,46 @@ +/* XMRig + * Copyright (c) 2016-2021 SChernykh + * Copyright (c) 2016-2021 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + + +#include "base/kernel/Entry.h" + + +bool xmrig::Entry::background(int &rc) +{ + const int i = fork(); + if (i < 0) { + rc = 1; + + return true; + } + + if (i > 0) { + return true; + } + + if (setsid() < 0) { + fprintf(stderr, "setsid() failed (errno = %d)\n", errno); + } + + return false; +} diff --git a/src/App_win.cpp b/src/base/kernel/Entry_win.cpp similarity index 57% rename from src/App_win.cpp rename to src/base/kernel/Entry_win.cpp index e803080c..b8632dcd 100644 --- a/src/App_win.cpp +++ b/src/base/kernel/Entry_win.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) 2016-2021 SChernykh + * Copyright (c) 2016-2021 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 @@ -22,21 +16,15 @@ * along with this program. If not, see . */ - #include #include -#include "App.h" -#include "core/Controller.h" +#include "base/kernel/Entry.h" -bool xmrig::App::background(int &) +bool xmrig::Entry::background(int &) { - if (!m_controller->isBackground()) { - return false; - } - HWND hcon = GetConsoleWindow(); if (hcon) { ShowWindow(hcon, SW_HIDE); diff --git a/src/xmrig.cpp b/src/xmrig.cpp index 635e071e..ff2fcd45 100644 --- a/src/xmrig.cpp +++ b/src/xmrig.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 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 @@ -14,11 +14,19 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * 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 "App.h" #include "base/kernel/Entry.h" #include "base/kernel/Process.h" +#include "core/config/usage.h" int main(int argc, char **argv) @@ -26,11 +34,17 @@ int main(int argc, char **argv) using namespace xmrig; Process process(argc, argv); - const Entry::Id entry = Entry::get(process); - if (entry) { - return Entry::exec(process, entry); + + { + int rc = 0; + Entry entry(usage); + + if (entry.exec(rc)) { + return rc; + } } + App app(&process); return app.exec();