From 7d3b35004b039f2bd606bb46a540de7babdbc41e Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Mon, 8 May 2023 11:32:13 +0200 Subject: refactor: Move system from util to common library Since the kernel library no longer depends on the system file, move it to the common library instead in accordance to the diagram in doc/design/libraries.md. --- src/Makefile.am | 4 +- src/banman.cpp | 2 +- src/bench/checkqueue.cpp | 2 +- src/bitcoin-cli.cpp | 2 +- src/bitcoin-tx.cpp | 2 +- src/bitcoin-util.cpp | 2 +- src/bitcoin-wallet.cpp | 2 +- src/bitcoind.cpp | 2 +- src/blockencodings.cpp | 4 +- src/common/system.cpp | 107 +++++++++++++++++++++++++ src/common/system.h | 38 +++++++++ src/core_write.cpp | 2 +- src/external_signer.h | 2 +- src/i2p.cpp | 2 +- src/init.cpp | 2 +- src/ipc/interfaces.cpp | 2 +- src/mapport.cpp | 2 +- src/net_permissions.cpp | 2 +- src/node/blockstorage.cpp | 1 - src/node/kernel_notifications.cpp | 2 +- src/node/txreconciliation.cpp | 2 +- src/policy/fees.cpp | 2 +- src/protocol.cpp | 2 +- src/qt/bitcoin.cpp | 2 +- src/qt/bitcoingui.cpp | 5 +- src/qt/clientmodel.cpp | 2 +- src/qt/optionsdialog.cpp | 6 +- src/qt/rpcconsole.cpp | 2 +- src/qt/splashscreen.cpp | 2 +- src/qt/test/rpcnestedtests.cpp | 4 +- src/qt/transactiondesc.cpp | 2 +- src/rpc/external_signer.cpp | 2 +- src/rpc/mining.cpp | 2 +- src/rpc/request.cpp | 2 +- src/rpc/server.cpp | 2 +- src/script/sigcache.cpp | 2 +- src/signet.cpp | 2 +- src/test/allocator_tests.cpp | 2 +- src/test/bloom_tests.cpp | 2 +- src/test/fuzz/integer.cpp | 2 +- src/test/fuzz/string.cpp | 2 +- src/test/fuzz/system.cpp | 4 +- src/test/key_tests.cpp | 2 +- src/test/mempool_tests.cpp | 2 +- src/test/miner_tests.cpp | 2 +- src/test/miniminer_tests.cpp | 1 - src/test/rbf_tests.cpp | 2 +- src/test/script_tests.cpp | 2 +- src/test/sighash_tests.cpp | 2 +- src/test/sock_tests.cpp | 2 +- src/test/util/setup_common.cpp | 2 +- src/test/util_tests.cpp | 2 +- src/txmempool.cpp | 2 +- src/util/sock.cpp | 2 +- src/util/system.cpp | 107 ------------------------- src/util/system.h | 38 --------- src/wallet/bdb.h | 2 +- src/wallet/coinselection.cpp | 2 +- src/wallet/crypter.cpp | 2 +- src/wallet/external_signer_scriptpubkeyman.cpp | 2 +- src/wallet/feebumper.cpp | 2 +- src/wallet/spend.cpp | 2 +- src/wallet/walletdb.cpp | 2 +- src/warnings.cpp | 2 +- 64 files changed, 211 insertions(+), 212 deletions(-) create mode 100644 src/common/system.cpp create mode 100644 src/common/system.h delete mode 100644 src/util/system.cpp delete mode 100644 src/util/system.h diff --git a/src/Makefile.am b/src/Makefile.am index e2c7af8c58..0b25ef9c6b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -143,6 +143,7 @@ BITCOIN_CORE_H = \ compat/compat.h \ compat/cpuid.h \ compat/endian.h \ + common/system.h \ compressor.h \ consensus/consensus.h \ consensus/tx_check.h \ @@ -314,7 +315,6 @@ BITCOIN_CORE_H = \ util/string.h \ util/syscall_sandbox.h \ util/syserror.h \ - util/system.h \ util/thread.h \ util/threadinterrupt.h \ util/threadnames.h \ @@ -663,6 +663,7 @@ libbitcoin_common_a_SOURCES = \ common/init.cpp \ common/interfaces.cpp \ common/run_command.cpp \ + common/system.cpp \ compressor.cpp \ core_read.cpp \ core_write.cpp \ @@ -728,7 +729,6 @@ libbitcoin_util_a_SOURCES = \ util/hasher.cpp \ util/sock.cpp \ util/syserror.cpp \ - util/system.cpp \ util/message.cpp \ util/moneystr.cpp \ util/rbf.cpp \ diff --git a/src/banman.cpp b/src/banman.cpp index 5b2049d654..a96b7e3c53 100644 --- a/src/banman.cpp +++ b/src/banman.cpp @@ -5,11 +5,11 @@ #include +#include #include #include #include #include -#include #include #include diff --git a/src/bench/checkqueue.cpp b/src/bench/checkqueue.cpp index 8ad6fde6bf..70e0b86eba 100644 --- a/src/bench/checkqueue.cpp +++ b/src/bench/checkqueue.cpp @@ -4,11 +4,11 @@ #include #include +#include #include #include #include #include -#include #include diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 48dc11b95a..45db7a9a66 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +24,6 @@ #include #include #include -#include #include #include diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index e291f20a11..0c25ddf373 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -27,7 +28,6 @@ #include #include #include -#include #include #include diff --git a/src/bitcoin-util.cpp b/src/bitcoin-util.cpp index f62a9f7bbf..582c18c9c6 100644 --- a/src/bitcoin-util.cpp +++ b/src/bitcoin-util.cpp @@ -12,11 +12,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include diff --git a/src/bitcoin-wallet.cpp b/src/bitcoin-wallet.cpp index 1863173aa8..d5dfbbec27 100644 --- a/src/bitcoin-wallet.cpp +++ b/src/bitcoin-wallet.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -18,7 +19,6 @@ #include #include #include -#include #include #include diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index e476b06017..aefb130e9c 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index a29e4f794e..9aa0a6ba20 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -3,16 +3,16 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include +#include #include #include -#include #include #include #include #include #include #include -#include #include diff --git a/src/common/system.cpp b/src/common/system.cpp new file mode 100644 index 0000000000..1d1c5fa56a --- /dev/null +++ b/src/common/system.cpp @@ -0,0 +1,107 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +#ifndef WIN32 +#include +#else +#include +#endif + +#ifdef HAVE_MALLOPT_ARENA_MAX +#include +#endif + +#include +#include +#include +#include +#include + +// Application startup time (used for uptime calculation) +const int64_t nStartupTime = GetTime(); + +#ifndef WIN32 +std::string ShellEscape(const std::string& arg) +{ + std::string escaped = arg; + ReplaceAll(escaped, "'", "'\"'\"'"); + return "'" + escaped + "'"; +} +#endif + +#if HAVE_SYSTEM +void runCommand(const std::string& strCommand) +{ + if (strCommand.empty()) return; +#ifndef WIN32 + int nErr = ::system(strCommand.c_str()); +#else + int nErr = ::_wsystem(std::wstring_convert,wchar_t>().from_bytes(strCommand).c_str()); +#endif + if (nErr) + LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr); +} +#endif + +void SetupEnvironment() +{ +#ifdef HAVE_MALLOPT_ARENA_MAX + // glibc-specific: On 32-bit systems set the number of arenas to 1. + // By default, since glibc 2.10, the C library will create up to two heap + // arenas per core. This is known to cause excessive virtual address space + // usage in our usage. Work around it by setting the maximum number of + // arenas to 1. + if (sizeof(void*) == 4) { + mallopt(M_ARENA_MAX, 1); + } +#endif + // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale + // may be invalid, in which case the "C.UTF-8" locale is used as fallback. +#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) + try { + std::locale(""); // Raises a runtime error if current locale is invalid + } catch (const std::runtime_error&) { + setenv("LC_ALL", "C.UTF-8", 1); + } +#elif defined(WIN32) + // Set the default input/output charset is utf-8 + SetConsoleCP(CP_UTF8); + SetConsoleOutputCP(CP_UTF8); +#endif + +#ifndef WIN32 + constexpr mode_t private_umask = 0077; + umask(private_umask); +#endif +} + +bool SetupNetworking() +{ +#ifdef WIN32 + // Initialize Windows Sockets + WSADATA wsadata; + int ret = WSAStartup(MAKEWORD(2,2), &wsadata); + if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2) + return false; +#endif + return true; +} + +int GetNumCores() +{ + return std::thread::hardware_concurrency(); +} + +// Obtain the application startup time (used for uptime calculation) +int64_t GetStartupTime() +{ + return nStartupTime; +} diff --git a/src/common/system.h b/src/common/system.h new file mode 100644 index 0000000000..40206aaa01 --- /dev/null +++ b/src/common/system.h @@ -0,0 +1,38 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_COMMON_SYSTEM_H +#define BITCOIN_COMMON_SYSTEM_H + +#if defined(HAVE_CONFIG_H) +#include +#endif + +#include +#include + +#include +#include +#include + +// Application startup time (used for uptime calculation) +int64_t GetStartupTime(); + +void SetupEnvironment(); +bool SetupNetworking(); +#ifndef WIN32 +std::string ShellEscape(const std::string& arg); +#endif +#if HAVE_SYSTEM +void runCommand(const std::string& strCommand); +#endif + +/** + * Return the number of cores available on the current system. + * @note This does count virtual cores, such as those provided by HyperThreading. + */ +int GetNumCores(); + +#endif // BITCOIN_COMMON_SYSTEM_H diff --git a/src/core_write.cpp b/src/core_write.cpp index b0e3b0b3c4..54ca306f60 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -17,7 +18,6 @@ #include #include #include -#include #include #include diff --git a/src/external_signer.h b/src/external_signer.h index 90f07478e3..81a601811a 100644 --- a/src/external_signer.h +++ b/src/external_signer.h @@ -5,8 +5,8 @@ #ifndef BITCOIN_EXTERNAL_SIGNER_H #define BITCOIN_EXTERNAL_SIGNER_H +#include #include -#include #include #include diff --git a/src/i2p.cpp b/src/i2p.cpp index c67b6ed185..f03e375adf 100644 --- a/src/i2p.cpp +++ b/src/i2p.cpp @@ -336,7 +336,7 @@ void Session::GenerateAndSavePrivateKey(const Sock& sock) { DestGenerate(sock); - // umask is set to 0077 in util/system.cpp, which is ok. + // umask is set to 0077 in common/system.cpp, which is ok. if (!WriteBinaryFile(m_private_key_file, std::string(m_private_key.begin(), m_private_key.end()))) { throw std::runtime_error( diff --git a/src/init.cpp b/src/init.cpp index 48492eab6b..bb14d3dbfd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -80,7 +81,6 @@ #include #include #include -#include #include #include #include diff --git a/src/ipc/interfaces.cpp b/src/ipc/interfaces.cpp index d804d9d291..e446cc98db 100644 --- a/src/ipc/interfaces.cpp +++ b/src/ipc/interfaces.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -10,7 +11,6 @@ #include #include #include -#include #include #include diff --git a/src/mapport.cpp b/src/mapport.cpp index 994fd12cf5..76f8a4d0c9 100644 --- a/src/mapport.cpp +++ b/src/mapport.cpp @@ -9,12 +9,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include diff --git a/src/net_permissions.cpp b/src/net_permissions.cpp index f829e56aa2..1e4ec5b529 100644 --- a/src/net_permissions.cpp +++ b/src/net_permissions.cpp @@ -2,10 +2,10 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include -#include #include const std::vector NET_PERMISSIONS_DOC{ diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index a97f837e28..b7afa8a7c3 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/src/node/kernel_notifications.cpp b/src/node/kernel_notifications.cpp index 2bc4544aee..926b157f3b 100644 --- a/src/node/kernel_notifications.cpp +++ b/src/node/kernel_notifications.cpp @@ -9,10 +9,10 @@ #endif #include +#include #include #include #include -#include #include #include diff --git a/src/node/txreconciliation.cpp b/src/node/txreconciliation.cpp index 9938759074..d62046daaa 100644 --- a/src/node/txreconciliation.cpp +++ b/src/node/txreconciliation.cpp @@ -4,9 +4,9 @@ #include +#include #include #include -#include #include #include diff --git a/src/policy/fees.cpp b/src/policy/fees.cpp index 6121224979..ae226f7011 100644 --- a/src/policy/fees.cpp +++ b/src/policy/fees.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -19,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/src/protocol.cpp b/src/protocol.cpp index 5725813b7e..5ecaabec36 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -5,7 +5,7 @@ #include -#include +#include #include diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index d602d2c1ac..e33753f5e7 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index d26ef52eb4..f201d8fa01 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -30,16 +30,17 @@ #include #endif -#include #include #include +#include #include #include #include -#include #include #include +#include + #include #include #include diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index b22f1bc35c..ff7405d139 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -12,11 +12,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 6dec4b2e42..ea624efeaa 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -15,11 +15,11 @@ #include #include +#include #include -#include // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS #include -#include // for -dbcache defaults -#include +#include +#include #include diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index bac64e3d5f..90aae0219e 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include #include #include diff --git a/src/qt/splashscreen.cpp b/src/qt/splashscreen.cpp index 096f8a0ded..8872f8be32 100644 --- a/src/qt/splashscreen.cpp +++ b/src/qt/splashscreen.cpp @@ -9,13 +9,13 @@ #include #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/src/qt/test/rpcnestedtests.cpp b/src/qt/test/rpcnestedtests.cpp index 669a05fe0f..72e8055425 100644 --- a/src/qt/test/rpcnestedtests.cpp +++ b/src/qt/test/rpcnestedtests.cpp @@ -4,12 +4,12 @@ #include +#include #include -#include #include +#include #include #include -#include #include diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index 2461a06930..fa110cfbc9 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -13,12 +13,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include diff --git a/src/rpc/external_signer.cpp b/src/rpc/external_signer.cpp index ac135ba216..310eec5f15 100644 --- a/src/rpc/external_signer.cpp +++ b/src/rpc/external_signer.cpp @@ -3,12 +3,12 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include #include #include #include -#include #include #include diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 68017e5af2..eb61d58a33 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index cf1b6cd92b..4c67da8b70 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -88,7 +88,7 @@ bool GenerateAuthCookie(std::string *cookie_out) std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd); /** the umask determines what permissions are used to create this file - - * these are set to 0077 in util/system.cpp. + * these are set to 0077 in common/system.cpp. */ std::ofstream file; fs::path filepath_tmp = GetAuthCookieFile(true); diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 354f949002..474b318c66 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -6,13 +6,13 @@ #include #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/src/script/sigcache.cpp b/src/script/sigcache.cpp index e52e8cd309..7c6c282cc4 100644 --- a/src/script/sigcache.cpp +++ b/src/script/sigcache.cpp @@ -5,11 +5,11 @@ #include