2011-05-14 17:25:05 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-10-16 14:18:28 +02:00
|
|
|
// Copyright (c) 2009-present The Bitcoin Core developers
|
2014-11-17 11:04:01 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 22:02:28 +08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-03-31 15:22:45 +02:00
|
|
|
|
2024-02-26 13:36:30 +01:00
|
|
|
#include <config/bitcoin-config.h> // IWYU pragma: keep
|
2024-02-13 08:03:02 +01:00
|
|
|
|
2023-05-08 11:32:13 +02:00
|
|
|
#include <common/system.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <logging.h>
|
2019-12-11 09:55:00 +00:00
|
|
|
#include <util/string.h>
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <util/time.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2013-01-28 00:07:51 +01:00
|
|
|
#ifndef WIN32
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#else
|
2023-10-16 14:18:28 +02:00
|
|
|
#include <compat/compat.h>
|
2018-08-06 01:03:33 +00:00
|
|
|
#include <codecvt>
|
2012-04-15 22:10:54 +02:00
|
|
|
#endif
|
2011-05-14 17:25:05 +02:00
|
|
|
|
2017-03-30 09:37:47 +02:00
|
|
|
#ifdef HAVE_MALLOPT_ARENA_MAX
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <locale>
|
|
|
|
#include <stdexcept>
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <string>
|
2017-04-25 09:34:23 +08:00
|
|
|
#include <thread>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2023-12-06 15:13:39 -05:00
|
|
|
using util::ReplaceAll;
|
|
|
|
|
2017-05-14 19:18:26 +01:00
|
|
|
// Application startup time (used for uptime calculation)
|
|
|
|
const int64_t nStartupTime = GetTime();
|
2011-05-14 17:25:05 +02:00
|
|
|
|
2018-05-29 14:37:53 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
std::string ShellEscape(const std::string& arg)
|
|
|
|
{
|
|
|
|
std::string escaped = arg;
|
2022-05-05 08:28:29 +02:00
|
|
|
ReplaceAll(escaped, "'", "'\"'\"'");
|
2018-05-29 14:37:53 +01:00
|
|
|
return "'" + escaped + "'";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-05 18:30:15 +02:00
|
|
|
#if HAVE_SYSTEM
|
2015-05-31 15:36:44 +02:00
|
|
|
void runCommand(const std::string& strCommand)
|
2012-05-23 23:10:59 -04:00
|
|
|
{
|
2017-07-27 11:35:01 +02:00
|
|
|
if (strCommand.empty()) return;
|
2018-08-06 01:03:33 +00:00
|
|
|
#ifndef WIN32
|
2012-05-23 23:10:59 -04:00
|
|
|
int nErr = ::system(strCommand.c_str());
|
2018-08-06 01:03:33 +00:00
|
|
|
#else
|
|
|
|
int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
|
|
|
|
#endif
|
2012-05-23 23:10:59 -04:00
|
|
|
if (nErr)
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
|
2012-05-23 23:10:59 -04:00
|
|
|
}
|
2019-03-14 11:30:37 +01:00
|
|
|
#endif
|
2012-05-23 23:10:59 -04:00
|
|
|
|
2014-05-13 10:15:00 +00:00
|
|
|
void SetupEnvironment()
|
|
|
|
{
|
2017-03-30 09:37:47 +02:00
|
|
|
#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
|
2015-03-11 13:34:20 +01:00
|
|
|
// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
|
2019-10-09 14:53:18 +02:00
|
|
|
// may be invalid, in which case the "C.UTF-8" locale is used as fallback.
|
2021-07-02 11:10:25 +08:00
|
|
|
#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
|
2015-03-11 13:34:20 +01:00
|
|
|
try {
|
2015-05-02 10:10:31 +02:00
|
|
|
std::locale(""); // Raises a runtime error if current locale is invalid
|
2014-12-07 13:29:06 +01:00
|
|
|
} catch (const std::runtime_error&) {
|
2019-10-09 14:53:18 +02:00
|
|
|
setenv("LC_ALL", "C.UTF-8", 1);
|
2014-05-13 10:15:00 +00:00
|
|
|
}
|
2018-08-05 16:38:25 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
// Set the default input/output charset is utf-8
|
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2018-08-04 16:39:14 +00:00
|
|
|
#endif
|
2023-02-06 11:08:03 +00:00
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
constexpr mode_t private_umask = 0077;
|
|
|
|
umask(private_umask);
|
|
|
|
#endif
|
2014-05-13 10:15:00 +00:00
|
|
|
}
|
2014-05-08 18:01:10 +02:00
|
|
|
|
2015-09-02 16:18:16 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:38:15 +02:00
|
|
|
int GetNumCores()
|
|
|
|
{
|
2017-04-25 09:34:23 +08:00
|
|
|
return std::thread::hardware_concurrency();
|
2015-07-01 17:38:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-14 19:18:26 +01:00
|
|
|
// Obtain the application startup time (used for uptime calculation)
|
|
|
|
int64_t GetStartupTime()
|
|
|
|
{
|
|
|
|
return nStartupTime;
|
|
|
|
}
|