2011-05-14 10:31:46 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-12 06:39:40 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
/**
|
|
|
|
* Server/client environment: argument handling, config file parsing,
|
2018-04-18 19:05:05 +02:00
|
|
|
* thread wrappers, startup time
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
*/
|
2011-05-14 10:31:46 +02:00
|
|
|
#ifndef BITCOIN_UTIL_H
|
|
|
|
#define BITCOIN_UTIL_H
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <config/bitcoin-config.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#endif
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <compat.h>
|
|
|
|
#include <fs.h>
|
2018-04-18 19:05:05 +02:00
|
|
|
#include <logging.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <sync.h>
|
|
|
|
#include <tinyformat.h>
|
2018-07-12 06:39:40 +02:00
|
|
|
#include <utilmemory.h>
|
2018-08-13 22:13:29 +02:00
|
|
|
#include <utiltime.h>
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2016-05-30 15:39:37 +02:00
|
|
|
#include <atomic>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
2018-04-04 10:06:00 +02:00
|
|
|
#include <set>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
2018-03-22 03:24:17 +01:00
|
|
|
#include <unordered_set>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <vector>
|
2013-05-24 15:45:08 +02:00
|
|
|
|
2017-11-18 20:35:14 +01:00
|
|
|
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2017-05-14 20:18:26 +02:00
|
|
|
// Application startup time (used for uptime calculation)
|
|
|
|
int64_t GetStartupTime();
|
|
|
|
|
2015-06-27 21:21:41 +02:00
|
|
|
extern const char * const BITCOIN_CONF_FILENAME;
|
|
|
|
extern const char * const BITCOIN_PID_FILENAME;
|
|
|
|
|
2018-08-13 22:13:29 +02:00
|
|
|
/** Translate a message to the native language of the user. */
|
|
|
|
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
|
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
/**
|
2018-08-13 22:13:29 +02:00
|
|
|
* Translation function.
|
|
|
|
* If no translation function is set, simply return the input.
|
2015-04-16 16:20:01 +02:00
|
|
|
*/
|
|
|
|
inline std::string _(const char* psz)
|
|
|
|
{
|
2018-08-13 22:13:29 +02:00
|
|
|
return G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz;
|
2015-04-16 16:20:01 +02:00
|
|
|
}
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2014-05-13 12:15:00 +02:00
|
|
|
void SetupEnvironment();
|
2015-09-02 16:18:16 +02:00
|
|
|
bool SetupNetworking();
|
2013-09-18 10:03:21 +02:00
|
|
|
|
2016-06-27 18:39:25 +02:00
|
|
|
template<typename... Args>
|
|
|
|
bool error(const char* fmt, const Args&... args)
|
2016-05-04 14:21:04 +02:00
|
|
|
{
|
2018-04-11 19:03:21 +02:00
|
|
|
LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
|
2014-01-16 15:52:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-09 14:43:06 +02:00
|
|
|
|
2014-12-07 13:29:06 +01:00
|
|
|
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
|
2018-04-20 11:21:08 +02:00
|
|
|
bool FileCommit(FILE *file);
|
2013-01-30 04:17:33 +01:00
|
|
|
bool TruncateFile(FILE *file, unsigned int length);
|
2013-04-26 00:46:47 +02:00
|
|
|
int RaiseFileDescriptorLimit(int nMinFD);
|
2012-08-16 02:21:28 +02:00
|
|
|
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
2017-03-01 17:05:50 +01:00
|
|
|
bool RenameOver(fs::path src, fs::path dest);
|
2017-12-26 07:41:55 +01:00
|
|
|
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
|
2018-03-07 12:08:55 +01:00
|
|
|
bool DirIsWritable(const fs::path& directory);
|
2018-02-13 13:53:17 +01:00
|
|
|
|
|
|
|
/** Release all directory locks. This is used for unit testing only, at runtime
|
|
|
|
* the global destructor will take care of the locks.
|
|
|
|
*/
|
|
|
|
void ReleaseDirectoryLocks();
|
|
|
|
|
2017-02-22 10:10:00 +01:00
|
|
|
bool TryCreateDirectories(const fs::path& p);
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetDefaultDataDir();
|
2018-03-09 05:12:43 +01:00
|
|
|
const fs::path &GetBlocksDir(bool fNetSpecific = true);
|
2017-03-01 17:05:50 +01:00
|
|
|
const fs::path &GetDataDir(bool fNetSpecific = true);
|
2015-03-03 16:49:12 +01:00
|
|
|
void ClearDatadirCache();
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetConfigFile(const std::string& confPath);
|
2013-07-24 09:30:09 +02:00
|
|
|
#ifndef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetPidFile();
|
|
|
|
void CreatePidFile(const fs::path &path, pid_t pid);
|
2013-07-24 09:30:09 +02:00
|
|
|
#endif
|
2012-04-22 16:22:45 +02:00
|
|
|
#ifdef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
2012-04-22 16:22:45 +02:00
|
|
|
#endif
|
2015-05-31 15:36:44 +02:00
|
|
|
void runCommand(const std::string& strCommand);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2018-01-31 04:33:49 +01:00
|
|
|
/**
|
|
|
|
* Most paths passed as configuration arguments are treated as relative to
|
|
|
|
* the datadir if they are not absolute.
|
|
|
|
*
|
|
|
|
* @param path The path to be conditionally prefixed with datadir.
|
|
|
|
* @param net_specific Forwarded to GetDataDir().
|
|
|
|
* @return The normalized path.
|
|
|
|
*/
|
|
|
|
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
|
|
|
|
|
2011-05-14 10:31:46 +02:00
|
|
|
inline bool IsSwitchChar(char c)
|
|
|
|
{
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifdef WIN32
|
2011-05-14 10:31:46 +02:00
|
|
|
return c == '-' || c == '/';
|
|
|
|
#else
|
|
|
|
return c == '-';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
enum class OptionsCategory {
|
2018-04-28 22:54:58 +02:00
|
|
|
OPTIONS,
|
|
|
|
CONNECTION,
|
|
|
|
WALLET,
|
|
|
|
WALLET_DEBUG_TEST,
|
|
|
|
ZMQ,
|
|
|
|
DEBUG_TEST,
|
|
|
|
CHAINPARAMS,
|
|
|
|
NODE_RELAY,
|
|
|
|
BLOCK_CREATION,
|
|
|
|
RPC,
|
|
|
|
GUI,
|
|
|
|
COMMANDS,
|
2018-04-29 01:40:51 +02:00
|
|
|
REGISTER_COMMANDS,
|
|
|
|
|
|
|
|
HIDDEN // Always the last option to avoid printing these in the help
|
2018-04-28 22:54:58 +02:00
|
|
|
};
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
class ArgsManager
|
|
|
|
{
|
|
|
|
protected:
|
2018-04-04 10:01:00 +02:00
|
|
|
friend class ArgsManagerHelper;
|
|
|
|
|
2018-05-16 21:15:18 +02:00
|
|
|
struct Arg
|
|
|
|
{
|
|
|
|
std::string m_help_param;
|
|
|
|
std::string m_help_text;
|
|
|
|
bool m_debug_only;
|
|
|
|
|
|
|
|
Arg(const std::string& help_param, const std::string& help_text, bool debug_only) : m_help_param(help_param), m_help_text(help_text), m_debug_only(debug_only) {};
|
|
|
|
};
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
mutable CCriticalSection cs_args;
|
2018-08-29 22:33:33 +02:00
|
|
|
std::map<std::string, std::vector<std::string>> m_override_args GUARDED_BY(cs_args);
|
|
|
|
std::map<std::string, std::vector<std::string>> m_config_args GUARDED_BY(cs_args);
|
|
|
|
std::string m_network GUARDED_BY(cs_args);
|
|
|
|
std::set<std::string> m_network_only_args GUARDED_BY(cs_args);
|
|
|
|
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
|
2018-03-22 03:24:17 +01:00
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
bool ReadConfigStream(std::istream& stream, std::string& error, bool ignore_invalid_keys = false);
|
2018-03-29 07:02:00 +02:00
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
public:
|
2018-04-04 10:06:00 +02:00
|
|
|
ArgsManager();
|
|
|
|
|
2018-04-04 10:03:00 +02:00
|
|
|
/**
|
|
|
|
* Select the network in use
|
|
|
|
*/
|
|
|
|
void SelectConfigNetwork(const std::string& network);
|
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
bool ParseParameters(int argc, const char* const argv[], std::string& error);
|
|
|
|
bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false);
|
2017-07-22 01:00:52 +02:00
|
|
|
|
2018-04-04 10:07:00 +02:00
|
|
|
/**
|
|
|
|
* Log warnings for options in m_section_only_args when
|
|
|
|
* they are specified in the default section but not overridden
|
|
|
|
* on the command line or in a network-specific section in the
|
|
|
|
* config file.
|
|
|
|
*/
|
|
|
|
void WarnForSectionOnlyArgs();
|
|
|
|
|
2017-07-22 01:00:52 +02:00
|
|
|
/**
|
|
|
|
* Return a vector of strings of the given argument
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @return command-line arguments
|
|
|
|
*/
|
|
|
|
std::vector<std::string> GetArgs(const std::string& strArg) const;
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2017-06-14 23:13:00 +02:00
|
|
|
/**
|
|
|
|
* Return true if the given argument has been manually set
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @return true if the argument has been set
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
bool IsArgSet(const std::string& strArg) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
2018-03-22 03:24:17 +01:00
|
|
|
/**
|
|
|
|
* Return true if the argument was originally passed as a negated option,
|
|
|
|
* i.e. -nofoo.
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @return true if the argument was passed negated
|
|
|
|
*/
|
|
|
|
bool IsArgNegated(const std::string& strArg) const;
|
|
|
|
|
2017-06-14 23:13:00 +02:00
|
|
|
/**
|
|
|
|
* Return string argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param strDefault (e.g. "1")
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return integer argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param nDefault (e.g. 1)
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument (0 if invalid number) or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
int64_t GetArg(const std::string& strArg, int64_t nDefault) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return boolean argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param fDefault (true or false)
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
bool GetBoolArg(const std::string& strArg, bool fDefault) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param strValue Value (e.g. "1")
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
|
|
|
bool SoftSetArg(const std::string& strArg, const std::string& strValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a boolean argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param fValue Value (e.g. false)
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
|
|
|
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
|
|
|
|
|
|
|
|
// Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
|
|
|
|
// been set. Also called directly in testing.
|
|
|
|
void ForceSetArg(const std::string& strArg, const std::string& strValue);
|
2018-03-22 03:24:17 +01:00
|
|
|
|
2018-03-29 07:00:00 +02:00
|
|
|
/**
|
|
|
|
* Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
|
|
|
|
* @return CBaseChainParams::MAIN by default; raises runtime error if an invalid combination is given.
|
|
|
|
*/
|
|
|
|
std::string GetChainName() const;
|
2018-04-28 22:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add argument
|
|
|
|
*/
|
|
|
|
void AddArg(const std::string& name, const std::string& help, const bool debug_only, const OptionsCategory& cat);
|
|
|
|
|
2018-06-11 23:23:13 +02:00
|
|
|
/**
|
|
|
|
* Add many hidden arguments
|
|
|
|
*/
|
|
|
|
void AddHiddenArgs(const std::vector<std::string>& args);
|
|
|
|
|
2018-04-29 01:40:51 +02:00
|
|
|
/**
|
|
|
|
* Clear available arguments
|
|
|
|
*/
|
2018-08-27 23:19:18 +02:00
|
|
|
void ClearArgs() {
|
|
|
|
LOCK(cs_args);
|
|
|
|
m_available_args.clear();
|
|
|
|
}
|
2018-04-29 01:40:51 +02:00
|
|
|
|
2018-04-28 22:54:58 +02:00
|
|
|
/**
|
|
|
|
* Get the help string
|
|
|
|
*/
|
2018-07-21 11:53:54 +02:00
|
|
|
std::string GetHelpMessage() const;
|
2018-04-29 01:40:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether we know of this arg
|
|
|
|
*/
|
2018-07-21 11:53:54 +02:00
|
|
|
bool IsArgKnown(const std::string& key) const;
|
2017-05-06 01:36:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern ArgsManager gArgs;
|
|
|
|
|
2018-03-30 22:47:36 +02:00
|
|
|
/**
|
|
|
|
* @return true if help has been requested via a command-line arg
|
|
|
|
*/
|
|
|
|
bool HelpRequested(const ArgsManager& args);
|
|
|
|
|
2015-02-04 09:11:49 +01:00
|
|
|
/**
|
|
|
|
* Format a string to be used as group of options in help messages
|
|
|
|
*
|
|
|
|
* @param message Group name (e.g. "RPC server options:")
|
|
|
|
* @return the formatted string
|
|
|
|
*/
|
|
|
|
std::string HelpMessageGroup(const std::string& message);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a string to be used as option description in help messages
|
|
|
|
*
|
|
|
|
* @param option Option message (e.g. "-rpcuser=<user>")
|
|
|
|
* @param message Option description (e.g. "Username for JSON-RPC connections")
|
|
|
|
* @return the formatted string
|
|
|
|
*/
|
|
|
|
std::string HelpMessageOpt(const std::string& option, const std::string& message);
|
|
|
|
|
2015-07-01 17:38:15 +02:00
|
|
|
/**
|
2017-04-25 03:34:23 +02:00
|
|
|
* Return the number of cores available on the current system.
|
|
|
|
* @note This does count virtual cores, such as those provided by HyperThreading.
|
2015-07-01 17:38:15 +02:00
|
|
|
*/
|
|
|
|
int GetNumCores();
|
|
|
|
|
2012-06-24 17:03:57 +02:00
|
|
|
void RenameThread(const char* name);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* .. and a wrapper that just calls func once
|
|
|
|
*/
|
2013-03-09 02:19:17 +01:00
|
|
|
template <typename Callable> void TraceThread(const char* name, Callable func)
|
|
|
|
{
|
|
|
|
std::string s = strprintf("bitcoin-%s", name);
|
|
|
|
RenameThread(s.c_str());
|
|
|
|
try
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread start\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
func();
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread exit\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const boost::thread_interrupted&)
|
2013-03-09 02:19:17 +01:00
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread interrupt\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
throw;
|
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception& e) {
|
2014-02-26 13:23:52 +01:00
|
|
|
PrintExceptionContinue(&e, name);
|
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
2017-08-07 07:36:37 +02:00
|
|
|
PrintExceptionContinue(nullptr, name);
|
2014-02-26 13:23:52 +01:00
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 06:38:27 +01:00
|
|
|
std::string CopyrightHolders(const std::string& strPrefix);
|
2015-12-22 13:29:13 +01:00
|
|
|
|
2018-03-06 18:50:20 +01:00
|
|
|
/**
|
|
|
|
* On platforms that support it, tell the kernel the calling thread is
|
|
|
|
* CPU-intensive and non-interactive. See SCHED_BATCH in sched(7) for details.
|
|
|
|
*
|
|
|
|
* @return The return value of sched_setschedule(), or 1 on systems without
|
2018-04-24 21:12:02 +02:00
|
|
|
* sched_setschedule().
|
2018-03-06 18:50:20 +01:00
|
|
|
*/
|
2018-09-13 19:36:41 +02:00
|
|
|
int ScheduleBatchPriority();
|
2018-03-06 18:50:20 +01:00
|
|
|
|
2018-07-17 07:05:04 +02:00
|
|
|
namespace util {
|
|
|
|
|
|
|
|
//! Simplification of std insertion
|
|
|
|
template <typename Tdst, typename Tsrc>
|
|
|
|
inline void insert(Tdst& dst, const Tsrc& src) {
|
|
|
|
dst.insert(dst.begin(), src.begin(), src.end());
|
|
|
|
}
|
|
|
|
template <typename TsetT, typename Tsrc>
|
|
|
|
inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
|
|
|
|
dst.insert(src.begin(), src.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UTIL_H
|