Changed directory structure.

This commit is contained in:
XMRig 2018-04-13 06:38:18 +07:00
parent 01c8245846
commit f197f6b1eb
13 changed files with 19 additions and 20 deletions

View file

@ -0,0 +1,332 @@
/* 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 2016-2018 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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include "common/config/CommonConfig.h"
#include "donate.h"
#include "log/Log.h"
#include "net/Pool.h"
#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/prettywriter.h"
#include "xmrig.h"
xmrig::CommonConfig::CommonConfig() :
m_algorithm(CRYPTONIGHT),
m_adjusted(false),
m_apiIPv6(false),
m_apiRestricted(true),
m_background(false),
m_colors(true),
m_syslog(false),
# ifdef XMRIG_PROXY_PROJECT
m_watch(true),
# else
m_watch(false), // TODO: enable config file watch by default when this feature propertly handled and tested.
# endif
m_apiPort(0),
m_donateLevel(kDefaultDonateLevel),
m_printTime(60),
m_retries(5),
m_retryPause(5)
{
m_pools.push_back(Pool());
# ifdef XMRIG_PROXY_PROJECT
m_retries = 2;
m_retryPause = 1;
# endif
}
xmrig::CommonConfig::~CommonConfig()
{
}
bool xmrig::CommonConfig::adjust()
{
if (m_adjusted) {
return false;
}
m_adjusted = true;
for (Pool &pool : m_pools) {
pool.adjust(algorithm());
}
return true;
}
bool xmrig::CommonConfig::isValid() const
{
return m_pools[0].isValid() && m_algorithm != INVALID_ALGO;
}
bool xmrig::CommonConfig::parseBoolean(int key, bool enable)
{
switch (key) {
case BackgroundKey: /* --background */
m_background = enable;
break;
case SyslogKey: /* --syslog */
m_syslog = enable;
break;
case KeepAliveKey: /* --keepalive */
m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0);
break;
# ifndef XMRIG_PROXY_PROJECT
case NicehashKey: /* --nicehash */
m_pools.back().setNicehash(enable);
break;
# endif
case ColorKey: /* --no-color */
m_colors = enable;
break;
case WatchKey: /* watch */
m_watch = enable;
break;
case ApiIPv6Key: /* ipv6 */
m_apiIPv6 = enable;
case ApiRestrictedKey: /* restricted */
m_apiRestricted = enable;
default:
break;
}
return true;
}
bool xmrig::CommonConfig::parseString(int key, const char *arg)
{
switch (key) {
case AlgorithmKey: /* --algo */
setAlgo(arg);
break;
case UserpassKey: /* --userpass */
if (!m_pools.back().setUserpass(arg)) {
return false;
}
break;
case UrlKey: /* --url */
if (m_pools.size() > 1 || m_pools[0].isValid()) {
Pool pool(arg);
if (pool.isValid()) {
m_pools.push_back(std::move(pool));
}
}
else {
m_pools[0].parse(arg);
}
if (!m_pools.back().isValid()) {
return false;
}
break;
case UserKey: /* --user */
m_pools.back().setUser(arg);
break;
case PasswordKey: /* --pass */
m_pools.back().setPassword(arg);
break;
case LogFileKey: /* --log-file */
m_logFile = arg;
break;
case ApiAccessTokenKey: /* --api-access-token */
m_apiToken = arg;
break;
case ApiWorkerIdKey: /* --api-worker-id */
m_apiWorkerId = arg;
break;
case UserAgentKey: /* --user-agent */
m_userAgent = arg;
break;
case RetriesKey: /* --retries */
case RetryPauseKey: /* --retry-pause */
case VariantKey: /* --variant */
case ApiPort: /* --api-port */
case PrintTimeKey: /* --cpu-priority */
return parseUint64(key, strtol(arg, nullptr, 10));
case BackgroundKey: /* --background */
case SyslogKey: /* --syslog */
case KeepAliveKey: /* --keepalive */
case NicehashKey: /* --nicehash */
return parseBoolean(key, true);
case ColorKey: /* --no-color */
case WatchKey: /* --no-watch */
case ApiRestrictedKey: /* --api-no-restricted */
case ApiIPv6Key: /* --api-no-ipv6 */
return parseBoolean(key, false);
case DonateLevelKey: /* --donate-level */
# ifdef XMRIG_PROXY_PROJECT
if (strncmp(arg, "minemonero.pro", 14) == 0) {
m_donateLevel = 0;
return true;
}
# endif
return parseUint64(key, strtol(arg, nullptr, 10));
default:
break;
}
return true;
}
bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg)
{
return parseInt(key, static_cast<int>(arg));
}
bool xmrig::CommonConfig::save()
{
if (m_fileName.isNull()) {
return false;
}
uv_fs_t req;
const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr);
if (fd < 0) {
return false;
}
uv_fs_req_cleanup(&req);
rapidjson::Document doc;
getJSON(doc);
FILE *fp = fdopen(fd, "w");
char buf[4096];
rapidjson::FileWriteStream os(fp, buf, sizeof(buf));
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
doc.Accept(writer);
fclose(fp);
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
uv_fs_req_cleanup(&req);
LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data());
return true;
}
void xmrig::CommonConfig::setFileName(const char *fileName)
{
m_fileName = fileName;
}
bool xmrig::CommonConfig::parseInt(int key, int arg)
{
switch (key) {
case RetriesKey: /* --retries */
if (arg > 0 && arg <= 1000) {
m_retries = arg;
}
break;
case RetryPauseKey: /* --retry-pause */
if (arg > 0 && arg <= 3600) {
m_retryPause = arg;
}
break;
case KeepAliveKey: /* --keepalive */
m_pools.back().setKeepAlive(arg);
break;
case VariantKey: /* --variant */
m_pools.back().setVariant(arg);
break;
case DonateLevelKey: /* --donate-level */
if (arg >= kMinimumDonateLevel && arg <= 99) {
m_donateLevel = arg;
}
break;
case ApiPort: /* --api-port */
if (arg > 0 && arg <= 65536) {
m_apiPort = arg;
}
break;
case PrintTimeKey: /* --print-time */
if (arg >= 0 && arg <= 3600) {
m_printTime = arg;
}
break;
default:
break;
}
return true;
}
void xmrig::CommonConfig::setAlgo(const char *algo)
{
m_algorithm = Pool::algorithm(algo);
}

