From b7a0aa7546e6e6cbc18662d41ae352a83dff348f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 20 Dec 2016 10:34:48 +0000 Subject: [PATCH] Ensure we return 0 on exit from graceful disconnection Fixes #1042 --- src/main.cc | 8 ++++---- src/remote.cc | 18 +++++++++--------- src/remote.hh | 7 +++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main.cc b/src/main.cc index 254416eb..1af54935 100644 --- a/src/main.cc +++ b/src/main.cc @@ -464,10 +464,10 @@ int run_client(StringView session, StringView init_cmds, UIType ui_type) while (true) event_manager.handle_next_events(EventMode::Normal); } - catch (remote_error& e) + catch (disconnected& e) { write_stderr(format("{}\ndisconnecting\n", e.what())); - return -1; + return e.m_graceful ? 0 : -1; } return 0; } @@ -702,10 +702,10 @@ int run_pipe(StringView session) { send_command(session, command); } - catch (remote_error& e) + catch (disconnected& e) { write_stderr(format("{}\ndisconnecting\n", e.what())); - return -1; + return e.m_graceful ? 0 : -1; } return 0; } diff --git a/src/remote.cc b/src/remote.cc index 96f86a3f..946c14e4 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -146,7 +146,7 @@ public: if (m_write_pos == header_size) { if (size() < header_size) - throw remote_error{"invalid message received"}; + throw disconnected{"invalid message received"}; m_stream.resize(size()); } } @@ -174,7 +174,7 @@ public: void read(char* buffer, size_t size) { if (m_read_pos + size > m_stream.size()) - throw remote_error{"tried to read after message end"}; + throw disconnected{"tried to read after message end"}; memcpy(buffer, m_stream.data() + m_read_pos, size); m_read_pos += size; } @@ -232,8 +232,8 @@ private: kak_assert(m_write_pos + size <= m_stream.size()); int res = ::read(sock, m_stream.data() + m_write_pos, size); if (res <= 0) - throw remote_error{res ? "peer disconnected" - : format("socket read failed: {}", strerror(errno))}; + throw disconnected{res ? format("socket read failed: {}", strerror(errno)) + : "peer disconnected", res == 0}; m_write_pos += res; } @@ -346,8 +346,8 @@ static bool send_data(int fd, RemoteBuffer& buffer) { int res = ::write(fd, buffer.data(), buffer.size()); if (res <= 0) - throw remote_error{res ? "peer disconnected" - : format("socket write failed: {}", strerror(errno))}; + throw disconnected{res ? format("socket write failed: {}", strerror(errno)) + : "peer disconnected", res == 0}; buffer.erase(buffer.begin(), buffer.begin() + res); } return buffer.empty(); @@ -382,7 +382,7 @@ RemoteUI::RemoteUI(int socket, DisplayCoord dimensions) m_on_key(key); } } - catch (const remote_error& err) + catch (const disconnected& err) { write_to_debug_buffer(format("Error while transfering remote messages: {}", err.what())); ClientManager::instance().remove_client(*m_client, false); @@ -507,7 +507,7 @@ static int connect_to(StringView session) fcntl(sock, F_SETFD, FD_CLOEXEC); sockaddr_un addr = session_addr(session); if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1) - throw remote_error(format("connect to {} failed", addr.sun_path)); + throw disconnected(format("connect to {} failed", addr.sun_path)); return sock; } @@ -695,7 +695,7 @@ private: Server::instance().remove_accepter(this); } } - catch (const remote_error& err) + catch (const disconnected& err) { write_to_debug_buffer(format("accepting connection failed: {}", err.what())); close(sock); diff --git a/src/remote.hh b/src/remote.hh index 6aaac49d..111a4973 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -11,9 +11,12 @@ namespace Kakoune { -struct remote_error : runtime_error +struct disconnected : runtime_error { - using runtime_error::runtime_error; + disconnected(String what, bool graceful = false) + : runtime_error{std::move(what)}, m_graceful{graceful} {} + + const bool m_graceful; }; class FDWatcher;