You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.cpp 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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.
  5. #if defined(HAVE_CONFIG_H)
  6. #include "config/bitcoin-config.h"
  7. #endif
  8. #include "util.h"
  9. #include "chainparamsbase.h"
  10. #include "fs.h"
  11. #include "random.h"
  12. #include "serialize.h"
  13. #include "utilstrencodings.h"
  14. #include "utiltime.h"
  15. #include <stdarg.h>
  16. #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
  17. #include <pthread.h>
  18. #include <pthread_np.h>
  19. #endif
  20. #ifndef WIN32
  21. // for posix_fallocate
  22. #ifdef __linux__
  23. #ifdef _POSIX_C_SOURCE
  24. #undef _POSIX_C_SOURCE
  25. #endif
  26. #define _POSIX_C_SOURCE 200112L
  27. #endif // __linux__
  28. #include <algorithm>
  29. #include <fcntl.h>
  30. #include <sys/resource.h>
  31. #include <sys/stat.h>
  32. #else
  33. #ifdef _MSC_VER
  34. #pragma warning(disable:4786)
  35. #pragma warning(disable:4804)
  36. #pragma warning(disable:4805)
  37. #pragma warning(disable:4717)
  38. #endif
  39. #ifdef _WIN32_WINNT
  40. #undef _WIN32_WINNT
  41. #endif
  42. #define _WIN32_WINNT 0x0501
  43. #ifdef _WIN32_IE
  44. #undef _WIN32_IE
  45. #endif
  46. #define _WIN32_IE 0x0501
  47. #define WIN32_LEAN_AND_MEAN 1
  48. #ifndef NOMINMAX
  49. #define NOMINMAX
  50. #endif
  51. #include <io.h> /* for _commit */
  52. #include <shlobj.h>
  53. #endif
  54. #ifdef HAVE_SYS_PRCTL_H
  55. #include <sys/prctl.h>
  56. #endif
  57. #ifdef HAVE_MALLOPT_ARENA_MAX
  58. #include <malloc.h>
  59. #endif
  60. #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
  61. #include <boost/algorithm/string/join.hpp>
  62. #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
  63. #include <boost/foreach.hpp>
  64. #include <boost/program_options/detail/config_file.hpp>
  65. #include <boost/program_options/parsers.hpp>
  66. #include <boost/thread.hpp>
  67. #include <openssl/crypto.h>
  68. #include <openssl/rand.h>
  69. #include <openssl/conf.h>
  70. const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
  71. const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
  72. ArgsManager gArgs;
  73. static std::map<std::string, std::vector<std::string> > _mapMultiArgs;
  74. const std::map<std::string, std::vector<std::string> >& mapMultiArgs = _mapMultiArgs;
  75. bool fPrintToConsole = false;
  76. bool fPrintToDebugLog = true;
  77. bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
  78. bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
  79. bool fLogIPs = DEFAULT_LOGIPS;
  80. std::atomic<bool> fReopenDebugLog(false);
  81. CTranslationInterface translationInterface;
  82. /** Log categories bitfield. */
  83. std::atomic<uint32_t> logCategories(0);
  84. /** Init OpenSSL library multithreading support */
  85. static std::unique_ptr<CCriticalSection[]> ppmutexOpenSSL;
  86. void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
  87. {
  88. if (mode & CRYPTO_LOCK) {
  89. ENTER_CRITICAL_SECTION(ppmutexOpenSSL[i]);
  90. } else {
  91. LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]);
  92. }
  93. }
  94. // Singleton for wrapping OpenSSL setup/teardown.
  95. class CInit
  96. {
  97. public:
  98. CInit()
  99. {
  100. // Init OpenSSL library multithreading support
  101. ppmutexOpenSSL.reset(new CCriticalSection[CRYPTO_num_locks()]);
  102. CRYPTO_set_locking_callback(locking_callback);
  103. // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
  104. // We don't use them so we don't require the config. However some of our libs may call functions
  105. // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
  106. // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
  107. // that the config appears to have been loaded and there are no modules/engines available.
  108. OPENSSL_no_config();
  109. #ifdef WIN32
  110. // Seed OpenSSL PRNG with current contents of the screen
  111. RAND_screen();
  112. #endif
  113. // Seed OpenSSL PRNG with performance counter
  114. RandAddSeed();
  115. }
  116. ~CInit()
  117. {
  118. // Securely erase the memory used by the PRNG
  119. RAND_cleanup();
  120. // Shutdown OpenSSL library multithreading support
  121. CRYPTO_set_locking_callback(NULL);
  122. // Clear the set of locks now to maintain symmetry with the constructor.
  123. ppmutexOpenSSL.reset();
  124. }
  125. }
  126. instance_of_cinit;
  127. /**
  128. * LogPrintf() has been broken a couple of times now
  129. * by well-meaning people adding mutexes in the most straightforward way.
  130. * It breaks because it may be called by global destructors during shutdown.
  131. * Since the order of destruction of static/global objects is undefined,
  132. * defining a mutex as a global object doesn't work (the mutex gets
  133. * destroyed, and then some later destructor calls OutputDebugStringF,
  134. * maybe indirectly, and you get a core dump at shutdown trying to lock
  135. * the mutex).
  136. */
  137. static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
  138. /**
  139. * We use boost::call_once() to make sure mutexDebugLog and
  140. * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
  141. *
  142. * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
  143. * are leaked on exit. This is ugly, but will be cleaned up by
  144. * the OS/libc. When the shutdown sequence is fully audited and
  145. * tested, explicit destruction of these objects can be implemented.
  146. */
  147. static FILE* fileout = NULL;
  148. static boost::mutex* mutexDebugLog = NULL;
  149. static std::list<std::string>* vMsgsBeforeOpenLog;
  150. static int FileWriteStr(const std::string &str, FILE *fp)
  151. {
  152. return fwrite(str.data(), 1, str.size(), fp);
  153. }
  154. static void DebugPrintInit()
  155. {
  156. assert(mutexDebugLog == NULL);
  157. mutexDebugLog = new boost::mutex();
  158. vMsgsBeforeOpenLog = new std::list<std::string>;
  159. }
  160. void OpenDebugLog()
  161. {
  162. boost::call_once(&DebugPrintInit, debugPrintInitFlag);
  163. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  164. assert(fileout == NULL);
  165. assert(vMsgsBeforeOpenLog);
  166. fs::path pathDebug = GetDataDir() / "debug.log";
  167. fileout = fsbridge::fopen(pathDebug, "a");
  168. if (fileout) {
  169. setbuf(fileout, NULL); // unbuffered
  170. // dump buffered messages from before we opened the log
  171. while (!vMsgsBeforeOpenLog->empty()) {
  172. FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
  173. vMsgsBeforeOpenLog->pop_front();
  174. }
  175. }
  176. delete vMsgsBeforeOpenLog;
  177. vMsgsBeforeOpenLog = NULL;
  178. }
  179. struct CLogCategoryDesc
  180. {
  181. uint32_t flag;
  182. std::string category;
  183. };
  184. const CLogCategoryDesc LogCategories[] =
  185. {
  186. {BCLog::NONE, "0"},
  187. {BCLog::NET, "net"},
  188. {BCLog::TOR, "tor"},
  189. {BCLog::MEMPOOL, "mempool"},
  190. {BCLog::HTTP, "http"},
  191. {BCLog::BENCH, "bench"},
  192. {BCLog::ZMQ, "zmq"},
  193. {BCLog::DB, "db"},
  194. {BCLog::RPC, "rpc"},
  195. {BCLog::ESTIMATEFEE, "estimatefee"},
  196. {BCLog::ADDRMAN, "addrman"},
  197. {BCLog::SELECTCOINS, "selectcoins"},
  198. {BCLog::REINDEX, "reindex"},
  199. {BCLog::CMPCTBLOCK, "cmpctblock"},
  200. {BCLog::RAND, "rand"},
  201. {BCLog::PRUNE, "prune"},
  202. {BCLog::PROXY, "proxy"},
  203. {BCLog::MEMPOOLREJ, "mempoolrej"},
  204. {BCLog::LIBEVENT, "libevent"},
  205. {BCLog::COINDB, "coindb"},
  206. {BCLog::QT, "qt"},
  207. {BCLog::LEVELDB, "leveldb"},
  208. {BCLog::ALL, "1"},
  209. {BCLog::ALL, "all"},
  210. };
  211. bool GetLogCategory(uint32_t *f, const std::string *str)
  212. {
  213. if (f && str) {
  214. if (*str == "") {
  215. *f = BCLog::ALL;
  216. return true;
  217. }
  218. for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
  219. if (LogCategories[i].category == *str) {
  220. *f = LogCategories[i].flag;
  221. return true;
  222. }
  223. }
  224. }
  225. return false;
  226. }
  227. std::string ListLogCategories()
  228. {
  229. std::string ret;
  230. int outcount = 0;
  231. for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
  232. // Omit the special cases.
  233. if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
  234. if (outcount != 0) ret += ", ";
  235. ret += LogCategories[i].category;
  236. outcount++;
  237. }
  238. }
  239. return ret;
  240. }
  241. std::vector<CLogCategoryActive> ListActiveLogCategories()
  242. {
  243. std::vector<CLogCategoryActive> ret;
  244. for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
  245. // Omit the special cases.
  246. if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
  247. CLogCategoryActive catActive;
  248. catActive.category = LogCategories[i].category;
  249. catActive.active = LogAcceptCategory(LogCategories[i].flag);
  250. ret.push_back(catActive);
  251. }
  252. }
  253. return ret;
  254. }
  255. /**
  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.
  259. */
  260. static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
  261. {
  262. std::string strStamped;
  263. if (!fLogTimestamps)
  264. return str;
  265. if (*fStartedNewLine) {
  266. int64_t nTimeMicros = GetLogTimeMicros();
  267. strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
  268. if (fLogTimeMicros)
  269. strStamped += strprintf(".%06d", nTimeMicros%1000000);
  270. strStamped += ' ' + str;
  271. } else
  272. strStamped = str;
  273. if (!str.empty() && str[str.size()-1] == '\n')
  274. *fStartedNewLine = true;
  275. else
  276. *fStartedNewLine = false;
  277. return strStamped;
  278. }
  279. int LogPrintStr(const std::string &str)
  280. {
  281. int ret = 0; // Returns total number of characters written
  282. static std::atomic_bool fStartedNewLine(true);
  283. std::string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
  284. if (fPrintToConsole)
  285. {
  286. // print to console
  287. ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout);
  288. fflush(stdout);
  289. }
  290. else if (fPrintToDebugLog)
  291. {
  292. boost::call_once(&DebugPrintInit, debugPrintInitFlag);
  293. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  294. // buffer if we haven't opened the log yet
  295. if (fileout == NULL) {
  296. assert(vMsgsBeforeOpenLog);
  297. ret = strTimestamped.length();
  298. vMsgsBeforeOpenLog->push_back(strTimestamped);
  299. }
  300. else
  301. {
  302. // reopen the log file, if requested
  303. if (fReopenDebugLog) {
  304. fReopenDebugLog = false;
  305. fs::path pathDebug = GetDataDir() / "debug.log";
  306. if (fsbridge::freopen(pathDebug,"a",fileout) != NULL)
  307. setbuf(fileout, NULL); // unbuffered
  308. }
  309. ret = FileWriteStr(strTimestamped, fileout);
  310. }
  311. }
  312. return ret;
  313. }
  314. /** Interpret string as boolean, for argument parsing */
  315. static bool InterpretBool(const std::string& strValue)
  316. {
  317. if (strValue.empty())
  318. return true;
  319. return (atoi(strValue) != 0);
  320. }
  321. /** Turn -noX into -X=0 */
  322. static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
  323. {
  324. if (strKey.length()>3 && strKey[0]=='-' && strKey[1]=='n' && strKey[2]=='o')
  325. {
  326. strKey = "-" + strKey.substr(3);
  327. strValue = InterpretBool(strValue) ? "0" : "1";
  328. }
  329. }
  330. void ArgsManager::ParseParameters(int argc, const char* const argv[])
  331. {
  332. LOCK(cs_args);
  333. mapArgs.clear();
  334. _mapMultiArgs.clear();
  335. for (int i = 1; i < argc; i++)
  336. {
  337. std::string str(argv[i]);
  338. std::string strValue;
  339. size_t is_index = str.find('=');
  340. if (is_index != std::string::npos)
  341. {
  342. strValue = str.substr(is_index+1);
  343. str = str.substr(0, is_index);
  344. }
  345. #ifdef WIN32
  346. boost::to_lower(str);
  347. if (boost::algorithm::starts_with(str, "/"))
  348. str = "-" + str.substr(1);
  349. #endif
  350. if (str[0] != '-')
  351. break;
  352. // Interpret --foo as -foo.
  353. // If both --foo and -foo are set, the last takes effect.
  354. if (str.length() > 1 && str[1] == '-')
  355. str = str.substr(1);
  356. InterpretNegativeSetting(str, strValue);
  357. mapArgs[str] = strValue;
  358. _mapMultiArgs[str].push_back(strValue);
  359. }
  360. }
  361. std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
  362. {
  363. LOCK(cs_args);
  364. return mapMultiArgs.at(strArg);
  365. }
  366. bool ArgsManager::IsArgSet(const std::string& strArg)
  367. {
  368. LOCK(cs_args);
  369. return mapArgs.count(strArg);
  370. }
  371. std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault)
  372. {
  373. LOCK(cs_args);
  374. if (mapArgs.count(strArg))
  375. return mapArgs[strArg];
  376. return strDefault;
  377. }
  378. int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault)
  379. {
  380. LOCK(cs_args);
  381. if (mapArgs.count(strArg))
  382. return atoi64(mapArgs[strArg]);
  383. return nDefault;
  384. }
  385. bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault)
  386. {
  387. LOCK(cs_args);
  388. if (mapArgs.count(strArg))
  389. return InterpretBool(mapArgs[strArg]);
  390. return fDefault;
  391. }
  392. bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
  393. {
  394. LOCK(cs_args);
  395. if (mapArgs.count(strArg))
  396. return false;
  397. mapArgs[strArg] = strValue;
  398. return true;
  399. }
  400. bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
  401. {
  402. if (fValue)
  403. return SoftSetArg(strArg, std::string("1"));
  404. else
  405. return SoftSetArg(strArg, std::string("0"));
  406. }
  407. void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
  408. {
  409. LOCK(cs_args);
  410. mapArgs[strArg] = strValue;
  411. }
  412. static const int screenWidth = 79;
  413. static const int optIndent = 2;
  414. static const int msgIndent = 7;
  415. std::string HelpMessageGroup(const std::string &message) {
  416. return std::string(message) + std::string("\n\n");
  417. }
  418. std::string HelpMessageOpt(const std::string &option, const std::string &message) {
  419. return std::string(optIndent,' ') + std::string(option) +
  420. std::string("\n") + std::string(msgIndent,' ') +
  421. FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
  422. std::string("\n\n");
  423. }
  424. static std::string FormatException(const std::exception* pex, const char* pszThread)
  425. {
  426. #ifdef WIN32
  427. char pszModule[MAX_PATH] = "";
  428. GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
  429. #else
  430. const char* pszModule = "bitcoin";
  431. #endif
  432. if (pex)
  433. return strprintf(
  434. "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
  435. else
  436. return strprintf(
  437. "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
  438. }
  439. void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
  440. {
  441. std::string message = FormatException(pex, pszThread);
  442. LogPrintf("\n\n************************\n%s\n", message);
  443. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  444. }
  445. fs::path GetDefaultDataDir()
  446. {
  447. // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
  448. // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
  449. // Mac: ~/Library/Application Support/Bitcoin
  450. // Unix: ~/.bitcoin
  451. #ifdef WIN32
  452. // Windows
  453. return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
  454. #else
  455. fs::path pathRet;
  456. char* pszHome = getenv("HOME");
  457. if (pszHome == NULL || strlen(pszHome) == 0)
  458. pathRet = fs::path("/");
  459. else
  460. pathRet = fs::path(pszHome);
  461. #ifdef MAC_OSX
  462. // Mac
  463. return pathRet / "Library/Application Support/Bitcoin";
  464. #else
  465. // Unix
  466. return pathRet / ".bitcoin";
  467. #endif
  468. #endif
  469. }
  470. static fs::path pathCached;
  471. static fs::path pathCachedNetSpecific;
  472. static CCriticalSection csPathCached;
  473. const fs::path &GetDataDir(bool fNetSpecific)
  474. {
  475. LOCK(csPathCached);
  476. fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
  477. // This can be called during exceptions by LogPrintf(), so we cache the
  478. // value so we don't have to do memory allocations after that.
  479. if (!path.empty())
  480. return path;
  481. if (IsArgSet("-datadir")) {
  482. path = fs::system_complete(GetArg("-datadir", ""));
  483. if (!fs::is_directory(path)) {
  484. path = "";
  485. return path;
  486. }
  487. } else {
  488. path = GetDefaultDataDir();
  489. }
  490. if (fNetSpecific)
  491. path /= BaseParams().DataDir();
  492. fs::create_directories(path);
  493. return path;
  494. }
  495. void ClearDatadirCache()
  496. {
  497. LOCK(csPathCached);
  498. pathCached = fs::path();
  499. pathCachedNetSpecific = fs::path();
  500. }
  501. fs::path GetConfigFile(const std::string& confPath)
  502. {
  503. fs::path pathConfigFile(confPath);
  504. if (!pathConfigFile.is_complete())
  505. pathConfigFile = GetDataDir(false) / pathConfigFile;
  506. return pathConfigFile;
  507. }
  508. void ArgsManager::ReadConfigFile(const std::string& confPath)
  509. {
  510. fs::ifstream streamConfig(GetConfigFile(confPath));
  511. if (!streamConfig.good())
  512. return; // No bitcoin.conf file is OK
  513. {
  514. LOCK(cs_args);
  515. std::set<std::string> setOptions;
  516. setOptions.insert("*");
  517. for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
  518. {
  519. // Don't overwrite existing settings so command line settings override bitcoin.conf
  520. std::string strKey = std::string("-") + it->string_key;
  521. std::string strValue = it->value[0];
  522. InterpretNegativeSetting(strKey, strValue);
  523. if (mapArgs.count(strKey) == 0)
  524. mapArgs[strKey] = strValue;
  525. _mapMultiArgs[strKey].push_back(strValue);
  526. }
  527. }
  528. // If datadir is changed in .conf file:
  529. ClearDatadirCache();
  530. }
  531. #ifndef WIN32
  532. fs::path GetPidFile()
  533. {
  534. fs::path pathPidFile(GetArg("-pid", BITCOIN_PID_FILENAME));
  535. if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
  536. return pathPidFile;
  537. }
  538. void CreatePidFile(const fs::path &path, pid_t pid)
  539. {
  540. FILE* file = fsbridge::fopen(path, "w");
  541. if (file)
  542. {
  543. fprintf(file, "%d\n", pid);
  544. fclose(file);
  545. }
  546. }
  547. #endif
  548. bool RenameOver(fs::path src, fs::path dest)
  549. {
  550. #ifdef WIN32
  551. return MoveFileExA(src.string().c_str(), dest.string().c_str(),
  552. MOVEFILE_REPLACE_EXISTING) != 0;
  553. #else
  554. int rc = std::rename(src.string().c_str(), dest.string().c_str());
  555. return (rc == 0);
  556. #endif /* WIN32 */
  557. }
  558. /**
  559. * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
  560. * Specifically handles case where path p exists, but it wasn't possible for the user to
  561. * write to the parent directory.
  562. */
  563. bool TryCreateDirectory(const fs::path& p)
  564. {
  565. try
  566. {
  567. return fs::create_directory(p);
  568. } catch (const fs::filesystem_error&) {
  569. if (!fs::exists(p) || !fs::is_directory(p))
  570. throw;
  571. }
  572. // create_directory didn't create the directory, it had to have existed already
  573. return false;
  574. }
  575. void FileCommit(FILE *file)
  576. {
  577. fflush(file); // harmless if redundantly called
  578. #ifdef WIN32
  579. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
  580. FlushFileBuffers(hFile);
  581. #else
  582. #if defined(__linux__) || defined(__NetBSD__)
  583. fdatasync(fileno(file));
  584. #elif defined(__APPLE__) && defined(F_FULLFSYNC)
  585. fcntl(fileno(file), F_FULLFSYNC, 0);
  586. #else
  587. fsync(fileno(file));
  588. #endif
  589. #endif
  590. }
  591. bool TruncateFile(FILE *file, unsigned int length) {
  592. #if defined(WIN32)
  593. return _chsize(_fileno(file), length) == 0;
  594. #else
  595. return ftruncate(fileno(file), length) == 0;
  596. #endif
  597. }
  598. /**
  599. * this function tries to raise the file descriptor limit to the requested number.
  600. * It returns the actual file descriptor limit (which may be more or less than nMinFD)
  601. */
  602. int RaiseFileDescriptorLimit(int nMinFD) {
  603. #if defined(WIN32)
  604. return 2048;
  605. #else
  606. struct rlimit limitFD;
  607. if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
  608. if (limitFD.rlim_cur < (rlim_t)nMinFD) {
  609. limitFD.rlim_cur = nMinFD;
  610. if (limitFD.rlim_cur > limitFD.rlim_max)
  611. limitFD.rlim_cur = limitFD.rlim_max;
  612. setrlimit(RLIMIT_NOFILE, &limitFD);
  613. getrlimit(RLIMIT_NOFILE, &limitFD);
  614. }
  615. return limitFD.rlim_cur;
  616. }
  617. return nMinFD; // getrlimit failed, assume it's fine
  618. #endif
  619. }
  620. /**
  621. * this function tries to make a particular range of a file allocated (corresponding to disk space)
  622. * it is advisory, and the range specified in the arguments will never contain live data
  623. */
  624. void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
  625. #if defined(WIN32)
  626. // Windows-specific version
  627. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
  628. LARGE_INTEGER nFileSize;
  629. int64_t nEndPos = (int64_t)offset + length;
  630. nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
  631. nFileSize.u.HighPart = nEndPos >> 32;
  632. SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
  633. SetEndOfFile(hFile);
  634. #elif defined(MAC_OSX)
  635. // OSX specific version
  636. fstore_t fst;
  637. fst.fst_flags = F_ALLOCATECONTIG;
  638. fst.fst_posmode = F_PEOFPOSMODE;
  639. fst.fst_offset = 0;
  640. fst.fst_length = (off_t)offset + length;
  641. fst.fst_bytesalloc = 0;
  642. if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
  643. fst.fst_flags = F_ALLOCATEALL;
  644. fcntl(fileno(file), F_PREALLOCATE, &fst);
  645. }
  646. ftruncate(fileno(file), fst.fst_length);
  647. #elif defined(__linux__)
  648. // Version using posix_fallocate
  649. off_t nEndPos = (off_t)offset + length;
  650. posix_fallocate(fileno(file), 0, nEndPos);
  651. #else
  652. // Fallback version
  653. // TODO: just write one byte per block
  654. static const char buf[65536] = {};
  655. fseek(file, offset, SEEK_SET);
  656. while (length > 0) {
  657. unsigned int now = 65536;
  658. if (length < now)
  659. now = length;
  660. fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
  661. length -= now;
  662. }
  663. #endif
  664. }
  665. void ShrinkDebugFile()
  666. {
  667. // Amount of debug.log to save at end when shrinking (must fit in memory)
  668. constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
  669. // Scroll debug.log if it's getting too big
  670. fs::path pathLog = GetDataDir() / "debug.log";
  671. FILE* file = fsbridge::fopen(pathLog, "r");
  672. // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
  673. // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
  674. if (file && fs::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
  675. {
  676. // Restart the file with some of the end
  677. std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
  678. fseek(file, -((long)vch.size()), SEEK_END);
  679. int nBytes = fread(vch.data(), 1, vch.size(), file);
  680. fclose(file);
  681. file = fsbridge::fopen(pathLog, "w");
  682. if (file)
  683. {
  684. fwrite(vch.data(), 1, nBytes, file);
  685. fclose(file);
  686. }
  687. }
  688. else if (file != NULL)
  689. fclose(file);
  690. }
  691. #ifdef WIN32
  692. fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
  693. {
  694. char pszPath[MAX_PATH] = "";
  695. if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
  696. {
  697. return fs::path(pszPath);
  698. }
  699. LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
  700. return fs::path("");
  701. }
  702. #endif
  703. void runCommand(const std::string& strCommand)
  704. {
  705. int nErr = ::system(strCommand.c_str());
  706. if (nErr)
  707. LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
  708. }
  709. void RenameThread(const char* name)
  710. {
  711. #if defined(PR_SET_NAME)
  712. // Only the first 15 characters are used (16 - NUL terminator)
  713. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  714. #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
  715. pthread_set_name_np(pthread_self(), name);
  716. #elif defined(MAC_OSX)
  717. pthread_setname_np(name);
  718. #else
  719. // Prevent warnings for unused parameters...
  720. (void)name;
  721. #endif
  722. }
  723. void SetupEnvironment()
  724. {
  725. #ifdef HAVE_MALLOPT_ARENA_MAX
  726. // glibc-specific: On 32-bit systems set the number of arenas to 1.
  727. // By default, since glibc 2.10, the C library will create up to two heap
  728. // arenas per core. This is known to cause excessive virtual address space
  729. // usage in our usage. Work around it by setting the maximum number of
  730. // arenas to 1.
  731. if (sizeof(void*) == 4) {
  732. mallopt(M_ARENA_MAX, 1);
  733. }
  734. #endif
  735. // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
  736. // may be invalid, in which case the "C" locale is used as fallback.
  737. #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
  738. try {
  739. std::locale(""); // Raises a runtime error if current locale is invalid
  740. } catch (const std::runtime_error&) {
  741. setenv("LC_ALL", "C", 1);
  742. }
  743. #endif
  744. // The path locale is lazy initialized and to avoid deinitialization errors
  745. // in multithreading environments, it is set explicitly by the main thread.
  746. // A dummy locale is used to extract the internal default locale, used by
  747. // fs::path, which is then used to explicitly imbue the path.
  748. std::locale loc = fs::path::imbue(std::locale::classic());
  749. fs::path::imbue(loc);
  750. }
  751. bool SetupNetworking()
  752. {
  753. #ifdef WIN32
  754. // Initialize Windows Sockets
  755. WSADATA wsadata;
  756. int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
  757. if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
  758. return false;
  759. #endif
  760. return true;
  761. }
  762. int GetNumCores()
  763. {
  764. #if BOOST_VERSION >= 105600
  765. return boost::thread::physical_concurrency();
  766. #else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
  767. return boost::thread::hardware_concurrency();
  768. #endif
  769. }
  770. std::string CopyrightHolders(const std::string& strPrefix)
  771. {
  772. std::string strCopyrightHolders = strPrefix + strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION));
  773. // Check for untranslated substitution to make sure Bitcoin Core copyright is not removed by accident
  774. if (strprintf(COPYRIGHT_HOLDERS, COPYRIGHT_HOLDERS_SUBSTITUTION).find("Bitcoin Core") == std::string::npos) {
  775. strCopyrightHolders += "\n" + strPrefix + "The Bitcoin Core developers";
  776. }
  777. return strCopyrightHolders;
  778. }