aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp101
1 files changed, 70 insertions, 31 deletions
diff --git a/src/util.cpp b/src/util.cpp
index a3e4b0acfb..69ceefc8cd 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1,5 +1,5 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
-// Copyright (c) 2009-2016 The Bitcoin Core developers
+// Copyright (c) 2009-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -72,11 +72,13 @@
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
+#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/thread.hpp>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/conf.h>
+#include <thread>
// Application startup time (used for uptime calculation)
const int64_t nStartupTime = GetTime();
@@ -157,10 +159,10 @@ instance_of_cinit;
* the mutex).
*/
-static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
+static std::once_flag debugPrintInitFlag;
/**
- * We use boost::call_once() to make sure mutexDebugLog and
+ * We use std::call_once() to make sure mutexDebugLog and
* vMsgsBeforeOpenLog are initialized in a thread-safe manner.
*
* NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
@@ -169,7 +171,7 @@ static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
* tested, explicit destruction of these objects can be implemented.
*/
static FILE* fileout = nullptr;
-static boost::mutex* mutexDebugLog = nullptr;
+static std::mutex* mutexDebugLog = nullptr;
static std::list<std::string>* vMsgsBeforeOpenLog;
static int FileWriteStr(const std::string &str, FILE *fp)
@@ -180,24 +182,20 @@ static int FileWriteStr(const std::string &str, FILE *fp)
static void DebugPrintInit()
{
assert(mutexDebugLog == nullptr);
- mutexDebugLog = new boost::mutex();
+ mutexDebugLog = new std::mutex();
vMsgsBeforeOpenLog = new std::list<std::string>;
}
fs::path GetDebugLogPath()
{
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
- if (logfile.is_absolute()) {
- return logfile;
- } else {
- return GetDataDir() / logfile;
- }
+ return AbsPathForConfigVal(logfile);
}
bool OpenDebugLog()
{
- boost::call_once(&DebugPrintInit, debugPrintInitFlag);
- boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
+ std::call_once(debugPrintInitFlag, &DebugPrintInit);
+ std::lock_guard<std::mutex> scoped_lock(*mutexDebugLog);
assert(fileout == nullptr);
assert(vMsgsBeforeOpenLog);
@@ -316,12 +314,14 @@ static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fSt
if (*fStartedNewLine) {
int64_t nTimeMicros = GetTimeMicros();
- strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
- if (fLogTimeMicros)
- strStamped += strprintf(".%06d", nTimeMicros%1000000);
+ strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
+ if (fLogTimeMicros) {
+ strStamped.pop_back();
+ strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
+ }
int64_t mocktime = GetMockTime();
if (mocktime) {
- strStamped += " (mocktime: " + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", mocktime) + ")";
+ strStamped += " (mocktime: " + FormatISO8601DateTime(mocktime) + ")";
}
strStamped += ' ' + str;
} else
@@ -350,8 +350,8 @@ int LogPrintStr(const std::string &str)
}
else if (fPrintToDebugLog)
{
- boost::call_once(&DebugPrintInit, debugPrintInitFlag);
- boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
+ std::call_once(debugPrintInitFlag, &DebugPrintInit);
+ std::lock_guard<std::mutex> scoped_lock(*mutexDebugLog);
// buffer if we haven't opened the log yet
if (fileout == nullptr) {
@@ -375,6 +375,50 @@ int LogPrintStr(const std::string &str)
return ret;
}
+/** A map that contains all the currently held directory locks. After
+ * successful locking, these will be held here until the global destructor
+ * cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks
+ * is called.
+ */
+static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> dir_locks;
+/** Mutex to protect dir_locks. */
+static std::mutex cs_dir_locks;
+
+bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
+{
+ std::lock_guard<std::mutex> ulock(cs_dir_locks);
+ fs::path pathLockFile = directory / lockfile_name;
+
+ // If a lock for this directory already exists in the map, don't try to re-lock it
+ if (dir_locks.count(pathLockFile.string())) {
+ return true;
+ }
+
+ // Create empty lock file if it doesn't exist.
+ FILE* file = fsbridge::fopen(pathLockFile, "a");
+ if (file) fclose(file);
+
+ try {
+ auto lock = MakeUnique<boost::interprocess::file_lock>(pathLockFile.string().c_str());
+ if (!lock->try_lock()) {
+ return false;
+ }
+ if (!probe_only) {
+ // Lock successful and we're not just probing, put it into the map
+ dir_locks.emplace(pathLockFile.string(), std::move(lock));
+ }
+ } catch (const boost::interprocess::interprocess_exception& e) {
+ return error("Error while attempting to lock directory %s: %s", directory.string(), e.what());
+ }
+ return true;
+}
+
+void ReleaseDirectoryLocks()
+{
+ std::lock_guard<std::mutex> ulock(cs_dir_locks);
+ dir_locks.clear();
+}
+
/** Interpret string as boolean, for argument parsing */
static bool InterpretBool(const std::string& strValue)
{
@@ -602,11 +646,7 @@ void ClearDatadirCache()
fs::path GetConfigFile(const std::string& confPath)
{
- fs::path pathConfigFile(confPath);
- if (!pathConfigFile.is_complete())
- pathConfigFile = GetDataDir(false) / pathConfigFile;
-
- return pathConfigFile;
+ return AbsPathForConfigVal(fs::path(confPath), false);
}
void ArgsManager::ReadConfigFile(const std::string& confPath)
@@ -641,9 +681,7 @@ void ArgsManager::ReadConfigFile(const std::string& confPath)
#ifndef WIN32
fs::path GetPidFile()
{
- fs::path pathPidFile(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME));
- if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
- return pathPidFile;
+ return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
}
void CreatePidFile(const fs::path &path, pid_t pid)
@@ -891,11 +929,7 @@ bool SetupNetworking()
int GetNumCores()
{
-#if BOOST_VERSION >= 105600
- return boost::thread::physical_concurrency();
-#else // Must fall back to hardware_concurrency, which unfortunately counts virtual cores
- return boost::thread::hardware_concurrency();
-#endif
+ return std::thread::hardware_concurrency();
}
std::string CopyrightHolders(const std::string& strPrefix)
@@ -914,3 +948,8 @@ int64_t GetStartupTime()
{
return nStartupTime;
}
+
+fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
+{
+ return fs::absolute(path, GetDataDir(net_specific));
+}