From 7285c2e4ed54d1f0b04bc1c5ebacf2bb4064def0 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Mon, 5 Sep 2016 18:15:23 +0100 Subject: [PATCH] Make sure the container is stopped before we try to start it --- src/anbox/container/lxc_container.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/anbox/container/lxc_container.cpp b/src/anbox/container/lxc_container.cpp index d8804fc..6565002 100644 --- a/src/anbox/container/lxc_container.cpp +++ b/src/anbox/container/lxc_container.cpp @@ -57,8 +57,10 @@ void LxcContainer::start(const Configuration &configuration) { if (getuid() != 0) BOOST_THROW_EXCEPTION(std::runtime_error("You have to start the container as root")); - if (container_ && container_->is_running(container_)) - BOOST_THROW_EXCEPTION(std::runtime_error("Container already started")); + if (container_ && container_->is_running(container_)) { + BOOST_THROW_EXCEPTION(std::runtime_error("Container already started, stopping it now")); + container_->stop(container_); + } if (!container_) { DEBUG("Containers are stored in %s", config::container_config_path()); @@ -69,6 +71,11 @@ void LxcContainer::start(const Configuration &configuration) { container_ = lxc_container_new("default", config::container_config_path().c_str()); if (!container_) BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create LXC container instance")); + + // If container is still running (for example after a crash) we stop it here to ensure + // its configuration is synchronized. + if (container_->is_running(container_)) + container_->stop(container_); } // We drop all not needed capabilities