From 765bbc7fd3436583c27b394d27c10f90a3252d3f Mon Sep 17 00:00:00 2001 From: Jonathan Giszczak Date: Fri, 1 Feb 2019 18:04:56 -0600 Subject: [PATCH 1/2] Monitor wallet lock file and shut down if it's removed. Resolves #5998. --- .../eosio/wallet_plugin/wallet_manager.hpp | 2 ++ plugins/wallet_plugin/wallet_manager.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/plugins/wallet_plugin/include/eosio/wallet_plugin/wallet_manager.hpp b/plugins/wallet_plugin/include/eosio/wallet_plugin/wallet_manager.hpp index e73bc32d971..e61fb7b7617 100644 --- a/plugins/wallet_plugin/include/eosio/wallet_plugin/wallet_manager.hpp +++ b/plugins/wallet_plugin/include/eosio/wallet_plugin/wallet_manager.hpp @@ -5,6 +5,7 @@ #pragma once #include #include +#include #include #include #include @@ -142,6 +143,7 @@ class wallet_manager { boost::filesystem::path lock_path = dir / "wallet.lock"; std::unique_ptr wallet_dir_lock; + void start_lock_watch(std::shared_ptr t); void initialize_lock(); }; diff --git a/plugins/wallet_plugin/wallet_manager.cpp b/plugins/wallet_plugin/wallet_manager.cpp index 15a39c9d9bd..e54f97209b7 100644 --- a/plugins/wallet_plugin/wallet_manager.cpp +++ b/plugins/wallet_plugin/wallet_manager.cpp @@ -2,6 +2,7 @@ * @file * @copyright defined in eos/LICENSE */ +#include #include #include #include @@ -275,6 +276,22 @@ void wallet_manager::own_and_use_wallet(const string& name, std::unique_ptr t) +{ + t->async_wait([t, this](const boost::system::error_code& /*ec*/) + { + struct stat statbuf; + int rc = stat(lock_path.string().c_str(), &statbuf); + if(rc == -1) { + if(errno == ENOENT) { + appbase::app().quit(); + EOS_THROW(wallet_exception, "Lock file removed while keosd still running. Terminating."); + } + } + start_lock_watch(t); + }); +} + void wallet_manager::initialize_lock() { //This is technically somewhat racy in here -- if multiple keosd are in this function at once. //I've considered that an acceptable tradeoff to maintain cross-platform boost constructs here @@ -288,6 +305,8 @@ void wallet_manager::initialize_lock() { wallet_dir_lock.reset(); EOS_THROW(wallet_exception, "Failed to lock access to wallet directory; is another keosd running?"); } + auto timer = std::make_shared(appbase::app().get_io_service(), boost::posix_time::seconds(1)); + start_lock_watch(timer); } } // namespace wallet From b43679c8149c41cc8195befe2ed75602ec03be9f Mon Sep 17 00:00:00 2001 From: Jonathan Giszczak Date: Mon, 4 Feb 2019 12:38:35 -0600 Subject: [PATCH 2/2] Convert to boost::filesystem. Reset deadline timer correctly. Convert unrelated FC_THROW to EOS_THROW and correct English in error text. --- plugins/wallet_plugin/wallet_manager.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/wallet_plugin/wallet_manager.cpp b/plugins/wallet_plugin/wallet_manager.cpp index e54f97209b7..b5287173670 100644 --- a/plugins/wallet_plugin/wallet_manager.cpp +++ b/plugins/wallet_plugin/wallet_manager.cpp @@ -272,7 +272,7 @@ wallet_manager::sign_digest(const chain::digest_type& digest, const public_key_t void wallet_manager::own_and_use_wallet(const string& name, std::unique_ptr&& wallet) { if(wallets.find(name) != wallets.end()) - FC_THROW("tried to use wallet name the already existed"); + EOS_THROW(wallet_exception, "Tried to use wallet name that already exists."); wallets.emplace(name, std::move(wallet)); } @@ -280,14 +280,16 @@ void wallet_manager::start_lock_watch(std::shared_ptrasync_wait([t, this](const boost::system::error_code& /*ec*/) { - struct stat statbuf; - int rc = stat(lock_path.string().c_str(), &statbuf); - if(rc == -1) { - if(errno == ENOENT) { + namespace bfs = boost::filesystem; + boost::system::error_code ec; + auto rc = bfs::status(lock_path, ec); + if(ec != boost::system::error_code()) { + if(rc.type() == bfs::file_not_found) { appbase::app().quit(); EOS_THROW(wallet_exception, "Lock file removed while keosd still running. Terminating."); } } + t->expires_from_now(boost::posix_time::seconds(1)); start_lock_watch(t); }); }