Merge #12075: [scripts] Add missing univalue file to copyright_header.py
[bitcoinplatinum.git] / src / util.cpp
blob150bc503df2d6a10d627812b231c978459ecd34d
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
6 #include <util.h>
8 #include <chainparamsbase.h>
9 #include <random.h>
10 #include <serialize.h>
11 #include <utilstrencodings.h>
13 #include <stdarg.h>
15 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
16 #include <pthread.h>
17 #include <pthread_np.h>
18 #endif
20 #ifndef WIN32
21 // for posix_fallocate
22 #ifdef __linux__
24 #ifdef _POSIX_C_SOURCE
25 #undef _POSIX_C_SOURCE
26 #endif
28 #define _POSIX_C_SOURCE 200112L
30 #endif // __linux__
32 #include <algorithm>
33 #include <fcntl.h>
34 #include <sys/resource.h>
35 #include <sys/stat.h>
37 #else
39 #ifdef _MSC_VER
40 #pragma warning(disable:4786)
41 #pragma warning(disable:4804)
42 #pragma warning(disable:4805)
43 #pragma warning(disable:4717)
44 #endif
46 #ifdef _WIN32_WINNT
47 #undef _WIN32_WINNT
48 #endif
49 #define _WIN32_WINNT 0x0501
51 #ifdef _WIN32_IE
52 #undef _WIN32_IE
53 #endif
54 #define _WIN32_IE 0x0501
56 #define WIN32_LEAN_AND_MEAN 1
57 #ifndef NOMINMAX
58 #define NOMINMAX
59 #endif
61 #include <io.h> /* for _commit */
62 #include <shlobj.h>
63 #endif
65 #ifdef HAVE_SYS_PRCTL_H
66 #include <sys/prctl.h>
67 #endif
69 #ifdef HAVE_MALLOPT_ARENA_MAX
70 #include <malloc.h>
71 #endif
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";
88 ArgsManager gArgs;
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]);
107 } else {
108 LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]);
112 // Singleton for wrapping OpenSSL setup/teardown.
113 class CInit
115 public:
116 CInit()
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.
127 OPENSSL_no_config();
129 #ifdef WIN32
130 // Seed OpenSSL PRNG with current contents of the screen
131 RAND_screen();
132 #endif
134 // Seed OpenSSL PRNG with performance counter
135 RandAddSeed();
137 ~CInit()
139 // Securely erase the memory used by the PRNG
140 RAND_cleanup();
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();
147 instance_of_cinit;
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
157 * the mutex).
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()) {
191 return logfile;
192 } else {
193 return GetDataDir() / logfile;
197 bool OpenDebugLog()
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");
207 if (!fileout) {
208 return false;
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;
220 return true;
223 struct CLogCategoryDesc
225 uint32_t flag;
226 std::string category;
229 const CLogCategoryDesc LogCategories[] =
231 {BCLog::NONE, "0"},
232 {BCLog::NONE, "none"},
233 {BCLog::NET, "net"},
234 {BCLog::TOR, "tor"},
235 {BCLog::MEMPOOL, "mempool"},
236 {BCLog::HTTP, "http"},
237 {BCLog::BENCH, "bench"},
238 {BCLog::ZMQ, "zmq"},
239 {BCLog::DB, "db"},
240 {BCLog::RPC, "rpc"},
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"},
252 {BCLog::QT, "qt"},
253 {BCLog::LEVELDB, "leveldb"},
254 {BCLog::ALL, "1"},
255 {BCLog::ALL, "all"},
258 bool GetLogCategory(uint32_t *f, const std::string *str)
260 if (f && str) {
261 if (*str == "") {
262 *f = BCLog::ALL;
263 return true;
265 for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
266 if (LogCategories[i].category == *str) {
267 *f = LogCategories[i].flag;
268 return true;
272 return false;
275 std::string ListLogCategories()
277 std::string ret;
278 int outcount = 0;
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;
284 outcount++;
287 return ret;
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);
302 return ret;
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;
314 if (!fLogTimestamps)
315 return str;
317 if (*fStartedNewLine) {
318 int64_t nTimeMicros = GetTimeMicros();
319 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
320 if (fLogTimeMicros)
321 strStamped += strprintf(".%06d", nTimeMicros%1000000);
322 int64_t mocktime = GetMockTime();
323 if (mocktime) {
324 strStamped += " (mocktime: " + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", mocktime) + ")";
326 strStamped += ' ' + str;
327 } else
328 strStamped = str;
330 if (!str.empty() && str[str.size()-1] == '\n')
331 *fStartedNewLine = true;
332 else
333 *fStartedNewLine = false;
335 return strStamped;
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);
345 if (fPrintToConsole)
347 // print to console
348 ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
349 fflush(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);
362 else
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);
375 return ret;
378 /** Interpret string as boolean, for argument parsing */
379 static bool InterpretBool(const std::string& strValue)
381 if (strValue.empty())
382 return true;
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[])
398 LOCK(cs_args);
399 mapArgs.clear();
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);
412 #ifdef WIN32
413 boost::to_lower(str);
414 if (boost::algorithm::starts_with(str, "/"))
415 str = "-" + str.substr(1);
416 #endif
418 if (str[0] != '-')
419 break;
421 // Interpret --foo as -foo.
422 // If both --foo and -foo are set, the last takes effect.
423 if (str.length() > 1 && str[1] == '-')
424 str = str.substr(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
434 LOCK(cs_args);
435 auto it = mapMultiArgs.find(strArg);
436 if (it != mapMultiArgs.end()) return it->second;
437 return {};
440 bool ArgsManager::IsArgSet(const std::string& strArg) const
442 LOCK(cs_args);
443 return mapArgs.count(strArg);
446 std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
448 LOCK(cs_args);
449 auto it = mapArgs.find(strArg);
450 if (it != mapArgs.end()) return it->second;
451 return strDefault;
454 int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
456 LOCK(cs_args);
457 auto it = mapArgs.find(strArg);
458 if (it != mapArgs.end()) return atoi64(it->second);
459 return nDefault;
462 bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
464 LOCK(cs_args);
465 auto it = mapArgs.find(strArg);
466 if (it != mapArgs.end()) return InterpretBool(it->second);
467 return fDefault;
470 bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
472 LOCK(cs_args);
473 if (IsArgSet(strArg)) return false;
474 ForceSetArg(strArg, strValue);
475 return true;
478 bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
480 if (fValue)
481 return SoftSetArg(strArg, std::string("1"));
482 else
483 return SoftSetArg(strArg, std::string("0"));
486 void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
488 LOCK(cs_args);
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) +
507 std::string("\n\n");
510 static std::string FormatException(const std::exception* pex, const char* pszThread)
512 #ifdef WIN32
513 char pszModule[MAX_PATH] = "";
514 GetModuleFileNameA(nullptr, pszModule, sizeof(pszModule));
515 #else
516 const char* pszModule = "bitcoin";
517 #endif
518 if (pex)
519 return strprintf(
520 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
521 else
522 return strprintf(
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
538 // Unix: ~/.bitcoin
539 #ifdef WIN32
540 // Windows
541 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
542 #else
543 fs::path pathRet;
544 char* pszHome = getenv("HOME");
545 if (pszHome == nullptr || strlen(pszHome) == 0)
546 pathRet = fs::path("/");
547 else
548 pathRet = fs::path(pszHome);
549 #ifdef MAC_OSX
550 // Mac
551 return pathRet / "Library/Application Support/Bitcoin";
552 #else
553 // Unix
554 return pathRet / ".bitcoin";
555 #endif
556 #endif
559 static fs::path pathCached;
560 static fs::path pathCachedNetSpecific;
561 static CCriticalSection csPathCached;
563 const fs::path &GetDataDir(bool fNetSpecific)
566 LOCK(csPathCached);
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.
572 if (!path.empty())
573 return path;
575 if (gArgs.IsArgSet("-datadir")) {
576 path = fs::system_complete(gArgs.GetArg("-datadir", ""));
577 if (!fs::is_directory(path)) {
578 path = "";
579 return path;
581 } else {
582 path = GetDefaultDataDir();
584 if (fNetSpecific)
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");
592 return path;
595 void ClearDatadirCache()
597 LOCK(csPathCached);
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
619 LOCK(cs_args);
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:
635 ClearDatadirCache();
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()));
641 #ifndef WIN32
642 fs::path GetPidFile()
644 fs::path pathPidFile(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME));
645 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
646 return pathPidFile;
649 void CreatePidFile(const fs::path &path, pid_t pid)
651 FILE* file = fsbridge::fopen(path, "w");
652 if (file)
654 fprintf(file, "%d\n", pid);
655 fclose(file);
658 #endif
660 bool RenameOver(fs::path src, fs::path dest)
662 #ifdef WIN32
663 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
664 MOVEFILE_REPLACE_EXISTING) != 0;
665 #else
666 int rc = std::rename(src.string().c_str(), dest.string().c_str());
667 return (rc == 0);
668 #endif /* WIN32 */
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))
683 throw;
686 // create_directories didn't create the directory, it had to have existed already
687 return false;
690 void FileCommit(FILE *file)
692 fflush(file); // harmless if redundantly called
693 #ifdef WIN32
694 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
695 FlushFileBuffers(hFile);
696 #else
697 #if defined(__linux__) || defined(__NetBSD__)
698 fdatasync(fileno(file));
699 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
700 fcntl(fileno(file), F_FULLFSYNC, 0);
701 #else
702 fsync(fileno(file));
703 #endif
704 #endif
707 bool TruncateFile(FILE *file, unsigned int length) {
708 #if defined(WIN32)
709 return _chsize(_fileno(file), length) == 0;
710 #else
711 return ftruncate(fileno(file), length) == 0;
712 #endif
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) {
720 #if defined(WIN32)
721 return 2048;
722 #else
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
735 #endif
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) {
743 #if defined(WIN32)
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);
751 SetEndOfFile(hFile);
752 #elif defined(MAC_OSX)
753 // OSX specific version
754 fstore_t fst;
755 fst.fst_flags = F_ALLOCATECONTIG;
756 fst.fst_posmode = F_PEOFPOSMODE;
757 fst.fst_offset = 0;
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);
769 #else
770 // Fallback version
771 // TODO: just write one byte per block
772 static const char buf[65536] = {};
773 fseek(file, offset, SEEK_SET);
774 while (length > 0) {
775 unsigned int now = 65536;
776 if (length < now)
777 now = length;
778 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
779 length -= now;
781 #endif
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);
799 fclose(file);
801 file = fsbridge::fopen(pathLog, "w");
802 if (file)
804 fwrite(vch.data(), 1, nBytes, file);
805 fclose(file);
808 else if (file != nullptr)
809 fclose(file);
812 #ifdef WIN32
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");
823 return fs::path("");
825 #endif
827 void runCommand(const std::string& strCommand)
829 if (strCommand.empty()) return;
830 int nErr = ::system(strCommand.c_str());
831 if (nErr)
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);
845 #else
846 // Prevent warnings for unused parameters...
847 (void)name;
848 #endif
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
858 // arenas to 1.
859 if (sizeof(void*) == 4) {
860 mallopt(M_ARENA_MAX, 1);
862 #endif
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__)
866 try {
867 std::locale(""); // Raises a runtime error if current locale is invalid
868 } catch (const std::runtime_error&) {
869 setenv("LC_ALL", "C", 1);
871 #endif
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()
882 #ifdef WIN32
883 // Initialize Windows Sockets
884 WSADATA wsadata;
885 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
886 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
887 return false;
888 #endif
889 return true;
892 int GetNumCores()
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();
898 #endif
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()
915 return nStartupTime;