Bitcoin Core  25.99.0
P2P Digital Currency
system.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <common/system.h>
7 
8 #include <logging.h>
9 #include <util/string.h>
10 #include <util/time.h>
11 
12 #ifndef WIN32
13 #include <sys/stat.h>
14 #else
15 #include <codecvt>
16 #endif
17 
18 #ifdef HAVE_MALLOPT_ARENA_MAX
19 #include <malloc.h>
20 #endif
21 
22 #include <cstdlib>
23 #include <locale>
24 #include <stdexcept>
25 #include <string>
26 #include <thread>
27 
28 // Application startup time (used for uptime calculation)
29 const int64_t nStartupTime = GetTime();
30 
31 #ifndef WIN32
32 std::string ShellEscape(const std::string& arg)
33 {
34  std::string escaped = arg;
35  ReplaceAll(escaped, "'", "'\"'\"'");
36  return "'" + escaped + "'";
37 }
38 #endif
39 
40 #if HAVE_SYSTEM
41 void runCommand(const std::string& strCommand)
42 {
43  if (strCommand.empty()) return;
44 #ifndef WIN32
45  int nErr = ::system(strCommand.c_str());
46 #else
47  int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
48 #endif
49  if (nErr)
50  LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
51 }
52 #endif
53 
55 {
56 #ifdef HAVE_MALLOPT_ARENA_MAX
57  // glibc-specific: On 32-bit systems set the number of arenas to 1.
58  // By default, since glibc 2.10, the C library will create up to two heap
59  // arenas per core. This is known to cause excessive virtual address space
60  // usage in our usage. Work around it by setting the maximum number of
61  // arenas to 1.
62  if (sizeof(void*) == 4) {
63  mallopt(M_ARENA_MAX, 1);
64  }
65 #endif
66  // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
67  // may be invalid, in which case the "C.UTF-8" locale is used as fallback.
68 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
69  try {
70  std::locale(""); // Raises a runtime error if current locale is invalid
71  } catch (const std::runtime_error&) {
72  setenv("LC_ALL", "C.UTF-8", 1);
73  }
74 #elif defined(WIN32)
75  // Set the default input/output charset is utf-8
76  SetConsoleCP(CP_UTF8);
77  SetConsoleOutputCP(CP_UTF8);
78 #endif
79 
80 #ifndef WIN32
81  constexpr mode_t private_umask = 0077;
82  umask(private_umask);
83 #endif
84 }
85 
87 {
88 #ifdef WIN32
89  // Initialize Windows Sockets
90  WSADATA wsadata;
91  int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
92  if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
93  return false;
94 #endif
95  return true;
96 }
97 
99 {
100  return std::thread::hardware_concurrency();
101 }
102 
103 // Obtain the application startup time (used for uptime calculation)
104 int64_t GetStartupTime()
105 {
106  return nStartupTime;
107 }
int ret
int64_t GetStartupTime()
Definition: system.cpp:104
bool SetupNetworking()
Definition: system.cpp:86
void SetupEnvironment()
Definition: system.cpp:54
const int64_t nStartupTime
Definition: system.cpp:29
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:98
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:32
#define LogPrintf(...)
Definition: logging.h:237
int64_t GetTime()
Definition: time.cpp:97
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10