Monero
util.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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 
55 namespace tools
56 {
58  struct close_file
59  {
60  void operator()(std::FILE* handle) const noexcept
61  {
62  if (handle)
63  {
64  std::fclose(handle);
65  }
66  }
67  };
68 
70  class private_file {
71  std::unique_ptr<std::FILE, close_file> m_handle;
72  std::string m_filename;
73 
74  private_file(std::FILE* handle, std::string&& filename) noexcept;
75  public:
76 
78  private_file() noexcept;
79 
82  static private_file create(std::string filename);
83 
84  private_file(private_file&&) = default;
85  private_file& operator=(private_file&&) = default;
86 
88  ~private_file() noexcept;
89 
90  std::FILE* handle() const noexcept { return m_handle.get(); }
91  const std::string& filename() const noexcept { return m_filename; }
92  };
93 
95  {
96  public:
97  file_locker(const std::string &filename);
98  ~file_locker();
99  bool locked() const;
100  private:
101 #ifdef WIN32
102  HANDLE m_fd;
103 #else
104  int m_fd;
105 #endif
106  };
107 
118  std::string get_default_data_dir();
119 
120 #ifdef WIN32
121 
129  std::string get_special_folder_path(int nfolder, bool iscreate);
130 #endif
131 
138  std::string get_os_version_string();
139 
145  bool create_directories_if_necessary(const std::string& path);
148  std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name);
149 
150  bool sanitize_locale();
151 
152  bool disable_core_dumps();
153 
154  bool on_startup();
155 
159  {
160  public:
162  template<typename T>
163  static bool install(T t)
164  {
165 #if defined(WIN32)
166  bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE);
167  if (r)
168  {
169  m_handler = t;
170  }
171  return r;
172 #else
173  static struct sigaction sa;
174  memset(&sa, 0, sizeof(struct sigaction));
175  sa.sa_handler = posix_handler;
176  sa.sa_flags = 0;
177  /* Only blocks SIGINT, SIGTERM and SIGPIPE */
178  sigaction(SIGINT, &sa, NULL);
179  signal(SIGTERM, posix_handler);
180  signal(SIGPIPE, SIG_IGN);
181  m_handler = t;
182  return true;
183 #endif
184  }
185 
186  private:
187 #if defined(WIN32)
188 
189  static BOOL WINAPI win_handler(DWORD type)
190  {
191  if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type)
192  {
193  handle_signal(type);
194  }
195  else
196  {
197  MGINFO_RED("Got control signal " << type << ". Exiting without saving...");
198  return FALSE;
199  }
200  return TRUE;
201  }
202 #else
203 
204  static void posix_handler(int type)
205  {
206  handle_signal(type);
207  }
208 #endif
209 
211  static void handle_signal(int type)
212  {
213  static boost::mutex m_mutex;
214  boost::unique_lock<boost::mutex> lock(m_mutex);
215  m_handler(type);
216  }
217 
219  static std::function<void(int)> m_handler;
220  };
221 
222  void set_strict_default_file_permissions(bool strict);
223 
224  void set_max_concurrency(unsigned n);
225  unsigned get_max_concurrency();
226 
227  bool is_local_address(const std::string &address);
228  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
229 
230  bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
231  bool sha256sum(const std::string &filename, crypto::hash &hash);
232 
233  boost::optional<bool> is_hdd(const char *path);
234 
235  boost::optional<std::pair<uint32_t, uint32_t>> parse_subaddress_lookahead(const std::string& str);
236 
237  std::string glob_to_regex(const std::string &val);
238 }
std::FILE * handle() const noexcept
Definition: util.h:90
const uint32_t T[512]
Definition: groestl_tables.h:33
int m_fd
Definition: util.h:104
Definition: util.h:94
bool is_local_address(const std::string &address)
Definition: util.cpp:803
std::string get_os_version_string()
Returns the OS version string.
Definition: util.cpp:510
void set_max_concurrency(unsigned n)
Definition: util.cpp:786
std::unique_ptr< std::FILE, close_file > m_handle
Definition: util.h:71
bool disable_core_dumps()
Definition: util.cpp:692
std::string glob_to_regex(const std::string &val)
Definition: util.cpp:924
Definition: blockchain_ancestry.cpp:70
file_locker(const std::string &filename)
Definition: util.cpp:205
#define v1(p)
Definition: aesb.c:116
std::error_code replace_file(const std::string &replacement_name, const std::string &replaced_name)
std::rename wrapper for nix and something strange for windows.
Definition: util.cpp:592
unsigned get_max_concurrency()
Definition: util.cpp:797
std::string get_default_data_dir()
Returns the default data directory.
Definition: util.cpp:544
bool on_startup()
Definition: util.cpp:707
bool locked() const
Definition: util.cpp:263
Defines a signal handler for win32 and *nix.
Definition: util.h:158
static std::function< void(int)> m_handler
where the installed handler is stored
Definition: util.h:219
private_file() noexcept
handle() == nullptr && filename.empty().
Definition: util.cpp:87
static void handle_signal(int type)
calles m_handler
Definition: util.h:211
Various Tools.
Definition: apply_permutation.h:39
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash)
Definition: util.cpp:862
static void posix_handler(int type)
handler for NIX
Definition: util.h:204
~file_locker()
Definition: util.cpp:252
static bool install(T t)
installs a signal handler
Definition: util.h:163
void operator()(std::FILE *handle) const noexcept
Definition: util.h:60
boost::optional< std::pair< uint32_t, uint32_t > > parse_subaddress_lookahead(const std::string &str)
Definition: util.cpp:906
void set_strict_default_file_permissions(bool strict)
Definition: util.cpp:732
std::string m_filename
Definition: util.h:72
#define v0(p)
Definition: aesb.c:115
bool sanitize_locale()
Definition: util.cpp:634
bool create_directories_if_necessary(const std::string &path)
creates directories for a path
Definition: util.cpp:569
static private_file create(std::string filename)
Definition: util.cpp:92
POD_CLASS hash
Definition: hash.h:49
A file restricted to process owner AND process. Deletes file on destruction.
Definition: util.h:70
Functional class for closing C file handles.
Definition: util.h:58
const std::string & filename() const noexcept
Definition: util.h:91
int vercmp(const char *v0, const char *v1)
Definition: util.cpp:844
boost::optional< bool > is_hdd(const char *file_path)
Definition: util.cpp:742