From 2cd323b314e9f2b2cdf11bbf2974e9390914d5ea Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 19 Oct 2020 20:39:25 +1100 Subject: [PATCH] Allow quiting last client with unsaved buffer in daemon mode Because the server will out-live that client's disconnection it is still ok to have modified buffers, the server will complain on `kill`. Fixes #3801 --- src/commands.cc | 2 +- src/main.cc | 8 ++++---- src/remote.cc | 4 ++-- src/remote.hh | 5 ++++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 745a8201..e453fbf2 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -623,7 +623,7 @@ const CommandDesc force_kill_cmd = { template void quit(const ParametersParser& parser, Context& context, const ShellContext&) { - if (not force and ClientManager::instance().count() == 1) + if (not force and ClientManager::instance().count() == 1 and not Server::instance().is_daemon()) ensure_all_buffers_are_saved(); const int status = parser.positional_count() > 0 ? str_to_int(parser[0]) : 0; diff --git a/src/main.cc b/src/main.cc index af3d68ba..a65424ad 100644 --- a/src/main.cc +++ b/src/main.cc @@ -740,7 +740,8 @@ int run_server(StringView session, StringView server_init, } EventManager event_manager; - Server server{session.empty() ? to_string(getpid()) : session.str()}; + Server server{session.empty() ? to_string(getpid()) : session.str(), + (bool)(flags & ServerFlags::Daemon)}; StringRegistry string_registry; GlobalScope global_scope; @@ -832,7 +833,7 @@ int run_server(StringView session, StringView server_init, try { - if (not (flags & ServerFlags::Daemon)) + if (not server.is_daemon()) { local_client = client_manager.create_client( create_local_ui(ui_type), getpid(), {}, get_env_vars(), client_init, std::move(init_coord), @@ -849,8 +850,7 @@ int run_server(StringView session, StringView server_init, } while (not terminate and - (not client_manager.empty() or server.negotiating() or - (flags & ServerFlags::Daemon))) + (not client_manager.empty() or server.negotiating() or server.is_daemon())) { client_manager.redraw_clients(); diff --git a/src/remote.cc b/src/remote.cc index abb321e8..6ba9164e 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -849,8 +849,8 @@ private: MsgReader m_reader; }; -Server::Server(String session_name) - : m_session{std::move(session_name)} +Server::Server(String session_name, bool is_daemon) + : m_session{std::move(session_name)}, m_is_daemon{is_daemon} { if (not all_of(m_session, is_identifier)) throw runtime_error{format("invalid session name: '{}'", session_name)}; diff --git a/src/remote.hh b/src/remote.hh index 89c2f438..661053a8 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -50,7 +50,7 @@ String session_path(StringView session); struct Server : public Singleton { - Server(String session_name); + Server(String session_name, bool daemon); ~Server(); const String& session() const { return m_session; } @@ -59,11 +59,14 @@ struct Server : public Singleton bool negotiating() const { return not m_accepters.empty(); } + bool is_daemon() const { return m_is_daemon; } + private: class Accepter; void remove_accepter(Accepter* accepter); String m_session; + bool m_is_daemon; std::unique_ptr m_listener; Vector, MemoryDomain::Remote> m_accepters; };