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