1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
8 #include <chainparamsbase.h>
10 #include <serialize.h>
11 #include <utilstrencodings.h>
15 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
17 #include <pthread_np.h>
21 // for posix_fallocate
24 #ifdef _POSIX_C_SOURCE
25 #undef _POSIX_C_SOURCE
28 #define _POSIX_C_SOURCE 200112L
34 #include <sys/resource.h>
40 #pragma warning(disable:4786)
41 #pragma warning(disable:4804)
42 #pragma warning(disable:4805)
43 #pragma warning(disable:4717)
49 #define _WIN32_WINNT 0x0501
54 #define _WIN32_IE 0x0501
56 #define WIN32_LEAN_AND_MEAN 1
61 #include <io.h> /* for _commit */
65 #ifdef HAVE_SYS_PRCTL_H
66 #include <sys/prctl.h>
69 #ifdef HAVE_MALLOPT_ARENA_MAX
73 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
74 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
75 #include <boost/program_options/detail/config_file.hpp>
76 #include <boost/thread.hpp>
77 #include <openssl/crypto.h>
78 #include <openssl/rand.h>
79 #include <openssl/conf.h>
81 // Application startup time (used for uptime calculation)
82 const int64_t nStartupTime
= GetTime();
84 const char * const BITCOIN_CONF_FILENAME
= "bitcoin.conf";
85 const char * const BITCOIN_PID_FILENAME
= "bitcoind.pid";
86 const char * const DEFAULT_DEBUGLOGFILE
= "debug.log";
89 bool fPrintToConsole
= false;
90 bool fPrintToDebugLog
= true;
92 bool fLogTimestamps
= DEFAULT_LOGTIMESTAMPS
;
93 bool fLogTimeMicros
= DEFAULT_LOGTIMEMICROS
;
94 bool fLogIPs
= DEFAULT_LOGIPS
;
95 std::atomic
<bool> fReopenDebugLog(false);
96 CTranslationInterface translationInterface
;
98 /** Log categories bitfield. */
99 std::atomic
<uint32_t> logCategories(0);
101 /** Init OpenSSL library multithreading support */
102 static std::unique_ptr
<CCriticalSection
[]> ppmutexOpenSSL
;
103 void locking_callback(int mode
, int i
, const char* file
, int line
) NO_THREAD_SAFETY_ANALYSIS
105 if (mode
& CRYPTO_LOCK
) {
106 ENTER_CRITICAL_SECTION(ppmutexOpenSSL
[i
]);
108 LEAVE_CRITICAL_SECTION(ppmutexOpenSSL
[i
]);
112 // Singleton for wrapping OpenSSL setup/teardown.
118 // Init OpenSSL library multithreading support
119 ppmutexOpenSSL
.reset(new CCriticalSection
[CRYPTO_num_locks()]);
120 CRYPTO_set_locking_callback(locking_callback
);
122 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
123 // We don't use them so we don't require the config. However some of our libs may call functions
124 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
125 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
126 // that the config appears to have been loaded and there are no modules/engines available.
130 // Seed OpenSSL PRNG with current contents of the screen
134 // Seed OpenSSL PRNG with performance counter
139 // Securely erase the memory used by the PRNG
141 // Shutdown OpenSSL library multithreading support
142 CRYPTO_set_locking_callback(nullptr);
143 // Clear the set of locks now to maintain symmetry with the constructor.
144 ppmutexOpenSSL
.reset();
150 * LogPrintf() has been broken a couple of times now
151 * by well-meaning people adding mutexes in the most straightforward way.
152 * It breaks because it may be called by global destructors during shutdown.
153 * Since the order of destruction of static/global objects is undefined,
154 * defining a mutex as a global object doesn't work (the mutex gets
155 * destroyed, and then some later destructor calls OutputDebugStringF,
156 * maybe indirectly, and you get a core dump at shutdown trying to lock
160 static boost::once_flag debugPrintInitFlag
= BOOST_ONCE_INIT
;
163 * We use boost::call_once() to make sure mutexDebugLog and
164 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
166 * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
167 * are leaked on exit. This is ugly, but will be cleaned up by
168 * the OS/libc. When the shutdown sequence is fully audited and
169 * tested, explicit destruction of these objects can be implemented.
171 static FILE* fileout
= nullptr;
172 static boost::mutex
* mutexDebugLog
= nullptr;
173 static std::list
<std::string
>* vMsgsBeforeOpenLog
;
175 static int FileWriteStr(const std::string
&str
, FILE *fp
)
177 return fwrite(str
.data(), 1, str
.size(), fp
);
180 static void DebugPrintInit()
182 assert(mutexDebugLog
== nullptr);
183 mutexDebugLog
= new boost::mutex();
184 vMsgsBeforeOpenLog
= new std::list
<std::string
>;
187 fs::path
GetDebugLogPath()
189 fs::path
logfile(gArgs
.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE
));
190 if (logfile
.is_absolute()) {
193 return GetDataDir() / logfile
;
199 boost::call_once(&DebugPrintInit
, debugPrintInitFlag
);
200 boost::mutex::scoped_lock
scoped_lock(*mutexDebugLog
);
202 assert(fileout
== nullptr);
203 assert(vMsgsBeforeOpenLog
);
204 fs::path pathDebug
= GetDebugLogPath();
206 fileout
= fsbridge::fopen(pathDebug
, "a");
211 setbuf(fileout
, nullptr); // unbuffered
212 // dump buffered messages from before we opened the log
213 while (!vMsgsBeforeOpenLog
->empty()) {
214 FileWriteStr(vMsgsBeforeOpenLog
->front(), fileout
);
215 vMsgsBeforeOpenLog
->pop_front();
218 delete vMsgsBeforeOpenLog
;
219 vMsgsBeforeOpenLog
= nullptr;
223 struct CLogCategoryDesc
226 std::string category
;
229 const CLogCategoryDesc LogCategories
[] =
232 {BCLog::NONE
, "none"},
235 {BCLog::MEMPOOL
, "mempool"},
236 {BCLog::HTTP
, "http"},
237 {BCLog::BENCH
, "bench"},
241 {BCLog::ESTIMATEFEE
, "estimatefee"},
242 {BCLog::ADDRMAN
, "addrman"},
243 {BCLog::SELECTCOINS
, "selectcoins"},
244 {BCLog::REINDEX
, "reindex"},
245 {BCLog::CMPCTBLOCK
, "cmpctblock"},
246 {BCLog::RAND
, "rand"},
247 {BCLog::PRUNE
, "prune"},
248 {BCLog::PROXY
, "proxy"},
249 {BCLog::MEMPOOLREJ
, "mempoolrej"},
250 {BCLog::LIBEVENT
, "libevent"},
251 {BCLog::COINDB
, "coindb"},
253 {BCLog::LEVELDB
, "leveldb"},
258 bool GetLogCategory(uint32_t *f
, const std::string
*str
)
265 for (unsigned int i
= 0; i
< ARRAYLEN(LogCategories
); i
++) {
266 if (LogCategories
[i
].category
== *str
) {
267 *f
= LogCategories
[i
].flag
;
275 std::string
ListLogCategories()
279 for (unsigned int i
= 0; i
< ARRAYLEN(LogCategories
); i
++) {
280 // Omit the special cases.
281 if (LogCategories
[i
].flag
!= BCLog::NONE
&& LogCategories
[i
].flag
!= BCLog::ALL
) {
282 if (outcount
!= 0) ret
+= ", ";
283 ret
+= LogCategories
[i
].category
;
290 std::vector
<CLogCategoryActive
> ListActiveLogCategories()
292 std::vector
<CLogCategoryActive
> ret
;
293 for (unsigned int i
= 0; i
< ARRAYLEN(LogCategories
); i
++) {
294 // Omit the special cases.
295 if (LogCategories
[i
].flag
!= BCLog::NONE
&& LogCategories
[i
].flag
!= BCLog::ALL
) {
296 CLogCategoryActive catActive
;
297 catActive
.category
= LogCategories
[i
].category
;
298 catActive
.active
= LogAcceptCategory(LogCategories
[i
].flag
);
299 ret
.push_back(catActive
);
306 * fStartedNewLine is a state variable held by the calling context that will
307 * suppress printing of the timestamp when multiple calls are made that don't
308 * end in a newline. Initialize it to true, and hold it, in the calling context.
310 static std::string
LogTimestampStr(const std::string
&str
, std::atomic_bool
*fStartedNewLine
)
312 std::string strStamped
;
317 if (*fStartedNewLine
) {
318 int64_t nTimeMicros
= GetTimeMicros();
319 strStamped
= DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros
/1000000);
321 strStamped
+= strprintf(".%06d", nTimeMicros
%1000000);
322 int64_t mocktime
= GetMockTime();
324 strStamped
+= " (mocktime: " + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", mocktime
) + ")";
326 strStamped
+= ' ' + str
;
330 if (!str
.empty() && str
[str
.size()-1] == '\n')
331 *fStartedNewLine
= true;
333 *fStartedNewLine
= false;
338 int LogPrintStr(const std::string
&str
)
340 int ret
= 0; // Returns total number of characters written
341 static std::atomic_bool
fStartedNewLine(true);
343 std::string strTimestamped
= LogTimestampStr(str
, &fStartedNewLine
);
348 ret
= fwrite(strTimestamped
.data(), 1, strTimestamped
.size(), stdout
);
351 else if (fPrintToDebugLog
)
353 boost::call_once(&DebugPrintInit
, debugPrintInitFlag
);
354 boost::mutex::scoped_lock
scoped_lock(*mutexDebugLog
);
356 // buffer if we haven't opened the log yet
357 if (fileout
== nullptr) {
358 assert(vMsgsBeforeOpenLog
);
359 ret
= strTimestamped
.length();
360 vMsgsBeforeOpenLog
->push_back(strTimestamped
);
364 // reopen the log file, if requested
365 if (fReopenDebugLog
) {
366 fReopenDebugLog
= false;
367 fs::path pathDebug
= GetDebugLogPath();
368 if (fsbridge::freopen(pathDebug
,"a",fileout
) != nullptr)
369 setbuf(fileout
, nullptr); // unbuffered
372 ret
= FileWriteStr(strTimestamped
, fileout
);
378 /** Interpret string as boolean, for argument parsing */
379 static bool InterpretBool(const std::string
& strValue
)
381 if (strValue
.empty())
383 return (atoi(strValue
) != 0);
386 /** Turn -noX into -X=0 */
387 static void InterpretNegativeSetting(std::string
& strKey
, std::string
& strValue
)
389 if (strKey
.length()>3 && strKey
[0]=='-' && strKey
[1]=='n' && strKey
[2]=='o')
391 strKey
= "-" + strKey
.substr(3);
392 strValue
= InterpretBool(strValue
) ? "0" : "1";
396 void ArgsManager::ParseParameters(int argc
, const char* const argv
[])
400 mapMultiArgs
.clear();
402 for (int i
= 1; i
< argc
; i
++)
404 std::string
str(argv
[i
]);
405 std::string strValue
;
406 size_t is_index
= str
.find('=');
407 if (is_index
!= std::string::npos
)
409 strValue
= str
.substr(is_index
+1);
410 str
= str
.substr(0, is_index
);
413 boost::to_lower(str
);
414 if (boost::algorithm::starts_with(str
, "/"))
415 str
= "-" + str
.substr(1);
421 // Interpret --foo as -foo.
422 // If both --foo and -foo are set, the last takes effect.
423 if (str
.length() > 1 && str
[1] == '-')
425 InterpretNegativeSetting(str
, strValue
);
427 mapArgs
[str
] = strValue
;
428 mapMultiArgs
[str
].push_back(strValue
);
432 std::vector
<std::string
> ArgsManager::GetArgs(const std::string
& strArg
) const
435 auto it
= mapMultiArgs
.find(strArg
);
436 if (it
!= mapMultiArgs
.end()) return it
->second
;
440 bool ArgsManager::IsArgSet(const std::string
& strArg
) const
443 return mapArgs
.count(strArg
);
446 std::string
ArgsManager::GetArg(const std::string
& strArg
, const std::string
& strDefault
) const
449 auto it
= mapArgs
.find(strArg
);
450 if (it
!= mapArgs
.end()) return it
->second
;
454 int64_t ArgsManager::GetArg(const std::string
& strArg
, int64_t nDefault
) const
457 auto it
= mapArgs
.find(strArg
);
458 if (it
!= mapArgs
.end()) return atoi64(it
->second
);
462 bool ArgsManager::GetBoolArg(const std::string
& strArg
, bool fDefault
) const
465 auto it
= mapArgs
.find(strArg
);
466 if (it
!= mapArgs
.end()) return InterpretBool(it
->second
);
470 bool ArgsManager::SoftSetArg(const std::string
& strArg
, const std::string
& strValue
)
473 if (IsArgSet(strArg
)) return false;
474 ForceSetArg(strArg
, strValue
);
478 bool ArgsManager::SoftSetBoolArg(const std::string
& strArg
, bool fValue
)
481 return SoftSetArg(strArg
, std::string("1"));
483 return SoftSetArg(strArg
, std::string("0"));
486 void ArgsManager::ForceSetArg(const std::string
& strArg
, const std::string
& strValue
)
489 mapArgs
[strArg
] = strValue
;
490 mapMultiArgs
[strArg
] = {strValue
};
495 static const int screenWidth
= 79;
496 static const int optIndent
= 2;
497 static const int msgIndent
= 7;
499 std::string
HelpMessageGroup(const std::string
&message
) {
500 return std::string(message
) + std::string("\n\n");
503 std::string
HelpMessageOpt(const std::string
&option
, const std::string
&message
) {
504 return std::string(optIndent
,' ') + std::string(option
) +
505 std::string("\n") + std::string(msgIndent
,' ') +
506 FormatParagraph(message
, screenWidth
- msgIndent
, msgIndent
) +
510 static std::string
FormatException(const std::exception
* pex
, const char* pszThread
)
513 char pszModule
[MAX_PATH
] = "";
514 GetModuleFileNameA(nullptr, pszModule
, sizeof(pszModule
));
516 const char* pszModule
= "bitcoin";
520 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex
).name(), pex
->what(), pszModule
, pszThread
);
523 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule
, pszThread
);
526 void PrintExceptionContinue(const std::exception
* pex
, const char* pszThread
)
528 std::string message
= FormatException(pex
, pszThread
);
529 LogPrintf("\n\n************************\n%s\n", message
);
530 fprintf(stderr
, "\n\n************************\n%s\n", message
.c_str());
533 fs::path
GetDefaultDataDir()
535 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
536 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
537 // Mac: ~/Library/Application Support/Bitcoin
541 return GetSpecialFolderPath(CSIDL_APPDATA
) / "Bitcoin";
544 char* pszHome
= getenv("HOME");
545 if (pszHome
== nullptr || strlen(pszHome
) == 0)
546 pathRet
= fs::path("/");
548 pathRet
= fs::path(pszHome
);
551 return pathRet
/ "Library/Application Support/Bitcoin";
554 return pathRet
/ ".bitcoin";
559 static fs::path pathCached
;
560 static fs::path pathCachedNetSpecific
;
561 static CCriticalSection csPathCached
;
563 const fs::path
&GetDataDir(bool fNetSpecific
)
568 fs::path
&path
= fNetSpecific
? pathCachedNetSpecific
: pathCached
;
570 // This can be called during exceptions by LogPrintf(), so we cache the
571 // value so we don't have to do memory allocations after that.
575 if (gArgs
.IsArgSet("-datadir")) {
576 path
= fs::system_complete(gArgs
.GetArg("-datadir", ""));
577 if (!fs::is_directory(path
)) {
582 path
= GetDefaultDataDir();
585 path
/= BaseParams().DataDir();
587 if (fs::create_directories(path
)) {
588 // This is the first run, create wallets subdirectory too
589 fs::create_directories(path
/ "wallets");
595 void ClearDatadirCache()
599 pathCached
= fs::path();
600 pathCachedNetSpecific
= fs::path();
603 fs::path
GetConfigFile(const std::string
& confPath
)
605 fs::path
pathConfigFile(confPath
);
606 if (!pathConfigFile
.is_complete())
607 pathConfigFile
= GetDataDir(false) / pathConfigFile
;
609 return pathConfigFile
;
612 void ArgsManager::ReadConfigFile(const std::string
& confPath
)
614 fs::ifstream
streamConfig(GetConfigFile(confPath
));
615 if (!streamConfig
.good())
616 return; // No bitcoin.conf file is OK
620 std::set
<std::string
> setOptions
;
621 setOptions
.insert("*");
623 for (boost::program_options::detail::config_file_iterator
it(streamConfig
, setOptions
), end
; it
!= end
; ++it
)
625 // Don't overwrite existing settings so command line settings override bitcoin.conf
626 std::string strKey
= std::string("-") + it
->string_key
;
627 std::string strValue
= it
->value
[0];
628 InterpretNegativeSetting(strKey
, strValue
);
629 if (mapArgs
.count(strKey
) == 0)
630 mapArgs
[strKey
] = strValue
;
631 mapMultiArgs
[strKey
].push_back(strValue
);
634 // If datadir is changed in .conf file:
636 if (!fs::is_directory(GetDataDir(false))) {
637 throw std::runtime_error(strprintf("specified data directory \"%s\" does not exist.", gArgs
.GetArg("-datadir", "").c_str()));
642 fs::path
GetPidFile()
644 fs::path
pathPidFile(gArgs
.GetArg("-pid", BITCOIN_PID_FILENAME
));
645 if (!pathPidFile
.is_complete()) pathPidFile
= GetDataDir() / pathPidFile
;
649 void CreatePidFile(const fs::path
&path
, pid_t pid
)
651 FILE* file
= fsbridge::fopen(path
, "w");
654 fprintf(file
, "%d\n", pid
);
660 bool RenameOver(fs::path src
, fs::path dest
)
663 return MoveFileExA(src
.string().c_str(), dest
.string().c_str(),
664 MOVEFILE_REPLACE_EXISTING
) != 0;
666 int rc
= std::rename(src
.string().c_str(), dest
.string().c_str());
672 * Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
673 * Specifically handles case where path p exists, but it wasn't possible for the user to
674 * write to the parent directory.
676 bool TryCreateDirectories(const fs::path
& p
)
680 return fs::create_directories(p
);
681 } catch (const fs::filesystem_error
&) {
682 if (!fs::exists(p
) || !fs::is_directory(p
))
686 // create_directories didn't create the directory, it had to have existed already
690 void FileCommit(FILE *file
)
692 fflush(file
); // harmless if redundantly called
694 HANDLE hFile
= (HANDLE
)_get_osfhandle(_fileno(file
));
695 FlushFileBuffers(hFile
);
697 #if defined(__linux__) || defined(__NetBSD__)
698 fdatasync(fileno(file
));
699 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
700 fcntl(fileno(file
), F_FULLFSYNC
, 0);
707 bool TruncateFile(FILE *file
, unsigned int length
) {
709 return _chsize(_fileno(file
), length
) == 0;
711 return ftruncate(fileno(file
), length
) == 0;
716 * this function tries to raise the file descriptor limit to the requested number.
717 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
719 int RaiseFileDescriptorLimit(int nMinFD
) {
723 struct rlimit limitFD
;
724 if (getrlimit(RLIMIT_NOFILE
, &limitFD
) != -1) {
725 if (limitFD
.rlim_cur
< (rlim_t
)nMinFD
) {
726 limitFD
.rlim_cur
= nMinFD
;
727 if (limitFD
.rlim_cur
> limitFD
.rlim_max
)
728 limitFD
.rlim_cur
= limitFD
.rlim_max
;
729 setrlimit(RLIMIT_NOFILE
, &limitFD
);
730 getrlimit(RLIMIT_NOFILE
, &limitFD
);
732 return limitFD
.rlim_cur
;
734 return nMinFD
; // getrlimit failed, assume it's fine
739 * this function tries to make a particular range of a file allocated (corresponding to disk space)
740 * it is advisory, and the range specified in the arguments will never contain live data
742 void AllocateFileRange(FILE *file
, unsigned int offset
, unsigned int length
) {
744 // Windows-specific version
745 HANDLE hFile
= (HANDLE
)_get_osfhandle(_fileno(file
));
746 LARGE_INTEGER nFileSize
;
747 int64_t nEndPos
= (int64_t)offset
+ length
;
748 nFileSize
.u
.LowPart
= nEndPos
& 0xFFFFFFFF;
749 nFileSize
.u
.HighPart
= nEndPos
>> 32;
750 SetFilePointerEx(hFile
, nFileSize
, 0, FILE_BEGIN
);
752 #elif defined(MAC_OSX)
753 // OSX specific version
755 fst
.fst_flags
= F_ALLOCATECONTIG
;
756 fst
.fst_posmode
= F_PEOFPOSMODE
;
758 fst
.fst_length
= (off_t
)offset
+ length
;
759 fst
.fst_bytesalloc
= 0;
760 if (fcntl(fileno(file
), F_PREALLOCATE
, &fst
) == -1) {
761 fst
.fst_flags
= F_ALLOCATEALL
;
762 fcntl(fileno(file
), F_PREALLOCATE
, &fst
);
764 ftruncate(fileno(file
), fst
.fst_length
);
765 #elif defined(__linux__)
766 // Version using posix_fallocate
767 off_t nEndPos
= (off_t
)offset
+ length
;
768 posix_fallocate(fileno(file
), 0, nEndPos
);
771 // TODO: just write one byte per block
772 static const char buf
[65536] = {};
773 fseek(file
, offset
, SEEK_SET
);
775 unsigned int now
= 65536;
778 fwrite(buf
, 1, now
, file
); // allowed to fail; this function is advisory anyway
784 void ShrinkDebugFile()
786 // Amount of debug.log to save at end when shrinking (must fit in memory)
787 constexpr size_t RECENT_DEBUG_HISTORY_SIZE
= 10 * 1000000;
788 // Scroll debug.log if it's getting too big
789 fs::path pathLog
= GetDebugLogPath();
790 FILE* file
= fsbridge::fopen(pathLog
, "r");
791 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
792 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
793 if (file
&& fs::file_size(pathLog
) > 11 * (RECENT_DEBUG_HISTORY_SIZE
/ 10))
795 // Restart the file with some of the end
796 std::vector
<char> vch(RECENT_DEBUG_HISTORY_SIZE
, 0);
797 fseek(file
, -((long)vch
.size()), SEEK_END
);
798 int nBytes
= fread(vch
.data(), 1, vch
.size(), file
);
801 file
= fsbridge::fopen(pathLog
, "w");
804 fwrite(vch
.data(), 1, nBytes
, file
);
808 else if (file
!= nullptr)
813 fs::path
GetSpecialFolderPath(int nFolder
, bool fCreate
)
815 char pszPath
[MAX_PATH
] = "";
817 if(SHGetSpecialFolderPathA(nullptr, pszPath
, nFolder
, fCreate
))
819 return fs::path(pszPath
);
822 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
827 void runCommand(const std::string
& strCommand
)
829 if (strCommand
.empty()) return;
830 int nErr
= ::system(strCommand
.c_str());
832 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand
, nErr
);
835 void RenameThread(const char* name
)
837 #if defined(PR_SET_NAME)
838 // Only the first 15 characters are used (16 - NUL terminator)
839 ::prctl(PR_SET_NAME
, name
, 0, 0, 0);
840 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
841 pthread_set_name_np(pthread_self(), name
);
843 #elif defined(MAC_OSX)
844 pthread_setname_np(name
);
846 // Prevent warnings for unused parameters...
851 void SetupEnvironment()
853 #ifdef HAVE_MALLOPT_ARENA_MAX
854 // glibc-specific: On 32-bit systems set the number of arenas to 1.
855 // By default, since glibc 2.10, the C library will create up to two heap
856 // arenas per core. This is known to cause excessive virtual address space
857 // usage in our usage. Work around it by setting the maximum number of
859 if (sizeof(void*) == 4) {
860 mallopt(M_ARENA_MAX
, 1);
863 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
864 // may be invalid, in which case the "C" locale is used as fallback.
865 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
867 std::locale(""); // Raises a runtime error if current locale is invalid
868 } catch (const std::runtime_error
&) {
869 setenv("LC_ALL", "C", 1);
872 // The path locale is lazy initialized and to avoid deinitialization errors
873 // in multithreading environments, it is set explicitly by the main thread.
874 // A dummy locale is used to extract the internal default locale, used by
875 // fs::path, which is then used to explicitly imbue the path.
876 std::locale loc
= fs::path::imbue(std::locale::classic());
877 fs::path::imbue(loc
);
880 bool SetupNetworking()
883 // Initialize Windows Sockets
885 int ret
= WSAStartup(MAKEWORD(2,2), &wsadata
);
886 if (ret
!= NO_ERROR
|| LOBYTE(wsadata
.wVersion
) != 2 || HIBYTE(wsadata
.wVersion
) != 2)
894 #if BOOST_VERSION >= 105600
895 return boost::thread::physical_concurrency();
896 #else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
897 return boost::thread::hardware_concurrency();
901 std::string
CopyrightHolders(const std::string
& strPrefix
)
903 std::string strCopyrightHolders
= strPrefix
+ strprintf(_(COPYRIGHT_HOLDERS
), _(COPYRIGHT_HOLDERS_SUBSTITUTION
));
905 // Check for untranslated substitution to make sure Bitcoin Core copyright is not removed by accident
906 if (strprintf(COPYRIGHT_HOLDERS
, COPYRIGHT_HOLDERS_SUBSTITUTION
).find("Bitcoin Core") == std::string::npos
) {
907 strCopyrightHolders
+= "\n" + strPrefix
+ "The Bitcoin Core developers";
909 return strCopyrightHolders
;
912 // Obtain the application startup time (used for uptime calculation)
913 int64_t GetStartupTime()