xmrig/src/App.cpp

212 lines
4.7 KiB
C++
Raw Normal View History

2017-04-15 06:02:08 +00:00
/* 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>
2018-03-27 07:01:38 +00:00
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
2019-02-14 22:42:46 +00:00
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
2017-04-15 06:02:08 +00:00
*
* 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/>.
*/
2017-06-04 17:52:21 +00:00
2017-06-14 13:11:01 +00:00
#include <stdlib.h>
2017-06-04 17:52:21 +00:00
#include <uv.h>
2017-08-31 01:30:59 +00:00
#include "api/Api.h"
2017-06-04 17:52:21 +00:00
#include "App.h"
#include "common/Console.h"
#include "common/cpu/Cpu.h"
2018-04-20 11:54:58 +00:00
#include "common/log/Log.h"
2018-04-13 00:23:01 +00:00
#include "common/Platform.h"
#include "core/Config.h"
2018-03-27 07:01:38 +00:00
#include "core/Controller.h"
2017-06-08 06:47:25 +00:00
#include "crypto/CryptoNight.h"
2017-06-09 12:09:21 +00:00
#include "Mem.h"
2017-06-04 17:52:21 +00:00
#include "net/Network.h"
2017-06-07 22:16:45 +00:00
#include "Summary.h"
2017-06-04 17:52:21 +00:00
#include "version.h"
2017-06-10 06:41:08 +00:00
#include "workers/Workers.h"
2017-06-04 17:52:21 +00:00
#ifndef XMRIG_NO_HTTPD
2018-04-13 00:12:53 +00:00
# include "common/api/Httpd.h"
#endif
2017-06-24 23:04:59 +00:00
2019-02-14 22:42:46 +00:00
xmrig::App *xmrig::App::m_self = nullptr;
2017-06-04 17:52:21 +00:00
2017-06-13 10:20:15 +00:00
2019-02-14 22:42:46 +00:00
xmrig::App::App(Process *process) :
2017-07-23 05:35:26 +00:00
m_console(nullptr),
m_httpd(nullptr)
2017-06-04 17:52:21 +00:00
{
2017-06-13 10:20:15 +00:00
m_self = this;
2019-02-14 22:42:46 +00:00
m_controller = new xmrig::Controller(process);
if (m_controller->init() != 0) {
return;
}
if (!m_controller->config()->isBackground()) {
2017-07-23 06:36:30 +00:00
m_console = new Console(this);
}
2017-06-07 21:10:26 +00:00
2018-01-04 04:38:32 +00:00
uv_signal_init(uv_default_loop(), &m_sigHUP);
uv_signal_init(uv_default_loop(), &m_sigINT);
uv_signal_init(uv_default_loop(), &m_sigTERM);
2017-06-04 17:52:21 +00:00
}
2019-02-14 22:42:46 +00:00
xmrig::App::~App()
2017-06-04 17:52:21 +00:00
{
uv_tty_reset_mode();
delete m_console;
delete m_controller;
2017-09-02 09:12:40 +00:00
# ifndef XMRIG_NO_HTTPD
delete m_httpd;
2017-09-02 09:12:40 +00:00
# endif
2017-06-04 17:52:21 +00:00
}
2019-02-14 22:42:46 +00:00
int xmrig::App::exec()
2017-06-04 17:52:21 +00:00
{
2018-03-31 10:51:33 +00:00
if (!m_controller->isReady()) {
2018-01-20 05:58:43 +00:00
return 2;
2017-06-04 17:52:21 +00:00
}
2018-01-04 04:38:32 +00:00
uv_signal_start(&m_sigHUP, App::onSignal, SIGHUP);
uv_signal_start(&m_sigINT, App::onSignal, SIGINT);
uv_signal_start(&m_sigTERM, App::onSignal, SIGTERM);
2017-06-13 10:20:15 +00:00
background();
2018-04-15 04:08:47 +00:00
Mem::init(m_controller->config()->isHugePages());
2017-06-07 22:16:45 +00:00
Summary::print(m_controller);
if (m_controller->config()->isDryRun()) {
2018-01-20 13:43:31 +00:00
LOG_NOTICE("OK");
release();
return 0;
}
2017-09-02 09:12:40 +00:00
# ifndef XMRIG_NO_API
2018-03-27 07:01:38 +00:00
Api::start(m_controller);
2017-09-02 09:12:40 +00:00
# endif
2017-08-31 01:30:59 +00:00
# ifndef XMRIG_NO_HTTPD
m_httpd = new Httpd(
m_controller->config()->apiPort(),
m_controller->config()->apiToken(),
m_controller->config()->isApiIPv6(),
m_controller->config()->isApiRestricted()
);
m_httpd->start();
# endif
2018-04-02 20:27:44 +00:00
Workers::start(m_controller);
m_controller->network()->connect();
2017-06-04 17:52:21 +00:00
const int r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
2018-01-20 13:43:31 +00:00
release();
2017-06-04 17:52:21 +00:00
return r;
}
2017-06-13 10:20:15 +00:00
2019-02-14 22:42:46 +00:00
void xmrig::App::onConsoleCommand(char command)
2017-07-23 06:36:30 +00:00
{
switch (command) {
case 'h':
case 'H':
Workers::printHashrate(true);
break;
case 'p':
case 'P':
if (Workers::isEnabled()) {
LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;33mpaused\x1B[0m, press \x1B[01;35mr\x1B[0m to resume" : "paused, press 'r' to resume");
Workers::setEnabled(false);
}
2017-07-23 06:36:30 +00:00
break;
case 'r':
case 'R':
if (!Workers::isEnabled()) {
LOG_INFO(m_controller->config()->isColors() ? "\x1B[01;32mresumed" : "resumed");
2017-07-23 06:36:30 +00:00
Workers::setEnabled(true);
}
break;
case 3:
LOG_WARN("Ctrl+C received, exiting");
close();
break;
default:
break;
}
}
2019-02-14 22:42:46 +00:00
void xmrig::App::close()
2017-06-13 10:20:15 +00:00
{
m_controller->network()->stop();
2017-07-18 02:20:36 +00:00
Workers::stop();
2017-07-19 01:28:59 +00:00
uv_stop(uv_default_loop());
2017-06-13 10:20:15 +00:00
}
2019-02-14 22:42:46 +00:00
void xmrig::App::release()
2018-01-20 13:43:31 +00:00
{
}
2019-02-14 22:42:46 +00:00
void xmrig::App::onSignal(uv_signal_t *handle, int signum)
2017-06-13 10:20:15 +00:00
{
switch (signum)
{
case SIGHUP:
LOG_WARN("SIGHUP received, exiting");
break;
case SIGTERM:
LOG_WARN("SIGTERM received, exiting");
break;
case SIGINT:
LOG_WARN("SIGINT received, exiting");
break;
default:
break;
}
2017-07-19 01:28:59 +00:00
uv_signal_stop(handle);
2017-06-13 10:20:15 +00:00
m_self->close();
}