Fix segfault crash when shutdown the GUI in disablewallet mode
[bitcoinplatinum.git] / src / util.cpp
blob78c353dfe521dabcf0050466810ee256026ae9fb
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 "random.h"
14 #include "serialize.h"
15 #include "sync.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 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
76 #include <boost/algorithm/string/join.hpp>
77 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
78 #include <boost/filesystem.hpp>
79 #include <boost/filesystem/fstream.hpp>
80 #include <boost/foreach.hpp>
81 #include <boost/program_options/detail/config_file.hpp>
82 #include <boost/program_options/parsers.hpp>
83 #include <boost/thread.hpp>
84 #include <openssl/crypto.h>
85 #include <openssl/rand.h>
86 #include <openssl/conf.h>
88 // Work around clang compilation problem in Boost 1.46:
89 // /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup
90 // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
91 // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
92 namespace boost {
94 namespace program_options {
95 std::string to_internal(const std::string&);
98 } // namespace boost
100 using namespace std;
102 const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
103 const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
105 CCriticalSection cs_args;
106 map<string, string> mapArgs;
107 static map<string, vector<string> > _mapMultiArgs;
108 const map<string, vector<string> >& mapMultiArgs = _mapMultiArgs;
109 bool fDebug = false;
110 bool fPrintToConsole = false;
111 bool fPrintToDebugLog = true;
113 bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
114 bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
115 bool fLogIPs = DEFAULT_LOGIPS;
116 std::atomic<bool> fReopenDebugLog(false);
117 CTranslationInterface translationInterface;
119 /** Init OpenSSL library multithreading support */
120 static CCriticalSection** ppmutexOpenSSL;
121 void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
123 if (mode & CRYPTO_LOCK) {
124 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
125 } else {
126 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
130 // Init
131 class CInit
133 public:
134 CInit()
136 // Init OpenSSL library multithreading support
137 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
138 for (int i = 0; i < CRYPTO_num_locks(); i++)
139 ppmutexOpenSSL[i] = new CCriticalSection();
140 CRYPTO_set_locking_callback(locking_callback);
142 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
143 // We don't use them so we don't require the config. However some of our libs may call functions
144 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
145 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
146 // that the config appears to have been loaded and there are no modules/engines available.
147 OPENSSL_no_config();
149 #ifdef WIN32
150 // Seed OpenSSL PRNG with current contents of the screen
151 RAND_screen();
152 #endif
154 // Seed OpenSSL PRNG with performance counter
155 RandAddSeed();
157 ~CInit()
159 // Securely erase the memory used by the PRNG
160 RAND_cleanup();
161 // Shutdown OpenSSL library multithreading support
162 CRYPTO_set_locking_callback(NULL);
163 for (int i = 0; i < CRYPTO_num_locks(); i++)
164 delete ppmutexOpenSSL[i];
165 OPENSSL_free(ppmutexOpenSSL);
168 instance_of_cinit;
171 * LogPrintf() has been broken a couple of times now
172 * by well-meaning people adding mutexes in the most straightforward way.
173 * It breaks because it may be called by global destructors during shutdown.
174 * Since the order of destruction of static/global objects is undefined,
175 * defining a mutex as a global object doesn't work (the mutex gets
176 * destroyed, and then some later destructor calls OutputDebugStringF,
177 * maybe indirectly, and you get a core dump at shutdown trying to lock
178 * the mutex).
181 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
184 * We use boost::call_once() to make sure mutexDebugLog and
185 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
187 * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
188 * are leaked on exit. This is ugly, but will be cleaned up by
189 * the OS/libc. When the shutdown sequence is fully audited and
190 * tested, explicit destruction of these objects can be implemented.
192 static FILE* fileout = NULL;
193 static boost::mutex* mutexDebugLog = NULL;
194 static list<string> *vMsgsBeforeOpenLog;
196 static int FileWriteStr(const std::string &str, FILE *fp)
198 return fwrite(str.data(), 1, str.size(), fp);
201 static void DebugPrintInit()
203 assert(mutexDebugLog == NULL);
204 mutexDebugLog = new boost::mutex();
205 vMsgsBeforeOpenLog = new list<string>;
208 void OpenDebugLog()
210 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
211 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
213 assert(fileout == NULL);
214 assert(vMsgsBeforeOpenLog);
215 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
216 fileout = fopen(pathDebug.string().c_str(), "a");
217 if (fileout) {
218 setbuf(fileout, NULL); // unbuffered
219 // dump buffered messages from before we opened the log
220 while (!vMsgsBeforeOpenLog->empty()) {
221 FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
222 vMsgsBeforeOpenLog->pop_front();
226 delete vMsgsBeforeOpenLog;
227 vMsgsBeforeOpenLog = NULL;
230 bool LogAcceptCategory(const char* category)
232 if (category != NULL)
234 if (!fDebug)
235 return false;
237 // Give each thread quick access to -debug settings.
238 // This helps prevent issues debugging global destructors,
239 // where mapMultiArgs might be deleted before another
240 // global destructor calls LogPrint()
241 static boost::thread_specific_ptr<set<string> > ptrCategory;
242 if (ptrCategory.get() == NULL)
244 if (mapMultiArgs.count("-debug")) {
245 const vector<string>& categories = mapMultiArgs.at("-debug");
246 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
247 // thread_specific_ptr automatically deletes the set when the thread ends.
248 } else
249 ptrCategory.reset(new set<string>());
251 const set<string>& setCategories = *ptrCategory.get();
253 // if not debugging everything and not debugging specific category, LogPrint does nothing.
254 if (setCategories.count(string("")) == 0 &&
255 setCategories.count(string("1")) == 0 &&
256 setCategories.count(string(category)) == 0)
257 return false;
259 return true;
263 * fStartedNewLine is a state variable held by the calling context that will
264 * suppress printing of the timestamp when multiple calls are made that don't
265 * end in a newline. Initialize it to true, and hold it, in the calling context.
267 static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
269 string strStamped;
271 if (!fLogTimestamps)
272 return str;
274 if (*fStartedNewLine) {
275 int64_t nTimeMicros = GetLogTimeMicros();
276 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
277 if (fLogTimeMicros)
278 strStamped += strprintf(".%06d", nTimeMicros%1000000);
279 strStamped += ' ' + str;
280 } else
281 strStamped = str;
283 if (!str.empty() && str[str.size()-1] == '\n')
284 *fStartedNewLine = true;
285 else
286 *fStartedNewLine = false;
288 return strStamped;
291 int LogPrintStr(const std::string &str)
293 int ret = 0; // Returns total number of characters written
294 static std::atomic_bool fStartedNewLine(true);
296 string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
298 if (fPrintToConsole)
300 // print to console
301 ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
302 fflush(stdout);
304 else if (fPrintToDebugLog)
306 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
307 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
309 // buffer if we haven't opened the log yet
310 if (fileout == NULL) {
311 assert(vMsgsBeforeOpenLog);
312 ret = strTimestamped.length();
313 vMsgsBeforeOpenLog->push_back(strTimestamped);
315 else
317 // reopen the log file, if requested
318 if (fReopenDebugLog) {
319 fReopenDebugLog = false;
320 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
321 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
322 setbuf(fileout, NULL); // unbuffered
325 ret = FileWriteStr(strTimestamped, fileout);
328 return ret;
331 /** Interpret string as boolean, for argument parsing */
332 static bool InterpretBool(const std::string& strValue)
334 if (strValue.empty())
335 return true;
336 return (atoi(strValue) != 0);
339 /** Turn -noX into -X=0 */
340 static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
342 if (strKey.length()>3 && strKey[0]=='-' && strKey[1]=='n' && strKey[2]=='o')
344 strKey = "-" + strKey.substr(3);
345 strValue = InterpretBool(strValue) ? "0" : "1";
349 void ParseParameters(int argc, const char* const argv[])
351 LOCK(cs_args);
352 mapArgs.clear();
353 _mapMultiArgs.clear();
355 for (int i = 1; i < argc; i++)
357 std::string str(argv[i]);
358 std::string strValue;
359 size_t is_index = str.find('=');
360 if (is_index != std::string::npos)
362 strValue = str.substr(is_index+1);
363 str = str.substr(0, is_index);
365 #ifdef WIN32
366 boost::to_lower(str);
367 if (boost::algorithm::starts_with(str, "/"))
368 str = "-" + str.substr(1);
369 #endif
371 if (str[0] != '-')
372 break;
374 // Interpret --foo as -foo.
375 // If both --foo and -foo are set, the last takes effect.
376 if (str.length() > 1 && str[1] == '-')
377 str = str.substr(1);
378 InterpretNegativeSetting(str, strValue);
380 mapArgs[str] = strValue;
381 _mapMultiArgs[str].push_back(strValue);
385 bool IsArgSet(const std::string& strArg)
387 LOCK(cs_args);
388 return mapArgs.count(strArg);
391 std::string GetArg(const std::string& strArg, const std::string& strDefault)
393 LOCK(cs_args);
394 if (mapArgs.count(strArg))
395 return mapArgs[strArg];
396 return strDefault;
399 int64_t GetArg(const std::string& strArg, int64_t nDefault)
401 LOCK(cs_args);
402 if (mapArgs.count(strArg))
403 return atoi64(mapArgs[strArg]);
404 return nDefault;
407 bool GetBoolArg(const std::string& strArg, bool fDefault)
409 LOCK(cs_args);
410 if (mapArgs.count(strArg))
411 return InterpretBool(mapArgs[strArg]);
412 return fDefault;
415 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
417 LOCK(cs_args);
418 if (mapArgs.count(strArg))
419 return false;
420 mapArgs[strArg] = strValue;
421 return true;
424 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
426 if (fValue)
427 return SoftSetArg(strArg, std::string("1"));
428 else
429 return SoftSetArg(strArg, std::string("0"));
432 void ForceSetArg(const std::string& strArg, const std::string& strValue)
434 LOCK(cs_args);
435 mapArgs[strArg] = strValue;
440 static const int screenWidth = 79;
441 static const int optIndent = 2;
442 static const int msgIndent = 7;
444 std::string HelpMessageGroup(const std::string &message) {
445 return std::string(message) + std::string("\n\n");
448 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
449 return std::string(optIndent,' ') + std::string(option) +
450 std::string("\n") + std::string(msgIndent,' ') +
451 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
452 std::string("\n\n");
455 static std::string FormatException(const std::exception* pex, const char* pszThread)
457 #ifdef WIN32
458 char pszModule[MAX_PATH] = "";
459 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
460 #else
461 const char* pszModule = "bitcoin";
462 #endif
463 if (pex)
464 return strprintf(
465 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
466 else
467 return strprintf(
468 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
471 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
473 std::string message = FormatException(pex, pszThread);
474 LogPrintf("\n\n************************\n%s\n", message);
475 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
478 boost::filesystem::path GetDefaultDataDir()
480 namespace fs = boost::filesystem;
481 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
482 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
483 // Mac: ~/Library/Application Support/Bitcoin
484 // Unix: ~/.bitcoin
485 #ifdef WIN32
486 // Windows
487 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
488 #else
489 fs::path pathRet;
490 char* pszHome = getenv("HOME");
491 if (pszHome == NULL || strlen(pszHome) == 0)
492 pathRet = fs::path("/");
493 else
494 pathRet = fs::path(pszHome);
495 #ifdef MAC_OSX
496 // Mac
497 return pathRet / "Library/Application Support/Bitcoin";
498 #else
499 // Unix
500 return pathRet / ".bitcoin";
501 #endif
502 #endif
505 static boost::filesystem::path pathCached;
506 static boost::filesystem::path pathCachedNetSpecific;
507 static CCriticalSection csPathCached;
509 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
511 namespace fs = boost::filesystem;
513 LOCK(csPathCached);
515 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
517 // This can be called during exceptions by LogPrintf(), so we cache the
518 // value so we don't have to do memory allocations after that.
519 if (!path.empty())
520 return path;
522 if (IsArgSet("-datadir")) {
523 path = fs::system_complete(GetArg("-datadir", ""));
524 if (!fs::is_directory(path)) {
525 path = "";
526 return path;
528 } else {
529 path = GetDefaultDataDir();
531 if (fNetSpecific)
532 path /= BaseParams().DataDir();
534 fs::create_directories(path);
536 return path;
539 void ClearDatadirCache()
541 LOCK(csPathCached);
543 pathCached = boost::filesystem::path();
544 pathCachedNetSpecific = boost::filesystem::path();
547 boost::filesystem::path GetConfigFile(const std::string& confPath)
549 boost::filesystem::path pathConfigFile(confPath);
550 if (!pathConfigFile.is_complete())
551 pathConfigFile = GetDataDir(false) / pathConfigFile;
553 return pathConfigFile;
556 void ReadConfigFile(const std::string& confPath)
558 boost::filesystem::ifstream streamConfig(GetConfigFile(confPath));
559 if (!streamConfig.good())
560 return; // No bitcoin.conf file is OK
563 LOCK(cs_args);
564 set<string> setOptions;
565 setOptions.insert("*");
567 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
569 // Don't overwrite existing settings so command line settings override bitcoin.conf
570 string strKey = string("-") + it->string_key;
571 string strValue = it->value[0];
572 InterpretNegativeSetting(strKey, strValue);
573 if (mapArgs.count(strKey) == 0)
574 mapArgs[strKey] = strValue;
575 _mapMultiArgs[strKey].push_back(strValue);
578 // If datadir is changed in .conf file:
579 ClearDatadirCache();
582 #ifndef WIN32
583 boost::filesystem::path GetPidFile()
585 boost::filesystem::path pathPidFile(GetArg("-pid", BITCOIN_PID_FILENAME));
586 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
587 return pathPidFile;
590 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
592 FILE* file = fopen(path.string().c_str(), "w");
593 if (file)
595 fprintf(file, "%d\n", pid);
596 fclose(file);
599 #endif
601 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
603 #ifdef WIN32
604 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
605 MOVEFILE_REPLACE_EXISTING) != 0;
606 #else
607 int rc = std::rename(src.string().c_str(), dest.string().c_str());
608 return (rc == 0);
609 #endif /* WIN32 */
613 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
614 * Specifically handles case where path p exists, but it wasn't possible for the user to
615 * write to the parent directory.
617 bool TryCreateDirectory(const boost::filesystem::path& p)
621 return boost::filesystem::create_directory(p);
622 } catch (const boost::filesystem::filesystem_error&) {
623 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
624 throw;
627 // create_directory didn't create the directory, it had to have existed already
628 return false;
631 void FileCommit(FILE *file)
633 fflush(file); // harmless if redundantly called
634 #ifdef WIN32
635 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
636 FlushFileBuffers(hFile);
637 #else
638 #if defined(__linux__) || defined(__NetBSD__)
639 fdatasync(fileno(file));
640 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
641 fcntl(fileno(file), F_FULLFSYNC, 0);
642 #else
643 fsync(fileno(file));
644 #endif
645 #endif
648 bool TruncateFile(FILE *file, unsigned int length) {
649 #if defined(WIN32)
650 return _chsize(_fileno(file), length) == 0;
651 #else
652 return ftruncate(fileno(file), length) == 0;
653 #endif
657 * this function tries to raise the file descriptor limit to the requested number.
658 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
660 int RaiseFileDescriptorLimit(int nMinFD) {
661 #if defined(WIN32)
662 return 2048;
663 #else
664 struct rlimit limitFD;
665 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
666 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
667 limitFD.rlim_cur = nMinFD;
668 if (limitFD.rlim_cur > limitFD.rlim_max)
669 limitFD.rlim_cur = limitFD.rlim_max;
670 setrlimit(RLIMIT_NOFILE, &limitFD);
671 getrlimit(RLIMIT_NOFILE, &limitFD);
673 return limitFD.rlim_cur;
675 return nMinFD; // getrlimit failed, assume it's fine
676 #endif
680 * this function tries to make a particular range of a file allocated (corresponding to disk space)
681 * it is advisory, and the range specified in the arguments will never contain live data
683 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
684 #if defined(WIN32)
685 // Windows-specific version
686 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
687 LARGE_INTEGER nFileSize;
688 int64_t nEndPos = (int64_t)offset + length;
689 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
690 nFileSize.u.HighPart = nEndPos >> 32;
691 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
692 SetEndOfFile(hFile);
693 #elif defined(MAC_OSX)
694 // OSX specific version
695 fstore_t fst;
696 fst.fst_flags = F_ALLOCATECONTIG;
697 fst.fst_posmode = F_PEOFPOSMODE;
698 fst.fst_offset = 0;
699 fst.fst_length = (off_t)offset + length;
700 fst.fst_bytesalloc = 0;
701 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
702 fst.fst_flags = F_ALLOCATEALL;
703 fcntl(fileno(file), F_PREALLOCATE, &fst);
705 ftruncate(fileno(file), fst.fst_length);
706 #elif defined(__linux__)
707 // Version using posix_fallocate
708 off_t nEndPos = (off_t)offset + length;
709 posix_fallocate(fileno(file), 0, nEndPos);
710 #else
711 // Fallback version
712 // TODO: just write one byte per block
713 static const char buf[65536] = {};
714 fseek(file, offset, SEEK_SET);
715 while (length > 0) {
716 unsigned int now = 65536;
717 if (length < now)
718 now = length;
719 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
720 length -= now;
722 #endif
725 void ShrinkDebugFile()
727 // Amount of debug.log to save at end when shrinking (must fit in memory)
728 constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
729 // Scroll debug.log if it's getting too big
730 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
731 FILE* file = fopen(pathLog.string().c_str(), "r");
732 // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
733 // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
734 if (file && boost::filesystem::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
736 // Restart the file with some of the end
737 std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
738 fseek(file, -((long)vch.size()), SEEK_END);
739 int nBytes = fread(vch.data(), 1, vch.size(), file);
740 fclose(file);
742 file = fopen(pathLog.string().c_str(), "w");
743 if (file)
745 fwrite(vch.data(), 1, nBytes, file);
746 fclose(file);
749 else if (file != NULL)
750 fclose(file);
753 #ifdef WIN32
754 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
756 namespace fs = boost::filesystem;
758 char pszPath[MAX_PATH] = "";
760 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
762 return fs::path(pszPath);
765 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
766 return fs::path("");
768 #endif
770 void runCommand(const std::string& strCommand)
772 int nErr = ::system(strCommand.c_str());
773 if (nErr)
774 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
777 void RenameThread(const char* name)
779 #if defined(PR_SET_NAME)
780 // Only the first 15 characters are used (16 - NUL terminator)
781 ::prctl(PR_SET_NAME, name, 0, 0, 0);
782 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
783 pthread_set_name_np(pthread_self(), name);
785 #elif defined(MAC_OSX)
786 pthread_setname_np(name);
787 #else
788 // Prevent warnings for unused parameters...
789 (void)name;
790 #endif
793 void SetupEnvironment()
795 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
796 // may be invalid, in which case the "C" locale is used as fallback.
797 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
798 try {
799 std::locale(""); // Raises a runtime error if current locale is invalid
800 } catch (const std::runtime_error&) {
801 setenv("LC_ALL", "C", 1);
803 #endif
804 // The path locale is lazy initialized and to avoid deinitialization errors
805 // in multithreading environments, it is set explicitly by the main thread.
806 // A dummy locale is used to extract the internal default locale, used by
807 // boost::filesystem::path, which is then used to explicitly imbue the path.
808 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
809 boost::filesystem::path::imbue(loc);
812 bool SetupNetworking()
814 #ifdef WIN32
815 // Initialize Windows Sockets
816 WSADATA wsadata;
817 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
818 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
819 return false;
820 #endif
821 return true;
824 int GetNumCores()
826 #if BOOST_VERSION >= 105600
827 return boost::thread::physical_concurrency();
828 #else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
829 return boost::thread::hardware_concurrency();
830 #endif
833 std::string CopyrightHolders(const std::string& strPrefix)
835 std::string strCopyrightHolders = strPrefix + strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION));
837 // Check for untranslated substitution to make sure Bitcoin Core copyright is not removed by accident
838 if (strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION).find("Bitcoin Core") == std::string::npos) {
839 strCopyrightHolders += "\n" + strPrefix + "The Bitcoin Core developers";
841 return strCopyrightHolders;