LCOV - code coverage report
Current view: top level - src/common - system.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 14 27 51.9 %
Date: 2023-09-26 12:08:55 Functions: 6 8 75.0 %

          Line data    Source code
       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         173 : const int64_t nStartupTime = GetTime();
      30             : 
      31             : #ifndef WIN32
      32           0 : std::string ShellEscape(const std::string& arg)
      33             : {
      34           0 :     std::string escaped = arg;
      35           0 :     ReplaceAll(escaped, "'", "'\"'\"'");
      36           0 :     return "'" + escaped + "'";
      37           0 : }
      38             : #endif
      39             : 
      40             : #if HAVE_SYSTEM
      41           0 : void runCommand(const std::string& strCommand)
      42             : {
      43           0 :     if (strCommand.empty()) return;
      44             : #ifndef WIN32
      45           0 :     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           0 :     if (nErr)
      50           0 :         LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
      51           0 : }
      52             : #endif
      53             : 
      54          69 : void SetupEnvironment()
      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          69 :         std::locale(""); // Raises a runtime error if current locale is invalid
      71          69 :     } catch (const std::runtime_error&) {
      72           0 :         setenv("LC_ALL", "C.UTF-8", 1);
      73           0 :     }
      74         173 : #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          69 :     constexpr mode_t private_umask = 0077;
      82          69 :     umask(private_umask);
      83             : #endif
      84          69 : }
      85             : 
      86          69 : bool SetupNetworking()
      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          69 :     return true;
      96             : }
      97             : 
      98          69 : int GetNumCores()
      99             : {
     100          69 :     return std::thread::hardware_concurrency();
     101             : }
     102             : 
     103             : // Obtain the application startup time (used for uptime calculation)
     104           3 : int64_t GetStartupTime()
     105             : {
     106           3 :     return nStartupTime;
     107             : }

Generated by: LCOV version 1.14