/root/bitcoin/src/common/system.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
31 | | using util::ReplaceAll; |
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 | 0 | { |
39 | 0 | std::string escaped = arg; |
40 | 0 | ReplaceAll(escaped, "'", "'\"'\"'"); |
41 | 0 | return "'" + escaped + "'"; |
42 | 0 | } |
43 | | #endif |
44 | | |
45 | | #if HAVE_SYSTEM |
46 | | void runCommand(const std::string& strCommand) |
47 | 0 | { |
48 | 0 | if (strCommand.empty()) return; |
49 | 0 | #ifndef WIN32 |
50 | 0 | 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 | 0 | if (nErr) |
55 | 0 | LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr); |
56 | 0 | } |
57 | | #endif |
58 | | |
59 | | void SetupEnvironment() |
60 | 0 | { |
61 | 0 | #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 | 0 | if (sizeof(void*) == 4) { |
68 | 0 | mallopt(M_ARENA_MAX, 1); |
69 | 0 | } |
70 | 0 | #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 | 0 | #if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) |
74 | 0 | try { |
75 | 0 | std::locale(""); // Raises a runtime error if current locale is invalid |
76 | 0 | } catch (const std::runtime_error&) { |
77 | 0 | setenv("LC_ALL", "C.UTF-8", 1); |
78 | 0 | } |
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 | 0 | #ifndef WIN32 |
86 | 0 | constexpr mode_t private_umask = 0077; |
87 | 0 | umask(private_umask); |
88 | 0 | #endif |
89 | 0 | } |
90 | | |
91 | | bool SetupNetworking() |
92 | 0 | { |
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 | 0 | return true; |
101 | 0 | } |
102 | | |
103 | | int GetNumCores() |
104 | 0 | { |
105 | 0 | return std::thread::hardware_concurrency(); |
106 | 0 | } |
107 | | |
108 | | // Obtain the application startup time (used for uptime calculation) |
109 | | int64_t GetStartupTime() |
110 | 0 | { |
111 | 0 | return nStartupTime; |
112 | 0 | } |