aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/password.cpp2
-rw-r--r--src/common/scoped_message_writer.h2
-rw-r--r--src/common/stack_trace.cpp2
-rw-r--r--src/common/threadpool.cpp43
-rw-r--r--src/common/threadpool.h21
-rw-r--r--src/common/util.cpp142
-rw-r--r--src/common/util.h19
8 files changed, 194 insertions, 38 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 808ef7630..f0df05b0d 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -78,7 +78,6 @@ monero_add_library(common
DEPENDS generate_translations_header)
target_link_libraries(common
PUBLIC
- epee
cncrypto
${UNBOUND_LIBRARY}
${LIBUNWIND_LIBRARIES}
diff --git a/src/common/password.cpp b/src/common/password.cpp
index 9336a14fc..3ce2ba42a 100644
--- a/src/common/password.cpp
+++ b/src/common/password.cpp
@@ -164,7 +164,7 @@ namespace
while (true)
{
if (message)
- std::cout << message <<": ";
+ std::cout << message <<": " << std::flush;
if (!read_from_tty(pass1))
return false;
if (verify)
diff --git a/src/common/scoped_message_writer.h b/src/common/scoped_message_writer.h
index d7517babb..d887a13c9 100644
--- a/src/common/scoped_message_writer.h
+++ b/src/common/scoped_message_writer.h
@@ -73,7 +73,7 @@ public:
#if defined(_MSC_VER)
, m_oss(std::move(rhs.m_oss))
#else
- // GCC bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
+ // GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
, m_oss(rhs.m_oss.str(), std::ios_base::out | std::ios_base::ate)
#endif
, m_color(std::move(rhs.m_color))
diff --git a/src/common/stack_trace.cpp b/src/common/stack_trace.cpp
index 9c2bf4b53..d6dc4d7cc 100644
--- a/src/common/stack_trace.cpp
+++ b/src/common/stack_trace.cpp
@@ -51,7 +51,7 @@
#define ST_LOG(x) CINFO(el::base::Writer,el::base::DispatchAction::FileOnlyLog,MONERO_DEFAULT_LOG_CATEGORY) << x
-// from http://stackoverflow.com/questions/11665829/how-can-i-print-stack-trace-for-caught-exceptions-in-c-code-injection-in-c
+// from https://stackoverflow.com/questions/11665829/how-can-i-print-stack-trace-for-caught-exceptions-in-c-code-injection-in-c
// The decl of __cxa_throw in /usr/include/.../cxxabi.h uses
// 'std::type_info *', but GCC's built-in protype uses 'void *'.
diff --git a/src/common/threadpool.cpp b/src/common/threadpool.cpp
index 51e071577..6b69e2a12 100644
--- a/src/common/threadpool.cpp
+++ b/src/common/threadpool.cpp
@@ -36,16 +36,17 @@
#include "common/util.h"
static __thread int depth = 0;
+static __thread bool is_leaf = false;
namespace tools
{
-threadpool::threadpool() : running(true), active(0) {
+threadpool::threadpool(unsigned int max_threads) : running(true), active(0) {
boost::thread::attributes attrs;
attrs.set_stack_size(THREAD_STACK_SIZE);
- max = tools::get_max_concurrency();
- size_t i = max;
+ max = max_threads ? max_threads : tools::get_max_concurrency();
+ size_t i = max ? max - 1 : 0;
while(i--) {
- threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this)));
+ threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this, false)));
}
}
@@ -60,25 +61,30 @@ threadpool::~threadpool() {
}
}
-void threadpool::submit(waiter *obj, std::function<void()> f) {
- entry e = {obj, f};
+void threadpool::submit(waiter *obj, std::function<void()> f, bool leaf) {
+ CHECK_AND_ASSERT_THROW_MES(!is_leaf, "A leaf routine is using a thread pool");
boost::unique_lock<boost::mutex> lock(mutex);
- if ((active == max && !queue.empty()) || depth > 0) {
+ if (!leaf && ((active == max && !queue.empty()) || depth > 0)) {
// if all available threads are already running
// and there's work waiting, just run in current thread
lock.unlock();
++depth;
+ is_leaf = leaf;
f();
--depth;
+ is_leaf = false;
} else {
if (obj)
obj->inc();
- queue.push_back(e);
+ if (leaf)
+ queue.push_front({obj, f, leaf});
+ else
+ queue.push_back({obj, f, leaf});
has_work.notify_one();
}
}
-int threadpool::get_max_concurrency() {
+unsigned int threadpool::get_max_concurrency() const {
return max;
}
@@ -91,7 +97,7 @@ threadpool::waiter::~waiter()
}
try
{
- wait();
+ wait(NULL);
}
catch (const std::exception &e)
{
@@ -99,9 +105,12 @@ threadpool::waiter::~waiter()
}
}
-void threadpool::waiter::wait() {
+void threadpool::waiter::wait(threadpool *tpool) {
+ if (tpool)
+ tpool->run(true);
boost::unique_lock<boost::mutex> lock(mt);
- while(num) cv.wait(lock);
+ while(num)
+ cv.wait(lock);
}
void threadpool::waiter::inc() {
@@ -113,15 +122,19 @@ void threadpool::waiter::dec() {
const boost::unique_lock<boost::mutex> lock(mt);
num--;
if (!num)
- cv.notify_one();
+ cv.notify_all();
}
-void threadpool::run() {
+void threadpool::run(bool flush) {
boost::unique_lock<boost::mutex> lock(mutex);
while (running) {
entry e;
while(queue.empty() && running)
+ {
+ if (flush)
+ return;
has_work.wait(lock);
+ }
if (!running) break;
active++;
@@ -129,8 +142,10 @@ void threadpool::run() {
queue.pop_front();
lock.unlock();
++depth;
+ is_leaf = e.leaf;
e.f();
--depth;
+ is_leaf = false;
if (e.wo)
e.wo->dec();
diff --git a/src/common/threadpool.h b/src/common/threadpool.h
index 34152541c..a43e38a76 100644
--- a/src/common/threadpool.h
+++ b/src/common/threadpool.h
@@ -46,6 +46,9 @@ public:
static threadpool instance;
return instance;
}
+ static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
+ return new threadpool(max_threads);
+ }
// The waiter lets the caller know when all of its
// tasks are completed.
@@ -56,7 +59,7 @@ public:
public:
void inc();
void dec();
- void wait(); //! Wait for a set of tasks to finish.
+ void wait(threadpool *tpool); //! Wait for a set of tasks to finish.
waiter() : num(0){}
~waiter();
};
@@ -64,25 +67,27 @@ public:
// Submit a task to the pool. The waiter pointer may be
// NULL if the caller doesn't care to wait for the
// task to finish.
- void submit(waiter *waiter, std::function<void()> f);
+ void submit(waiter *waiter, std::function<void()> f, bool leaf = false);
+
+ unsigned int get_max_concurrency() const;
- int get_max_concurrency();
+ ~threadpool();
private:
- threadpool();
- ~threadpool();
+ threadpool(unsigned int max_threads = 0);
typedef struct entry {
waiter *wo;
std::function<void()> f;
+ bool leaf;
} entry;
std::deque<entry> queue;
boost::condition_variable has_work;
boost::mutex mutex;
std::vector<boost::thread> threads;
- int active;
- int max;
+ unsigned int active;
+ unsigned int max;
bool running;
- void run();
+ void run(bool flush = false);
};
}
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 008610117..f644c573c 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -195,6 +195,73 @@ namespace tools
catch (...) {}
}
+ file_locker::file_locker(const std::string &filename)
+ {
+#ifdef WIN32
+ m_fd = INVALID_HANDLE_VALUE;
+ std::wstring filename_wide;
+ try
+ {
+ filename_wide = string_tools::utf8_to_utf16(filename);
+ }
+ catch (const std::exception &e)
+ {
+ MERROR("Failed to convert path \"" << filename << "\" to UTF-16: " << e.what());
+ return;
+ }
+ m_fd = CreateFileW(filename_wide.c_str(), GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (m_fd != INVALID_HANDLE_VALUE)
+ {
+ OVERLAPPED ov;
+ memset(&ov, 0, sizeof(ov));
+ if (!LockFileEx(m_fd, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ov))
+ {
+ MERROR("Failed to lock " << filename << ": " << std::error_code(GetLastError(), std::system_category()));
+ CloseHandle(m_fd);
+ m_fd = INVALID_HANDLE_VALUE;
+ }
+ }
+ else
+ {
+ MERROR("Failed to open " << filename << ": " << std::error_code(GetLastError(), std::system_category()));
+ }
+#else
+ m_fd = open(filename.c_str(), O_RDONLY | O_CREAT, 0666);
+ if (m_fd != -1)
+ {
+ if (flock(m_fd, LOCK_EX | LOCK_NB) == -1)
+ {
+ MERROR("Failed to lock " << filename << ": " << std::strerror(errno));
+ close(m_fd);
+ m_fd = -1;
+ }
+ }
+ else
+ {
+ MERROR("Failed to open " << filename << ": " << std::strerror(errno));
+ }
+#endif
+ }
+ file_locker::~file_locker()
+ {
+ if (locked())
+ {
+#ifdef WIN32
+ CloseHandle(m_fd);
+#else
+ close(m_fd);
+#endif
+ }
+ }
+ bool file_locker::locked() const
+ {
+#ifdef WIN32
+ return m_fd != INVALID_HANDLE_VALUE;
+#else
+ return m_fd != -1;
+#endif
+ }
+
#ifdef WIN32
std::string get_windows_version_display_string()
{
@@ -451,10 +518,15 @@ std::string get_nix_version_display_string()
if (SHGetSpecialFolderPathW(NULL, psz_path, nfolder, iscreate))
{
- int size_needed = WideCharToMultiByte(CP_UTF8, 0, psz_path, wcslen(psz_path), NULL, 0, NULL, NULL);
- std::string folder_name(size_needed, 0);
- WideCharToMultiByte(CP_UTF8, 0, psz_path, wcslen(psz_path), &folder_name[0], size_needed, NULL, NULL);
- return folder_name;
+ try
+ {
+ return string_tools::utf16_to_utf8(psz_path);
+ }
+ catch (const std::exception &e)
+ {
+ MERROR("utf16_to_utf8 failed: " << e.what());
+ return "";
+ }
}
LOG_ERROR("SHGetSpecialFolderPathW() failed, could not obtain requested path.");
@@ -515,18 +587,20 @@ std::string get_nix_version_display_string()
int code;
#if defined(WIN32)
// Maximizing chances for success
- WCHAR wide_replacement_name[1000];
- MultiByteToWideChar(CP_UTF8, 0, replacement_name.c_str(), replacement_name.size() + 1, wide_replacement_name, 1000);
- WCHAR wide_replaced_name[1000];
- MultiByteToWideChar(CP_UTF8, 0, replaced_name.c_str(), replaced_name.size() + 1, wide_replaced_name, 1000);
-
- DWORD attributes = ::GetFileAttributesW(wide_replaced_name);
+ std::wstring wide_replacement_name;
+ try { wide_replacement_name = string_tools::utf8_to_utf16(replacement_name); }
+ catch (...) { return std::error_code(GetLastError(), std::system_category()); }
+ std::wstring wide_replaced_name;
+ try { wide_replaced_name = string_tools::utf8_to_utf16(replaced_name); }
+ catch (...) { return std::error_code(GetLastError(), std::system_category()); }
+
+ DWORD attributes = ::GetFileAttributesW(wide_replaced_name.c_str());
if (INVALID_FILE_ATTRIBUTES != attributes)
{
- ::SetFileAttributesW(wide_replaced_name, attributes & (~FILE_ATTRIBUTE_READONLY));
+ ::SetFileAttributesW(wide_replaced_name.c_str(), attributes & (~FILE_ATTRIBUTE_READONLY));
}
- bool ok = 0 != ::MoveFileExW(wide_replacement_name, wide_replaced_name, MOVEFILE_REPLACE_EXISTING);
+ bool ok = 0 != ::MoveFileExW(wide_replacement_name.c_str(), wide_replaced_name.c_str(), MOVEFILE_REPLACE_EXISTING);
code = ok ? 0 : static_cast<int>(::GetLastError());
#else
bool ok = 0 == std::rename(replacement_name.c_str(), replaced_name.c_str());
@@ -727,6 +801,13 @@ std::string get_nix_version_display_string()
bool is_local_address(const std::string &address)
{
+ // always assume Tor/I2P addresses to be untrusted by default
+ if (boost::ends_with(address, ".onion") || boost::ends_with(address, ".i2p"))
+ {
+ MDEBUG("Address '" << address << "' is Tor/I2P, non local");
+ return false;
+ }
+
// extract host
epee::net_utils::http::url_content u_c;
if (!epee::net_utils::parse_url(address, u_c))
@@ -820,4 +901,41 @@ std::string get_nix_version_display_string()
return false;
return true;
}
+
+ boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str)
+ {
+ auto pos = str.find(":");
+ bool r = pos != std::string::npos;
+ uint32_t major;
+ r = r && epee::string_tools::get_xtype_from_string(major, str.substr(0, pos));
+ uint32_t minor;
+ r = r && epee::string_tools::get_xtype_from_string(minor, str.substr(pos + 1));
+ if (r)
+ {
+ return std::make_pair(major, minor);
+ }
+ else
+ {
+ return {};
+ }
+ }
+
+ std::string glob_to_regex(const std::string &val)
+ {
+ std::string newval;
+
+ bool escape = false;
+ for (char c: val)
+ {
+ if (c == '*')
+ newval += escape ? "*" : ".*";
+ else if (c == '?')
+ newval += escape ? "?" : ".";
+ else if (c == '\\')
+ newval += '\\', escape = !escape;
+ else
+ newval += c;
+ }
+ return newval;
+ }
}
diff --git a/src/common/util.h b/src/common/util.h
index 7caf0e3c5..6ec901e7f 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -32,6 +32,7 @@
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
+#include <boost/optional.hpp>
#include <system_error>
#include <csignal>
#include <cstdio>
@@ -90,6 +91,20 @@ namespace tools
const std::string& filename() const noexcept { return m_filename; }
};
+ class file_locker
+ {
+ public:
+ file_locker(const std::string &filename);
+ ~file_locker();
+ bool locked() const;
+ private:
+#ifdef WIN32
+ HANDLE m_fd;
+#else
+ int m_fd;
+#endif
+ };
+
/*! \brief Returns the default data directory.
*
* \details Windows < Vista: C:\\Documents and Settings\\Username\\Application Data\\CRYPTONOTE_NAME
@@ -214,4 +229,8 @@ namespace tools
bool sha256sum(const std::string &filename, crypto::hash &hash);
bool is_hdd(const char *path);
+
+ boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str);
+
+ std::string glob_to_regex(const std::string &val);
}