Monero
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
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
56namespace tools
57{
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
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
81 private_file() noexcept;
82
85 static private_file create(std::string filename, uint32_t extra_flags = 0);
86
90
92 private_file& operator=(private_file&&) = default;
93
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
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
125 std::string get_default_data_dir();
126
127#ifdef WIN32
136 std::string get_special_folder_path(int nfolder, bool iscreate);
137#endif
138
145 std::string get_os_version_string();
146
152 bool create_directories_if_necessary(const std::string& path);
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
166 {
167 public:
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 /* Only blocks SIGINT, SIGTERM and SIGPIPE */
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)
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
211 static void posix_handler(int type)
212 {
213 handle_signal(type);
214 }
215#endif
216
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
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); // returns < 0, 0, > 0, similar to strcmp, but more human friendly than lexical - does not attempt to validate
239
249 bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
250
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
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
285}
#define s(x, c)
Definition aesb.c:47
#define v0(p)
Definition aesb.c:116
#define fd(x)
Definition aesb.c:127
#define v1(p)
Definition aesb.c:117
uint64_t num_blocks(const std::vector< test_event_entry > &events)
Definition chaingen.cpp:1220
file_locker(const std::string &filename)
Definition util.cpp:273
int m_fd
Definition util.h:111
~file_locker()
Definition util.cpp:320
bool locked() const
Definition util.cpp:331
const std::string & filename() const noexcept
Definition util.h:98
std::unique_ptr< std::FILE, close_file > m_handle
Definition util.h:74
static private_file drop_and_recreate(std::string filename)
Definition util.cpp:246
std::string m_filename
Definition util.h:75
private_file() noexcept
handle() == nullptr && filename.empty().
Definition util.cpp:138
std::FILE * handle() const noexcept
Definition util.h:97
private_file(std::FILE *handle, std::string &&filename) noexcept
Definition util.cpp:140
static private_file create(std::string filename, uint32_t extra_flags=0)
Definition util.cpp:143
Defines a signal handler for win32 and *nix.
Definition util.h:166
static void handle_signal(int type)
calles m_handler
Definition util.h:218
static bool install(T t)
installs a signal handler
Definition util.h:170
static std::function< void(int)> m_handler
where the installed handler is stored
Definition util.h:226
static void posix_handler(int type)
handler for NIX
Definition util.h:211
#define HANDLE
Definition mdb.c:459
#define const
Definition ipfrdr.c:80
uint32_t address
Definition getifaddr.c:269
POD_CLASS hash
Definition hash.h:49
network_type
Definition cryptonote_config.h:302
Definition enums.h:68
Various Tools.
Definition apply_permutation.h:40
std::string get_human_readable_timespan(uint64_t seconds)
Definition util.cpp:1104
std::string get_human_readable_timestamp(uint64_t ts)
Definition util.cpp:1092
void closefrom(int fd)
Definition util.cpp:1073
bool is_privacy_preserving_network(const std::string &address)
Definition util.cpp:909
int vercmp(const char *v0, const char *v1)
Definition util.cpp:954
bool disable_core_dumps()
Definition util.cpp:783
void set_max_concurrency(unsigned n)
Definition util.cpp:892
std::vector< std::pair< std::string, size_t > > split_string_by_width(const std::string &s, size_t columns)
Definition util.cpp:1414
boost::optional< bool > is_hdd(const char *file_path)
Definition util.cpp:848
void set_strict_default_file_permissions(bool strict)
Definition util.cpp:838
std::error_code replace_file(const std::string &old_name, const std::string &new_name)
std::rename wrapper for nix and something strange for windows.
Definition util.cpp:683
bool sanitize_locale()
Definition util.cpp:725
std::string glob_to_regex(const std::string &val)
Definition util.cpp:1027
void clear_screen()
Definition util.cpp:1172
uint64_t cumulative_block_sync_weight(cryptonote::network_type nettype, uint64_t start_block, uint64_t num_blocks)
Definition util.cpp:1332
boost::optional< std::pair< uint32_t, uint32_t > > parse_subaddress_lookahead(const std::string &str)
Definition util.cpp:1009
bool create_directories_if_necessary(const std::string &path)
creates directories for a path
Definition util.cpp:660
bool is_local_address(const std::string &address)
Definition util.cpp:918
ssize_t get_lockable_memory()
Definition util.cpp:798
bool on_startup()
Definition util.cpp:813
std::string get_human_readable_bytes(uint64_t bytes)
Definition util.cpp:1138
unsigned get_max_concurrency()
Definition util.cpp:903
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash)
Creates a SHA-256 digest of a data buffer.
Definition util.cpp:972
std::string get_os_version_string()
Returns the OS version string.
Definition util.cpp:601
std::string get_default_data_dir()
Returns the default data directory.
Definition util.cpp:635
void copy_file(const std::string &from, const std::string &to)
Definition util.cpp:119
const char *const str
Definition portlistingparse.c:23
#define ts
Definition skein.c:522
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
Functional class for closing C file handles.
Definition util.h:60
void operator()(std::FILE *handle) const noexcept
Definition util.h:61
std::string data
Definition base58.cpp:37
#define T(x)