Merge #11590: [Wallet] always show help-line of wallet encryption calls
[bitcoinplatinum.git] / src / util.cpp
blob51ccc94787f9aebcd32301edb6113f6786520957
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.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
10 #include "util.h"
12 #include "chainparamsbase.h"
13 #include "fs.h"
14 #include "random.h"
15 #include "serialize.h"
16 #include "utilstrencodings.h"
17 #include "utiltime.h"
19 #include <stdarg.h>
21 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
22 #include <pthread.h>
23 #include <pthread_np.h>
24 #endif
26 #ifndef WIN32
27 // for posix_fallocate
28 #ifdef __linux__
30 #ifdef _POSIX_C_SOURCE
31 #undef _POSIX_C_SOURCE
32 #endif
34 #define _POSIX_C_SOURCE 200112L
36 #endif // __linux__
38 #include <algorithm>
39 #include <fcntl.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
43 #else
45 #ifdef _MSC_VER
46 #pragma warning(disable:4786)
47 #pragma warning(disable:4804)
48 #pragma warning(disable:4805)
49 #pragma warning(disable:4717)
50 #endif
52 #ifdef _WIN32_WINNT
53 #undef _WIN32_WINNT
54 #endif
55 #define _WIN32_WINNT 0x0501
57 #ifdef _WIN32_IE
58 #undef _WIN32_IE
59 #endif
60 #define _WIN32_IE 0x0501
62 #define WIN32_LEAN_AND_MEAN 1
63 #ifndef NOMINMAX
64 #define NOMINMAX
65 #endif
67 #include <io.h> /* for _commit */
68 #include <shlobj.h>
69 #endif
71 #ifdef HAVE_SYS_PRCTL_H
72 #include <sys/prctl.h>
73 #endif
75 #ifdef HAVE_MALLOPT_ARENA_MAX
76 #include <malloc.h>
77 #endif
79 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
80 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
81 #include <boost/program_options/detail/config_file.hpp>
82 #include <boost/thread.hpp>
83 #include <openssl/crypto.h>
84 #include <openssl/rand.h>
85 #include <openssl/conf.h>
87 // Application startup time (used for uptime calculation)
88 const int64_t nStartupTime = GetTime();
90 const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
91 const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
93 ArgsManager gArgs;
94 bool fPrintToConsole = false;
95 bool fPrintToDebugLog = true;
97 bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
98 bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
99 bool fLogIPs = DEFAULT_LOGIPS;
100 std::atomic<bool> fReopenDebugLog(false);
101 CTranslationInterface translationInterface;
103 /** Log categories bitfield. */
104 std::atomic<uint32_t> logCategories(0);
106 /** Init OpenSSL library multithreading support */
107 static std::unique_ptr<CCriticalSection[]> ppmutexOpenSSL;
108 void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
110 if (mode & CRYPTO_LOCK) {
111 ENTER_CRITICAL_SECTION(ppmutexOpenSSL[i]);
112 } else {
113 LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]);
117 // Singleton for wrapping OpenSSL setup/teardown.
118 class CInit
120 public:
121 CInit()
123 // Init OpenSSL library multithreading support
124 ppmutexOpenSSL.reset(new CCriticalSection[CRYPTO_num_locks()]);
125 CRYPTO_set_locking_callback(locking_callback);
127 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
128 // We don't use them so we don't require the config. However some of our libs may call functions
129 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
130 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
131 // that the config appears to have been loaded and there are no modules/engines available.
132 OPENSSL_no_config();
134 #ifdef WIN32
135 // Seed OpenSSL PRNG with current contents of the screen
136 RAND_screen();
137 #endif
139 // Seed OpenSSL PRNG with performance counter
140 RandAddSeed();
142 ~CInit()
144 // Securely erase the memory used by the PRNG
145 RAND_cleanup();
146 // Shutdown OpenSSL library multithreading support
147 CRYPTO_set_locking_callback(nullptr);
148 // Clear the set of locks now to maintain symmetry with the constructor.
149 ppmutexOpenSSL.reset();
152 instance_of_cinit;
155 * LogPrintf() has been broken a couple of times now
156 * by well-meaning people adding mutexes in the most straightforward way.
157 * It breaks because it may be called by global destructors during shutdown.
158 * Since the order of destruction of static/global objects is undefined,
159 * defining a mutex as a global object doesn't work (the mutex gets
160 * destroyed, and then some later destructor calls OutputDebugStringF,
161 * maybe indirectly, and you get a core dump at shutdown trying to lock
162 * the mutex).
165 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
168 * We use boost::call_once() to make sure mutexDebugLog and
169 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
171 * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
172 * are leaked on exit. This is ugly, but will be cleaned up by
173 * the OS/libc. When the shutdown sequence is fully audited and
174 * tested, explicit destruction of these objects can be implemented.
176 static FILE* fileout = nullptr;
177 static boost::mutex* mutexDebugLog = nullptr;
178 static std::list<std::string>* vMsgsBeforeOpenLog;
180 static int FileWriteStr(const std::string &str, FILE *fp)
182 return fwrite(str.data(), 1, str.size(), fp);
185 static void DebugPrintInit()
187 assert(mutexDebugLog == nullptr);
188 mutexDebugLog = new boost::mutex();
189 vMsgsBeforeOpenLog = new std::list<std::string>;
192 void OpenDebugLog()
194 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
195 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
197 assert(fileout == nullptr);
198 assert(vMsgsBeforeOpenLog);
199 fs::path pathDebug = GetDataDir() / "debug.log";
200 fileout = fsbridge::fopen(pathDebug, "a");
201 if (fileout) {
202 setbuf(fileout, nullptr); // unbuffered
203 // dump buffered messages from before we opened the log
204 while (!vMsgsBeforeOpenLog->empty()) {
205 FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
206 vMsgsBeforeOpenLog->pop_front();
210 delete vMsgsBeforeOpenLog;
211 vMsgsBeforeOpenLog = nullptr;
214 struct CLogCategoryDesc
216 uint32_t flag;
217 std::string category;
220 const CLogCategoryDesc LogCategories[] =
222 {BCLog::NONE, "0"},
223 {BCLog::NET, "net"},
224 {BCLog::TOR, "tor"},
225 {BCLog::MEMPOOL, "mempool"},
226 {BCLog::HTTP, "http"},
227 {BCLog::BENCH, "bench"},
228 {BCLog::ZMQ, "zmq"},
229 {BCLog::DB, "db"},
230 {BCLog::RPC, "rpc"},
231 {BCLog::ESTIMATEFEE, "estimatefee"},
232 {BCLog::ADDRMAN, "addrman"},
233 {BCLog::SELECTCOINS, "selectcoins"},
234 {BCLog::REINDEX, "reindex"},
235 {BCLog::CMPCTBLOCK, "cmpctblock"},
236 {BCLog::RAND, "rand"},
237 {BCLog::PRUNE, "prune"},
238 {BCLog::PROXY, "proxy"},
239 {BCLog::MEMPOOLREJ, "mempoolrej"},
240 {BCLog::LIBEVENT, "libevent"},
241 {BCLog::COINDB, "coindb"},
242 {BCLog::QT, "qt"},
243 {BCLog::LEVELDB, "leveldb"},
244 {BCLog::ALL, "1"},
245 {BCLog::ALL, "all"},
248 bool GetLogCategory(uint32_t *f, const std::string *str)
250 if (f && str) {
251 if (*str == "") {
252 *f = BCLog::ALL;
253 return true;
255 for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
256 if (LogCategories[i].category == *str) {
257 *f = LogCategories[i].flag;
258 return true;
262 return false;
265 std::string ListLogCategories()
267 std::string ret;
268 int outcount = 0;
269 for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
270 // Omit the special cases.
271 if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
272 if (outcount != 0) ret += ", ";
273 ret += LogCategories[i].category;
274 outcount++;
277 return ret;
280 std::vector<CLogCategoryActive> ListActiveLogCategories()
282 std::vector<CLogCategoryActive> ret;
283 for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
284 // Omit the special cases.
285 if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
286 CLogCategoryActive catActive;
287 catActive.category = LogCategories[i].category;
288 catActive.active = LogAcceptCategory(LogCategories[i].flag);
289 ret.push_back(catActive);
292 return ret;
296 * fStartedNewLine is a state variable held by the calling context that will
297 * suppress printing of the timestamp when multiple calls are made that don't
298 * end in a newline. Initialize it to true, and hold it, in the calling context.
300 static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
302 std::string strStamped;
304 if (!fLogTimestamps)
305 return str;
307 if (*fStartedNewLine) {
308 int64_t nTimeMicros = GetTimeMicros();
309 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
310 if (fLogTimeMicros)
311 strStamped += strprintf(".%06d", nTimeMicros%1000000);
312 int64_t mocktime = GetMockTime();
313 if (mocktime) {
314 strStamped += " (mocktime: " + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", mocktime) + ")";
316 strStamped += ' ' + str;
317 } else
318 strStamped = str;
320 if (!str.empty() && str[str.size()-1] == '\n')
321 *fStartedNewLine = true;
322 else
323 *fStartedNewLine = false;
325 return strStamped;
328 int LogPrintStr(const std::string &str)
330 int ret = 0; // Returns total number of characters written
331 static std::atomic_bool fStartedNewLine(true);
333 std::string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
335 if (fPrintToConsole)
337 // print to console
338 ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
339 fflush(stdout);
341 else if (fPrintToDebugLog)
343 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
344 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
346 // buffer if we haven't opened the log yet
347 if (fileout == nullptr) {
348 assert(vMsgsBeforeOpenLog);
349 ret = strTimestamped.length();
350 vMsgsBeforeOpenLog->push_back(strTimestamped);
352 else
354 // reopen the log file, if requested
355 if (fReopenDebugLog) {
356 fReopenDebugLog = false;
357 fs::path pathDebug = GetDataDir() / "debug.log";
358 if (fsbridge::freopen(pathDebug,"a",fileout) != nullptr)
359 setbuf(fileout, nullptr); // unbuffered
362 ret = FileWriteStr(strTimestamped, fileout);
365 return ret;
368 /** Interpret string as boolean, for argument parsing */
369 static bool InterpretBool(const std::string& strValue)
371 if (strValue.empty())
372 return true;
373 return (atoi(strValue) != 0);
376 /** Turn -noX into -X=0 */
377 static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
379 if (strKey.length()>3 && strKey[0]=='-' && strKey[1]=='n' && strKey[2]=='o')
381 strKey = "-" + strKey.substr(3);
382 strValue = InterpretBool(strValue) ? "0" : "1";
386 void ArgsManager::ParseParameters(int argc, const char* const argv[])
388 LOCK(cs_args);
389 mapArgs.clear();
390 mapMultiArgs.clear();
392 for (int i = 1; i < argc; i++)
394 std::string str(argv[i]);
395 std::string strValue;
396 size_t is_index = str.find('=');
397 if (is_index != std::string::npos)
399 strValue = str.substr(is_index+1);
400 str = str.substr(0, is_index);
402 #ifdef WIN32
403 boost::to_lower(str);
404 if (boost::algorithm::starts_with(str, "/"))
405 str = "-" + str.substr(1);
406 #endif
408 if (str[0] != '-')
409 break;
411 // Interpret --foo as -foo.
412 // If both --foo and -foo are set, the last takes effect.
413 if (str.length() > 1 && str[1] == '-')
414 str = str.substr(1);
415 InterpretNegativeSetting(str, strValue);
417 mapArgs[str] = strValue;
418 mapMultiArgs[str].push_back(strValue);
422 std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
424 LOCK(cs_args);
425 auto it = mapMultiArgs.find(strArg);
426 if (it != mapMultiArgs.end()) return it->second;
427 return {};
430 bool ArgsManager::IsArgSet(const std::string& strArg) const
432 LOCK(cs_args);
433 return mapArgs.count(strArg);
436 std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
438 LOCK(cs_args);
439 auto it = mapArgs.find(strArg);
440 if (it != mapArgs.end()) return it->second;
441 return strDefault;
444 int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault) const
446 LOCK(cs_args);
447 auto it = mapArgs.find(strArg);
448 if (it != mapArgs.end()) return atoi64(it->second);
449 return nDefault;
452 bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
454 LOCK(cs_args);
455 auto it = mapArgs.find(strArg);
456 if (it != mapArgs.end()) return InterpretBool(it->second);
457 return fDefault;
460 bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
462 LOCK(cs_args);
463 if (IsArgSet(strArg)) return false;
464 ForceSetArg(strArg, strValue);
465 return true;
468 bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
470 if (fValue)
471 return SoftSetArg(strArg, std::string("1"));
472 else
473 return SoftSetArg(strArg, std::string("0"));
476 void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
478 LOCK(cs_args);
479 mapArgs[strArg] = strValue;
480 mapMultiArgs[strArg] = {strValue};
485 static const int screenWidth = 79;
486 static const int optIndent = 2;
487 static const int msgIndent = 7;
489 std::string HelpMessageGroup(const std::string &message) {
490 return std::string(message) + std::string("\n\n");
493 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
494 return std::string(optIndent,' ') + std::string(option) +
495 std::string("\n") + std::string(msgIndent,' ') +
496 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
497 std::string("\n\n");
500 static std::string FormatException(const std::exception* pex, const char* pszThread)
502 #ifdef WIN32
503 char pszModule[MAX_PATH] = "";
504 GetModuleFileNameA(nullptr, pszModule, sizeof(pszModule));
505 #else
506 const char* pszModule = "bitcoin";
507 #endif
508 if (pex)
509 return strprintf(
510 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
511 else
512 return strprintf(
513 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
516 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
518 std::string message = FormatException(pex, pszThread);
519 LogPrintf("\n\n************************\n%s\n", message);
520 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
523 fs::path GetDefaultDataDir()
525 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
526 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
527 // Mac: ~/Library/Application Support/Bitcoin
528 // Unix: ~/.bitcoin
529 #ifdef WIN32
530 // Windows
531 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
532 #else
533 fs::path pathRet;
534 char* pszHome = getenv("HOME");
535 if (pszHome == nullptr || strlen(pszHome) == 0)
536 pathRet = fs::path("/");
537 else
538 pathRet = fs::path(pszHome);
539 #ifdef MAC_OSX
540 // Mac
541 return pathRet / "Library/Application Support/Bitcoin";
542 #else
543 // Unix
544 return pathRet / ".bitcoin";
545 #endif
546 #endif
549 static fs::path pathCached;
550 static fs::path pathCachedNetSpecific;
551 static CCriticalSection csPathCached;
553 const fs::path &GetDataDir(bool fNetSpecific)
556 LOCK(csPathCached);
558 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
560 // This can be called during exceptions by LogPrintf(), so we cache the
561 // value so we don't have to do memory allocations after that.
562 if (!path.empty())
563 return path;
565 if (gArgs.IsArgSet("-datadir")) {
566 path = fs::system_complete(gArgs.GetArg("-datadir", ""));
567 if (!fs::is_directory(path)) {
568 path = "";
569 return path;
571 } else {
572 path = GetDefaultDataDir();
574 if (fNetSpecific)
575 path /= BaseParams().DataDir();
577 fs::create_directories(path);
579 return path;
582 void ClearDatadirCache()
584 LOCK(csPathCached);
586 pathCached = fs::path();
587 pathCachedNetSpecific = fs::path();
590 fs::path GetConfigFile(const std::string& confPath)
592 fs::path pathConfigFile(confPath);
593 if (!pathConfigFile.is_complete())
594 pathConfigFile = GetDataDir(false) / pathConfigFile;
596 return pathConfigFile;
599 void ArgsManager::ReadConfigFile(const std::string& confPath)
601 fs::ifstream streamConfig(GetConfigFile(confPath));
602 if (!streamConfig.good())
603 return; // No bitcoin.conf file is OK
606 LOCK(cs_args);
607 std::set<std::string> setOptions;
608 setOptions.insert("*");
610 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
612 // Don't overwrite existing settings so command line settings override bitcoin.conf
613 std::string strKey = std::string("-") + it->string_key;
614 std::string strValue = it->value[0];
615 InterpretNegativeSetting(strKey, strValue);
616 if (mapArgs.count(strKey) == 0)
617 mapArgs[strKey] = strValue;
618 mapMultiArgs[strKey].push_back(strValue);
621 // If datadir is changed in .conf file:
622 ClearDatadirCache();
625 #ifndef WIN32
626 fs::path GetPidFile()
628 fs::path pathPidFile(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME));
629 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
630 return pathPidFile;
633 void CreatePidFile(const fs::path &path, pid_t pid)
635 FILE* file = fsbridge::fopen(path, "w");
636 if (file)
638 fprintf(file, "%d\n", pid);
639 fclose(file);
642 #endif
644 bool RenameOver(fs::path src, fs::path dest)
646 #ifdef WIN32
647 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
648 MOVEFILE_REPLACE_EXISTING) != 0;
649 #else
650 int rc = std::rename(src.string().c_str(), dest.string().c_str());
651 return (rc == 0);
652 #endif /* WIN32 */
656 * Ignores exceptions thrown by Boost's create_directories if the requested directory exists.
657 * Specifically handles case where path p exists, but it wasn't possible for the user to
658 * write to the parent directory.
660 bool TryCreateDirectories(const fs::path& p)
664 return fs::create_directories(p);
665 } catch (const fs::filesystem_error&) {
666 if (!fs::exists(p) || !fs::is_directory(p))
667 throw;
670 // create_directories didn't create the directory, it had to have existed already
671 return false;
674 void FileCommit(FILE *file)
676 fflush(file); // harmless if redundantly called
677 #ifdef WIN32
678 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
679 FlushFileBuffers(hFile);
680 #else
681 #if defined(__linux__) || defined(__NetBSD__)
682 fdatasync(fileno(file));
683 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
684 fcntl(fileno(file), F_FULLFSYNC, 0);
685 #else
686 fsync(fileno(file));
687 #endif
688 #endif
691 bool TruncateFile(FILE *file, unsigned int length) {
692 #if defined(WIN32)
693 return _chsize(_fileno(file), length) == 0;
694 #else
695 return ftruncate(fileno(file), length) == 0;
696 #endif
700 * this function tries to raise the file descriptor limit to the requested number.
701 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
703 int RaiseFileDescriptorLimit(int nMinFD) {
704 #if defined(WIN32)
705 return 2048;
706 #else
707 struct rlimit limitFD;
708 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
709 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
710 limitFD.rlim_cur = nMinFD;
711 if (limitFD.rlim_cur > limitFD.rlim_max)
712 limitFD.rlim_cur = limitFD.rlim_max;
713 setrlimit(RLIMIT_NOFILE, &limitFD);
714 getrlimit(RLIMIT_NOFILE, &limitFD);
716 return limitFD.rlim_cur;
718 return nMinFD; // getrlimit failed, assume it's fine
719 #endif
723 * this function tries to make a particular range of a file allocated (corresponding to disk space)
724 * it is advisory, and the range specified in the arguments will never contain live data
726 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
727 #if defined(WIN32)
728 // Windows-specific version
729 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
730 LARGE_INTEGER nFileSize;
731 int64_t nEndPos = (int64_t)offset + length;
732 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
733 nFileSize.u.HighPart = nEndPos >> 32;
734 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
735 SetEndOfFile(hFile);
736 #elif defined(MAC_OSX)
737 // OSX specific version
738 fstore_t fst;
739 fst.fst_flags = F_ALLOCATECONTIG;
740 fst.fst_posmode = F_PEOFPOSMODE;
741 fst.fst_offset = 0;
742 fst.fst_length = (off_t)offset + length;
743 fst.fst_bytesalloc = 0;
744 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
745 fst.fst_flags = F_ALLOCATEALL;
746 fcntl(fileno(file), F_PREALLOCATE, &fst);
748 ftruncate(fileno(file), fst.fst_length);
749 #elif defined(__linux__)
750 // Version using posix_fallocate
751 off_t nEndPos = (off_t)offset + length;
752 posix_fallocate(fileno(file), 0, nEndPos);
753 #else
754 // Fallback version
755 // TODO: just write one byte per block
756 static const char buf[65536] = {};
757 fseek(file, offset, SEEK_SET);
758 while (length > 0) {
759 unsigned int now = 65536;
760 if (length < now)
761 now = length;
762 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
763 length -= now;
765 #endif
768 void ShrinkDebugFile()
770 // Amount of debug.log to save at end when shrinking (must fit in memory)
771 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
772 // Scroll debug.log if it's getting too big
773 fs::path pathLog = GetDataDir() / "debug.log";
774 FILE* file = fsbridge::fopen(pathLog, "r");
775 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
776 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
777 if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
779 // Restart the file with some of the end
780 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
781 fseek(file, -((long)vch.size()), SEEK_END);
782 int nBytes = fread(vch.data(), 1, vch.size(), file);
783 fclose(file);
785 file = fsbridge::fopen(pathLog, "w");
786 if (file)
788 fwrite(vch.data(), 1, nBytes, file);
789 fclose(file);
792 else if (file != nullptr)
793 fclose(file);
796 #ifdef WIN32
797 fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
799 char pszPath[MAX_PATH] = "";
801 if(SHGetSpecialFolderPathA(nullptr, pszPath, nFolder, fCreate))
803 return fs::path(pszPath);
806 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
807 return fs::path("");
809 #endif
811 void runCommand(const std::string& strCommand)
813 if (strCommand.empty()) return;
814 int nErr = ::system(strCommand.c_str());
815 if (nErr)
816 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
819 void RenameThread(const char* name)
821 #if defined(PR_SET_NAME)
822 // Only the first 15 characters are used (16 - NUL terminator)
823 ::prctl(PR_SET_NAME, name, 0, 0, 0);
824 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
825 pthread_set_name_np(pthread_self(), name);
827 #elif defined(MAC_OSX)
828 pthread_setname_np(name);
829 #else
830 // Prevent warnings for unused parameters...
831 (void)name;
832 #endif
835 void SetupEnvironment()
837 #ifdef HAVE_MALLOPT_ARENA_MAX
838 // glibc-specific: On 32-bit systems set the number of arenas to 1.
839 // By default, since glibc 2.10, the C library will create up to two heap
840 // arenas per core. This is known to cause excessive virtual address space
841 // usage in our usage. Work around it by setting the maximum number of
842 // arenas to 1.
843 if (sizeof(void*) == 4) {
844 mallopt(M_ARENA_MAX, 1);
846 #endif
847 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
848 // may be invalid, in which case the "C" locale is used as fallback.
849 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
850 try {
851 std::locale(""); // Raises a runtime error if current locale is invalid
852 } catch (const std::runtime_error&) {
853 setenv("LC_ALL", "C", 1);
855 #endif
856 // The path locale is lazy initialized and to avoid deinitialization errors
857 // in multithreading environments, it is set explicitly by the main thread.
858 // A dummy locale is used to extract the internal default locale, used by
859 // fs::path, which is then used to explicitly imbue the path.
860 std::locale loc = fs::path::imbue(std::locale::classic());
861 fs::path::imbue(loc);
864 bool SetupNetworking()
866 #ifdef WIN32
867 // Initialize Windows Sockets
868 WSADATA wsadata;
869 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
870 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
871 return false;
872 #endif
873 return true;
876 int GetNumCores()
878 #if BOOST_VERSION >= 105600
879 return boost::thread::physical_concurrency();
880 #else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
881 return boost::thread::hardware_concurrency();
882 #endif
885 std::string CopyrightHolders(const std::string& strPrefix)
887 std::string strCopyrightHolders = strPrefix + strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION));
889 // Check for untranslated substitution to make sure Bitcoin Core copyright is not removed by accident
890 if (strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION).find("Bitcoin Core") == std::string::npos) {
891 strCopyrightHolders += "\n" + strPrefix + "The Bitcoin Core developers";
893 return strCopyrightHolders;
896 // Obtain the application startup time (used for uptime calculation)
897 int64_t GetStartupTime()
899 return nStartupTime;