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"
12 #include "chainparamsbase.h"
14 #include "serialize.h"
16 #include "utilstrencodings.h"
21 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
23 #include <pthread_np.h>
27 // for posix_fallocate
30 #ifdef _POSIX_C_SOURCE
31 #undef _POSIX_C_SOURCE
34 #define _POSIX_C_SOURCE 200112L
40 #include <sys/resource.h>
46 #pragma warning(disable:4786)
47 #pragma warning(disable:4804)
48 #pragma warning(disable:4805)
49 #pragma warning(disable:4717)
55 #define _WIN32_WINNT 0x0501
60 #define _WIN32_IE 0x0501
62 #define WIN32_LEAN_AND_MEAN 1
67 #include <io.h> /* for _commit */
71 #ifdef HAVE_SYS_PRCTL_H
72 #include <sys/prctl.h>
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/conf.h>
87 // Work around clang compilation problem in Boost 1.46:
88 // /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
89 // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
90 // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
93 namespace program_options {
94 std::string to_internal(const std::string&);
101 map<string, string> mapArgs;
102 map<string, vector<string> > mapMultiArgs;
104 bool fPrintToConsole = false;
105 bool fPrintToDebugLog = true;
106 bool fDaemon = false;
107 bool fServer = false;
108 string strMiscWarning;
109 bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
110 bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
111 bool fLogIPs = DEFAULT_LOGIPS;
112 std::atomic<bool> fReopenDebugLog(false);
113 CTranslationInterface translationInterface;
115 /** Init OpenSSL library multithreading support */
116 static CCriticalSection** ppmutexOpenSSL;
117 void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
119 if (mode & CRYPTO_LOCK) {
120 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
122 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
132 // Init OpenSSL library multithreading support
133 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
134 for (int i = 0; i < CRYPTO_num_locks(); i++)
135 ppmutexOpenSSL[i] = new CCriticalSection();
136 CRYPTO_set_locking_callback(locking_callback);
138 // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
139 // We don't use them so we don't require the config. However some of our libs may call functions
140 // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
141 // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
142 // that the config appears to have been loaded and there are no modules/engines available.
147 // Shutdown OpenSSL library multithreading support
148 CRYPTO_set_locking_callback(NULL);
149 for (int i = 0; i < CRYPTO_num_locks(); i++)
150 delete ppmutexOpenSSL[i];
151 OPENSSL_free(ppmutexOpenSSL);
157 * LogPrintf() has been broken a couple of times now
158 * by well-meaning people adding mutexes in the most straightforward way.
159 * It breaks because it may be called by global destructors during shutdown.
160 * Since the order of destruction of static/global objects is undefined,
161 * defining a mutex as a global object doesn't work (the mutex gets
162 * destroyed, and then some later destructor calls OutputDebugStringF,
163 * maybe indirectly, and you get a core dump at shutdown trying to lock
167 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
169 * We use boost::call_once() to make sure these are initialized
170 * in a thread-safe manner the first time called:
172 static FILE* fileout = NULL;
173 static boost::mutex* mutexDebugLog = NULL;
175 static void DebugPrintInit()
177 assert(fileout == NULL);
178 assert(mutexDebugLog == NULL);
180 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
181 fileout = fopen(pathDebug.string().c_str(), "a");
182 if (fileout) setbuf(fileout, NULL); // unbuffered
184 mutexDebugLog = new boost::mutex();
187 bool LogAcceptCategory(const char* category)
189 if (category != NULL)
194 // Give each thread quick access to -debug settings.
195 // This helps prevent issues debugging global destructors,
196 // where mapMultiArgs might be deleted before another
197 // global destructor calls LogPrint()
198 static boost::thread_specific_ptr<set<string> > ptrCategory;
199 if (ptrCategory.get() == NULL)
201 const vector<string>& categories = mapMultiArgs["-debug"];
202 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
203 // thread_specific_ptr automatically deletes the set when the thread ends.
205 const set<string>& setCategories = *ptrCategory.get();
207 // if not debugging everything and not debugging specific category, LogPrint does nothing.
208 if (setCategories.count(string("")) == 0 &&
209 setCategories.count(string("1")) == 0 &&
210 setCategories.count(string(category)) == 0)
216 int LogPrintStr(const std::string &str)
218 int ret = 0; // Returns total number of characters written
222 ret = fwrite(str.data(), 1, str.size(), stdout);
225 else if (fPrintToDebugLog && AreBaseParamsConfigured())
227 static bool fStartedNewLine = true;
228 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
233 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
235 // reopen the log file, if requested
236 if (fReopenDebugLog) {
237 fReopenDebugLog = false;
238 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
239 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
240 setbuf(fileout, NULL); // unbuffered
243 // Debug print useful for profiling
244 if (fLogTimestamps && fStartedNewLine)
245 ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
246 if (!str.empty() && str[str.size()-1] == '\n')
247 fStartedNewLine = true;
249 fStartedNewLine = false;
251 ret = fwrite(str.data(), 1, str.size(), fileout);
257 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
259 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
260 if (name.find("-no") == 0)
262 std::string positive("-");
263 positive.append(name.begin()+3, name.end());
264 if (mapSettingsRet.count(positive) == 0)
266 bool value = !GetBoolArg(name, false);
267 mapSettingsRet[positive] = (value ? "1" : "0");
272 void ParseParameters(int argc, const char* const argv[])
275 mapMultiArgs.clear();
277 for (int i = 1; i < argc; i++)
279 std::string str(argv[i]);
280 std::string strValue;
281 size_t is_index = str.find('=');
282 if (is_index != std::string::npos)
284 strValue = str.substr(is_index+1);
285 str = str.substr(0, is_index);
288 boost::to_lower(str);
289 if (boost::algorithm::starts_with(str, "/"))
290 str = "-" + str.substr(1);
296 // Interpret --foo as -foo.
297 // If both --foo and -foo are set, the last takes effect.
298 if (str.length() > 1 && str[1] == '-')
301 mapArgs[str] = strValue;
302 mapMultiArgs[str].push_back(strValue);
306 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
308 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
309 InterpretNegativeSetting(entry.first, mapArgs);
313 std::string GetArg(const std::string& strArg, const std::string& strDefault)
315 if (mapArgs.count(strArg))
316 return mapArgs[strArg];
320 int64_t GetArg(const std::string& strArg, int64_t nDefault)
322 if (mapArgs.count(strArg))
323 return atoi64(mapArgs[strArg]);
327 bool GetBoolArg(const std::string& strArg, bool fDefault)
329 if (mapArgs.count(strArg))
331 if (mapArgs[strArg].empty())
333 return (atoi(mapArgs[strArg]) != 0);
338 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
340 if (mapArgs.count(strArg))
342 mapArgs[strArg] = strValue;
346 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
349 return SoftSetArg(strArg, std::string("1"));
351 return SoftSetArg(strArg, std::string("0"));
354 static const int screenWidth = 79;
355 static const int optIndent = 2;
356 static const int msgIndent = 7;
358 std::string HelpMessageGroup(const std::string &message) {
359 return std::string(message) + std::string("\n\n");
362 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
363 return std::string(optIndent,' ') + std::string(option) +
364 std::string("\n") + std::string(msgIndent,' ') +
365 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
369 static std::string FormatException(const std::exception* pex, const char* pszThread)
372 char pszModule[MAX_PATH] = "";
373 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
375 const char* pszModule = "Zcash";
379 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
382 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
385 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
387 std::string message = FormatException(pex, pszThread);
388 LogPrintf("\n\n************************\n%s\n", message);
389 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
390 strMiscWarning = message;
393 boost::filesystem::path GetDefaultDataDir()
395 namespace fs = boost::filesystem;
396 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
397 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
398 // Mac: ~/Library/Application Support/Zcash
402 return GetSpecialFolderPath(CSIDL_APPDATA) / "Zcash";
405 char* pszHome = getenv("HOME");
406 if (pszHome == NULL || strlen(pszHome) == 0)
407 pathRet = fs::path("/");
409 pathRet = fs::path(pszHome);
412 pathRet /= "Library/Application Support";
413 TryCreateDirectory(pathRet);
414 return pathRet / "Zcash";
417 return pathRet / ".zcash";
422 static boost::filesystem::path pathCached;
423 static boost::filesystem::path pathCachedNetSpecific;
424 static boost::filesystem::path zc_paramsPathCached;
425 static CCriticalSection csPathCached;
427 static boost::filesystem::path ZC_GetBaseParamsDir()
429 // Copied from GetDefaultDataDir and adapter for zcash params.
431 namespace fs = boost::filesystem;
432 // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
433 // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
434 // Mac: ~/Library/Application Support/ZcashParams
435 // Unix: ~/.zcash-params
438 return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
441 char* pszHome = getenv("HOME");
442 if (pszHome == NULL || strlen(pszHome) == 0)
443 pathRet = fs::path("/");
445 pathRet = fs::path(pszHome);
448 pathRet /= "Library/Application Support";
449 TryCreateDirectory(pathRet);
450 return pathRet / "ZcashParams";
453 return pathRet / ".zcash-params";
458 const boost::filesystem::path &ZC_GetParamsDir()
460 namespace fs = boost::filesystem;
462 LOCK(csPathCached); // Reuse the same lock as upstream.
464 fs::path &path = zc_paramsPathCached;
466 // This can be called during exceptions by LogPrintf(), so we cache the
467 // value so we don't have to do memory allocations after that.
471 path = ZC_GetBaseParamsDir();
476 // Return the user specified export directory. Create directory if it doesn't exist.
477 // If user did not set option, return an empty path.
478 // If there is a filesystem problem, throw an exception.
479 const boost::filesystem::path GetExportDir()
481 namespace fs = boost::filesystem;
483 if (mapArgs.count("-exportdir")) {
484 path = fs::system_complete(mapArgs["-exportdir"]);
485 if (fs::exists(path) && !fs::is_directory(path)) {
486 throw std::runtime_error(strprintf("The -exportdir '%s' already exists and is not a directory", path.string()));
488 if (!fs::exists(path) && !fs::create_directories(path)) {
489 throw std::runtime_error(strprintf("Failed to create directory at -exportdir '%s'", path.string()));
496 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
498 namespace fs = boost::filesystem;
502 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
504 // This can be called during exceptions by LogPrintf(), so we cache the
505 // value so we don't have to do memory allocations after that.
509 if (mapArgs.count("-datadir")) {
510 path = fs::system_complete(mapArgs["-datadir"]);
511 if (!fs::is_directory(path)) {
516 path = GetDefaultDataDir();
519 path /= BaseParams().DataDir();
521 fs::create_directories(path);
526 void ClearDatadirCache()
528 pathCached = boost::filesystem::path();
529 pathCachedNetSpecific = boost::filesystem::path();
532 boost::filesystem::path GetConfigFile()
534 boost::filesystem::path pathConfigFile(GetArg("-conf", "zcash.conf"));
535 if (!pathConfigFile.is_complete())
536 pathConfigFile = GetDataDir(false) / pathConfigFile;
538 return pathConfigFile;
541 void ReadConfigFile(map<string, string>& mapSettingsRet,
542 map<string, vector<string> >& mapMultiSettingsRet)
544 boost::filesystem::ifstream streamConfig(GetConfigFile());
545 if (!streamConfig.good())
546 throw missing_zcash_conf();
548 set<string> setOptions;
549 setOptions.insert("*");
551 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
553 // Don't overwrite existing settings so command line settings override zcash.conf
554 string strKey = string("-") + it->string_key;
555 if (mapSettingsRet.count(strKey) == 0)
557 mapSettingsRet[strKey] = it->value[0];
558 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
559 InterpretNegativeSetting(strKey, mapSettingsRet);
561 mapMultiSettingsRet[strKey].push_back(it->value[0]);
563 // If datadir is changed in .conf file:
568 boost::filesystem::path GetPidFile()
570 boost::filesystem::path pathPidFile(GetArg("-pid", "zcashd.pid"));
571 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
575 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
577 FILE* file = fopen(path.string().c_str(), "w");
580 fprintf(file, "%d\n", pid);
586 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
589 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
590 MOVEFILE_REPLACE_EXISTING) != 0;
592 int rc = std::rename(src.string().c_str(), dest.string().c_str());
598 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
599 * Specifically handles case where path p exists, but it wasn't possible for the user to
600 * write to the parent directory.
602 bool TryCreateDirectory(const boost::filesystem::path& p)
606 return boost::filesystem::create_directory(p);
607 } catch (const boost::filesystem::filesystem_error&) {
608 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
612 // create_directory didn't create the directory, it had to have existed already
616 void FileCommit(FILE *fileout)
618 fflush(fileout); // harmless if redundantly called
620 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
621 FlushFileBuffers(hFile);
623 #if defined(__linux__) || defined(__NetBSD__)
624 fdatasync(fileno(fileout));
625 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
626 fcntl(fileno(fileout), F_FULLFSYNC, 0);
628 fsync(fileno(fileout));
633 bool TruncateFile(FILE *file, unsigned int length) {
635 return _chsize(_fileno(file), length) == 0;
637 return ftruncate(fileno(file), length) == 0;
642 * this function tries to raise the file descriptor limit to the requested number.
643 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
645 int RaiseFileDescriptorLimit(int nMinFD) {
649 struct rlimit limitFD;
650 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
651 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
652 limitFD.rlim_cur = nMinFD;
653 if (limitFD.rlim_cur > limitFD.rlim_max)
654 limitFD.rlim_cur = limitFD.rlim_max;
655 setrlimit(RLIMIT_NOFILE, &limitFD);
656 getrlimit(RLIMIT_NOFILE, &limitFD);
658 return limitFD.rlim_cur;
660 return nMinFD; // getrlimit failed, assume it's fine
665 * this function tries to make a particular range of a file allocated (corresponding to disk space)
666 * it is advisory, and the range specified in the arguments will never contain live data
668 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
670 // Windows-specific version
671 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
672 LARGE_INTEGER nFileSize;
673 int64_t nEndPos = (int64_t)offset + length;
674 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
675 nFileSize.u.HighPart = nEndPos >> 32;
676 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
678 #elif defined(MAC_OSX)
679 // OSX specific version
681 fst.fst_flags = F_ALLOCATECONTIG;
682 fst.fst_posmode = F_PEOFPOSMODE;
684 fst.fst_length = (off_t)offset + length;
685 fst.fst_bytesalloc = 0;
686 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
687 fst.fst_flags = F_ALLOCATEALL;
688 fcntl(fileno(file), F_PREALLOCATE, &fst);
690 ftruncate(fileno(file), fst.fst_length);
691 #elif defined(__linux__)
692 // Version using posix_fallocate
693 off_t nEndPos = (off_t)offset + length;
694 posix_fallocate(fileno(file), 0, nEndPos);
697 // TODO: just write one byte per block
698 static const char buf[65536] = {};
699 fseek(file, offset, SEEK_SET);
701 unsigned int now = 65536;
704 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
710 void ShrinkDebugFile()
712 // Scroll debug.log if it's getting too big
713 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
714 FILE* file = fopen(pathLog.string().c_str(), "r");
715 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
717 // Restart the file with some of the end
718 std::vector <char> vch(200000,0);
719 fseek(file, -((long)vch.size()), SEEK_END);
720 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
723 file = fopen(pathLog.string().c_str(), "w");
726 fwrite(begin_ptr(vch), 1, nBytes, file);
730 else if (file != NULL)
735 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
737 namespace fs = boost::filesystem;
739 char pszPath[MAX_PATH] = "";
741 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
743 return fs::path(pszPath);
746 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
751 boost::filesystem::path GetTempPath() {
752 #if BOOST_FILESYSTEM_VERSION == 3
753 return boost::filesystem::temp_directory_path();
755 // TODO: remove when we don't support filesystem v2 anymore
756 boost::filesystem::path path;
758 char pszPath[MAX_PATH] = "";
760 if (GetTempPathA(MAX_PATH, pszPath))
761 path = boost::filesystem::path(pszPath);
763 path = boost::filesystem::path("/tmp");
765 if (path.empty() || !boost::filesystem::is_directory(path)) {
766 LogPrintf("GetTempPath(): failed to find temp path\n");
767 return boost::filesystem::path("");
773 void runCommand(std::string strCommand)
775 int nErr = ::system(strCommand.c_str());
777 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
780 void RenameThread(const char* name)
782 #if defined(PR_SET_NAME)
783 // Only the first 15 characters are used (16 - NUL terminator)
784 ::prctl(PR_SET_NAME, name, 0, 0, 0);
785 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
786 pthread_set_name_np(pthread_self(), name);
788 #elif defined(MAC_OSX)
789 pthread_setname_np(name);
791 // Prevent warnings for unused parameters...
796 void SetupEnvironment()
798 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
799 // may be invalid, in which case the "C" locale is used as fallback.
800 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
802 std::locale(""); // Raises a runtime error if current locale is invalid
803 } catch (const std::runtime_error&) {
804 setenv("LC_ALL", "C", 1);
807 // The path locale is lazy initialized and to avoid deinitialization errors
808 // in multithreading environments, it is set explicitly by the main thread.
809 // A dummy locale is used to extract the internal default locale, used by
810 // boost::filesystem::path, which is then used to explicitly imbue the path.
811 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
812 boost::filesystem::path::imbue(loc);
815 void SetThreadPriority(int nPriority)
818 SetThreadPriority(GetCurrentThread(), nPriority);
821 setpriority(PRIO_THREAD, 0, nPriority);
823 setpriority(PRIO_PROCESS, 0, nPriority);
824 #endif // PRIO_THREAD
828 std::string LicenseInfo()
830 return FormatParagraph(strprintf(_("Copyright (C) 2009-%i The Bitcoin Core Developers"), COPYRIGHT_YEAR)) + "\n" +
831 FormatParagraph(strprintf(_("Copyright (C) 2015-%i The Zcash Developers"), COPYRIGHT_YEAR)) + "\n" +
833 FormatParagraph(_("This is experimental software.")) + "\n" +
835 FormatParagraph(_("Distributed under the MIT software license, see the accompanying file COPYING or <http://www.opensource.org/licenses/mit-license.php>.")) + "\n" +
837 FormatParagraph(_("This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit <https://www.openssl.org/> and cryptographic software written by Eric Young and UPnP software written by Thomas Bernard.")) +