From 8ad2e8356f344989e6f962d5af46f896acba3209 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Sat, 3 Jun 2017 10:28:58 +0100 Subject: [PATCH] Fix: grant console correctly Services marked "starts-on-console" need to be given console access. Services that restart via "smooth recovery" need to be given console access if they run-on-console. --- src/service.cc | 39 +++++++++++++++++++++++++-------------- src/service.h | 9 ++++++--- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/service.cc b/src/service.cc index f8cc7e3..7b0461f 100644 --- a/src/service.cc +++ b/src/service.cc @@ -767,7 +767,7 @@ bool base_process_service::start_ps_process() noexcept return true; } else { - return start_ps_process(exec_arg_parts, onstart_flags.runs_on_console); + return start_ps_process(exec_arg_parts, onstart_flags.starts_on_console); } } @@ -1183,6 +1183,28 @@ base_process_service::base_process_service(ServiceSet *sset, string name, Servic restart_timer.add_timer(eventLoop); } +void base_process_service::do_restart() noexcept +{ + restarting = false; + + // We may be STARTING (regular restart) or STARTED ("smooth recovery"). This affects whether + // the process should be granted access to the console: + bool on_console = service_state == ServiceState::STARTING + ? onstart_flags.starts_on_console : onstart_flags.runs_on_console; + + if (! start_ps_process(exec_arg_parts, on_console)) { + + if (service_state == ServiceState::STARTING) { + failed_to_start(); + } + else { + desired_state = ServiceState::STOPPED; + forceStop(); + } + service_set->processQueues(); + } +} + void base_process_service::restart_ps_process() noexcept { timespec current_time; @@ -1199,10 +1221,7 @@ void base_process_service::restart_ps_process() noexcept if (tdiff_s > 0 || tdiff_ns > 200000000) { // > 200ms - restarting = false; - if (! start_ps_process(exec_arg_parts, onstart_flags.runs_on_console)) { - // TODO handle appropriately; mark service stopped. - } + do_restart(); } else { timespec timeout; @@ -1214,14 +1233,6 @@ void base_process_service::restart_ps_process() noexcept dasynq::rearm process_restart_timer::timer_expiry(EventLoop_t &, int expiry_count) { - return service->restart_timer_expired(); -} - -dasynq::rearm base_process_service::restart_timer_expired() noexcept -{ - // begin starting process: - if (! start_ps_process(exec_arg_parts, onstart_flags.runs_on_console)) { - // TODO handle appropriately; mark service stopped. - } + service->do_restart(); return dasynq::rearm::DISARM; } diff --git a/src/service.h b/src/service.h index 21411b6..8bb6389 100644 --- a/src/service.h +++ b/src/service.h @@ -575,23 +575,26 @@ class base_process_service : public ServiceRecord friend class ServiceIoWatcher; friend class process_restart_timer; + private: + // Re-launch process + void do_restart() noexcept; + protected: ServiceChildWatcher child_listener; ServiceIoWatcher child_status_listener; process_restart_timer restart_timer; timespec last_start_time; - // start the process, return true on success + // Start the process, return true on success virtual bool start_ps_process() noexcept; bool start_ps_process(const std::vector &args, bool on_console) noexcept; - // restart the process (due to start failure or unexpected termination). Restarts will be + // Restart the process (due to start failure or unexpected termination). Restarts will be // rate-limited. void restart_ps_process() noexcept; virtual void all_deps_stopped() noexcept; virtual void handle_exit_status(int exit_status) noexcept = 0; - dasynq::rearm restart_timer_expired() noexcept; public: base_process_service(ServiceSet *sset, string name, ServiceType service_type, string &&command, -- 2.25.1