mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-09 22:28:51 +02:00
Merge bitcoin/bitcoin#27636: kernel: Remove util/system from kernel library, interface_ui from validation.
7d3b35004brefactor: Move system from util to common library (TheCharlatan)7eee356c0arefactor: Split util::AnyPtr into its own file (TheCharlatan)44de325d95refactor: Split util::insert into its own file (TheCharlatan)9ec5da36b6refactor: Move ScheduleBatchPriority to its own file (TheCharlatan)f871c69191kernel: Add warning method to notifications (TheCharlatan)4452707edekernel: Add progress method to notifications (TheCharlatan)84d71457e7kernel: Add headerTip method to notifications (TheCharlatan)447761c822kernel: Add notification interface (TheCharlatan) Pull request description: This pull request is part of the `libbitcoinkernel` project https://github.com/bitcoin/bitcoin/issues/27587 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". --- It removes the kernel library's dependency on `util/system` and `interface_ui`. `util/system` contains networking and shell-related code that should not be part of the kernel library. The following pull requests prepared `util/system` for this final step: https://github.com/bitcoin/bitcoin/pull/27419 https://github.com/bitcoin/bitcoin/pull/27254 https://github.com/bitcoin/bitcoin/pull/27238. `interface_ui` defines functions for a more general node interface and has a dependency on `boost/signals2`. After applying the patches from this pull request, the kernel's reliance on boost is down to `boost::multiindex`. The approach implemented here introduces some indirection, which makes the code a bit harder to read. Any suggestions for improving or reworking this pull request to make it more concise, or even reworking it into a more proper interface, are appreciated. ACKs for top commit: MarcoFalke: re-ACK7d3b35004b(no change) 🎋 stickies-v: Code Review ACK7d3b35004bhebasto: re-ACK7d3b35004b, only last two commits dropped since my [recent](https://github.com/bitcoin/bitcoin/pull/27636#pullrequestreview-1435394620) review. Tree-SHA512: c8cfc698dc9d78e20191c444708f2d957501229abe95e5806106d1126fb9c5fbcee686fb55645658c0107ce71f10646f37a2fdf7fde16bbf22cbf1ac885dd08d
This commit is contained in:
107
src/common/system.cpp
Normal file
107
src/common/system.cpp
Normal file
@@ -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 <common/system.h>
|
||||
|
||||
#include <logging.h>
|
||||
#include <util/string.h>
|
||||
#include <util/time.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <sys/stat.h>
|
||||
#else
|
||||
#include <codecvt>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MALLOPT_ARENA_MAX
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include <locale>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
// 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<std::codecvt_utf8_utf16<wchar_t>,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;
|
||||
}
|
||||
38
src/common/system.h
Normal file
38
src/common/system.h
Normal file
@@ -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 <config/bitcoin-config.h>
|
||||
#endif
|
||||
|
||||
#include <compat/assumptions.h>
|
||||
#include <compat/compat.h>
|
||||
|
||||
#include <set>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
// 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
|
||||
Reference in New Issue
Block a user