util.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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.
  5. #if defined(HAVE_CONFIG_H)
  6. #include "config/bitcoin-config.h"
  7. #endif
  8. #include "util.h"
  9. #include "chainparamsbase.h"
  10. #include "random.h"
  11. #include "serialize.h"
  12. #include "sync.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. #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
  58. #include <boost/algorithm/string/join.hpp>
  59. #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
  60. #include <boost/filesystem.hpp>
  61. #include <boost/filesystem/fstream.hpp>
  62. #include <boost/foreach.hpp>
  63. #include <boost/program_options/detail/config_file.hpp>
  64. #include <boost/program_options/parsers.hpp>
  65. #include <boost/thread.hpp>
  66. #include <openssl/crypto.h>
  67. #include <openssl/rand.h>
  68. #include <openssl/conf.h>
  69. // Work around clang compilation problem in Boost 1.46:
  70. // /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
  71. // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
  72. // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
  73. namespace boost {
  74. namespace program_options {
  75. std::string to_internal(const std::string&);
  76. }
  77. } // namespace boost
  78. using namespace std;
  79. map<string, string> mapArgs;
  80. map<string, vector<string> > mapMultiArgs;
  81. bool fDebug = false;
  82. bool fPrintToConsole = false;
  83. bool fPrintToDebugLog = true;
  84. bool fDaemon = false;
  85. bool fServer = false;
  86. string strMiscWarning;
  87. bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS;
  88. bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS;
  89. bool fLogIPs = DEFAULT_LOGIPS;
  90. std::atomic<bool> fReopenDebugLog(false);
  91. CTranslationInterface translationInterface;
  92. /** Init OpenSSL library multithreading support */
  93. static CCriticalSection** ppmutexOpenSSL;
  94. void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
  95. {
  96. if (mode & CRYPTO_LOCK) {
  97. ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  98. } else {
  99. LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  100. }
  101. }
  102. // Init
  103. class CInit
  104. {
  105. public:
  106. CInit()
  107. {
  108. // Init OpenSSL library multithreading support
  109. ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
  110. for (int i = 0; i < CRYPTO_num_locks(); i++)
  111. ppmutexOpenSSL[i] = new CCriticalSection();
  112. CRYPTO_set_locking_callback(locking_callback);
  113. // OpenSSL can optionally load a config file which lists optional loadable modules and engines.
  114. // We don't use them so we don't require the config. However some of our libs may call functions
  115. // which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
  116. // or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
  117. // that the config appears to have been loaded and there are no modules/engines available.
  118. OPENSSL_no_config();
  119. #ifdef WIN32
  120. // Seed OpenSSL PRNG with current contents of the screen
  121. RAND_screen();
  122. #endif
  123. // Seed OpenSSL PRNG with performance counter
  124. RandAddSeed();
  125. }
  126. ~CInit()
  127. {
  128. // Securely erase the memory used by the PRNG
  129. RAND_cleanup();
  130. // Shutdown OpenSSL library multithreading support
  131. CRYPTO_set_locking_callback(NULL);
  132. for (int i = 0; i < CRYPTO_num_locks(); i++)
  133. delete ppmutexOpenSSL[i];
  134. OPENSSL_free(ppmutexOpenSSL);
  135. }
  136. }
  137. instance_of_cinit;
  138. /**
  139. * LogPrintf() has been broken a couple of times now
  140. * by well-meaning people adding mutexes in the most straightforward way.
  141. * It breaks because it may be called by global destructors during shutdown.
  142. * Since the order of destruction of static/global objects is undefined,
  143. * defining a mutex as a global object doesn't work (the mutex gets
  144. * destroyed, and then some later destructor calls OutputDebugStringF,
  145. * maybe indirectly, and you get a core dump at shutdown trying to lock
  146. * the mutex).
  147. */
  148. static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
  149. /**
  150. * We use boost::call_once() to make sure these are initialized
  151. * in a thread-safe manner the first time called:
  152. */
  153. static FILE* fileout = NULL;
  154. static boost::mutex* mutexDebugLog = NULL;
  155. static void DebugPrintInit()
  156. {
  157. assert(fileout == NULL);
  158. assert(mutexDebugLog == NULL);
  159. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  160. fileout = fopen(pathDebug.string().c_str(), "a");
  161. if (fileout) setbuf(fileout, NULL); // unbuffered
  162. mutexDebugLog = new boost::mutex();
  163. }
  164. bool LogAcceptCategory(const char* category)
  165. {
  166. if (category != NULL)
  167. {
  168. if (!fDebug)
  169. return false;
  170. // Give each thread quick access to -debug settings.
  171. // This helps prevent issues debugging global destructors,
  172. // where mapMultiArgs might be deleted before another
  173. // global destructor calls LogPrint()
  174. static boost::thread_specific_ptr<set<string> > ptrCategory;
  175. if (ptrCategory.get() == NULL)
  176. {
  177. const vector<string>& categories = mapMultiArgs["-debug"];
  178. ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
  179. // thread_specific_ptr automatically deletes the set when the thread ends.
  180. }
  181. const set<string>& setCategories = *ptrCategory.get();
  182. // if not debugging everything and not debugging specific category, LogPrint does nothing.
  183. if (setCategories.count(string("")) == 0 &&
  184. setCategories.count(string(category)) == 0)
  185. return false;
  186. }
  187. return true;
  188. }
  189. int LogPrintStr(const std::string &str)
  190. {
  191. int ret = 0; // Returns total number of characters written
  192. if (fPrintToConsole)
  193. {
  194. // print to console
  195. ret = fwrite(str.data(), 1, str.size(), stdout);
  196. fflush(stdout);
  197. }
  198. else if (fPrintToDebugLog && AreBaseParamsConfigured())
  199. {
  200. static bool fStartedNewLine = true;
  201. boost::call_once(&DebugPrintInit, debugPrintInitFlag);
  202. if (fileout == NULL)
  203. return ret;
  204. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  205. // reopen the log file, if requested
  206. if (fReopenDebugLog) {
  207. fReopenDebugLog = false;
  208. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  209. if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
  210. setbuf(fileout, NULL); // unbuffered
  211. }
  212. // Debug print useful for profiling
  213. if (fLogTimestamps && fStartedNewLine)
  214. ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
  215. if (!str.empty() && str[str.size()-1] == '\n')
  216. fStartedNewLine = true;
  217. else
  218. fStartedNewLine = false;
  219. ret = fwrite(str.data(), 1, str.size(), fileout);
  220. }
  221. return ret;
  222. }
  223. static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
  224. {
  225. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  226. if (name.find("-no") == 0)
  227. {
  228. std::string positive("-");
  229. positive.append(name.begin()+3, name.end());
  230. if (mapSettingsRet.count(positive) == 0)
  231. {
  232. bool value = !GetBoolArg(name, false);
  233. mapSettingsRet[positive] = (value ? "1" : "0");
  234. }
  235. }
  236. }
  237. void ParseParameters(int argc, const char* const argv[])
  238. {
  239. mapArgs.clear();
  240. mapMultiArgs.clear();
  241. for (int i = 1; i < argc; i++)
  242. {
  243. std::string str(argv[i]);
  244. std::string strValue;
  245. size_t is_index = str.find('=');
  246. if (is_index != std::string::npos)
  247. {
  248. strValue = str.substr(is_index+1);
  249. str = str.substr(0, is_index);
  250. }
  251. #ifdef WIN32
  252. boost::to_lower(str);
  253. if (boost::algorithm::starts_with(str, "/"))
  254. str = "-" + str.substr(1);
  255. #endif
  256. if (str[0] != '-')
  257. break;
  258. // Interpret --foo as -foo.
  259. // If both --foo and -foo are set, the last takes effect.
  260. if (str.length() > 1 && str[1] == '-')
  261. str = str.substr(1);
  262. mapArgs[str] = strValue;
  263. mapMultiArgs[str].push_back(strValue);
  264. }
  265. // New 0.6 features:
  266. BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
  267. {
  268. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  269. InterpretNegativeSetting(entry.first, mapArgs);
  270. }
  271. }
  272. std::string GetArg(const std::string& strArg, const std::string& strDefault)
  273. {
  274. if (mapArgs.count(strArg))
  275. return mapArgs[strArg];
  276. return strDefault;
  277. }
  278. int64_t GetArg(const std::string& strArg, int64_t nDefault)
  279. {
  280. if (mapArgs.count(strArg))
  281. return atoi64(mapArgs[strArg]);
  282. return nDefault;
  283. }
  284. bool GetBoolArg(const std::string& strArg, bool fDefault)
  285. {
  286. if (mapArgs.count(strArg))
  287. {
  288. if (mapArgs[strArg].empty())
  289. return true;
  290. return (atoi(mapArgs[strArg]) != 0);
  291. }
  292. return fDefault;
  293. }
  294. bool SoftSetArg(const std::string& strArg, const std::string& strValue)
  295. {
  296. if (mapArgs.count(strArg))
  297. return false;
  298. mapArgs[strArg] = strValue;
  299. return true;
  300. }
  301. bool SoftSetBoolArg(const std::string& strArg, bool fValue)
  302. {
  303. if (fValue)
  304. return SoftSetArg(strArg, std::string("1"));
  305. else
  306. return SoftSetArg(strArg, std::string("0"));
  307. }
  308. static const int screenWidth = 79;
  309. static const int optIndent = 2;
  310. static const int msgIndent = 7;
  311. std::string HelpMessageGroup(const std::string &message) {
  312. return std::string(message) + std::string("\n\n");
  313. }
  314. std::string HelpMessageOpt(const std::string &option, const std::string &message) {
  315. return std::string(optIndent,' ') + std::string(option) +
  316. std::string("\n") + std::string(msgIndent,' ') +
  317. FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
  318. std::string("\n\n");
  319. }
  320. static std::string FormatException(const std::exception* pex, const char* pszThread)
  321. {
  322. #ifdef WIN32
  323. char pszModule[MAX_PATH] = "";
  324. GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
  325. #else
  326. const char* pszModule = "Zcash";
  327. #endif
  328. if (pex)
  329. return strprintf(
  330. "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
  331. else
  332. return strprintf(
  333. "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
  334. }
  335. void PrintExceptionContinue(const std::exception* pex, const char* pszThread)
  336. {
  337. std::string message = FormatException(pex, pszThread);
  338. LogPrintf("\n\n************************\n%s\n", message);
  339. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  340. strMiscWarning = message;
  341. }
  342. boost::filesystem::path GetDefaultDataDir()
  343. {
  344. namespace fs = boost::filesystem;
  345. // Windows < Vista: C:\Documents and Settings\Username\Application Data\Zcash
  346. // Windows >= Vista: C:\Users\Username\AppData\Roaming\Zcash
  347. // Mac: ~/Library/Application Support/Zcash
  348. // Unix: ~/.zcash
  349. #ifdef WIN32
  350. // Windows
  351. return GetSpecialFolderPath(CSIDL_APPDATA) / "Zcash";
  352. #else
  353. fs::path pathRet;
  354. char* pszHome = getenv("HOME");
  355. if (pszHome == NULL || strlen(pszHome) == 0)
  356. pathRet = fs::path("/");
  357. else
  358. pathRet = fs::path(pszHome);
  359. #ifdef MAC_OSX
  360. // Mac
  361. pathRet /= "Library/Application Support";
  362. TryCreateDirectory(pathRet);
  363. return pathRet / "Zcash";
  364. #else
  365. // Unix
  366. return pathRet / ".zcash";
  367. #endif
  368. #endif
  369. }
  370. static boost::filesystem::path pathCached;
  371. static boost::filesystem::path pathCachedNetSpecific;
  372. static boost::filesystem::path zc_paramsPathCached;
  373. static CCriticalSection csPathCached;
  374. static boost::filesystem::path ZC_GetBaseParamsDir()
  375. {
  376. // Copied from GetDefaultDataDir and adapter for zcash params.
  377. namespace fs = boost::filesystem;
  378. // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
  379. // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
  380. // Mac: ~/Library/Application Support/ZcashParams
  381. // Unix: ~/.zcash-params
  382. #ifdef WIN32
  383. // Windows
  384. return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
  385. #else
  386. fs::path pathRet;
  387. char* pszHome = getenv("HOME");
  388. if (pszHome == NULL || strlen(pszHome) == 0)
  389. pathRet = fs::path("/");
  390. else
  391. pathRet = fs::path(pszHome);
  392. #ifdef MAC_OSX
  393. // Mac
  394. pathRet /= "Library/Application Support";
  395. TryCreateDirectory(pathRet);
  396. return pathRet / "ZcashParams";
  397. #else
  398. // Unix
  399. return pathRet / ".zcash-params";
  400. #endif
  401. #endif
  402. }
  403. const boost::filesystem::path &ZC_GetParamsDir()
  404. {
  405. namespace fs = boost::filesystem;
  406. LOCK(csPathCached); // Reuse the same lock as upstream.
  407. fs::path &path = zc_paramsPathCached;
  408. // This can be called during exceptions by LogPrintf(), so we cache the
  409. // value so we don't have to do memory allocations after that.
  410. if (!path.empty())
  411. return path;
  412. path = ZC_GetBaseParamsDir();
  413. return path;
  414. }
  415. const boost::filesystem::path &GetDataDir(bool fNetSpecific)
  416. {
  417. namespace fs = boost::filesystem;
  418. LOCK(csPathCached);
  419. fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
  420. // This can be called during exceptions by LogPrintf(), so we cache the
  421. // value so we don't have to do memory allocations after that.
  422. if (!path.empty())
  423. return path;
  424. if (mapArgs.count("-datadir")) {
  425. path = fs::system_complete(mapArgs["-datadir"]);
  426. if (!fs::is_directory(path)) {
  427. path = "";
  428. return path;
  429. }
  430. } else {
  431. path = GetDefaultDataDir();
  432. }
  433. if (fNetSpecific)
  434. path /= BaseParams().DataDir();
  435. fs::create_directories(path);
  436. return path;
  437. }
  438. void ClearDatadirCache()
  439. {
  440. pathCached = boost::filesystem::path();
  441. pathCachedNetSpecific = boost::filesystem::path();
  442. }
  443. boost::filesystem::path GetConfigFile()
  444. {
  445. boost::filesystem::path pathConfigFile(GetArg("-conf", "zcash.conf"));
  446. if (!pathConfigFile.is_complete())
  447. pathConfigFile = GetDataDir(false) / pathConfigFile;
  448. return pathConfigFile;
  449. }
  450. void ReadConfigFile(map<string, string>& mapSettingsRet,
  451. map<string, vector<string> >& mapMultiSettingsRet)
  452. {
  453. boost::filesystem::ifstream streamConfig(GetConfigFile());
  454. if (!streamConfig.good())
  455. return; // No zcash.conf file is OK
  456. set<string> setOptions;
  457. setOptions.insert("*");
  458. for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
  459. {
  460. // Don't overwrite existing settings so command line settings override zcash.conf
  461. string strKey = string("-") + it->string_key;
  462. if (mapSettingsRet.count(strKey) == 0)
  463. {
  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);
  467. }
  468. mapMultiSettingsRet[strKey].push_back(it->value[0]);
  469. }
  470. // If datadir is changed in .conf file:
  471. ClearDatadirCache();
  472. }
  473. #ifndef WIN32
  474. boost::filesystem::path GetPidFile()
  475. {
  476. boost::filesystem::path pathPidFile(GetArg("-pid", "zcashd.pid"));
  477. if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
  478. return pathPidFile;
  479. }
  480. void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
  481. {
  482. FILE* file = fopen(path.string().c_str(), "w");
  483. if (file)
  484. {
  485. fprintf(file, "%d\n", pid);
  486. fclose(file);
  487. }
  488. }
  489. #endif
  490. bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
  491. {
  492. #ifdef WIN32
  493. return MoveFileExA(src.string().c_str(), dest.string().c_str(),
  494. MOVEFILE_REPLACE_EXISTING) != 0;
  495. #else
  496. int rc = std::rename(src.string().c_str(), dest.string().c_str());
  497. return (rc == 0);
  498. #endif /* WIN32 */
  499. }
  500. /**
  501. * Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
  502. * Specifically handles case where path p exists, but it wasn't possible for the user to
  503. * write to the parent directory.
  504. */
  505. bool TryCreateDirectory(const boost::filesystem::path& p)
  506. {
  507. try
  508. {
  509. return boost::filesystem::create_directory(p);
  510. } catch (const boost::filesystem::filesystem_error&) {
  511. if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
  512. throw;
  513. }
  514. // create_directory didn't create the directory, it had to have existed already
  515. return false;
  516. }
  517. void FileCommit(FILE *fileout)
  518. {
  519. fflush(fileout); // harmless if redundantly called
  520. #ifdef WIN32
  521. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
  522. FlushFileBuffers(hFile);
  523. #else
  524. #if defined(__linux__) || defined(__NetBSD__)
  525. fdatasync(fileno(fileout));
  526. #elif defined(__APPLE__) && defined(F_FULLFSYNC)
  527. fcntl(fileno(fileout), F_FULLFSYNC, 0);
  528. #else
  529. fsync(fileno(fileout));
  530. #endif
  531. #endif
  532. }
  533. bool TruncateFile(FILE *file, unsigned int length) {
  534. #if defined(WIN32)
  535. return _chsize(_fileno(file), length) == 0;
  536. #else
  537. return ftruncate(fileno(file), length) == 0;
  538. #endif
  539. }
  540. /**
  541. * this function tries to raise the file descriptor limit to the requested number.
  542. * It returns the actual file descriptor limit (which may be more or less than nMinFD)
  543. */
  544. int RaiseFileDescriptorLimit(int nMinFD) {
  545. #if defined(WIN32)
  546. return 2048;
  547. #else
  548. struct rlimit limitFD;
  549. if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
  550. if (limitFD.rlim_cur < (rlim_t)nMinFD) {
  551. limitFD.rlim_cur = nMinFD;
  552. if (limitFD.rlim_cur > limitFD.rlim_max)
  553. limitFD.rlim_cur = limitFD.rlim_max;
  554. setrlimit(RLIMIT_NOFILE, &limitFD);
  555. getrlimit(RLIMIT_NOFILE, &limitFD);
  556. }
  557. return limitFD.rlim_cur;
  558. }
  559. return nMinFD; // getrlimit failed, assume it's fine
  560. #endif
  561. }
  562. /**
  563. * this function tries to make a particular range of a file allocated (corresponding to disk space)
  564. * it is advisory, and the range specified in the arguments will never contain live data
  565. */
  566. void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
  567. #if defined(WIN32)
  568. // Windows-specific version
  569. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
  570. LARGE_INTEGER nFileSize;
  571. int64_t nEndPos = (int64_t)offset + length;
  572. nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
  573. nFileSize.u.HighPart = nEndPos >> 32;
  574. SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
  575. SetEndOfFile(hFile);
  576. #elif defined(MAC_OSX)
  577. // OSX specific version
  578. fstore_t fst;
  579. fst.fst_flags = F_ALLOCATECONTIG;
  580. fst.fst_posmode = F_PEOFPOSMODE;
  581. fst.fst_offset = 0;
  582. fst.fst_length = (off_t)offset + length;
  583. fst.fst_bytesalloc = 0;
  584. if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
  585. fst.fst_flags = F_ALLOCATEALL;
  586. fcntl(fileno(file), F_PREALLOCATE, &fst);
  587. }
  588. ftruncate(fileno(file), fst.fst_length);
  589. #elif defined(__linux__)
  590. // Version using posix_fallocate
  591. off_t nEndPos = (off_t)offset + length;
  592. posix_fallocate(fileno(file), 0, nEndPos);
  593. #else
  594. // Fallback version
  595. // TODO: just write one byte per block
  596. static const char buf[65536] = {};
  597. fseek(file, offset, SEEK_SET);
  598. while (length > 0) {
  599. unsigned int now = 65536;
  600. if (length < now)
  601. now = length;
  602. fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
  603. length -= now;
  604. }
  605. #endif
  606. }
  607. void ShrinkDebugFile()
  608. {
  609. // Scroll debug.log if it's getting too big
  610. boost::filesystem::path pathLog = GetDataDir() / "debug.log";
  611. FILE* file = fopen(pathLog.string().c_str(), "r");
  612. if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
  613. {
  614. // Restart the file with some of the end
  615. std::vector <char> vch(200000,0);
  616. fseek(file, -((long)vch.size()), SEEK_END);
  617. int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
  618. fclose(file);
  619. file = fopen(pathLog.string().c_str(), "w");
  620. if (file)
  621. {
  622. fwrite(begin_ptr(vch), 1, nBytes, file);
  623. fclose(file);
  624. }
  625. }
  626. else if (file != NULL)
  627. fclose(file);
  628. }
  629. #ifdef WIN32
  630. boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
  631. {
  632. namespace fs = boost::filesystem;
  633. char pszPath[MAX_PATH] = "";
  634. if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
  635. {
  636. return fs::path(pszPath);
  637. }
  638. LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
  639. return fs::path("");
  640. }
  641. #endif
  642. boost::filesystem::path GetTempPath() {
  643. #if BOOST_FILESYSTEM_VERSION == 3
  644. return boost::filesystem::temp_directory_path();
  645. #else
  646. // TODO: remove when we don't support filesystem v2 anymore
  647. boost::filesystem::path path;
  648. #ifdef WIN32
  649. char pszPath[MAX_PATH] = "";
  650. if (GetTempPathA(MAX_PATH, pszPath))
  651. path = boost::filesystem::path(pszPath);
  652. #else
  653. path = boost::filesystem::path("/tmp");
  654. #endif
  655. if (path.empty() || !boost::filesystem::is_directory(path)) {
  656. LogPrintf("GetTempPath(): failed to find temp path\n");
  657. return boost::filesystem::path("");
  658. }
  659. return path;
  660. #endif
  661. }
  662. void runCommand(std::string strCommand)
  663. {
  664. int nErr = ::system(strCommand.c_str());
  665. if (nErr)
  666. LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
  667. }
  668. void RenameThread(const char* name)
  669. {
  670. #if defined(PR_SET_NAME)
  671. // Only the first 15 characters are used (16 - NUL terminator)
  672. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  673. #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
  674. pthread_set_name_np(pthread_self(), name);
  675. #elif defined(MAC_OSX)
  676. pthread_setname_np(name);
  677. #else
  678. // Prevent warnings for unused parameters...
  679. (void)name;
  680. #endif
  681. }
  682. void SetupEnvironment()
  683. {
  684. // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
  685. // may be invalid, in which case the "C" locale is used as fallback.
  686. #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
  687. try {
  688. std::locale(""); // Raises a runtime error if current locale is invalid
  689. } catch (const std::runtime_error&) {
  690. setenv("LC_ALL", "C", 1);
  691. }
  692. #endif
  693. // The path locale is lazy initialized and to avoid deinitialization errors
  694. // in multithreading environments, it is set explicitly by the main thread.
  695. // A dummy locale is used to extract the internal default locale, used by
  696. // boost::filesystem::path, which is then used to explicitly imbue the path.
  697. std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
  698. boost::filesystem::path::imbue(loc);
  699. }
  700. void SetThreadPriority(int nPriority)
  701. {
  702. #ifdef WIN32
  703. SetThreadPriority(GetCurrentThread(), nPriority);
  704. #else // WIN32
  705. #ifdef PRIO_THREAD
  706. setpriority(PRIO_THREAD, 0, nPriority);
  707. #else // PRIO_THREAD
  708. setpriority(PRIO_PROCESS, 0, nPriority);
  709. #endif // PRIO_THREAD
  710. #endif // WIN32
  711. }