2021-09-01 11:49:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2024-01-02 13:06:19 +00:00
|
|
|
* Copyright (c) 2021-2024 SChernykh <https://github.com/SChernykh>
|
2021-09-01 11:49:58 +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, version 3.
|
|
|
|
*
|
|
|
|
* 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 "common.h"
|
|
|
|
#include "p2pool_api.h"
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2023-08-16 11:00:11 +00:00
|
|
|
LOG_CATEGORY(P2Pool API)
|
2021-09-01 11:49:58 +00:00
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
2022-08-25 07:52:58 +00:00
|
|
|
p2pool_api::p2pool_api(const std::string& api_path, const bool local_stats)
|
|
|
|
: m_apiPath(api_path)
|
|
|
|
, m_counter(0)
|
2021-09-01 11:49:58 +00:00
|
|
|
{
|
|
|
|
if (m_apiPath.empty()) {
|
|
|
|
LOGERR(1, "api path is empty");
|
2023-01-14 11:19:25 +00:00
|
|
|
PANIC_STOP();
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_apiPath.back() != '/')
|
|
|
|
#ifdef _WIN32
|
|
|
|
&& (m_apiPath.back() != '\\')
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
m_apiPath += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(m_apiPath.c_str(), &buf) != 0) {
|
|
|
|
LOGERR(1, "path " << m_apiPath << " doesn't exist");
|
2023-01-14 11:19:25 +00:00
|
|
|
PANIC_STOP();
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int result = uv_async_init(uv_default_loop_checked(), &m_dumpToFileAsync, on_dump_to_file);
|
|
|
|
if (result) {
|
|
|
|
LOGERR(1, "uv_async_init failed, error " << uv_err_name(result));
|
2023-01-14 11:19:25 +00:00
|
|
|
PANIC_STOP();
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
m_dumpToFileAsync.data = this;
|
|
|
|
|
|
|
|
uv_mutex_init_checked(&m_dumpDataLock);
|
|
|
|
|
|
|
|
m_networkPath = m_apiPath + "network/";
|
2021-09-01 14:26:56 +00:00
|
|
|
m_poolPath = m_apiPath + "pool/";
|
2021-10-01 23:09:42 +00:00
|
|
|
m_localPath = m_apiPath + "local/";
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2021-09-01 14:26:56 +00:00
|
|
|
create_dir(m_networkPath);
|
|
|
|
create_dir(m_poolPath);
|
2021-10-01 23:09:42 +00:00
|
|
|
|
|
|
|
if (local_stats) {
|
|
|
|
create_dir(m_localPath);
|
|
|
|
}
|
2021-09-01 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p2pool_api::~p2pool_api()
|
|
|
|
{
|
|
|
|
uv_mutex_destroy(&m_dumpDataLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::create_dir(const std::string& path)
|
|
|
|
{
|
2021-09-01 11:49:58 +00:00
|
|
|
#ifdef _MSC_VER
|
2021-09-01 14:26:56 +00:00
|
|
|
int result = _mkdir(path.c_str());
|
2021-09-01 11:49:58 +00:00
|
|
|
#else
|
2021-09-05 07:47:48 +00:00
|
|
|
int result = mkdir(path.c_str()
|
|
|
|
#ifndef _WIN32
|
|
|
|
, 0775
|
|
|
|
#endif
|
|
|
|
);
|
2021-09-01 11:49:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
result = errno;
|
|
|
|
if (result != EEXIST) {
|
2021-09-01 14:26:56 +00:00
|
|
|
LOGERR(1, "mkdir(" << path << ") failed, error " << result);
|
2023-01-14 11:19:25 +00:00
|
|
|
PANIC_STOP();
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::on_stop()
|
|
|
|
{
|
2023-09-04 17:33:31 +00:00
|
|
|
MutexLock lock(m_dumpDataLock);
|
2021-09-01 11:49:58 +00:00
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(&m_dumpToFileAsync), nullptr);
|
|
|
|
}
|
|
|
|
|
2023-04-19 09:36:12 +00:00
|
|
|
void p2pool_api::dump_to_file_async_internal(Category category, const char* filename, Callback<void, log::Stream&>::Base&& callback)
|
2021-09-01 11:49:58 +00:00
|
|
|
{
|
2023-09-16 17:38:16 +00:00
|
|
|
std::vector<char> buf(1024);
|
2021-09-03 16:04:54 +00:00
|
|
|
log::Stream s(buf.data(), buf.size());
|
2021-09-01 11:49:58 +00:00
|
|
|
callback(s);
|
2023-09-16 17:38:16 +00:00
|
|
|
|
|
|
|
// If the buffer was too small, try again with big enough buffer
|
|
|
|
if (s.m_spilled) {
|
|
|
|
// Assume that the second call will use no more than 2X bytes
|
|
|
|
buf.resize((static_cast<ptrdiff_t>(s.m_pos) + s.m_spilled) * 2 + 1);
|
|
|
|
s.reset(buf.data(), buf.size());
|
|
|
|
callback(s);
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:49:58 +00:00
|
|
|
buf.resize(s.m_pos);
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
switch (category) {
|
2021-09-08 07:57:31 +00:00
|
|
|
case Category::GLOBAL: path = m_apiPath + filename; break;
|
2021-09-01 14:26:56 +00:00
|
|
|
case Category::NETWORK: path = m_networkPath + filename; break;
|
|
|
|
case Category::POOL: path = m_poolPath + filename; break;
|
2021-10-04 08:28:56 +00:00
|
|
|
case Category::LOCAL: path = m_localPath + filename; break;
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:33:31 +00:00
|
|
|
MutexLock lock(m_dumpDataLock);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2024-08-11 22:47:27 +00:00
|
|
|
std::pair<std::vector<char>, bool>& data = m_dumpData[path];
|
|
|
|
|
|
|
|
if (data.first != buf) {
|
|
|
|
data.first = buf;
|
|
|
|
data.second = true;
|
|
|
|
|
|
|
|
if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(&m_dumpToFileAsync))) {
|
|
|
|
uv_async_send(&m_dumpToFileAsync);
|
|
|
|
}
|
2023-01-13 17:25:04 +00:00
|
|
|
}
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::dump_to_file()
|
|
|
|
{
|
2024-08-11 22:47:27 +00:00
|
|
|
MutexLock lock(m_dumpDataLock);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2023-06-27 12:29:19 +00:00
|
|
|
char buf[log::Stream::BUF_SIZE + 1];
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
2024-08-11 22:47:27 +00:00
|
|
|
for (auto& it : m_dumpData) {
|
|
|
|
if (!it.second.second) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it.second.second = false;
|
|
|
|
|
2023-06-27 12:29:19 +00:00
|
|
|
log::Stream s(buf);
|
|
|
|
s << it.first << m_counter << '\0';
|
|
|
|
|
2024-08-11 22:47:27 +00:00
|
|
|
DumpFileWork* work = new DumpFileWork{ {}, 0, it.first, buf, it.second.first };
|
2022-08-24 15:28:53 +00:00
|
|
|
work->req.data = work;
|
2022-08-25 07:52:58 +00:00
|
|
|
++m_counter;
|
2021-09-01 11:49:58 +00:00
|
|
|
|
|
|
|
const int flags = O_WRONLY | O_CREAT | O_TRUNC
|
|
|
|
#ifdef O_BINARY
|
|
|
|
| O_BINARY
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2022-08-25 07:52:58 +00:00
|
|
|
const int result = uv_fs_open(uv_default_loop_checked(), &work->req, work->tmp_name.c_str(), flags, 0644, on_fs_open);
|
2021-09-01 11:49:58 +00:00
|
|
|
if (result < 0) {
|
2022-08-26 07:39:44 +00:00
|
|
|
LOGWARN(4, "failed to open " << work->tmp_name << ", error " << uv_err_name(result));
|
2021-09-01 11:49:58 +00:00
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::on_fs_open(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
2022-08-24 15:28:53 +00:00
|
|
|
work->fd = static_cast<int>(req->result);
|
|
|
|
uv_fs_req_cleanup(req);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2022-08-24 15:28:53 +00:00
|
|
|
if (work->fd < 0) {
|
2022-08-26 07:39:44 +00:00
|
|
|
LOGWARN(4, "failed to open " << work->tmp_name << ", error " << uv_err_name(work->fd));
|
2021-09-01 11:49:58 +00:00
|
|
|
delete work;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_buf_t buf[1];
|
|
|
|
buf[0].base = work->buf.data();
|
|
|
|
buf[0].len = static_cast<uint32_t>(work->buf.size());
|
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
int result = uv_fs_write(uv_default_loop_checked(), &work->req, static_cast<uv_file>(work->fd), buf, 1, -1, on_fs_write);
|
2021-09-01 11:49:58 +00:00
|
|
|
if (result < 0) {
|
2022-08-26 07:39:44 +00:00
|
|
|
LOGWARN(4, "failed to write to " << work->tmp_name << ", error " << uv_err_name(result));
|
2023-01-13 13:43:13 +00:00
|
|
|
|
|
|
|
result = uv_fs_close(uv_default_loop_checked(), &work->req, static_cast<uv_file>(work->fd), on_fs_error_cleanup);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to close " << work->tmp_name << ", error " << uv_err_name(result));
|
|
|
|
delete work;
|
|
|
|
}
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::on_fs_write(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
2023-01-13 13:43:13 +00:00
|
|
|
int result = static_cast<int>(req->result);
|
|
|
|
uv_fs_req_cleanup(req);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to write to " << work->tmp_name << ", error " << uv_err_name(result));
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
2023-01-13 13:43:13 +00:00
|
|
|
else if (result && (static_cast<size_t>(result) < work->buf.size())) {
|
|
|
|
work->buf.erase(work->buf.begin(), work->buf.begin() + result);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
uv_buf_t buf[1];
|
|
|
|
buf[0].base = work->buf.data();
|
|
|
|
buf[0].len = static_cast<uint32_t>(work->buf.size());
|
|
|
|
|
|
|
|
result = uv_fs_write(uv_default_loop_checked(), &work->req, static_cast<uv_file>(work->fd), buf, 1, -1, on_fs_write);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to write to " << work->tmp_name << ", error " << uv_err_name(result));
|
2022-08-24 15:28:53 +00:00
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
result = uv_fs_close(uv_default_loop_checked(), &work->req, static_cast<uv_file>(work->fd), on_fs_error_cleanup);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to close " << work->tmp_name << ", error " << uv_err_name(result));
|
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = uv_fs_close(uv_default_loop_checked(), &work->req, static_cast<uv_file>(work->fd), on_fs_close);
|
2021-09-01 11:49:58 +00:00
|
|
|
if (result < 0) {
|
2022-08-26 07:39:44 +00:00
|
|
|
LOGWARN(4, "failed to close " << work->tmp_name << ", error " << uv_err_name(result));
|
2021-09-01 11:49:58 +00:00
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::on_fs_close(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
2023-01-13 13:43:13 +00:00
|
|
|
int result = static_cast<int>(req->result);
|
|
|
|
uv_fs_req_cleanup(req);
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to close " << work->tmp_name << ", error " << uv_err_name(result));
|
2021-09-01 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
result = uv_fs_rename(uv_default_loop_checked(), &work->req, work->tmp_name.c_str(), work->name.c_str(), on_fs_rename);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to rename " << work->tmp_name << " to " << work->name << ", error " << uv_err_name(result));
|
|
|
|
|
|
|
|
result = uv_fs_unlink(uv_default_loop_checked(), &work->req, work->tmp_name.c_str(), on_fs_error_cleanup);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to delete " << work->tmp_name << ", error " << uv_err_name(result));
|
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p2pool_api::on_fs_rename(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
|
|
|
int result = static_cast<int>(req->result);
|
2022-08-24 15:28:53 +00:00
|
|
|
uv_fs_req_cleanup(req);
|
|
|
|
|
|
|
|
if (result < 0) {
|
2022-08-26 07:39:44 +00:00
|
|
|
LOGWARN(4, "failed to rename " << work->tmp_name << " to " << work->name << ", error " << uv_err_name(result));
|
2023-01-13 13:43:13 +00:00
|
|
|
|
|
|
|
result = uv_fs_unlink(uv_default_loop_checked(), &work->req, work->tmp_name.c_str(), on_fs_error_cleanup);
|
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to delete " << work->tmp_name << ", error " << uv_err_name(result));
|
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:28:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-13 13:43:13 +00:00
|
|
|
|
|
|
|
delete work;
|
2022-08-24 15:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
void p2pool_api::on_fs_error_cleanup(uv_fs_t* req)
|
2022-08-24 15:28:53 +00:00
|
|
|
{
|
|
|
|
DumpFileWork* work = reinterpret_cast<DumpFileWork*>(req->data);
|
2023-01-13 13:43:13 +00:00
|
|
|
int result = static_cast<int>(req->result);
|
|
|
|
uv_fs_req_cleanup(req);
|
2022-08-24 15:28:53 +00:00
|
|
|
|
2023-01-13 13:43:13 +00:00
|
|
|
if (result < 0) {
|
|
|
|
LOGWARN(4, "failed to cleanup after previous errors " << work->tmp_name << ", error " << uv_err_name(result));
|
2022-08-24 15:28:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 11:49:58 +00:00
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace p2pool
|