View file

@ -0,0 +1,105 @@
/* 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 2016-2018 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 __COMMONCONFIG_H__
#define __COMMONCONFIG_H__
#include <vector>
#include "common/utils/c_str.h"
#include "interfaces/IConfig.h"
#include "net/Pool.h"
#include "xmrig.h"
namespace xmrig {
class CommonConfig : public IConfig
{
public:
CommonConfig();
~CommonConfig();
inline Algo algorithm() const { return m_algorithm; }
inline bool isApiIPv6() const { return m_apiIPv6; }
inline bool isApiRestricted() const { return m_apiRestricted; }
inline bool isBackground() const { return m_background; }
inline bool isColors() const { return m_colors; }
inline bool isSyslog() const { return m_syslog; }
inline const char *algoName() const { return Pool::algoName(m_algorithm); }
inline const char *apiToken() const { return m_apiToken.data(); }
inline const char *apiWorkerId() const { return m_apiWorkerId.data(); }
inline const char *logFile() const { return m_logFile.data(); }
inline const char *userAgent() const { return m_userAgent.data(); }
inline const std::vector<Pool> &pools() const { return m_pools; }
inline int apiPort() const { return m_apiPort; }
inline int donateLevel() const { return m_donateLevel; }
inline int printTime() const { return m_printTime; }
inline int retries() const { return m_retries; }
inline int retryPause() const { return m_retryPause; }
inline void setColors(bool colors) { m_colors = colors; }
inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); }
inline const char *fileName() const override { return m_fileName.data(); }
protected:
bool adjust() override;
bool isValid() const override;
bool parseBoolean(int key, bool enable) override;
bool parseString(int key, const char *arg) override;
bool parseUint64(int key, uint64_t arg) override;
bool save() override;
void setFileName(const char *fileName) override;
Algo m_algorithm;
bool m_adjusted;
bool m_apiIPv6;
bool m_apiRestricted;
bool m_background;
bool m_colors;
bool m_syslog;
bool m_watch;
int m_apiPort;
int m_donateLevel;
int m_printTime;
int m_retries;
int m_retryPause;
std::vector<Pool> m_pools;
xmrig::c_str m_apiToken;
xmrig::c_str m_apiWorkerId;
xmrig::c_str m_fileName;
xmrig::c_str m_logFile;
xmrig::c_str m_userAgent;
private:
bool parseInt(int key, int arg);
void setAlgo(const char *algo);
};
} /* namespace xmrig */
#endif /* __COMMONCONFIG_H__ */

