Fix: grant console correctly
authorDavin McCall <davmac@davmac.org>
Sat, 3 Jun 2017 09:28:58 +0000 (10:28 +0100)
committerDavin McCall <davmac@davmac.org>
Sat, 3 Jun 2017 09:28:58 +0000 (10:28 +0100)
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
src/service.h

index f8cc7e3bc489c4dfdeb01f579345ce27965f310c..7b0461fc20bcad2b7fdb786677b4d06642c6e069 100644 (file)
@@ -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;
 }
index 21411b6bbf05f0da7f70519c1e80d000beb679ab..8bb63898bd3e8fef83e5d7e4adeb38c2074f3684 100644 (file)
@@ -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<const char *> &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,