From a9a19f8b772188a9f52db71a886d5701e101e5c8 Mon Sep 17 00:00:00 2001 From: w_angrong Date: Thu, 29 Oct 2020 22:41:53 +0800 Subject: [PATCH] add recover mechanism if system_server die in Android --- src/anbox/platform/sdl/platform.cpp | 27 +++++++++++++++++++++++++++ src/anbox/platform/sdl/platform.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/anbox/platform/sdl/platform.cpp b/src/anbox/platform/sdl/platform.cpp index a5850b7..9cac5a6 100644 --- a/src/anbox/platform/sdl/platform.cpp +++ b/src/anbox/platform/sdl/platform.cpp @@ -149,6 +149,7 @@ Platform::~Platform() { if (ime_socket_ != -1) { close(ime_socket_); } + monitor_exit = false; } void Platform::set_renderer(const std::shared_ptr &renderer) { @@ -226,10 +227,36 @@ void Platform::create_ime_socket() { } ime_fd_ = client_socket; ime_socket_ = ime_socket; + + recover_container(ime_fd_); + DEBUG("accept ime socket successful"); } // Added for Chinese input anbox end +void Platform::recover_container(int monitor_socket) +{ + + std::thread t([this, monitor_socket](){ + char ch = 0; + this->monitor_exit = true; + while(this->monitor_exit) { + ssize_t ret = recv(monitor_socket, &ch, sizeof(char), 0); + /*:the peer has performed and orderly shutdown, recv() shall return 0. + *the peer is InputMethodManagerService in Android, and be stationed in process system_server. + *normally, this socket is only used to send data to Android. + *Android never send anything back, so if recv return anything back and ret is zero, system_server is dead. + *if system_server is dead, we should restart the whole container to avoid error spreads + */ + if(ret == 0) { + ERROR("system_server is dead, GOODBYE!"); + exit(0); + } + } + }); + t.detach(); +} + void Platform::process_events() { event_thread_running_ = true; diff --git a/src/anbox/platform/sdl/platform.h b/src/anbox/platform/sdl/platform.h index d3552d7..ddeb3e3 100644 --- a/src/anbox/platform/sdl/platform.h +++ b/src/anbox/platform/sdl/platform.h @@ -87,6 +87,7 @@ class Platform : public std::enable_shared_from_this, bool mbd_event_fliter(const SDL_Event &event); SDL_Scancode removeKPPropertyIfNeeded(const SDL_Scancode &scan_code); void sync_mod_state(); + void recover_container(int monitor_socket); bool adjust_coordinates(std::int32_t &x, std::int32_t &y); bool adjust_coordinates(SDL_Window *window, std::int32_t &x, std::int32_t &y); @@ -115,6 +116,7 @@ class Platform : public std::enable_shared_from_this, std::uint32_t focused_sdl_window_id_ = 0; Configuration config_; std::string ime_socket_file_; + bool monitor_exit = false; static const int MAX_FINGERS = 10; static const int MAX_TRACKING_ID = 10; -- Gitee