提交 d11a281d 编写于 作者: S Simon Fels

Terminate process when container manager disconnects or container start fails

上级 7a2cef31
...@@ -144,6 +144,10 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -144,6 +144,10 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
})); }));
container::Client container(rt); container::Client container(rt);
container.register_terminate_handler([&]() {
WARNING("Lost connection to container manager, terminating.");
trap->stop();
});
container::Configuration container_configuration; container::Configuration container_configuration;
container_configuration.bind_mounts = { container_configuration.bind_mounts = {
{qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"}, {qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"},
......
...@@ -18,11 +18,11 @@ ...@@ -18,11 +18,11 @@
#include "anbox/container/client.h" #include "anbox/container/client.h"
#include "anbox/config.h" #include "anbox/config.h"
#include "anbox/container/management_api_stub.h" #include "anbox/container/management_api_stub.h"
#include "anbox/logger.h"
#include "anbox/network/local_socket_messenger.h" #include "anbox/network/local_socket_messenger.h"
#include "anbox/rpc/channel.h" #include "anbox/rpc/channel.h"
#include "anbox/rpc/message_processor.h" #include "anbox/rpc/message_processor.h"
#include "anbox/rpc/pending_call_cache.h" #include "anbox/rpc/pending_call_cache.h"
#include "anbox/logger.h"
namespace ba = boost::asio; namespace ba = boost::asio;
namespace bs = boost::system; namespace bs = boost::system;
...@@ -43,7 +43,17 @@ Client::Client(const std::shared_ptr<Runtime> &rt) ...@@ -43,7 +43,17 @@ Client::Client(const std::shared_ptr<Runtime> &rt)
Client::~Client() {} Client::~Client() {}
void Client::start_container(const Configuration &configuration) { void Client::start_container(const Configuration &configuration) {
management_api_->start_container(configuration); try {
management_api_->start_container(configuration);
} catch (const std::exception &e) {
ERROR("Failed to start container: %s", e.what());
if (terminate_callback_)
terminate_callback_();
}
}
void Client::register_terminate_handler(const TerminateCallback &callback) {
terminate_callback_ = callback;
} }
void Client::read_next_message() { void Client::read_next_message() {
...@@ -54,7 +64,11 @@ void Client::read_next_message() { ...@@ -54,7 +64,11 @@ void Client::read_next_message() {
void Client::on_read_size(const boost::system::error_code &error, void Client::on_read_size(const boost::system::error_code &error,
std::size_t bytes_read) { std::size_t bytes_read) {
if (error) BOOST_THROW_EXCEPTION(std::runtime_error(error.message())); if (error) {
if (terminate_callback_)
terminate_callback_();
return;
}
std::vector<std::uint8_t> data(bytes_read); std::vector<std::uint8_t> data(bytes_read);
std::copy(buffer_.data(), buffer_.data() + bytes_read, data.data()); std::copy(buffer_.data(), buffer_.data() + bytes_read, data.data());
......
...@@ -34,11 +34,15 @@ namespace container { ...@@ -34,11 +34,15 @@ namespace container {
class ManagementApiStub; class ManagementApiStub;
class Client { class Client {
public: public:
typedef std::function<void()> TerminateCallback;
Client(const std::shared_ptr<Runtime> &rt); Client(const std::shared_ptr<Runtime> &rt);
~Client(); ~Client();
void start_container(const Configuration &configuration); void start_container(const Configuration &configuration);
void register_terminate_handler(const TerminateCallback &callback);
private: private:
void read_next_message(); void read_next_message();
void on_read_size(const boost::system::error_code &ec, void on_read_size(const boost::system::error_code &ec,
...@@ -50,6 +54,7 @@ class Client { ...@@ -50,6 +54,7 @@ class Client {
std::shared_ptr<ManagementApiStub> management_api_; std::shared_ptr<ManagementApiStub> management_api_;
std::shared_ptr<rpc::MessageProcessor> processor_; std::shared_ptr<rpc::MessageProcessor> processor_;
std::array<std::uint8_t, 8192> buffer_; std::array<std::uint8_t, 8192> buffer_;
TerminateCallback terminate_callback_;
}; };
} // namespace container } // namespace container
} // namespace anbox } // namespace anbox
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册