View file

@ -0,0 +1,311 @@
/* 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 2016-2018 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 <limits.h>
#include <stdio.h>
#include <uv.h>
#ifndef XMRIG_NO_HTTPD
# include <microhttpd.h>
#endif
#include "common/config/ConfigLoader.h"
#include "common/config/ConfigWatcher.h"
#include "core/ConfigCreator.h"
#include "core/ConfigLoader_platform.h"
#include "interfaces/IConfig.h"
#include "interfaces/IWatcherListener.h"
#include "net/Pool.h"
#include "Platform.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/filereadstream.h"
xmrig::ConfigWatcher *xmrig::ConfigLoader::m_watcher = nullptr;
xmrig::IConfigCreator *xmrig::ConfigLoader::m_creator = nullptr;
xmrig::IWatcherListener *xmrig::ConfigLoader::m_listener = nullptr;
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
bool xmrig::ConfigLoader::loadFromFile(xmrig::IConfig *config, const char *fileName)
{
rapidjson::Document doc;
if (!getJSON(fileName, doc)) {
return false;
}
config->setFileName(fileName);
return loadFromJSON(config, doc);
}
bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const char *json)
{
rapidjson::Document doc;
doc.Parse(json);
if (doc.HasParseError() || !doc.IsObject()) {
return false;
}
return loadFromJSON(config, doc);
}
bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson::Document &doc)
{
for (size_t i = 0; i < ARRAY_SIZE(config_options); i++) {
parseJSON(config, &config_options[i], doc);
}
const rapidjson::Value &pools = doc["pools"];
if (pools.IsArray()) {
for (const rapidjson::Value &value : pools.GetArray()) {
if (!value.IsObject()) {
continue;
}
for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) {
parseJSON(config, &pool_options[i], value);
}
}
}
const rapidjson::Value &api = doc["api"];
if (api.IsObject()) {
for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) {
parseJSON(config, &api_options[i], api);
}
}
config->parseJSON(doc);
config->adjust();
return config->isValid();
}
bool xmrig::ConfigLoader::reload(xmrig::IConfig *oldConfig, const char *json)
{
xmrig::IConfig *config = m_creator->create();
if (!loadFromJSON(config, json)) {
delete config;
return false;
}
config->setFileName(oldConfig->fileName());
const bool saved = config->save();
if (config->isWatch() && m_watcher && saved) {
delete config;
return true;
}
m_listener->onNewConfig(config);
return true;
}
xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener)
{
m_creator = creator;
m_listener = listener;
xmrig::IConfig *config = m_creator->create();
int key;
while (1) {
key = getopt_long(argc, argv, short_options, options, NULL);
if (key < 0) {
break;
}
if (!parseArg(config, key, optarg)) {
delete config;
return nullptr;
}
}
if (optind < argc) {
fprintf(stderr, "%s: unsupported non-option argument '%s'\n", argv[0], argv[optind]);
delete config;
return nullptr;
}
if (!config->isValid()) {
loadFromFile(config, Platform::defaultConfigName());
}
if (!config->isValid()) {
fprintf(stderr, "No valid configuration found. Exiting.\n");
delete config;
return nullptr;
}
if (config->isWatch()) {
m_watcher = new xmrig::ConfigWatcher(config->fileName(), creator, listener);
}
config->adjust();
return config;
}
void xmrig::ConfigLoader::release()
{
delete m_watcher;
delete m_creator;
m_watcher = nullptr;
m_creator = nullptr;
}
bool xmrig::ConfigLoader::getJSON(const char *fileName, rapidjson::Document &doc)
{
uv_fs_t req;
const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr);
if (fd < 0) {
fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd));
return false;
}
uv_fs_req_cleanup(&req);
FILE *fp = fdopen(fd, "rb");
char buf[8192];
rapidjson::FileReadStream is(fp, buf, sizeof(buf));
doc.ParseStream(is);
uv_fs_close(uv_default_loop(), &req, fd, nullptr);
uv_fs_req_cleanup(&req);
if (doc.HasParseError()) {
printf("%s<%d>: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
return false;
}
return doc.IsObject();
}
bool xmrig::ConfigLoader::parseArg(xmrig::IConfig *config, int key, const char *arg)
{
switch (key) {
case xmrig::IConfig::VersionKey: /* --version */
showVersion();
return false;
case xmrig::IConfig::HelpKey: /* --help */
showUsage();
return false;
case xmrig::IConfig::ConfigKey: /* --config */
loadFromFile(config, arg);
break;
default:
return config->parseString(key, arg);;
}
return true;
}
void xmrig::ConfigLoader::parseJSON(xmrig::IConfig *config, const struct option *option, const rapidjson::Value &object)
{
if (!option->name || !object.HasMember(option->name)) {
return;
}
const rapidjson::Value &value = object[option->name];
if (option->has_arg) {
if (value.IsString()) {
config->parseString(option->val, value.GetString());
}
else if (value.IsInt64()) {
config->parseUint64(option->val, value.GetUint64());
}
else if (value.IsBool()) {
config->parseBoolean(option->val, value.IsTrue());
}
}
else if (value.IsBool()) {
config->parseBoolean(option->val, value.IsTrue());
}
}
void xmrig::ConfigLoader::showUsage()
{
printf(usage);
}
void xmrig::ConfigLoader::showVersion()
{
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());
# ifndef XMRIG_NO_HTTPD
printf("libmicrohttpd/%s\n", MHD_get_version());
# endif
}

