Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Monitor wallet lock file and shut down if it's removed. #6691

Merged
merged 2 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once
#include <eosio/chain/transaction.hpp>
#include <eosio/wallet_plugin/wallet_api.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
#include <chrono>
Expand Down Expand Up @@ -142,6 +143,7 @@ class wallet_manager {
boost::filesystem::path lock_path = dir / "wallet.lock";
std::unique_ptr<boost::interprocess::file_lock> wallet_dir_lock;

void start_lock_watch(std::shared_ptr<boost::asio::deadline_timer> t);
void initialize_lock();
};

Expand Down
19 changes: 19 additions & 0 deletions plugins/wallet_plugin/wallet_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file
* @copyright defined in eos/LICENSE
*/
#include <appbase/application.hpp>
#include <eosio/wallet_plugin/wallet_manager.hpp>
#include <eosio/wallet_plugin/wallet.hpp>
#include <eosio/wallet_plugin/se_wallet.hpp>
Expand Down Expand Up @@ -275,6 +276,22 @@ void wallet_manager::own_and_use_wallet(const string& name, std::unique_ptr<wall
wallets.emplace(name, std::move(wallet));
}

void wallet_manager::start_lock_watch(std::shared_ptr<boost::asio::deadline_timer> t)
{
t->async_wait([t, this](const boost::system::error_code& /*ec*/)
{
struct stat statbuf;
int rc = stat(lock_path.string().c_str(), &statbuf);
jgiszczak marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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<boost::asio::deadline_timer>(appbase::app().get_io_service(), boost::posix_time::seconds(1));
start_lock_watch(timer);
}

} // namespace wallet
Expand Down