From eb95d0339e8af990b0f4c29c978641f8f08df0a7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 2 Nov 2020 13:56:21 +0700 Subject: [PATCH] Update Process class and use APP_ID instead of hardcoded config name for #1925 --- CHANGELOG.md | 1 + src/base/base.cmake | 3 +++ src/base/kernel/Base.cpp | 8 +++--- src/base/kernel/Process.cpp | 44 ++++++++++++++++++++------------ src/base/kernel/Process.h | 23 +++++++---------- src/base/kernel/Process_unix.cpp | 34 ++++++++++++++++++++++++ src/base/kernel/Process_win.cpp | 33 ++++++++++++++++++++++++ 7 files changed, 110 insertions(+), 36 deletions(-) create mode 100644 src/base/kernel/Process_unix.cpp create mode 100644 src/base/kernel/Process_win.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index fb2fa868..2edca701 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - **Added [online benchmark](https://xmrig.com/benchmark) mode for sharing results.** - Added new command line options: `--submit`, ` --verify=ID`, ` --seed=SEED`, `--hash=HASH`. - [#1912](https://github.com/xmrig/xmrig/pull/1912) Fixed MSR kernel module warning with new Linux kernels. +- [#1925](https://github.com/xmrig/xmrig/pull/1925) Add checking for config files in user home directory. - Added vendor to ARM CPUs name and added `"arch"` field to API. - Removed legacy CUDA plugin API. diff --git a/src/base/base.cmake b/src/base/base.cmake index 30169a7b..04506927 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -123,16 +123,19 @@ if (WIN32) set(SOURCES_OS src/base/io/json/Json_win.cpp src/base/kernel/Platform_win.cpp + src/base/kernel/Process_win.cpp ) elseif (APPLE) set(SOURCES_OS src/base/io/json/Json_unix.cpp src/base/kernel/Platform_mac.cpp + src/base/kernel/Process_unix.cpp ) else() set(SOURCES_OS src/base/io/json/Json_unix.cpp src/base/kernel/Platform_unix.cpp + src/base/kernel/Process_unix.cpp ) endif() diff --git a/src/base/kernel/Base.cpp b/src/base/kernel/Base.cpp index 309e4c4d..1e5019ca 100644 --- a/src/base/kernel/Base.cpp +++ b/src/base/kernel/Base.cpp @@ -41,6 +41,7 @@ #include "base/net/tools/NetBuffer.h" #include "core/config/Config.h" #include "core/config/ConfigTransform.h" +#include "version.h" #ifdef HAVE_SYSLOG_H @@ -132,19 +133,16 @@ private: } chain.addFile(Process::location(Process::DataLocation, "config.json")); - if (read(chain, config)) { return config.release(); } - chain.addFile(Process::location(Process::HomeLocation, ".xmrig.json")); - + chain.addFile(Process::location(Process::HomeLocation, "." APP_ID ".json")); if (read(chain, config)) { return config.release(); } - chain.addFile(Process::location(Process::HomeLocation, ".config/xmrig.json")); - + chain.addFile(Process::location(Process::HomeLocation, ".config" XMRIG_DIR_SEPARATOR APP_ID ".json")); if (read(chain, config)) { return config.release(); } diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 36dcae78..6f63b647 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.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-2020 SChernykh - * Copyright 2016-2020 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,21 @@ #include "base/kernel/Process.h" +#include "3rdparty/fmt/core.h" #include "base/tools/Chrono.h" +#include "version.h" + + +#ifdef XMRIG_OS_WIN +# ifdef _MSC_VER +# include +# define MKDIR(path) _mkdir(path.c_str()); +# else +# define MKDIR(path) mkdir((path).c_str()); +# endif +#else +# define MKDIR(path) mkdir(path.c_str(), 0700); +#endif namespace xmrig { @@ -73,7 +81,7 @@ static std::string getPath(Process::Location location) } const auto path = std::string(pathBuf, size); - const auto pos = path.rfind(Process::kDirSeparator); + const auto pos = path.rfind(*XMRIG_DIR_SEPARATOR); if (pos != std::string::npos) { return path.substr(0, pos); @@ -116,15 +124,17 @@ xmrig::Process::Process(int argc, char **argv) : srand(static_cast(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast(this))); setDataDir(m_arguments.value("--data-dir", "-d")); -} +# ifdef XMRIG_SHARED_DATADIR + if (dataDir.empty()) { + dataDir = fmt::format("{}" XMRIG_DIR_SEPARATOR ".xmrig" XMRIG_DIR_SEPARATOR, location(HomeLocation)); + MKDIR(dataDir); -int xmrig::Process::pid() -{ -# if UV_VERSION_HEX >= 0x011200 - return uv_os_getpid(); -# else - return 0; + dataDir += APP_KIND; + MKDIR(dataDir); + + uv_chdir(dataDir.c_str()); + } # endif } @@ -154,5 +164,5 @@ xmrig::String xmrig::Process::location(Location location, const char *fileName) return path.c_str(); } - return (path + kDirSeparator + fileName).c_str(); + return fmt::format("{}" XMRIG_DIR_SEPARATOR "{}", path, fileName).c_str(); } diff --git a/src/base/kernel/Process.h b/src/base/kernel/Process.h index a97a3dcc..754e7a56 100644 --- a/src/base/kernel/Process.h +++ b/src/base/kernel/Process.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-2020 SChernykh - * Copyright 2016-2020 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,6 +23,13 @@ #include "base/tools/Arguments.h" +#ifdef WIN32 +# define XMRIG_DIR_SEPARATOR "\\" +#else +# define XMRIG_DIR_SEPARATOR "/" +#endif + + namespace xmrig { @@ -43,12 +44,6 @@ public: TempLocation }; -# ifdef WIN32 - constexpr const static char kDirSeparator = '\\'; -# else - constexpr const static char kDirSeparator = '/'; -# endif - Process(int argc, char **argv); static int pid(); diff --git a/src/base/kernel/Process_unix.cpp b/src/base/kernel/Process_unix.cpp new file mode 100644 index 00000000..3f1fdeb2 --- /dev/null +++ b/src/base/kernel/Process_unix.cpp @@ -0,0 +1,34 @@ +/* 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 + * 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 "base/kernel/Process.h" + + +int xmrig::Process::pid() +{ +# if UV_VERSION_HEX >= 0x011200 + return uv_os_getpid(); +# else + return getpid(); +# endif +} diff --git a/src/base/kernel/Process_win.cpp b/src/base/kernel/Process_win.cpp new file mode 100644 index 00000000..a8d5d2d4 --- /dev/null +++ b/src/base/kernel/Process_win.cpp @@ -0,0 +1,33 @@ +/* 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 + * 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 "base/kernel/Process.h" + + +int xmrig::Process::pid() +{ +# if UV_VERSION_HEX >= 0x011200 + return uv_os_getpid(); +# else + return GetCurrentProcessId(); +# endif +}