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/common/system.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/system.h | 38 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/common/system.cpp create mode 100644 src/common/system.h (limited to 'src/common') 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 -- cgit v1.2.3