View file

@ -0,0 +1,71 @@
/* 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 2016-2018 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 __CONFIGLOADER_H__
#define __CONFIGLOADER_H__
#include <stdint.h>
#include "rapidjson/fwd.h"
struct option;
namespace xmrig {
class ConfigWatcher;
class IConfigCreator;
class IWatcherListener;
class IConfig;
class ConfigLoader
{
public:
static bool loadFromFile(IConfig *config, const char *fileName);
static bool loadFromJSON(IConfig *config, const char *json);
static bool loadFromJSON(IConfig *config, const rapidjson::Document &doc);
static bool reload(IConfig *oldConfig, const char *json);
static IConfig *load(int argc, char **argv, IConfigCreator *creator, IWatcherListener *listener);
static void release();
private:
static bool getJSON(const char *fileName, rapidjson::Document &doc);
static bool parseArg(IConfig *config, int key, const char *arg);
static void parseJSON(IConfig *config, const struct option *option, const rapidjson::Value &object);
static void showUsage();
static void showVersion();
static ConfigWatcher *m_watcher;
static IConfigCreator *m_creator;
static IWatcherListener *m_listener;
};
} /* namespace xmrig */
#endif /* __CONFIGLOADER_H__ */

View file

@ -0,0 +1,105 @@
/* 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 2016-2018 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 <stdio.h>
#include "common/config/ConfigLoader.h"
#include "common/config/ConfigWatcher.h"
#include "core/ConfigCreator.h"
#include "interfaces/IWatcherListener.h"
#include "log/Log.h"
xmrig::ConfigWatcher::ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener) :
m_creator(creator),
m_listener(listener),
m_path(path)
{
uv_fs_event_init(uv_default_loop(), &m_fsEvent);
uv_timer_init(uv_default_loop(), &m_timer);
m_fsEvent.data = m_timer.data = this;
start();
}
xmrig::ConfigWatcher::~ConfigWatcher()
{
uv_timer_stop(&m_timer);
uv_fs_event_stop(&m_fsEvent);
}
void xmrig::ConfigWatcher::onTimer(uv_timer_t* handle)
{
static_cast<xmrig::ConfigWatcher *>(handle->data)->reload();
}
void xmrig::ConfigWatcher::onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status)
{
if (!filename) {
return;
}
static_cast<xmrig::ConfigWatcher *>(handle->data)->queueUpdate();
}
void xmrig::ConfigWatcher::queueUpdate()
{
uv_timer_stop(&m_timer);
uv_timer_start(&m_timer, xmrig::ConfigWatcher::onTimer, kDelay, 0);
}
void xmrig::ConfigWatcher::reload()
{
LOG_WARN("\"%s\" was changed, reloading configuration", m_path.data());
IConfig *config = m_creator->create();
ConfigLoader::loadFromFile(config, m_path.data());
if (!config->isValid()) {
LOG_ERR("reloading failed");
delete config;
return;
}
m_listener->onNewConfig(config);
# ifndef _WIN32
uv_fs_event_stop(&m_fsEvent);
start();
# endif
}
void xmrig::ConfigWatcher::start()
{
uv_fs_event_start(&m_fsEvent, xmrig::ConfigWatcher::onFsEvent, m_path.data(), 0);
}

View file

@ -0,0 +1,71 @@
/* 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 2016-2018 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 __CONFIGWATCHER_H__
#define __CONFIGWATCHER_H__
#include <stdint.h>
#include <uv.h>
#include "common/utils/c_str.h"
#include "rapidjson/fwd.h"
struct option;
namespace xmrig {
class IConfigCreator;
class IWatcherListener;
class ConfigWatcher
{
public:
ConfigWatcher(const char *path, IConfigCreator *creator, IWatcherListener *listener);
~ConfigWatcher();
private:
constexpr static int kDelay = 500;
static void onFsEvent(uv_fs_event_t* handle, const char *filename, int events, int status);
static void onTimer(uv_timer_t* handle);
void queueUpdate();
void reload();
void start();
IConfigCreator *m_creator;
IWatcherListener *m_listener;
uv_fs_event_t m_fsEvent;
uv_timer_t m_timer;
xmrig::c_str m_path;
};
} /* namespace xmrig */
#endif /* __CONFIGWATCHER_H__ */

