| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <boost/thread/locks.hpp> |
| 34 |
#include <boost/thread/mutex.hpp> |
| 35 |
#include <boost/optional.hpp> |
| 36 |
#include <system_error> |
| 37 |
#include <csignal> |
| 38 |
#include <cstdio> |
| 39 |
#include <functional> |
| 40 |
#include <memory> |
| 41 |
#include <string> |
| 42 |
|
| 43 |
#ifdef _WIN32 |
| 44 |
#include "windows.h" |
| 45 |
#include "misc_log_ex.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "crypto/hash.h" |
| 49 |
#include "cryptonote_config.h" |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
namespace tools |
| 57 |
{ |
| 58 |
|
| 59 |
struct close_file |
| 60 |
{ |
| 61 |
void operator()(std::FILE* handle) const noexcept |
| 62 |
{ |
| 63 |
if (handle) |
| 64 |
{ |
| 65 |
std::fclose(handle); |
| 66 |
} |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
void copy_file(const std::string& from, const std::string& to); |
| 71 |
|
| 72 |
|
| 73 |
class private_file { |
| 74 |
std::unique_ptr<std::FILE, close_file> m_handle; |
| 75 |
std::string m_filename; |
| 76 |
|
| 77 |
private_file(std::FILE* handle, std::string&& filename) noexcept; |
| 78 |
public: |
| 79 |
|
| 80 |
|
| 81 |
private_file() noexcept; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static private_file create(std::string filename, uint32_t extra_flags = 0); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static private_file drop_and_recreate(std::string filename); |
| 90 |
|
| 91 |
private_file(private_file&&) = default; |
| 92 |
private_file& operator=(private_file&&) = default; |
| 93 |
|
| 94 |
|
| 95 |
~private_file() noexcept; |
| 96 |
|
| 97 |
std::FILE* handle() const noexcept { return m_handle.get(); } |
| 98 |
const std::string& filename() const noexcept { return m_filename; } |
| 99 |
}; |
| 100 |
|
| 101 |
class file_locker |
| 102 |
{ |
| 103 |
public: |
| 104 |
file_locker(const std::string &filename); |
| 105 |
~file_locker(); |
| 106 |
bool locked() const; |
| 107 |
private: |
| 108 |
#ifdef WIN32 |
| 109 |
HANDLE m_fd; |
| 110 |
#else |
| 111 |
int m_fd; |
| 112 |
#endif |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
std::string get_default_data_dir(); |
| 126 |
|
| 127 |
#ifdef WIN32 |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
std::string get_special_folder_path(int nfolder, bool iscreate); |
| 137 |
#endif |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
std::string get_os_version_string(); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool create_directories_if_necessary(const std::string& path); |
| 153 |
|
| 154 |
|
| 155 |
std::error_code replace_file(const std::string& old_name, const std::string& new_name); |
| 156 |
|
| 157 |
bool sanitize_locale(); |
| 158 |
|
| 159 |
bool disable_core_dumps(); |
| 160 |
|
| 161 |
bool on_startup(); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
class signal_handler |
| 166 |
{ |
| 167 |
public: |
| 168 |
|
| 169 |
template<typename T> |
| 170 |
static bool install(T t) |
| 171 |
{ |
| 172 |
#if defined(WIN32) |
| 173 |
bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE); |
| 174 |
if (r) |
| 175 |
{ |
| 176 |
m_handler = t; |
| 177 |
} |
| 178 |
return r; |
| 179 |
#else |
| 180 |
static struct sigaction sa; |
| 181 |
memset(&sa, 0, sizeof(struct sigaction)); |
| 182 |
sa.sa_handler = posix_handler; |
| 183 |
sa.sa_flags = 0; |
| 184 |
|
| 185 |
sigaction(SIGINT, &sa, NULL); |
| 186 |
signal(SIGTERM, posix_handler); |
| 187 |
signal(SIGPIPE, SIG_IGN); |
| 188 |
m_handler = t; |
| 189 |
return true; |
| 190 |
#endif |
| 191 |
} |
| 192 |
|
| 193 |
private: |
| 194 |
#if defined(WIN32) |
| 195 |
|
| 196 |
static BOOL WINAPI win_handler(DWORD type) |
| 197 |
{ |
| 198 |
if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) |
| 199 |
{ |
| 200 |
handle_signal(type); |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
MGINFO_RED("Got control signal " << type << ". Exiting without saving..."); |
| 205 |
return FALSE; |
| 206 |
} |
| 207 |
return TRUE; |
| 208 |
} |
| 209 |
#else |
| 210 |
|
| 211 |
static void posix_handler(int type) |
| 212 |
{ |
| 213 |
handle_signal(type); |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
|
| 218 |
static void handle_signal(int type) |
| 219 |
{ |
| 220 |
static boost::mutex m_mutex; |
| 221 |
boost::unique_lock<boost::mutex> lock(m_mutex); |
| 222 |
m_handler(type); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static std::function<void(int)> m_handler; |
| 227 |
}; |
| 228 |
|
| 229 |
void set_strict_default_file_permissions(bool strict); |
| 230 |
|
| 231 |
ssize_t get_lockable_memory(); |
| 232 |
|
| 233 |
void set_max_concurrency(unsigned n); |
| 234 |
unsigned get_max_concurrency(); |
| 235 |
|
| 236 |
bool is_local_address(const std::string &address); |
| 237 |
bool is_privacy_preserving_network(const std::string &address); |
| 238 |
int vercmp(const char *v0, const char *v1); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
bool sha256sum(const std::string &filename, crypto::hash &hash); |
| 262 |
|
| 263 |
boost::optional<bool> is_hdd(const char *path); |
| 264 |
|
| 265 |
boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str); |
| 266 |
|
| 267 |
std::string glob_to_regex(const std::string &val); |
| 268 |
#ifdef _WIN32 |
| 269 |
std::string input_line_win(); |
| 270 |
#endif |
| 271 |
|
| 272 |
void closefrom(int fd); |
| 273 |
|
| 274 |
std::string get_human_readable_timestamp(uint64_t ts); |
| 275 |
|
| 276 |
std::string get_human_readable_timespan(uint64_t seconds); |
| 277 |
|
| 278 |
std::string get_human_readable_bytes(uint64_t bytes); |
| 279 |
|
| 280 |
void clear_screen(); |
| 281 |
|
| 282 |
std::vector<std::pair<std::string, size_t>> split_string_by_width(const std::string &s, size_t columns); |
| 283 |
|
| 284 |
uint64_t cumulative_block_sync_weight(cryptonote::network_type nettype, uint64_t start_block, uint64_t num_blocks); |
| 285 |
} |