[depends] native ccache 3.2.4
[bitcoinplatinum.git] / src / util.cpp
blobe8514a2ef0ea6902cfbee58b579dc40dbc1d9e0b
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 map<string, string> mapArgs;
103 map<string, vector<string> > mapMultiArgs;
104 bool fDebug = false;
105 bool fPrintToConsole = false;
106 bool fPrintToDebugLog = true;
107 bool fDaemon = false;
108 bool fServer = false;
109 string strMiscWarning;
110 bool fLogTimestamps = false;
111 bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
112 bool fLogIPs = false;
113 volatile bool fReopenDebugLog = false;
114 CTranslationInterface translationInterface;
116 /** Init OpenSSL library multithreading support */
117 static CCriticalSection** ppmutexOpenSSL;
118 void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
120 if (mode & CRYPTO_LOCK) {
121 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
122 } else {
123 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
127 // Init
128 class CInit
130 public:
131 CInit()
133 // Init OpenSSL library multithreading support
134 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
135 for (int i = 0; i < CRYPTO_num_locks(); i++)
136 ppmutexOpenSSL[i] = new CCriticalSection();
137 CRYPTO_set_locking_callback(locking_callback);
139 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
140 // We don't use them so we don't require the config. However some of our libs may call functions
141 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
142 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
143 // that the config appears to have been loaded and there are no modules/engines available.
144 OPENSSL_no_config();
146 #ifdef WIN32
147 // Seed OpenSSL PRNG with current contents of the screen
148 RAND_screen();
149 #endif
151 // Seed OpenSSL PRNG with performance counter
152 RandAddSeed();
154 ~CInit()
156 // Securely erase the memory used by the PRNG
157 RAND_cleanup();
158 // Shutdown OpenSSL library multithreading support
159 CRYPTO_set_locking_callback(NULL);
160 for (int i = 0; i < CRYPTO_num_locks(); i++)
161 delete ppmutexOpenSSL[i];
162 OPENSSL_free(ppmutexOpenSSL);
165 instance_of_cinit;
168 * LogPrintf() has been broken a couple of times now
169 * by well-meaning people adding mutexes in the most straightforward way.
170 * It breaks because it may be called by global destructors during shutdown.
171 * Since the order of destruction of static/global objects is undefined,
172 * defining a mutex as a global object doesn't work (the mutex gets
173 * destroyed, and then some later destructor calls OutputDebugStringF,
174 * maybe indirectly, and you get a core dump at shutdown trying to lock
175 * the mutex).
178 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
181 * We use boost::call_once() to make sure mutexDebugLog and
182 * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
184 * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
185 * are leaked on exit. This is ugly, but will be cleaned up by
186 * the OS/libc. When the shutdown sequence is fully audited and
187 * tested, explicit destruction of these objects can be implemented.
189 static FILE* fileout = NULL;
190 static boost::mutex* mutexDebugLog = NULL;
191 static list<string> *vMsgsBeforeOpenLog;
193 static int FileWriteStr(const std::string &str, FILE *fp)
195 return fwrite(str.data(), 1, str.size(), fp);
198 static void DebugPrintInit()
200 assert(mutexDebugLog == NULL);
201 mutexDebugLog = new boost::mutex();
202 vMsgsBeforeOpenLog = new list<string>;
205 void OpenDebugLog()
207 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
208 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
210 assert(fileout == NULL);
211 assert(vMsgsBeforeOpenLog);
212 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
213 fileout = fopen(pathDebug.string().c_str(), "a");
214 if (fileout) setbuf(fileout, NULL); // unbuffered
216 // dump buffered messages from before we opened the log
217 while (!vMsgsBeforeOpenLog->empty()) {
218 FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
219 vMsgsBeforeOpenLog->pop_front();
222 delete vMsgsBeforeOpenLog;
223 vMsgsBeforeOpenLog = NULL;
226 bool LogAcceptCategory(const char* category)
228 if (category != NULL)
230 if (!fDebug)
231 return false;
233 // Give each thread quick access to -debug settings.
234 // This helps prevent issues debugging global destructors,
235 // where mapMultiArgs might be deleted before another
236 // global destructor calls LogPrint()
237 static boost::thread_specific_ptr<set<string> > ptrCategory;
238 if (ptrCategory.get() == NULL)
240 const vector<string>& categories = mapMultiArgs["-debug"];
241 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
242 // thread_specific_ptr automatically deletes the set when the thread ends.
244 const set<string>& setCategories = *ptrCategory.get();
246 // if not debugging everything and not debugging specific category, LogPrint does nothing.
247 if (setCategories.count(string("")) == 0 &&
248 setCategories.count(string("1")) == 0 &&
249 setCategories.count(string(category)) == 0)
250 return false;
252 return true;
256 * fStartedNewLine is a state variable held by the calling context that will
257 * suppress printing of the timestamp when multiple calls are made that don't
258 * end in a newline. Initialize it to true, and hold it, in the calling context.
260 static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
262 string strStamped;
264 if (!fLogTimestamps)
265 return str;
267 if (*fStartedNewLine) {
268 int64_t nTimeMicros = GetLogTimeMicros();
269 strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
270 if (fLogTimeMicros)
271 strStamped += strprintf(".%06d", nTimeMicros%1000000);
272 strStamped += ' ' + str;
273 } else
274 strStamped = str;
276 if (!str.empty() && str[str.size()-1] == '\n')
277 *fStartedNewLine = true;
278 else
279 *fStartedNewLine = false;
281 return strStamped;
284 int LogPrintStr(const std::string &str)
286 int ret = 0; // Returns total number of characters written
287 static bool fStartedNewLine = true;
289 string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
291 if (fPrintToConsole)
293 // print to console
294 ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
295 fflush(stdout);
297 else if (fPrintToDebugLog)
299 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
300 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
302 // buffer if we haven't opened the log yet
303 if (fileout == NULL) {
304 assert(vMsgsBeforeOpenLog);
305 ret = strTimestamped.length();
306 vMsgsBeforeOpenLog->push_back(strTimestamped);
308 else
310 // reopen the log file, if requested
311 if (fReopenDebugLog) {
312 fReopenDebugLog = false;
313 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
314 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
315 setbuf(fileout, NULL); // unbuffered
318 ret = FileWriteStr(strTimestamped, fileout);
321 return ret;
324 /** Interpret string as boolean, for argument parsing */
325 static bool InterpretBool(const std::string& strValue)
327 if (strValue.empty())
328 return true;
329 return (atoi(strValue) != 0);
332 /** Turn -noX into -X=0 */
333 static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
335 if (strKey.length()>3 && strKey[0]=='-' && strKey[1]=='n' && strKey[2]=='o')
337 strKey = "-" + strKey.substr(3);
338 strValue = InterpretBool(strValue) ? "0" : "1";
342 void ParseParameters(int argc, const char* const argv[])
344 mapArgs.clear();
345 mapMultiArgs.clear();
347 for (int i = 1; i < argc; i++)
349 std::string str(argv[i]);
350 std::string strValue;
351 size_t is_index = str.find('=');
352 if (is_index != std::string::npos)
354 strValue = str.substr(is_index+1);
355 str = str.substr(0, is_index);
357 #ifdef WIN32
358 boost::to_lower(str);
359 if (boost::algorithm::starts_with(str, "/"))
360 str = "-" + str.substr(1);
361 #endif
363 if (str[0] != '-')
364 break;
366 // Interpret --foo as -foo.
367 // If both --foo and -foo are set, the last takes effect.
368 if (str.length() > 1 && str[1] == '-')
369 str = str.substr(1);
370 InterpretNegativeSetting(str, strValue);
372 mapArgs[str] = strValue;
373 mapMultiArgs[str].push_back(strValue);
377 std::string GetArg(const std::string& strArg, const std::string& strDefault)
379 if (mapArgs.count(strArg))
380 return mapArgs[strArg];
381 return strDefault;
384 int64_t GetArg(const std::string& strArg, int64_t nDefault)
386 if (mapArgs.count(strArg))
387 return atoi64(mapArgs[strArg]);
388 return nDefault;
391 bool GetBoolArg(const std::string& strArg, bool fDefault)
393 if (mapArgs.count(strArg))
394 return InterpretBool(mapArgs[strArg]);
395 return fDefault;
398 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
400 if (mapArgs.count(strArg))
401 return false;
402 mapArgs[strArg] = strValue;
403 return true;
406 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
408 if (fValue)
409 return SoftSetArg(strArg, std::string("1"));
410 else
411 return SoftSetArg(strArg, std::string("0"));
414 static const int screenWidth = 79;
415 static const int optIndent = 2;
416 static const int msgIndent = 7;
418 std::string HelpMessageGroup(const std::string &message) {
419 return std::string(message) + std::string("\n\n");
422 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
423 return std::string(optIndent,' ') + std::string(option) +
424 std::string("\n") + std::string(msgIndent,' ') +
425 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
426 std::string("\n\n");
429 static std::string FormatException(const std::exception* pex, const char* pszThread)
431 #ifdef WIN32
432 char pszModule[MAX_PATH] = "";
433 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
434 #else
435 const char* pszModule = "bitcoin";
436 #endif
437 if (pex)
438 return strprintf(
439 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
440 else
441 return strprintf(
442 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
445 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
447 std::string message = FormatException(pex, pszThread);
448 LogPrintf("\n\n************************\n%s\n", message);
449 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
450 strMiscWarning = message;
453 boost::filesystem::path GetDefaultDataDir()
455 namespace fs = boost::filesystem;
456 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
457 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
458 // Mac: ~/Library/Application Support/Bitcoin
459 // Unix: ~/.bitcoin
460 #ifdef WIN32
461 // Windows
462 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
463 #else
464 fs::path pathRet;
465 char* pszHome = getenv("HOME");
466 if (pszHome == NULL || strlen(pszHome) == 0)
467 pathRet = fs::path("/");
468 else
469 pathRet = fs::path(pszHome);
470 #ifdef MAC_OSX
471 // Mac
472 pathRet /= "Library/Application Support";
473 TryCreateDirectory(pathRet);
474 return pathRet / "Bitcoin";
475 #else
476 // Unix
477 return pathRet / ".bitcoin";
478 #endif
479 #endif
482 static boost::filesystem::path pathCached;
483 static boost::filesystem::path pathCachedNetSpecific;
484 static CCriticalSection csPathCached;
486 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
488 namespace fs = boost::filesystem;
490 LOCK(csPathCached);
492 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
494 // This can be called during exceptions by LogPrintf(), so we cache the
495 // value so we don't have to do memory allocations after that.
496 if (!path.empty())
497 return path;
499 if (mapArgs.count("-datadir")) {
500 path = fs::system_complete(mapArgs["-datadir"]);
501 if (!fs::is_directory(path)) {
502 path = "";
503 return path;
505 } else {
506 path = GetDefaultDataDir();
508 if (fNetSpecific)
509 path /= BaseParams().DataDir();
511 fs::create_directories(path);
513 return path;
516 void ClearDatadirCache()
518 pathCached = boost::filesystem::path();
519 pathCachedNetSpecific = boost::filesystem::path();
522 boost::filesystem::path GetConfigFile()
524 boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf"));
525 if (!pathConfigFile.is_complete())
526 pathConfigFile = GetDataDir(false) / pathConfigFile;
528 return pathConfigFile;
531 void ReadConfigFile(map<string, string>& mapSettingsRet,
532 map<string, vector<string> >& mapMultiSettingsRet)
534 boost::filesystem::ifstream streamConfig(GetConfigFile());
535 if (!streamConfig.good())
536 return; // No bitcoin.conf file is OK
538 set<string> setOptions;
539 setOptions.insert("*");
541 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
543 // Don't overwrite existing settings so command line settings override bitcoin.conf
544 string strKey = string("-") + it->string_key;
545 string strValue = it->value[0];
546 InterpretNegativeSetting(strKey, strValue);
547 if (mapSettingsRet.count(strKey) == 0)
548 mapSettingsRet[strKey] = strValue;
549 mapMultiSettingsRet[strKey].push_back(strValue);
551 // If datadir is changed in .conf file:
552 ClearDatadirCache();
555 #ifndef WIN32
556 boost::filesystem::path GetPidFile()
558 boost::filesystem::path pathPidFile(GetArg("-pid", "bitcoind.pid"));
559 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
560 return pathPidFile;
563 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
565 FILE* file = fopen(path.string().c_str(), "w");
566 if (file)
568 fprintf(file, "%d\n", pid);
569 fclose(file);
572 #endif
574 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
576 #ifdef WIN32
577 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
578 MOVEFILE_REPLACE_EXISTING) != 0;
579 #else
580 int rc = std::rename(src.string().c_str(), dest.string().c_str());
581 return (rc == 0);
582 #endif /* WIN32 */
586 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
587 * Specifically handles case where path p exists, but it wasn't possible for the user to
588 * write to the parent directory.
590 bool TryCreateDirectory(const boost::filesystem::path& p)
594 return boost::filesystem::create_directory(p);
595 } catch (const boost::filesystem::filesystem_error&) {
596 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
597 throw;
600 // create_directory didn't create the directory, it had to have existed already
601 return false;
604 void FileCommit(FILE *fileout)
606 fflush(fileout); // harmless if redundantly called
607 #ifdef WIN32
608 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
609 FlushFileBuffers(hFile);
610 #else
611 #if defined(__linux__) || defined(__NetBSD__)
612 fdatasync(fileno(fileout));
613 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
614 fcntl(fileno(fileout), F_FULLFSYNC, 0);
615 #else
616 fsync(fileno(fileout));
617 #endif
618 #endif
621 bool TruncateFile(FILE *file, unsigned int length) {
622 #if defined(WIN32)
623 return _chsize(_fileno(file), length) == 0;
624 #else
625 return ftruncate(fileno(file), length) == 0;
626 #endif
630 * this function tries to raise the file descriptor limit to the requested number.
631 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
633 int RaiseFileDescriptorLimit(int nMinFD) {
634 #if defined(WIN32)
635 return 2048;
636 #else
637 struct rlimit limitFD;
638 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
639 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
640 limitFD.rlim_cur = nMinFD;
641 if (limitFD.rlim_cur > limitFD.rlim_max)
642 limitFD.rlim_cur = limitFD.rlim_max;
643 setrlimit(RLIMIT_NOFILE, &limitFD);
644 getrlimit(RLIMIT_NOFILE, &limitFD);
646 return limitFD.rlim_cur;
648 return nMinFD; // getrlimit failed, assume it's fine
649 #endif
653 * this function tries to make a particular range of a file allocated (corresponding to disk space)
654 * it is advisory, and the range specified in the arguments will never contain live data
656 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
657 #if defined(WIN32)
658 // Windows-specific version
659 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
660 LARGE_INTEGER nFileSize;
661 int64_t nEndPos = (int64_t)offset + length;
662 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
663 nFileSize.u.HighPart = nEndPos >> 32;
664 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
665 SetEndOfFile(hFile);
666 #elif defined(MAC_OSX)
667 // OSX specific version
668 fstore_t fst;
669 fst.fst_flags = F_ALLOCATECONTIG;
670 fst.fst_posmode = F_PEOFPOSMODE;
671 fst.fst_offset = 0;
672 fst.fst_length = (off_t)offset + length;
673 fst.fst_bytesalloc = 0;
674 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
675 fst.fst_flags = F_ALLOCATEALL;
676 fcntl(fileno(file), F_PREALLOCATE, &fst);
678 ftruncate(fileno(file), fst.fst_length);
679 #elif defined(__linux__)
680 // Version using posix_fallocate
681 off_t nEndPos = (off_t)offset + length;
682 posix_fallocate(fileno(file), 0, nEndPos);
683 #else
684 // Fallback version
685 // TODO: just write one byte per block
686 static const char buf[65536] = {};
687 fseek(file, offset, SEEK_SET);
688 while (length > 0) {
689 unsigned int now = 65536;
690 if (length < now)
691 now = length;
692 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
693 length -= now;
695 #endif
698 void ShrinkDebugFile()
700 // Scroll debug.log if it's getting too big
701 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
702 FILE* file = fopen(pathLog.string().c_str(), "r");
703 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
705 // Restart the file with some of the end
706 std::vector <char> vch(200000,0);
707 fseek(file, -((long)vch.size()), SEEK_END);
708 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
709 fclose(file);
711 file = fopen(pathLog.string().c_str(), "w");
712 if (file)
714 fwrite(begin_ptr(vch), 1, nBytes, file);
715 fclose(file);
718 else if (file != NULL)
719 fclose(file);
722 #ifdef WIN32
723 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
725 namespace fs = boost::filesystem;
727 char pszPath[MAX_PATH] = "";
729 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
731 return fs::path(pszPath);
734 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
735 return fs::path("");
737 #endif
739 boost::filesystem::path GetTempPath() {
740 #if BOOST_FILESYSTEM_VERSION == 3
741 return boost::filesystem::temp_directory_path();
742 #else
743 // TODO: remove when we don't support filesystem v2 anymore
744 boost::filesystem::path path;
745 #ifdef WIN32
746 char pszPath[MAX_PATH] = "";
748 if (GetTempPathA(MAX_PATH, pszPath))
749 path = boost::filesystem::path(pszPath);
750 #else
751 path = boost::filesystem::path("/tmp");
752 #endif
753 if (path.empty() || !boost::filesystem::is_directory(path)) {
754 LogPrintf("GetTempPath(): failed to find temp path\n");
755 return boost::filesystem::path("");
757 return path;
758 #endif
761 void runCommand(const std::string& strCommand)
763 int nErr = ::system(strCommand.c_str());
764 if (nErr)
765 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
768 void RenameThread(const char* name)
770 #if defined(PR_SET_NAME)
771 // Only the first 15 characters are used (16 - NUL terminator)
772 ::prctl(PR_SET_NAME, name, 0, 0, 0);
773 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
774 pthread_set_name_np(pthread_self(), name);
776 #elif defined(MAC_OSX)
777 pthread_setname_np(name);
778 #else
779 // Prevent warnings for unused parameters...
780 (void)name;
781 #endif
784 void SetupEnvironment()
786 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
787 // may be invalid, in which case the "C" locale is used as fallback.
788 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
789 try {
790 std::locale(""); // Raises a runtime error if current locale is invalid
791 } catch (const std::runtime_error&) {
792 setenv("LC_ALL", "C", 1);
794 #endif
795 // The path locale is lazy initialized and to avoid deinitialization errors
796 // in multithreading environments, it is set explicitly by the main thread.
797 // A dummy locale is used to extract the internal default locale, used by
798 // boost::filesystem::path, which is then used to explicitly imbue the path.
799 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
800 boost::filesystem::path::imbue(loc);
803 bool SetupNetworking()
805 #ifdef WIN32
806 // Initialize Windows Sockets
807 WSADATA wsadata;
808 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
809 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
810 return false;
811 #endif
812 return true;
815 void SetThreadPriority(int nPriority)
817 #ifdef WIN32
818 SetThreadPriority(GetCurrentThread(), nPriority);
819 #else // WIN32
820 #ifdef PRIO_THREAD
821 setpriority(PRIO_THREAD, 0, nPriority);
822 #else // PRIO_THREAD
823 setpriority(PRIO_PROCESS, 0, nPriority);
824 #endif // PRIO_THREAD
825 #endif // WIN32
828 int GetNumCores()
830 #if BOOST_VERSION >= 105600
831 return boost::thread::physical_concurrency();
832 #else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
833 return boost::thread::hardware_concurrency();
834 #endif