From c60c085d0977513f1b3b3eec98b0fa79b1c68ff9 Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Thu, 27 Jan 2022 10:07:47 +0100
Subject: [PATCH] Robustified controller rolling reloads

---
 src/controller/controller.cpp | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/controller/controller.cpp b/src/controller/controller.cpp
index aa93ef3d..3a6fcd74 100644
--- a/src/controller/controller.cpp
+++ b/src/controller/controller.cpp
@@ -614,13 +614,18 @@ void handleUSR1(int signum, siginfo_t *sigInfo, void *ignore){
   raise(SIGINT); // trigger restart
 }
 
+void handleUSR1Parent(int signum, siginfo_t *sigInfo, void *ignore){
+  Controller::Log("CONF", "USR1 received - passing on to child");
+  Util::Config::is_restarting = true;
+}
+
 ///\brief The controller angel process.
 /// Starts a forked main_loop in a loop. Yes, you read that right.
 int main(int argc, char **argv){
   Util::Procs::setHandler(); // set child handler
   {
     struct sigaction new_action;
-    new_action.sa_sigaction = handleUSR1;
+    new_action.sa_sigaction = handleUSR1Parent;
     sigemptyset(&new_action.sa_mask);
     new_action.sa_flags = 0;
     sigaction(SIGUSR1, &new_action, NULL);
@@ -663,11 +668,11 @@ int main(int argc, char **argv){
       continue;
     }
     // if the exit was clean, don't restart it
-    if (WIFEXITED(status) && (WEXITSTATUS(status) == 0)){
+    if (WIFEXITED(status) && (WEXITSTATUS(status) == 0 && !Util::Config::is_restarting)){
       MEDIUM_MSG("Controller shut down cleanly");
       break;
     }
-    if (WIFEXITED(status) && (WEXITSTATUS(status) == 42)){
+    if (WIFEXITED(status) && (WEXITSTATUS(status) == 42 || Util::Config::is_restarting)){
       WARN_MSG("Refreshing angel process for update");
       std::string myFile = Util::getMyPath() + "MistController";
       execvp(myFile.c_str(), argv);