LCOV - code coverage report
Current view: top level - src/common - system.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 42.3 % 26 11
Test Date: 2024-08-28 04:44:32 Functions: 50.0 % 6 3
Branches: 6.2 % 16 1

             Branch data     Line data    Source code
       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 <config/bitcoin-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                 :           0 : std::string ShellEscape(const std::string& arg)
      38                 :             : {
      39                 :           0 :     std::string escaped = arg;
      40   [ #  #  #  #  :           0 :     ReplaceAll(escaped, "'", "'\"'\"'");
                   #  # ]
      41         [ #  # ]:           0 :     return "'" + escaped + "'";
      42                 :           0 : }
      43                 :             : #endif
      44                 :             : 
      45                 :             : #if HAVE_SYSTEM
      46                 :           0 : void runCommand(const std::string& strCommand)
      47                 :             : {
      48         [ #  # ]:           0 :     if (strCommand.empty()) return;
      49                 :             : #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                 :             : }
      57                 :             : #endif
      58                 :             : 
      59                 :         686 : void SetupEnvironment()
      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                 :         686 :     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                 :         686 :     try {
      75         [ +  - ]:         686 :         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                 :             : #ifndef WIN32
      86                 :         686 :     constexpr mode_t private_umask = 0077;
      87                 :         686 :     umask(private_umask);
      88                 :             : #endif
      89                 :         686 : }
      90                 :             : 
      91                 :         128 : bool SetupNetworking()
      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                 :         128 :     return true;
     101                 :             : }
     102                 :             : 
     103                 :         593 : int GetNumCores()
     104                 :             : {
     105                 :         593 :     return std::thread::hardware_concurrency();
     106                 :             : }
     107                 :             : 
     108                 :             : // Obtain the application startup time (used for uptime calculation)
     109                 :           0 : int64_t GetStartupTime()
     110                 :             : {
     111                 :           0 :     return nStartupTime;
     112                 :             : }
        

Generated by: LCOV version 2.0-1