Update base.
This commit is contained in:
parent
b38046db46
commit
46e49cde0b
40 changed files with 162 additions and 244 deletions
153
src/base/io/Env.cpp
Normal file
153
src/base/io/Env.cpp
Normal file
|
@ -0,0 +1,153 @@
|
|||
/* 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 "base/io/Env.h"
|
||||
#include "base/kernel/Process.h"
|
||||
#include "version.h"
|
||||
|
||||
|
||||
#include <regex>
|
||||
#include <uv.h>
|
||||
#include <map>
|
||||
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef UV_MAXHOSTNAMESIZE
|
||||
# ifdef MAXHOSTNAMELEN
|
||||
# define UV_MAXHOSTNAMESIZE (MAXHOSTNAMELEN + 1)
|
||||
# else
|
||||
# define UV_MAXHOSTNAMESIZE 256
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
#ifdef XMRIG_FEATURE_ENV
|
||||
static std::map<String, String> variables;
|
||||
|
||||
|
||||
static void createVariables()
|
||||
{
|
||||
variables.insert({ "XMRIG_VERSION", APP_VERSION });
|
||||
variables.insert({ "XMRIG_KIND", APP_KIND });
|
||||
variables.insert({ "XMRIG_HOSTNAME", Env::hostname() });
|
||||
variables.insert({ "XMRIG_EXE", Process::exepath() });
|
||||
variables.insert({ "XMRIG_EXE_DIR", Process::location(Process::ExeLocation) });
|
||||
variables.insert({ "XMRIG_CWD", Process::location(Process::CwdLocation) });
|
||||
variables.insert({ "XMRIG_HOME_DIR", Process::location(Process::HomeLocation) });
|
||||
variables.insert({ "XMRIG_TEMP_DIR", Process::location(Process::TempLocation) });
|
||||
variables.insert({ "XMRIG_DATA_DIR", Process::location(Process::DataLocation) });
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
xmrig::String xmrig::Env::expand(const char *in)
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_ENV
|
||||
if (in == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string text(in);
|
||||
if (text.size() < 4) {
|
||||
return text.c_str();
|
||||
}
|
||||
|
||||
static const std::regex env_re{R"--(\$\{([^}]+)\})--"};
|
||||
|
||||
std::map<std::string, String> vars;
|
||||
|
||||
for (std::sregex_iterator i = std::sregex_iterator(text.begin(), text.end(), env_re); i != std::sregex_iterator(); ++i) {
|
||||
std::smatch m = *i;
|
||||
const auto var = m.str();
|
||||
|
||||
if (vars.count(var)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vars.insert({ var, get(m[1].str().c_str()) });
|
||||
}
|
||||
|
||||
for (const auto &kv : vars) {
|
||||
if (kv.second.isNull()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = text.find(kv.first, pos)) != std::string::npos) {
|
||||
text.replace(pos, kv.first.size(), kv.second);
|
||||
pos += kv.second.size();
|
||||
}
|
||||
}
|
||||
|
||||
return text.c_str();
|
||||
# else
|
||||
return in;
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
xmrig::String xmrig::Env::get(const String &name)
|
||||
{
|
||||
# ifdef XMRIG_FEATURE_ENV
|
||||
if (variables.empty()) {
|
||||
createVariables();
|
||||
}
|
||||
|
||||
if (variables.count(name)) {
|
||||
return variables.at(name);
|
||||
}
|
||||
# endif
|
||||
|
||||
return static_cast<const char *>(getenv(name));
|
||||
}
|
||||
|
||||
|
||||
xmrig::String xmrig::Env::hostname()
|
||||
{
|
||||
char buf[UV_MAXHOSTNAMESIZE]{};
|
||||
size_t size = sizeof(buf);
|
||||
|
||||
# if UV_VERSION_HEX >= 0x010c00
|
||||
if (uv_os_gethostname(buf, &size) == 0) {
|
||||
return static_cast<const char *>(buf);
|
||||
}
|
||||
# else
|
||||
if (gethostname(buf, size) == 0) {
|
||||
return static_cast<const char *>(buf);
|
||||
}
|
||||
# endif
|
||||
|
||||
return {};
|
||||
}
|
47
src/base/io/Env.h
Normal file
47
src/base/io/Env.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_ENV_H
|
||||
#define XMRIG_ENV_H
|
||||
|
||||
|
||||
#include "base/tools/String.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class Env
|
||||
{
|
||||
public:
|
||||
static String expand(const char *in);
|
||||
static String get(const String &name);
|
||||
static String hostname();
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_ENV_H */
|
74
src/base/io/Signals.cpp
Normal file
74
src/base/io/Signals.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/* 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>
|
||||
*
|
||||
* 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 <uv.h>
|
||||
|
||||
|
||||
#include "base/io/Signals.h"
|
||||
#include "base/kernel/interfaces/ISignalListener.h"
|
||||
#include "base/tools/Handle.h"
|
||||
|
||||
|
||||
static const int signums[xmrig::Signals::kSignalsCount] = { SIGHUP, SIGINT, SIGTERM };
|
||||
|
||||
|
||||
xmrig::Signals::Signals(ISignalListener *listener)
|
||||
: m_listener(listener)
|
||||
{
|
||||
for (size_t i = 0; i < kSignalsCount; ++i) {
|
||||
uv_signal_t *signal = new uv_signal_t;
|
||||
signal->data = this;
|
||||
|
||||
m_signals[i] = signal;
|
||||
|
||||
uv_signal_init(uv_default_loop(), signal);
|
||||
uv_signal_start(signal, Signals::onSignal, signums[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Signals::onSignal(uv_signal_t *handle, int signum)
|
||||
{
|
||||
static_cast<Signals *>(handle->data)->m_listener->onSignal(signum);
|
||||
}
|
64
src/base/io/Signals.h
Normal file
64
src/base/io/Signals.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/* 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>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_SIGNALS_H
|
||||
#define XMRIG_SIGNALS_H
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
typedef struct uv_signal_s uv_signal_t;
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class ISignalListener;
|
||||
|
||||
|
||||
class Signals
|
||||
{
|
||||
public:
|
||||
constexpr static const size_t kSignalsCount = 3;
|
||||
|
||||
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];
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_SIGNALS_H */
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
|
||||
#include "base/io/log/FileLogWriter.h"
|
||||
#include "base/kernel/Env.h"
|
||||
#include "base/io/Env.h"
|
||||
|
||||
|
||||
#include <cassert>
|
||||
|
|
|
@ -141,7 +141,9 @@ private:
|
|||
#define LOG_WARN(x, ...) xmrig::Log::print(xmrig::Log::WARNING, x, ##__VA_ARGS__)
|
||||
#define LOG_NOTICE(x, ...) xmrig::Log::print(xmrig::Log::NOTICE, x, ##__VA_ARGS__)
|
||||
#define LOG_INFO(x, ...) xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__)
|
||||
#define LOG_VERBOSE(x, ...) if (xmrig::Log::isVerbose()) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); }
|
||||
#define LOG_VERBOSE(x, ...) if (xmrig::Log::verbose() > 0) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); }
|
||||
#define LOG_V(x, ...) if (xmrig::Log::verbose() > 0) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); }
|
||||
#define LOG_VV(x, ...) if (xmrig::Log::verbose() > 1) { xmrig::Log::print(xmrig::Log::INFO, x, ##__VA_ARGS__); }
|
||||
|
||||
#ifdef APP_DEBUG
|
||||
# define LOG_DEBUG(x, ...) xmrig::Log::print(xmrig::Log::DEBUG, x, ##__VA_ARGS__)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue