wallet: prevent the same wallet file from being opened by multiple processes

This commit is contained in:
stoffu 2018-06-12 15:29:33 +09:00
parent 59de6f8d99
commit 3d623a86d1
No known key found for this signature in database
GPG key ID: 41DAB8343A9EC012
3 changed files with 10 additions and 0 deletions

View file

@ -721,6 +721,7 @@ bool WalletImpl::close(bool store)
LOG_PRINT_L1("Calling wallet::stop..."); LOG_PRINT_L1("Calling wallet::stop...");
m_wallet->stop(); m_wallet->stop();
LOG_PRINT_L1("wallet::stop done"); LOG_PRINT_L1("wallet::stop done");
m_wallet->deinit();
result = true; result = true;
clearStatus(); clearStatus();
} catch (const std::exception &e) { } catch (const std::exception &e) {

View file

@ -2636,6 +2636,7 @@ void wallet2::detach_blockchain(uint64_t height)
bool wallet2::deinit() bool wallet2::deinit()
{ {
m_is_initialized=false; m_is_initialized=false;
m_keys_file_locker.reset();
return true; return true;
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -2802,10 +2803,12 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]); crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]);
keys_file_data.account_data = cipher; keys_file_data.account_data = cipher;
m_keys_file_locker.reset();
std::string buf; std::string buf;
r = ::serialization::dump_binary(keys_file_data, buf); r = ::serialization::dump_binary(keys_file_data, buf);
r = r && epee::file_io_utils::save_string_to_file(keys_file_name, buf); //and never touch wallet_keys_file again, only read r = r && epee::file_io_utils::save_string_to_file(keys_file_name, buf); //and never touch wallet_keys_file again, only read
CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << keys_file_name); CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << keys_file_name);
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
return true; return true;
} }
@ -3935,12 +3938,17 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
boost::system::error_code e; boost::system::error_code e;
bool exists = boost::filesystem::exists(m_keys_file, e); bool exists = boost::filesystem::exists(m_keys_file, e);
THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file); THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file);
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
THROW_WALLET_EXCEPTION_IF(!m_keys_file_locker->locked(), error::wallet_internal_error, "internal error: \"" + m_keys_file + "\" is opened by another wallet program");
// this temporary unlocking is necessary for Windows (otherwise the file couldn't be loaded).
m_keys_file_locker.reset();
if (!load_keys(m_keys_file, password)) if (!load_keys(m_keys_file, password))
{ {
THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, m_keys_file); THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, m_keys_file);
} }
LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_nettype)); LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_nettype));
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
//keys loaded ok! //keys loaded ok!
//try to load wallet file. but even if we failed, it is not big problem //try to load wallet file. but even if we failed, it is not big problem

View file

@ -1308,6 +1308,7 @@ namespace tools
boost::optional<crypto::chacha_key> m_ringdb_key; boost::optional<crypto::chacha_key> m_ringdb_key;
uint64_t m_last_block_reward; uint64_t m_last_block_reward;
std::unique_ptr<tools::file_locker> m_keys_file_locker;
}; };
} }
BOOST_CLASS_VERSION(tools::wallet2, 25) BOOST_CLASS_VERSION(tools::wallet2, 25)