Changeset View
Changeset View
Standalone View
Standalone View
src/util.cpp
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | |||||
const char *const BITCOIN_CONF_FILENAME = "bitcoin.conf"; | const char *const BITCOIN_CONF_FILENAME = "bitcoin.conf"; | ||||
const char *const BITCOIN_PID_FILENAME = "bitcoind.pid"; | const char *const BITCOIN_PID_FILENAME = "bitcoind.pid"; | ||||
ArgsManager gArgs; | ArgsManager gArgs; | ||||
CTranslationInterface translationInterface; | CTranslationInterface translationInterface; | ||||
/** Init OpenSSL library multithreading support */ | /** Init OpenSSL library multithreading support */ | ||||
static CCriticalSection **ppmutexOpenSSL; | static std::unique_ptr<CCriticalSection[]> ppmutexOpenSSL; | ||||
void locking_callback(int mode, int i, const char *file, | void locking_callback(int mode, int i, const char *file, | ||||
int line) NO_THREAD_SAFETY_ANALYSIS { | int line) NO_THREAD_SAFETY_ANALYSIS { | ||||
if (mode & CRYPTO_LOCK) { | if (mode & CRYPTO_LOCK) { | ||||
ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]); | ENTER_CRITICAL_SECTION(ppmutexOpenSSL[i]); | ||||
} else { | } else { | ||||
LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]); | LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]); | ||||
} | } | ||||
} | } | ||||
// Init | // Singleton for wrapping OpenSSL setup/teardown. | ||||
class CInit { | class CInit { | ||||
public: | public: | ||||
CInit() { | CInit() { | ||||
// Init OpenSSL library multithreading support. | // Init OpenSSL library multithreading support. | ||||
ppmutexOpenSSL = (CCriticalSection **)OPENSSL_malloc( | ppmutexOpenSSL.reset(new CCriticalSection[CRYPTO_num_locks()]); | ||||
CRYPTO_num_locks() * sizeof(CCriticalSection *)); | |||||
for (int i = 0; i < CRYPTO_num_locks(); i++) { | |||||
ppmutexOpenSSL[i] = new CCriticalSection(); | |||||
} | |||||
CRYPTO_set_locking_callback(locking_callback); | CRYPTO_set_locking_callback(locking_callback); | ||||
// OpenSSL can optionally load a config file which lists optional | // OpenSSL can optionally load a config file which lists optional | ||||
// loadable modules and engines. We don't use them so we don't require | // loadable modules and engines. We don't use them so we don't require | ||||
// the config. However some of our libs may call functions which attempt | // the config. However some of our libs may call functions which attempt | ||||
// to load the config file, possibly resulting in an exit() or crash if | // to load the config file, possibly resulting in an exit() or crash if | ||||
// it is missing or corrupt. Explicitly tell OpenSSL not to try to load | // it is missing or corrupt. Explicitly tell OpenSSL not to try to load | ||||
// the file. The result for our libs will be that the config appears to | // the file. The result for our libs will be that the config appears to | ||||
// have been loaded and there are no modules/engines available. | // have been loaded and there are no modules/engines available. | ||||
OPENSSL_no_config(); | OPENSSL_no_config(); | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
// Seed OpenSSL PRNG with current contents of the screen. | // Seed OpenSSL PRNG with current contents of the screen. | ||||
RAND_screen(); | RAND_screen(); | ||||
#endif | #endif | ||||
// Seed OpenSSL PRNG with performance counter. | // Seed OpenSSL PRNG with performance counter. | ||||
RandAddSeed(); | RandAddSeed(); | ||||
} | } | ||||
~CInit() { | ~CInit() { | ||||
// Securely erase the memory used by the PRNG. | // Securely erase the memory used by the PRNG. | ||||
RAND_cleanup(); | RAND_cleanup(); | ||||
// Shutdown OpenSSL library multithreading support. | // Shutdown OpenSSL library multithreading support. | ||||
CRYPTO_set_locking_callback(nullptr); | CRYPTO_set_locking_callback(nullptr); | ||||
for (int i = 0; i < CRYPTO_num_locks(); i++) { | // Clear the set of locks now to maintain symmetry with the constructor. | ||||
delete ppmutexOpenSSL[i]; | ppmutexOpenSSL.reset(); | ||||
} | |||||
OPENSSL_free(ppmutexOpenSSL); | |||||
} | } | ||||
} instance_of_cinit; | } instance_of_cinit; | ||||
/** | /** | ||||
* A map that contains all the currently held directory locks. After successful | * 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 | * locking, these will be held here until the global destructor cleans them up | ||||
* and thus automatically unlocks them, or ReleaseDirectoryLocks is called. | * and thus automatically unlocks them, or ReleaseDirectoryLocks is called. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 886 Lines • Show Last 20 Lines |