PIVX Core  5.6.99
P2P Digital Currency
system.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2015-2022 The PIVX Core developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
7 
12 #ifndef PIVX_UTIL_SYSTEM_H
13 #define PIVX_UTIL_SYSTEM_H
14 
15 #if defined(HAVE_CONFIG_H)
16 #include "config/pivx-config.h"
17 #endif
18 
19 #include "fs.h"
20 #include "logging.h"
21 #include "compat.h"
22 #include "optional.h"
23 #include "sync.h"
24 #include "tinyformat.h"
25 #include "utiltime.h"
26 #include "util/threadnames.h"
27 
28 #include <atomic>
29 #include <exception>
30 #include <map>
31 #include <memory>
32 #include <set>
33 #include <stdint.h>
34 #include <string>
35 #include <unordered_set>
36 #include <utility>
37 #include <vector>
38 
39 #include <boost/signals2/signal.hpp>
40 #include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
41 
44 {
45 public:
47  boost::signals2::signal<std::string (const char* psz)> Translate;
48 };
49 
50 extern const char * const PIVX_CONF_FILENAME;
51 extern const char * const PIVX_PID_FILENAME;
52 extern const char * const PIVX_MASTERNODE_CONF_FILENAME;
53 extern const char * const DEFAULT_DEBUGLOGFILE;
54 
55 //PIVX only features
56 
57 extern std::atomic<bool> fMasterNode;
58 
60 
65 inline std::string _(const char* psz)
66 {
67  // todo: this Optional is needed for now. Will get removed moving forward
69  return rv ? (*rv) : psz;
70 }
71 
72 
73 void SetupEnvironment();
74 bool SetupNetworking();
75 
76 template<typename... Args>
77 bool error(const char* fmt, const Args&... args)
78 {
79  LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
80  return false;
81 }
82 
83 void PrintExceptionContinue(const std::exception* pex, const char* pszThread);
84 bool FileCommit(FILE* file);
85 bool TruncateFile(FILE* file, unsigned int length);
86 int RaiseFileDescriptorLimit(int nMinFD);
87 void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
88 bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);
89 bool RenameOver(fs::path src, fs::path dest);
90 bool LockDirectory(const fs::path& directory, const std::string& lockfile_name, bool probe_only=false);
91 bool DirIsWritable(const fs::path& directory);
92 
97 
98 bool TryCreateDirectories(const fs::path& p);
99 fs::path GetDefaultDataDir();
100 // The blocks directory is always net specific.
101 const fs::path &GetBlocksDir();
102 const fs::path &GetDataDir(bool fNetSpecific = true);
103 // Return true if -datadir option points to a valid directory or is not specified.
104 bool CheckDataDirOption();
105 // Sapling network dir
106 const fs::path &ZC_GetParamsDir();
107 // Init sapling library
108 void initZKSNARKS();
109 void ClearDatadirCache();
110 fs::path GetConfigFile(const std::string& confPath);
111 fs::path GetMasternodeConfigFile();
112 #ifdef WIN32
113 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
114 #endif
115 
116 void runCommand(std::string strCommand);
117 
118 inline bool IsSwitchChar(char c)
119 {
120 #ifdef WIN32
121  return c == '-' || c == '/';
122 #else
123  return c == '-';
124 #endif
125 }
126 
128 {
129 protected:
130  friend class ArgsManagerHelper;
131 
133  std::map<std::string, std::vector<std::string>> m_override_args;
134  std::map<std::string, std::vector<std::string>> m_config_args;
135  std::string m_network;
136  std::set<std::string> m_network_only_args;
137 
138  void ReadConfigStream(std::istream& stream);
139 
140 public:
141  ArgsManager();
142 
146  void SelectConfigNetwork(const std::string& network);
147 
148  void ParseParameters(int argc, const char* const argv[]);
149  void ReadConfigFile(const std::string& confPath);
150 
157  void WarnForSectionOnlyArgs();
158 
165  std::vector<std::string> GetArgs(const std::string& strArg) const;
166 
173  bool IsArgSet(const std::string& strArg) const;
174 
182  bool IsArgNegated(const std::string& strArg) const;
183 
191  std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
192 
200  int64_t GetArg(const std::string& strArg, int64_t nDefault) const;
201 
209  bool GetBoolArg(const std::string& strArg, bool fDefault) const;
210 
218  bool SoftSetArg(const std::string& strArg, const std::string& strValue);
219 
227  bool SoftSetBoolArg(const std::string& strArg, bool fValue);
228 
229  // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
230  // been set. Also called directly in testing.
231  void ForceSetArg(const std::string& strArg, const std::string& strValue);
232 
237  std::string GetChainName() const;
238 };
239 
240 extern ArgsManager gArgs;
241 
248 std::string HelpMessageGroup(const std::string& message);
249 
257 std::string HelpMessageOpt(const std::string& option, const std::string& message);
258 
263 int GetNumCores();
264 
265 void SetThreadPriority(int nPriority);
266 
270 template <typename Callable>
271 void TraceThread(const std::string name, Callable func)
272 {
273  std::string s = "pivx-" + name;
274  util::ThreadRename(s.c_str());
275  try {
276  LogPrintf("%s thread start\n", name);
277  func();
278  LogPrintf("%s thread exit\n", name);
279  } catch (boost::thread_interrupted) {
280  LogPrintf("%s thread interrupt\n", name);
281  throw;
282  } catch (std::exception& e) {
283  PrintExceptionContinue(&e, name.c_str());
284  throw;
285  } catch (...) {
286  PrintExceptionContinue(nullptr, name.c_str());
287  throw;
288  }
289 }
290 
291 fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
292 
300 int ScheduleBatchPriority(void);
301 
302 namespace util {
303 
304 #ifdef WIN32
305 class WinCmdLineArgs
306 {
307 public:
308  WinCmdLineArgs();
309  ~WinCmdLineArgs();
310  std::pair<int, char**> get();
311 
312 private:
313  int argc;
314  char** argv;
315  std::vector<std::string> args;
316 };
317 #endif
318 
319 } // namespace util
320 
321 #endif // PIVX_UTIL_SYSTEM_H
Internal helper functions for ArgsManager.
Definition: system.cpp:178
void ReadConfigFile(const std::string &confPath)
Definition: system.cpp:832
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: system.cpp:431
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:489
void ParseParameters(int argc, const char *const argv[])
Definition: system.cpp:371
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:406
std::map< std::string, std::vector< std::string > > m_override_args
Definition: system.h:133
std::set< std::string > m_network_only_args
Definition: system.h:136
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: system.cpp:473
void SelectConfigNetwork(const std::string &network)
Select the network in use.
Definition: system.cpp:366
std::map< std::string, std::vector< std::string > > m_config_args
Definition: system.h:134
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:425
void WarnForSectionOnlyArgs()
Log warnings for options in m_section_only_args when they are specified in the default section but no...
Definition: system.cpp:338
void ReadConfigStream(std::istream &stream)
Definition: system.cpp:816
std::string m_network
Definition: system.h:135
RecursiveMutex cs_args
Definition: system.h:132
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:449
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: system.cpp:481
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:465
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: system.cpp:861
Server/client environment: argument handling, config file parsing, thread wrappers.
Definition: system.h:44
boost::signals2::signal< std::string(const char *psz)> Translate
Translate a message to the native language of the user.
Definition: system.h:47
char ** argv
Definition: fuzz.cpp:52
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:958
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:62
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
const char * name
Definition: rest.cpp:37
bool LockDirectory(const fs::path &directory, const std::string &lockfile_name, bool probe_only=false)
Definition: system.cpp:110
CTranslationInterface translationInterface
Definition: system.cpp:91
fs::path GetDefaultDataDir()
Definition: system.cpp:533
const char *const PIVX_CONF_FILENAME
Definition: system.cpp:81
void initZKSNARKS()
Definition: system.cpp:624
bool CheckDataDirOption()
Definition: system.cpp:755
bool DirIsWritable(const fs::path &directory)
Definition: system.cpp:140
bool RenameOver(fs::path src, fs::path dest)
Definition: system.cpp:875
bool SetupNetworking()
Definition: system.cpp:1070
const fs::path & ZC_GetParamsDir()
Definition: system.cpp:596
int RaiseFileDescriptorLimit(int nMinFD)
this function tries to raise the file descriptor limit to the requested number.
Definition: system.cpp:950
const char *const PIVX_PID_FILENAME
The PID file facilities.
Definition: init.cpp:114
void ReleaseDirectoryLocks()
Release all directory locks.
Definition: system.cpp:134
int ScheduleBatchPriority(void)
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
Definition: system.cpp:1101
void SetThreadPriority(int nPriority)
Definition: system.cpp:1082
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
Definition: system.cpp:891
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: system.cpp:974
ArgsManager gArgs
Definition: system.cpp:89
const char *const PIVX_MASTERNODE_CONF_FILENAME
Definition: system.cpp:82
fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific=true)
Definition: system.cpp:853
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
std::atomic< bool > fMasterNode
Definition: system.cpp:87
void SetupEnvironment()
Definition: system.cpp:1043
fs::path GetConfigFile(const std::string &confPath)
Definition: system.cpp:770
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:11
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:526
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a Optional result.
Definition: system.h:65
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: system.cpp:499
const fs::path & GetDataDir(bool fNetSpecific=true)
Definition: system.cpp:724
void ClearDatadirCache()
Definition: system.cpp:761
bool IsSwitchChar(char c)
Definition: system.h:118
fs::path GetMasternodeConfigFile()
Definition: system.cpp:776
bool TruncateFile(FILE *file, unsigned int length)
Definition: system.cpp:937
const fs::path & GetBlocksDir()
Definition: system.cpp:696
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes=0)
Definition: system.cpp:93
void runCommand(std::string strCommand)
Definition: system.cpp:1031
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1095
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: system.cpp:503
bool FileCommit(FILE *file)
Definition: system.cpp:904
void TraceThread(const std::string name, Callable func)
Definition: system.h:271