96
src/common/utils/c_str.h Normal file
View file

@ -0,0 +1,96 @@
/* 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 2016-2018 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 __C_STR_H__
#define __C_STR_H__
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
namespace xmrig {
/**
* @brief Simple C string wrapper.
*
* 1. I know about std:string.
* 2. For some reason I prefer don't use std:string in miner, eg because of file size of MSYS2 builds.
*/
class c_str
{
public:
inline c_str() : m_data(nullptr) {}
inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; }
inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); }
inline c_str(const char *str) : m_data(nullptr) { set(str); }
inline ~c_str() { free(m_data); }
inline void set(const char *str)
{
free(m_data);
m_data = str != nullptr ? strdup(str) : nullptr;
}
inline void set(char *str)
{
free(m_data);
m_data = str;
}
inline bool isEqual(const char *str) const
{
return (m_data != nullptr && str != nullptr && strcmp(m_data, str)) || (m_data == nullptr && m_data == nullptr);
}
inline bool isNull() const { return m_data == nullptr; }
inline const char *data() const { return m_data; }
inline size_t size() const { return m_data == nullptr ? 0 : strlen(m_data); }
inline bool operator!=(const c_str &str) const { return !isEqual(str.data()); }
inline bool operator!=(const char *str) const { return !isEqual(str); }
inline bool operator==(const c_str &str) const { return isEqual(str.data()); }
inline bool operator==(const char *str) const { return isEqual(str); }
inline c_str &operator=(char *str) { set(str); return *this; }
inline c_str &operator=(const c_str &str) { set(str.data()); return *this; }
inline c_str &operator=(const char *str) { set(str); return *this; }
private:
char *m_data;
};
} /* namespace xmrig */
#endif /* __C_STR_H__ */