Changeset View
Changeset View
Standalone View
Standalone View
src/util.cpp
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | |||||
#ifdef HAVE_SYS_PRCTL_H | #ifdef HAVE_SYS_PRCTL_H | ||||
#include <sys/prctl.h> | #include <sys/prctl.h> | ||||
#endif | #endif | ||||
#ifdef HAVE_MALLOPT_ARENA_MAX | #ifdef HAVE_MALLOPT_ARENA_MAX | ||||
#include <malloc.h> | #include <malloc.h> | ||||
#endif | #endif | ||||
#include <boost/interprocess/sync/file_lock.hpp> | |||||
#include <boost/program_options/detail/config_file.hpp> | #include <boost/program_options/detail/config_file.hpp> | ||||
#include <boost/thread.hpp> | #include <boost/thread.hpp> | ||||
#include <openssl/conf.h> | #include <openssl/conf.h> | ||||
#include <openssl/rand.h> | #include <openssl/rand.h> | ||||
// Application startup time (used for uptime calculation) | // Application startup time (used for uptime calculation) | ||||
const int64_t nStartupTime = GetTime(); | const int64_t nStartupTime = GetTime(); | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | ~CInit() { | ||||
CRYPTO_set_locking_callback(nullptr); | CRYPTO_set_locking_callback(nullptr); | ||||
for (int i = 0; i < CRYPTO_num_locks(); i++) { | for (int i = 0; i < CRYPTO_num_locks(); i++) { | ||||
delete ppmutexOpenSSL[i]; | delete ppmutexOpenSSL[i]; | ||||
} | } | ||||
OPENSSL_free(ppmutexOpenSSL); | OPENSSL_free(ppmutexOpenSSL); | ||||
} | } | ||||
} instance_of_cinit; | } instance_of_cinit; | ||||
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, | |||||
bool probe_only) { | |||||
fs::path pathLockFile = directory / lockfile_name; | |||||
// empty lock file; created if it doesn't exist. | |||||
FILE *file = fsbridge::fopen(pathLockFile, "a"); | |||||
if (file) { | |||||
fclose(file); | |||||
} | |||||
try { | |||||
static std::map<std::string, boost::interprocess::file_lock> locks; | |||||
boost::interprocess::file_lock &lock = | |||||
locks.emplace(pathLockFile.string(), pathLockFile.string().c_str()) | |||||
.first->second; | |||||
if (!lock.try_lock()) { | |||||
return false; | |||||
} | |||||
if (probe_only) { | |||||
lock.unlock(); | |||||
} | |||||
} catch (const boost::interprocess::interprocess_exception &e) { | |||||
return error("Error while attempting to lock directory %s: %s", | |||||
directory.string(), e.what()); | |||||
} | |||||
return true; | |||||
} | |||||
/** | /** | ||||
* Interpret a string argument as a boolean. | * Interpret a string argument as a boolean. | ||||
* | * | ||||
* The definition of atoi() requires that non-numeric string values like "foo", | * The definition of atoi() requires that non-numeric string values like "foo", | ||||
* return 0. This means that if a user unintentionally supplies a non-integer | * return 0. This means that if a user unintentionally supplies a non-integer | ||||
* argument here, the return value is always false. This means that -foo=false | * argument here, the return value is always false. This means that -foo=false | ||||
* does what the user probably expects, but -foo=true is well defined but does | * does what the user probably expects, but -foo=true is well defined but does | ||||
* not do what they probably expected. | * not do what they probably expected. | ||||
▲ Show 20 Lines • Show All 820 Lines • Show Last 20 Lines |