From 2ab66a5b1c2133633af710d924f540152c66fcfa Mon Sep 17 00:00:00 2001
From: Alexander Dahl <post@lespocky.de>
Date: Mon, 9 Oct 2023 15:20:36 +0200
Subject: [PATCH] Add more DEBUG output

---
 src/monit.c    |  4 ++++
 src/state.c    | 22 +++++++++++++++++++---
 src/validate.c | 18 +++++++++++++++---
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/src/monit.c b/src/monit.c
index 3e9eeb07..bdb80ac1 100644
--- a/src/monit.c
+++ b/src/monit.c
@@ -327,6 +327,8 @@ static void do_init() {
                 Util_printRunList();
                 Util_printServiceList();
         }
+        DEBUG("%s:%u: systeminfo.booted set to: %llu\n", __func__, __LINE__,
+              systeminfo.booted);
 
         /*
          * Reap any stray child processes we may have created
@@ -595,6 +597,8 @@ static void do_default() {
                 atexit(file_finalize);
 
 reload:
+                DEBUG("%s:%u: startdelay: %d\n", __func__, __LINE__,
+                      Run.startdelay);
                 if (Run.startdelay) {
                         if (State_reboot()) {
                                 time_t now = Time_monotonic();
diff --git a/src/state.c b/src/state.c
index 21d666b2..c76c1c47 100644
--- a/src/state.c
+++ b/src/state.c
@@ -245,14 +245,20 @@ static void _updateStart(Service_T S, int nstart, int ncycle) {
 static void _updateMonitor(Service_T S, Monitor_State monitor) {
         if (! State_reboot() || S->onreboot == Onreboot_Laststate) {
                 // Monit reload or restart within the same boot session OR persistent state => restore the monitoring state
-                if (monitor == Monitor_Not)
+                if (monitor == Monitor_Not) {
+                        DEBUG("%s:%u: '%s' setting monitor state to NOT\n",
+                              __func__, __LINE__, S->name);
                         S->monitor = Monitor_Not;
+                }
                 else if (S->monitor == Monitor_Not)
                         S->monitor = Monitor_Init;
         } else {
                 // System rebooted
-                if (S->onreboot == Onreboot_Nostart)
+                if (S->onreboot == Onreboot_Nostart) {
+                        DEBUG("%s:%u: '%s' setting monitor state to NOT\n",
+                              __func__, __LINE__, S->name);
                         S->monitor = Monitor_Not;
+                }
                 else
                         S->monitor = Monitor_Init;
         }
@@ -326,6 +332,8 @@ static void _restoreV4(void) {
         if (read(file, &booted, sizeof(booted)) != sizeof(booted)) {
                 THROW(IOException, "Unable to read system boot time");
         }
+        DEBUG("%s:%u: read 'booted' %llu from state file\n",
+              __func__, __LINE__, booted);
         // Services state
         State4_T state;
         while (read(file, &state, sizeof(state)) == sizeof(state)) {
@@ -373,6 +381,8 @@ static void _restoreV3(void) {
         if (read(file, &booted, sizeof(booted)) != sizeof(booted)) {
                 THROW(IOException, "Unable to read system boot time");
         }
+        DEBUG("%s:%u: read 'booted' %llu from state file\n",
+              __func__, __LINE__, booted);
         // Services state
         State3_T state;
         while (read(file, &state, sizeof(state)) == sizeof(state)) {
@@ -651,6 +661,7 @@ void State_restore() {
                                         break;
                         }
                         if (version != StateVersionLatest || State_reboot()) {
+                                DEBUG("%s:%u: set _stateDirty\n", __func__, __LINE__);
                                 _stateDirty  = true;
                         }
                 }
@@ -664,8 +675,11 @@ void State_restore() {
         // If the service state was not restored (e.g. new service or state file is missing), handle the onreboot flag
         for (Service_T s = servicelist; s; s = s->next) {
                 if (! s->onrebootRestored) {
-                        if (s->onreboot == Onreboot_Nostart)
+                        if (s->onreboot == Onreboot_Nostart) {
+                                DEBUG("%s:%u: '%s' setting monitor state to NOT\n",
+                                      __func__, __LINE__, s->name);
                                 s->monitor = Monitor_Not;
+                        }
                         s->onrebootRestored = true;
                 }
         }
@@ -673,6 +687,8 @@ void State_restore() {
 
 
 bool State_reboot() {
+        DEBUG("%s:%u: State_reboot() returning %s\n", __func__, __LINE__,
+              systeminfo.booted == booted ? "false" : "true");
         return systeminfo.booted == booted ? false : true;
 }
 
diff --git a/src/validate.c b/src/validate.c
index 46c86ee1..93a4786d 100644
--- a/src/validate.c
+++ b/src/validate.c
@@ -1547,6 +1547,8 @@ State_Type check_process(Service_T s) {
                 for (Exist_T l = s->existlist; l; l = l->next) {
                         Event_post(s, Event_Exist, State_Succeeded, l->action, "process is not running");
                 }
+                DEBUG("'%s' process not found, returning State_Type %d\n",
+                        s->name, rv);
                 return rv;
         }
         if (Run.flags & Run_ProcessEngineEnabled) {
@@ -1564,8 +1566,11 @@ State_Type check_process(Service_T s) {
                 Event_post(s, Event_Exist, State_Failed, l->action, "process is running with pid %d", (int)pid);
         }
         // Double-check the monitoring state: the "if does exist" may call unmonitor/stop, which resets the service object
-        if (s->monitor == Monitor_Not)
+        if (s->monitor == Monitor_Not) {
+                DEBUG("'%s' monitor state is 'Monitor_Not', returning State_Type %d\n",
+                        s->name, rv);
                 return rv;
+        }
         /* Reset the exec and timeout errors if active ... the process is running (most probably after manual intervention) */
         if (IS_EVENT_SET(s->error, Event_Exec))
                 Event_post(s, Event_Exec, State_Succeeded, s->action_EXEC, "process is running after previous exec error (slow starting or manually recovered?)");
