Bitcoin Core 28.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#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8#include <common/system.h>
9
10#include <logging.h>
11#include <util/string.h>
12#include <util/time.h>
13
14#ifndef WIN32
15#include <sys/stat.h>
16#else
17#include <compat/compat.h>
18#include <codecvt>
19#endif
20
21#ifdef HAVE_MALLOPT_ARENA_MAX
22#include <malloc.h>
23#endif
24
25#include <cstdlib>
26#include <locale>
27#include <stdexcept>
28#include <string>
29#include <thread>
30
32
33// Application startup time (used for uptime calculation)
34const int64_t nStartupTime = GetTime();
35
36#ifndef WIN32
37std::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
46void 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(__APPLE__) && !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)
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:266
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:11
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:47