Update Entry class.

This commit is contained in:
XMRig 2022-04-23 23:08:27 +07:00
parent de97f3492e
commit 603eb261c5
No known key found for this signature in database
GPG key ID: 446A53638BE94409
10 changed files with 262 additions and 240 deletions

View file

@ -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
)

View file

@ -1,13 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 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
@ -21,9 +14,15 @@
*
* 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 <cstdlib>
#include <uv.h>
@ -62,12 +61,7 @@ int xmrig::App::exec()
m_signals = std::make_shared<Signals>(this);
int rc = 0;
if (background(rc)) {
return rc;
}
rc = m_controller->init();
int rc = m_controller->init();
if (rc != 0) {
return rc;
}

View file

@ -1,13 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 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
@ -21,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_APP_H
@ -60,7 +60,6 @@ protected:
void onSignal(int signum) override;
private:
bool background(int &rc);
void close();
std::shared_ptr<Console> m_console;

View file

@ -1,68 +0,0 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <csignal>
#include <cerrno>
#include <unistd.h>
#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;
}

View file

@ -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
)

View file

@ -16,92 +16,93 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <uv.h>
#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 <openssl/opensslv.h>
#endif
#include <iostream>
#ifdef XMRIG_FEATURE_HWLOC
# include <hwloc.h>
#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 <hwloc.h>
#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<int>(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;
add([usage](int &/*rc*/) {
if (!Process::arguments().contains("-h", "--help")) {
return false;
}
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=<PATH> alternative working directory\n";
std::cout << " -c, --config=<FILE> 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=<N> print report every N seconds\n";
std::cout << " -l, --log-file=<FILE> log all output to a file\n";
# ifdef HAVE_SYSLOG_H
std::cout << " -S, --syslog use system log for output messages\n";
# endif
return Default;
}
# ifdef XMRIG_OS_WIN
std::cout << " --title=[TITLE] set custom console window title\n";
# endif
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();
std::cout << " --user-agent=<UA> 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> N seconds (default: 30) TTL for internal DNS cache\n";
# ifdef XMRIG_FEATURE_HWLOC
case Topo:
return exportTopology(process);
std::cout << " --export-topology export hwloc topology to a XML file and exit\n";
# endif
# endif
return true;
});
# ifdef XMRIG_FEATURE_OPENCL
case Platforms:
add([](int &/*rc*/) {
if (Process::arguments().contains("--print-platforms")) {
if (OclLib::init()) {
OclPlatform::print();
}
return 0;
# endif
default:
break;
return true;
}
return 1;
return false;
});
# endif
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 false;
}
void xmrig::Entry::add(Fn &&fn)
{
m_entries.emplace_back(std::move(fn));
}

View file

@ -20,29 +20,38 @@
#define XMRIG_ENTRY_H
#include "base/tools/Object.h"
#include <functional>
#include <string>
#include <vector>
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<bool(int &rc)>;
using Usage = std::function<std::string()>;
Entry(const Usage &usage);
bool exec(int &rc) const;
void add(Fn &&fn);
private:
static bool background(int &rc);
std::vector<Fn> m_entries;
};
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_ENTRY_H */
#endif // XMRIG_ENTRY_H

View file

@ -0,0 +1,46 @@
/* XMRig
* Copyright (c) 2016-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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
* 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 <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <csignal>
#include <cerrno>
#include <unistd.h>
#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;
}

View file

@ -1,12 +1,6 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2016-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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
@ -22,21 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <winsock2.h>
#include <windows.h>
#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);

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 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,11 +14,19 @@
*
* 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 "App.h"
#include "base/kernel/Entry.h"
#include "base/kernel/Process.h"
#include "core/config/usage.h"
int main(int argc, char **argv)
@ -26,10 +34,16 @@ 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);