@@ -1600,8 +1605,11 @@ State_Type check_process(Service_T s) {
                 //FIXME: instead of pause, try to test, but ignore any errors in the start timeout timeframe ... will allow to display the port response time as soon as available, instead of waiting for 30+ seconds
                 /* pause port tests in the start timeout timeframe while the process is starting (it may take some time to the process before it starts accepting connections) */
                 if (! s->start || uptimeMilli > s->start->timeout) {
-                        if (_checkConnection(s, pp) == State_Failed)
+                        if (_checkConnection(s, pp) == State_Failed) {
+                                DEBUG("'%s' _checkConnection(portlist) failed, returning State_Type %d\n",
+                                        s->name, rv);
                                 rv = State_Failed;
+                        }
                 } else {
                         pp->is_available = Connection_Init;
                         DEBUG("'%s' connection test paused for %s while the process is starting\n", s->name, Convert_time2str(s->start->timeout - (uptimeMilli < 0 ? 0 : uptimeMilli), (char[11]){}));
@@ -1611,13 +1619,17 @@ State_Type check_process(Service_T s) {
                 //FIXME: instead of pause, try to test, but ignore any errors in the start timeout timeframe ... will allow to display the port response time as soon as available, instead of waiting for 30+ seconds
                 /* pause socket tests in the start timeout timeframe while the process is starting (it may take some time to the process before it starts accepting connections) */
                 if (! s->start || uptimeMilli > s->start->timeout) {
-                        if (_checkConnection(s, pp) == State_Failed)
+                        if (_checkConnection(s, pp) == State_Failed) {
+                                DEBUG("'%s' _checkConnection(socketlist) failed, returning State_Type %d\n",
+                                        s->name, rv);
                                 rv = State_Failed;
+                        }
                 } else {
                         pp->is_available = Connection_Init;
                         DEBUG("'%s' connection test paused for %s while the process is starting\n", s->name, Convert_time2str(s->start->timeout - (uptimeMilli < 0 ? 0 : uptimeMilli), (char[11]){}));
                 }
         }
+        DEBUG("'%s' finally returning State_Type %d\n", s->name, rv);
         return rv;
 }
 
-- 
2.30.2

