1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 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"
12 #include "chainparamsbase.h"
14 #include "serialize.h"
16 #include "utilstrencodings.h"
22 // for posix_fallocate
25 #ifdef _POSIX_C_SOURCE
26 #undef _POSIX_C_SOURCE
29 #define _POSIX_C_SOURCE 200112L
35 #include <sys/resource.h>
41 #pragma warning(disable:4786)
42 #pragma warning(disable:4804)
43 #pragma warning(disable:4805)
44 #pragma warning(disable:4717)
50 #define _WIN32_WINNT 0x0501
55 #define _WIN32_IE 0x0501
57 #define WIN32_LEAN_AND_MEAN 1
62 #include <io.h> /* for _commit */
66 #ifdef HAVE_SYS_PRCTL_H
67 #include <sys/prctl.h>
70 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
71 #include <boost/algorithm/string/join.hpp>
72 #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
73 #include <boost/filesystem.hpp>
74 #include <boost/filesystem/fstream.hpp>
75 #include <boost/foreach.hpp>
76 #include <boost/program_options/detail/config_file.hpp>
77 #include <boost/program_options/parsers.hpp>
78 #include <boost/thread.hpp>
79 #include <openssl/crypto.h>
80 #include <openssl/rand.h>
82 // Work around clang compilation problem in Boost 1.46:
83 // /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
84 // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
85 // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
88 namespace program_options {
89 std::string to_internal(const std::string&);
96 map<string, string> mapArgs;
97 map<string, vector<string> > mapMultiArgs;
99 bool fPrintToConsole = false;
100 bool fPrintToDebugLog = true;
101 bool fDaemon = false;
102 bool fServer = false;
103 string strMiscWarning;
104 bool fLogTimestamps = false;
105 bool fLogIPs = false;
106 volatile bool fReopenDebugLog = false;
108 // Init OpenSSL library multithreading support
109 static CCriticalSection** ppmutexOpenSSL;
110 void locking_callback(int mode, int i, const char* file, int line)
112 if (mode & CRYPTO_LOCK) {
113 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
115 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
125 // Init OpenSSL library multithreading support
126 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
127 for (int i = 0; i < CRYPTO_num_locks(); i++)
128 ppmutexOpenSSL[i] = new CCriticalSection();
129 CRYPTO_set_locking_callback(locking_callback);
132 // Seed OpenSSL PRNG with current contents of the screen
136 // Seed OpenSSL PRNG with performance counter
141 // Securely erase the memory used by the PRNG
143 // Shutdown OpenSSL library multithreading support
144 CRYPTO_set_locking_callback(NULL);
145 for (int i = 0; i < CRYPTO_num_locks(); i++)
146 delete ppmutexOpenSSL[i];
147 OPENSSL_free(ppmutexOpenSSL);
152 // LogPrintf() has been broken a couple of times now
153 // by well-meaning people adding mutexes in the most straightforward way.
154 // It breaks because it may be called by global destructors during shutdown.
155 // Since the order of destruction of static/global objects is undefined,
156 // defining a mutex as a global object doesn't work (the mutex gets
157 // destroyed, and then some later destructor calls OutputDebugStringF,
158 // maybe indirectly, and you get a core dump at shutdown trying to lock
161 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
162 // We use boost::call_once() to make sure these are initialized in
163 // in a thread-safe manner the first time it is called:
164 static FILE* fileout = NULL;
165 static boost::mutex* mutexDebugLog = NULL;
167 static void DebugPrintInit()
169 assert(fileout == NULL);
170 assert(mutexDebugLog == NULL);
172 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
173 fileout = fopen(pathDebug.string().c_str(), "a");
174 if (fileout) setbuf(fileout, NULL); // unbuffered
176 mutexDebugLog = new boost::mutex();
179 bool LogAcceptCategory(const char* category)
181 if (category != NULL)
186 // Give each thread quick access to -debug settings.
187 // This helps prevent issues debugging global destructors,
188 // where mapMultiArgs might be deleted before another
189 // global destructor calls LogPrint()
190 static boost::thread_specific_ptr<set<string> > ptrCategory;
191 if (ptrCategory.get() == NULL)
193 const vector<string>& categories = mapMultiArgs["-debug"];
194 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
195 // thread_specific_ptr automatically deletes the set when the thread ends.
197 const set<string>& setCategories = *ptrCategory.get();
199 // if not debugging everything and not debugging specific category, LogPrint does nothing.
200 if (setCategories.count(string("")) == 0 &&
201 setCategories.count(string(category)) == 0)
207 int LogPrintStr(const std::string &str)
209 int ret = 0; // Returns total number of characters written
213 ret = fwrite(str.data(), 1, str.size(), stdout);
215 else if (fPrintToDebugLog && AreBaseParamsConfigured())
217 static bool fStartedNewLine = true;
218 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
223 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
225 // reopen the log file, if requested
226 if (fReopenDebugLog) {
227 fReopenDebugLog = false;
228 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
229 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
230 setbuf(fileout, NULL); // unbuffered
233 // Debug print useful for profiling
234 if (fLogTimestamps && fStartedNewLine)
235 ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
236 if (!str.empty() && str[str.size()-1] == '\n')
237 fStartedNewLine = true;
239 fStartedNewLine = false;
241 ret = fwrite(str.data(), 1, str.size(), fileout);
247 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
249 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
250 if (name.find("-no") == 0)
252 std::string positive("-");
253 positive.append(name.begin()+3, name.end());
254 if (mapSettingsRet.count(positive) == 0)
256 bool value = !GetBoolArg(name, false);
257 mapSettingsRet[positive] = (value ? "1" : "0");
262 void ParseParameters(int argc, const char* const argv[])
265 mapMultiArgs.clear();
267 for (int i = 1; i < argc; i++)
269 std::string str(argv[i]);
270 std::string strValue;
271 size_t is_index = str.find('=');
272 if (is_index != std::string::npos)
274 strValue = str.substr(is_index+1);
275 str = str.substr(0, is_index);
278 boost::to_lower(str);
279 if (boost::algorithm::starts_with(str, "/"))
280 str = "-" + str.substr(1);
286 // Interpret --foo as -foo.
287 // If both --foo and -foo are set, the last takes effect.
288 if (str.length() > 1 && str[1] == '-')
291 mapArgs[str] = strValue;
292 mapMultiArgs[str].push_back(strValue);
296 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
298 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
299 InterpretNegativeSetting(entry.first, mapArgs);
303 std::string GetArg(const std::string& strArg, const std::string& strDefault)
305 if (mapArgs.count(strArg))
306 return mapArgs[strArg];
310 int64_t GetArg(const std::string& strArg, int64_t nDefault)
312 if (mapArgs.count(strArg))
313 return atoi64(mapArgs[strArg]);
317 bool GetBoolArg(const std::string& strArg, bool fDefault)
319 if (mapArgs.count(strArg))
321 if (mapArgs[strArg].empty())
323 return (atoi(mapArgs[strArg]) != 0);
328 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
330 if (mapArgs.count(strArg))
332 mapArgs[strArg] = strValue;
336 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
339 return SoftSetArg(strArg, std::string("1"));
341 return SoftSetArg(strArg, std::string("0"));
344 static std::string FormatException(std::exception* pex, const char* pszThread)
347 char pszModule[MAX_PATH] = "";
348 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
350 const char* pszModule = "bitcoin";
354 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
357 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
360 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
362 std::string message = FormatException(pex, pszThread);
363 LogPrintf("\n\n************************\n%s\n", message);
364 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
365 strMiscWarning = message;
368 boost::filesystem::path GetDefaultDataDir()
370 namespace fs = boost::filesystem;
371 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
372 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
373 // Mac: ~/Library/Application Support/Bitcoin
377 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
380 char* pszHome = getenv("HOME");
381 if (pszHome == NULL || strlen(pszHome) == 0)
382 pathRet = fs::path("/");
384 pathRet = fs::path(pszHome);
387 pathRet /= "Library/Application Support";
388 TryCreateDirectory(pathRet);
389 return pathRet / "Bitcoin";
392 return pathRet / ".bitcoin";
397 static boost::filesystem::path pathCached[CBaseChainParams::MAX_NETWORK_TYPES+1];
398 static CCriticalSection csPathCached;
400 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
402 namespace fs = boost::filesystem;
406 int nNet = CBaseChainParams::MAX_NETWORK_TYPES;
407 if (fNetSpecific) nNet = BaseParams().NetworkID();
409 fs::path &path = pathCached[nNet];
411 // This can be called during exceptions by LogPrintf(), so we cache the
412 // value so we don't have to do memory allocations after that.
416 if (mapArgs.count("-datadir")) {
417 path = fs::system_complete(mapArgs["-datadir"]);
418 if (!fs::is_directory(path)) {
423 path = GetDefaultDataDir();
426 path /= BaseParams().DataDir();
428 fs::create_directories(path);
433 void ClearDatadirCache()
435 std::fill(&pathCached[0], &pathCached[CBaseChainParams::MAX_NETWORK_TYPES+1],
436 boost::filesystem::path());
439 boost::filesystem::path GetConfigFile()
441 boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf"));
442 if (!pathConfigFile.is_complete())
443 pathConfigFile = GetDataDir(false) / pathConfigFile;
445 return pathConfigFile;
448 void ReadConfigFile(map<string, string>& mapSettingsRet,
449 map<string, vector<string> >& mapMultiSettingsRet)
451 boost::filesystem::ifstream streamConfig(GetConfigFile());
452 if (!streamConfig.good())
453 return; // No bitcoin.conf file is OK
455 set<string> setOptions;
456 setOptions.insert("*");
458 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
460 // Don't overwrite existing settings so command line settings override bitcoin.conf
461 string strKey = string("-") + it->string_key;
462 if (mapSettingsRet.count(strKey) == 0)
464 mapSettingsRet[strKey] = it->value[0];
465 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
466 InterpretNegativeSetting(strKey, mapSettingsRet);
468 mapMultiSettingsRet[strKey].push_back(it->value[0]);
470 // If datadir is changed in .conf file:
474 boost::filesystem::path GetPidFile()
476 boost::filesystem::path pathPidFile(GetArg("-pid", "bitcoind.pid"));
477 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
482 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
484 FILE* file = fopen(path.string().c_str(), "w");
487 fprintf(file, "%d\n", pid);
493 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
496 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
497 MOVEFILE_REPLACE_EXISTING);
499 int rc = std::rename(src.string().c_str(), dest.string().c_str());
504 // Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
505 // Specifically handles case where path p exists, but it wasn't possible for the user to
506 // write to the parent directory.
507 bool TryCreateDirectory(const boost::filesystem::path& p)
511 return boost::filesystem::create_directory(p);
512 } catch (boost::filesystem::filesystem_error) {
513 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
517 // create_directory didn't create the directory, it had to have existed already
521 void FileCommit(FILE *fileout)
523 fflush(fileout); // harmless if redundantly called
525 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
526 FlushFileBuffers(hFile);
528 #if defined(__linux__) || defined(__NetBSD__)
529 fdatasync(fileno(fileout));
530 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
531 fcntl(fileno(fileout), F_FULLFSYNC, 0);
533 fsync(fileno(fileout));
538 bool TruncateFile(FILE *file, unsigned int length) {
540 return _chsize(_fileno(file), length) == 0;
542 return ftruncate(fileno(file), length) == 0;
546 // this function tries to raise the file descriptor limit to the requested number.
547 // It returns the actual file descriptor limit (which may be more or less than nMinFD)
548 int RaiseFileDescriptorLimit(int nMinFD) {
552 struct rlimit limitFD;
553 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
554 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
555 limitFD.rlim_cur = nMinFD;
556 if (limitFD.rlim_cur > limitFD.rlim_max)
557 limitFD.rlim_cur = limitFD.rlim_max;
558 setrlimit(RLIMIT_NOFILE, &limitFD);
559 getrlimit(RLIMIT_NOFILE, &limitFD);
561 return limitFD.rlim_cur;
563 return nMinFD; // getrlimit failed, assume it's fine
567 // this function tries to make a particular range of a file allocated (corresponding to disk space)
568 // it is advisory, and the range specified in the arguments will never contain live data
569 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
571 // Windows-specific version
572 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
573 LARGE_INTEGER nFileSize;
574 int64_t nEndPos = (int64_t)offset + length;
575 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
576 nFileSize.u.HighPart = nEndPos >> 32;
577 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
579 #elif defined(MAC_OSX)
580 // OSX specific version
582 fst.fst_flags = F_ALLOCATECONTIG;
583 fst.fst_posmode = F_PEOFPOSMODE;
585 fst.fst_length = (off_t)offset + length;
586 fst.fst_bytesalloc = 0;
587 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
588 fst.fst_flags = F_ALLOCATEALL;
589 fcntl(fileno(file), F_PREALLOCATE, &fst);
591 ftruncate(fileno(file), fst.fst_length);
592 #elif defined(__linux__)
593 // Version using posix_fallocate
594 off_t nEndPos = (off_t)offset + length;
595 posix_fallocate(fileno(file), 0, nEndPos);
598 // TODO: just write one byte per block
599 static const char buf[65536] = {};
600 fseek(file, offset, SEEK_SET);
602 unsigned int now = 65536;
605 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
611 void ShrinkDebugFile()
613 // Scroll debug.log if it's getting too big
614 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
615 FILE* file = fopen(pathLog.string().c_str(), "r");
616 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
618 // Restart the file with some of the end
619 std::vector <char> vch(200000,0);
620 fseek(file, -vch.size(), SEEK_END);
621 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
624 file = fopen(pathLog.string().c_str(), "w");
627 fwrite(begin_ptr(vch), 1, nBytes, file);
631 else if (file != NULL)
636 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
638 namespace fs = boost::filesystem;
640 char pszPath[MAX_PATH] = "";
642 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
644 return fs::path(pszPath);
647 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
652 boost::filesystem::path GetTempPath() {
653 #if BOOST_FILESYSTEM_VERSION == 3
654 return boost::filesystem::temp_directory_path();
656 // TODO: remove when we don't support filesystem v2 anymore
657 boost::filesystem::path path;
659 char pszPath[MAX_PATH] = "";
661 if (GetTempPathA(MAX_PATH, pszPath))
662 path = boost::filesystem::path(pszPath);
664 path = boost::filesystem::path("/tmp");
666 if (path.empty() || !boost::filesystem::is_directory(path)) {
667 LogPrintf("GetTempPath(): failed to find temp path\n");
668 return boost::filesystem::path("");
674 void runCommand(std::string strCommand)
676 int nErr = ::system(strCommand.c_str());
678 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
681 void RenameThread(const char* name)
683 #if defined(PR_SET_NAME)
684 // Only the first 15 characters are used (16 - NUL terminator)
685 ::prctl(PR_SET_NAME, name, 0, 0, 0);
686 #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
687 // TODO: This is currently disabled because it needs to be verified to work
688 // on FreeBSD or OpenBSD first. When verified the '0 &&' part can be
690 pthread_set_name_np(pthread_self(), name);
692 #elif defined(MAC_OSX) && defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
694 // pthread_setname_np is XCode 10.6-and-later
695 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
696 pthread_setname_np(name);
700 // Prevent warnings for unused parameters...
705 void SetupEnvironment()
710 #if BOOST_FILESYSTEM_VERSION == 3
711 boost::filesystem::path::codecvt(); // Raises runtime error if current locale is invalid
712 #else // boost filesystem v2
713 std::locale(); // Raises runtime error if current locale is invalid
715 } catch(std::runtime_error &e)
717 setenv("LC_ALL", "C", 1); // Force C locale
722 void SetThreadPriority(int nPriority)
725 SetThreadPriority(GetCurrentThread(), nPriority);
728 setpriority(PRIO_THREAD, 0, nPriority);
730 setpriority(PRIO_PROCESS, 0, nPriority);
731 #endif // PRIO_THREAD