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"
10 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
12 #include <pthread_np.h>
17 #include "chainparamsbase.h"
19 #include "serialize.h"
21 #include "utilstrencodings.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/rand.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 = false;
110 bool fLogIPs = false;
111 volatile bool fReopenDebugLog = false;
112 CTranslationInterface translationInterface;
114 /** Init OpenSSL library multithreading support */
115 static CCriticalSection** ppmutexOpenSSL;
116 void locking_callback(int mode, int i, const char* file, int line)
118 if (mode & CRYPTO_LOCK) {
119 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
121 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
131 // Init OpenSSL library multithreading support
132 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
133 for (int i = 0; i < CRYPTO_num_locks(); i++)
134 ppmutexOpenSSL[i] = new CCriticalSection();
135 CRYPTO_set_locking_callback(locking_callback);
138 // Seed OpenSSL PRNG with current contents of the screen
142 // Seed OpenSSL PRNG with performance counter
147 // Securely erase the memory used by the PRNG
149 // Shutdown OpenSSL library multithreading support
150 CRYPTO_set_locking_callback(NULL);
151 for (int i = 0; i < CRYPTO_num_locks(); i++)
152 delete ppmutexOpenSSL[i];
153 OPENSSL_free(ppmutexOpenSSL);
159 * LogPrintf() has been broken a couple of times now
160 * by well-meaning people adding mutexes in the most straightforward way.
161 * It breaks because it may be called by global destructors during shutdown.
162 * Since the order of destruction of static/global objects is undefined,
163 * defining a mutex as a global object doesn't work (the mutex gets
164 * destroyed, and then some later destructor calls OutputDebugStringF,
165 * maybe indirectly, and you get a core dump at shutdown trying to lock
169 static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
171 * We use boost::call_once() to make sure these are initialized
172 * in a thread-safe manner the first time called:
174 static FILE* fileout = NULL;
175 static boost::mutex* mutexDebugLog = NULL;
177 static void DebugPrintInit()
179 assert(fileout == NULL);
180 assert(mutexDebugLog == NULL);
182 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
183 fileout = fopen(pathDebug.string().c_str(), "a");
184 if (fileout) setbuf(fileout, NULL); // unbuffered
186 mutexDebugLog = new boost::mutex();
189 bool LogAcceptCategory(const char* category)
191 if (category != NULL)
196 // Give each thread quick access to -debug settings.
197 // This helps prevent issues debugging global destructors,
198 // where mapMultiArgs might be deleted before another
199 // global destructor calls LogPrint()
200 static boost::thread_specific_ptr<set<string> > ptrCategory;
201 if (ptrCategory.get() == NULL)
203 const vector<string>& categories = mapMultiArgs["-debug"];
204 ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
205 // thread_specific_ptr automatically deletes the set when the thread ends.
207 const set<string>& setCategories = *ptrCategory.get();
209 // if not debugging everything and not debugging specific category, LogPrint does nothing.
210 if (setCategories.count(string("")) == 0 &&
211 setCategories.count(string(category)) == 0)
217 int LogPrintStr(const std::string &str)
219 int ret = 0; // Returns total number of characters written
223 ret = fwrite(str.data(), 1, str.size(), stdout);
226 else if (fPrintToDebugLog && AreBaseParamsConfigured())
228 static bool fStartedNewLine = true;
229 boost::call_once(&DebugPrintInit, debugPrintInitFlag);
234 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
236 // reopen the log file, if requested
237 if (fReopenDebugLog) {
238 fReopenDebugLog = false;
239 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
240 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
241 setbuf(fileout, NULL); // unbuffered
244 // Debug print useful for profiling
245 if (fLogTimestamps && fStartedNewLine)
246 ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
247 if (!str.empty() && str[str.size()-1] == '\n')
248 fStartedNewLine = true;
250 fStartedNewLine = false;
252 ret = fwrite(str.data(), 1, str.size(), fileout);
258 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
260 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
261 if (name.find("-no") == 0)
263 std::string positive("-");
264 positive.append(name.begin()+3, name.end());
265 if (mapSettingsRet.count(positive) == 0)
267 bool value = !GetBoolArg(name, false);
268 mapSettingsRet[positive] = (value ? "1" : "0");
273 void ParseParameters(int argc, const char* const argv[])
276 mapMultiArgs.clear();
278 for (int i = 1; i < argc; i++)
280 std::string str(argv[i]);
281 std::string strValue;
282 size_t is_index = str.find('=');
283 if (is_index != std::string::npos)
285 strValue = str.substr(is_index+1);
286 str = str.substr(0, is_index);
289 boost::to_lower(str);
290 if (boost::algorithm::starts_with(str, "/"))
291 str = "-" + str.substr(1);
297 // Interpret --foo as -foo.
298 // If both --foo and -foo are set, the last takes effect.
299 if (str.length() > 1 && str[1] == '-')
302 mapArgs[str] = strValue;
303 mapMultiArgs[str].push_back(strValue);
307 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
309 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
310 InterpretNegativeSetting(entry.first, mapArgs);
314 std::string GetArg(const std::string& strArg, const std::string& strDefault)
316 if (mapArgs.count(strArg))
317 return mapArgs[strArg];
321 int64_t GetArg(const std::string& strArg, int64_t nDefault)
323 if (mapArgs.count(strArg))
324 return atoi64(mapArgs[strArg]);
328 bool GetBoolArg(const std::string& strArg, bool fDefault)
330 if (mapArgs.count(strArg))
332 if (mapArgs[strArg].empty())
334 return (atoi(mapArgs[strArg]) != 0);
339 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
341 if (mapArgs.count(strArg))
343 mapArgs[strArg] = strValue;
347 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
350 return SoftSetArg(strArg, std::string("1"));
352 return SoftSetArg(strArg, std::string("0"));
355 static const int screenWidth = 79;
356 static const int optIndent = 2;
357 static const int msgIndent = 7;
359 std::string HelpMessageGroup(const std::string &message) {
360 return std::string(message) + std::string("\n\n");
363 std::string HelpMessageOpt(const std::string &option, const std::string &message) {
364 return std::string(optIndent,' ') + std::string(option) +
365 std::string("\n") + std::string(msgIndent,' ') +
366 FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
370 static std::string FormatException(const std::exception* pex, const char* pszThread)
373 char pszModule[MAX_PATH] = "";
374 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
376 const char* pszModule = "bitcoin";
380 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
383 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
386 void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
388 std::string message = FormatException(pex, pszThread);
389 LogPrintf("\n\n************************\n%s\n", message);
390 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
391 strMiscWarning = message;
394 boost::filesystem::path GetDefaultDataDir()
396 namespace fs = boost::filesystem;
397 // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
398 // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
399 // Mac: ~/Library/Application Support/Bitcoin
403 return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
406 char* pszHome = getenv("HOME");
407 if (pszHome == NULL || strlen(pszHome) == 0)
408 pathRet = fs::path("/");
410 pathRet = fs::path(pszHome);
413 pathRet /= "Library/Application Support";
414 TryCreateDirectory(pathRet);
415 return pathRet / "Bitcoin";
418 return pathRet / ".bitcoin";
423 static boost::filesystem::path pathCached;
424 static boost::filesystem::path pathCachedNetSpecific;
425 static CCriticalSection csPathCached;
427 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
429 namespace fs = boost::filesystem;
433 fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
435 // This can be called during exceptions by LogPrintf(), so we cache the
436 // value so we don't have to do memory allocations after that.
440 if (mapArgs.count("-datadir")) {
441 path = fs::system_complete(mapArgs["-datadir"]);
442 if (!fs::is_directory(path)) {
447 path = GetDefaultDataDir();
450 path /= BaseParams().DataDir();
452 fs::create_directories(path);
457 void ClearDatadirCache()
459 pathCached = boost::filesystem::path();
460 pathCachedNetSpecific = boost::filesystem::path();
463 boost::filesystem::path GetConfigFile()
465 boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf"));
466 if (!pathConfigFile.is_complete())
467 pathConfigFile = GetDataDir(false) / pathConfigFile;
469 return pathConfigFile;
472 void ReadConfigFile(map<string, string>& mapSettingsRet,
473 map<string, vector<string> >& mapMultiSettingsRet)
475 boost::filesystem::ifstream streamConfig(GetConfigFile());
476 if (!streamConfig.good())
477 return; // No bitcoin.conf file is OK
479 set<string> setOptions;
480 setOptions.insert("*");
482 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
484 // Don't overwrite existing settings so command line settings override bitcoin.conf
485 string strKey = string("-") + it->string_key;
486 if (mapSettingsRet.count(strKey) == 0)
488 mapSettingsRet[strKey] = it->value[0];
489 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
490 InterpretNegativeSetting(strKey, mapSettingsRet);
492 mapMultiSettingsRet[strKey].push_back(it->value[0]);
494 // If datadir is changed in .conf file:
499 boost::filesystem::path GetPidFile()
501 boost::filesystem::path pathPidFile(GetArg("-pid", "bitcoind.pid"));
502 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
506 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
508 FILE* file = fopen(path.string().c_str(), "w");
511 fprintf(file, "%d\n", pid);
517 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
520 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
521 MOVEFILE_REPLACE_EXISTING) != 0;
523 int rc = std::rename(src.string().c_str(), dest.string().c_str());
529 * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
530 * Specifically handles case where path p exists, but it wasn't possible for the user to
531 * write to the parent directory.
533 bool TryCreateDirectory(const boost::filesystem::path& p)
537 return boost::filesystem::create_directory(p);
538 } catch (const boost::filesystem::filesystem_error&) {
539 if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
543 // create_directory didn't create the directory, it had to have existed already
547 void FileCommit(FILE *fileout)
549 fflush(fileout); // harmless if redundantly called
551 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
552 FlushFileBuffers(hFile);
554 #if defined(__linux__) || defined(__NetBSD__)
555 fdatasync(fileno(fileout));
556 #elif defined(__APPLE__) && defined(F_FULLFSYNC)
557 fcntl(fileno(fileout), F_FULLFSYNC, 0);
559 fsync(fileno(fileout));
564 bool TruncateFile(FILE *file, unsigned int length) {
566 return _chsize(_fileno(file), length) == 0;
568 return ftruncate(fileno(file), length) == 0;
573 * this function tries to raise the file descriptor limit to the requested number.
574 * It returns the actual file descriptor limit (which may be more or less than nMinFD)
576 int RaiseFileDescriptorLimit(int nMinFD) {
580 struct rlimit limitFD;
581 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
582 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
583 limitFD.rlim_cur = nMinFD;
584 if (limitFD.rlim_cur > limitFD.rlim_max)
585 limitFD.rlim_cur = limitFD.rlim_max;
586 setrlimit(RLIMIT_NOFILE, &limitFD);
587 getrlimit(RLIMIT_NOFILE, &limitFD);
589 return limitFD.rlim_cur;
591 return nMinFD; // getrlimit failed, assume it's fine
596 * this function tries to make a particular range of a file allocated (corresponding to disk space)
597 * it is advisory, and the range specified in the arguments will never contain live data
599 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
601 // Windows-specific version
602 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
603 LARGE_INTEGER nFileSize;
604 int64_t nEndPos = (int64_t)offset + length;
605 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
606 nFileSize.u.HighPart = nEndPos >> 32;
607 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
609 #elif defined(MAC_OSX)
610 // OSX specific version
612 fst.fst_flags = F_ALLOCATECONTIG;
613 fst.fst_posmode = F_PEOFPOSMODE;
615 fst.fst_length = (off_t)offset + length;
616 fst.fst_bytesalloc = 0;
617 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
618 fst.fst_flags = F_ALLOCATEALL;
619 fcntl(fileno(file), F_PREALLOCATE, &fst);
621 ftruncate(fileno(file), fst.fst_length);
622 #elif defined(__linux__)
623 // Version using posix_fallocate
624 off_t nEndPos = (off_t)offset + length;
625 posix_fallocate(fileno(file), 0, nEndPos);
628 // TODO: just write one byte per block
629 static const char buf[65536] = {};
630 fseek(file, offset, SEEK_SET);
632 unsigned int now = 65536;
635 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
641 void ShrinkDebugFile()
643 // Scroll debug.log if it's getting too big
644 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
645 FILE* file = fopen(pathLog.string().c_str(), "r");
646 if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
648 // Restart the file with some of the end
649 std::vector <char> vch(200000,0);
650 fseek(file, -((long)vch.size()), SEEK_END);
651 int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
654 file = fopen(pathLog.string().c_str(), "w");
657 fwrite(begin_ptr(vch), 1, nBytes, file);
661 else if (file != NULL)
666 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
668 namespace fs = boost::filesystem;
670 char pszPath[MAX_PATH] = "";
672 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
674 return fs::path(pszPath);
677 LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
682 boost::filesystem::path GetTempPath() {
683 #if BOOST_FILESYSTEM_VERSION == 3
684 return boost::filesystem::temp_directory_path();
686 // TODO: remove when we don't support filesystem v2 anymore
687 boost::filesystem::path path;
689 char pszPath[MAX_PATH] = "";
691 if (GetTempPathA(MAX_PATH, pszPath))
692 path = boost::filesystem::path(pszPath);
694 path = boost::filesystem::path("/tmp");
696 if (path.empty() || !boost::filesystem::is_directory(path)) {
697 LogPrintf("GetTempPath(): failed to find temp path\n");
698 return boost::filesystem::path("");
704 void runCommand(std::string strCommand)
706 int nErr = ::system(strCommand.c_str());
708 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
711 void RenameThread(const char* name)
713 #if defined(PR_SET_NAME)
714 // Only the first 15 characters are used (16 - NUL terminator)
715 ::prctl(PR_SET_NAME, name, 0, 0, 0);
716 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
717 pthread_set_name_np(pthread_self(), name);
719 #elif defined(MAC_OSX)
720 pthread_setname_np(name);
722 // Prevent warnings for unused parameters...
727 void SetupEnvironment()
729 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
730 // may be invalid, in which case the "C" locale is used as fallback.
731 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
733 std::locale(""); // Raises a runtime error if current locale is invalid
734 } catch (const std::runtime_error&) {
735 setenv("LC_ALL", "C", 1);
738 // The path locale is lazy initialized and to avoid deinitialization errors
739 // in multithreading environments, it is set explicitly by the main thread.
740 // A dummy locale is used to extract the internal default locale, used by
741 // boost::filesystem::path, which is then used to explicitly imbue the path.
742 std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
743 boost::filesystem::path::imbue(loc);
746 void SetThreadPriority(int nPriority)
749 SetThreadPriority(GetCurrentThread(), nPriority);
752 setpriority(PRIO_THREAD, 0, nPriority);
754 setpriority(PRIO_PROCESS, 0, nPriority);
755 #endif // PRIO_THREAD