ensure persistent settings only used on startup

queryAndUpdateProperties() is called in two code paths
- on startup when it is discovering the state of all the services from
  systemd and updating its internal data structures
- when it is issued commands over dbus to change service states

In the first case, the application needs to compare what is reported by
systemd and what it has in its persistent settings and update systemd to
match the persistent settings if there is a mismatch.

In the second case, the application simply needs to ensure the
persistent settings are updated to match what was requested over dbus.

Tested:
- Verified the curl command via Redfish to enable and disable IPMI works
  as expected and the persistent settings matched the dbus values
- Verified that if the persistent settings were changed in the
  filesystem and the service was restarted that the values in the
  persistent settings were used

Change-Id: I0ef7c19c95d32151f8db08a57c9e03f28aa663b1
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/inc/srvcfg_manager.hpp b/inc/srvcfg_manager.hpp
index 4fc9ccd..6920354 100644
--- a/inc/srvcfg_manager.hpp
+++ b/inc/srvcfg_manager.hpp
@@ -111,7 +111,7 @@
 
     bool isMaskedOut();
     void registerProperties();
-    void queryAndUpdateProperties();
+    void queryAndUpdateProperties(bool isStartup);
     void createSocketOverrideConf();
     void updateServiceProperties(
         const boost::container::flat_map<std::string, VariantType>&
diff --git a/src/srvcfg_manager.cpp b/src/srvcfg_manager.cpp
index 62a1837..24aac07 100644
--- a/src/srvcfg_manager.cpp
+++ b/src/srvcfg_manager.cpp
@@ -229,7 +229,7 @@
 #endif
 }
 
-void ServiceConfig::queryAndUpdateProperties()
+void ServiceConfig::queryAndUpdateProperties(bool isStartup = false)
 {
     std::string objectPath =
         isSocketActivatedService ? socketObjectPath : serviceObjectPath;
@@ -239,9 +239,10 @@
     }
 
     conn->async_method_call(
-        [this](boost::system::error_code ec,
-               const boost::container::flat_map<std::string, VariantType>&
-                   propertyMap) {
+        [this,
+         isStartup](boost::system::error_code ec,
+                    const boost::container::flat_map<std::string, VariantType>&
+                        propertyMap) {
             if (ec)
             {
                 lg2::error(
@@ -288,7 +289,19 @@
                 {
                     registerProperties();
                 }
-                loadStateFile();
+                if (isStartup)
+                {
+                    // On startup, load our persistent settings and compare to
+                    // what was read from systemd. If they are different, use
+                    // the persistent settings
+                    loadStateFile();
+                }
+                else
+                {
+                    // This is just an update once we're already running so
+                    // write the values out to our persistent settings
+                    writeStateFile();
+                }
             }
             catch (const std::exception& e)
             {
@@ -327,6 +340,8 @@
 void ServiceConfig::writeStateFile()
 {
 #ifdef PERSIST_SETTINGS
+    lg2::debug("Writing Persistent State File Information to {STATE_FILE}",
+               "STATE_FILE", stateFile);
     nlohmann::json stateMap;
     stateMap[persistDataFileVersionStr] = persistDataFileVersion;
     stateMap[srvCfgPropMasked] = unitMaskedState;
@@ -342,6 +357,8 @@
 void ServiceConfig::loadStateFile()
 {
 #ifdef PERSIST_SETTINGS
+    lg2::debug("Loading Persistent State File Information from {STATE_FILE}",
+               "STATE_FILE", stateFile);
     if (std::filesystem::exists(stateFile))
     {
         std::ifstream file(stateFile);
@@ -433,7 +450,7 @@
     instantiatedUnitName = baseUnitName + addInstanceName(instanceName, "@");
     updatedFlag = 0;
     stateFile = srvDataBaseDir + instantiatedUnitName;
-    queryAndUpdateProperties();
+    queryAndUpdateProperties(true);
     return;
 }
 
@@ -584,7 +601,8 @@
     // Reset the flag
     updatedFlag = 0;
 
-    lg2::info("Applied new settings: {OBJPATH}", "OBJPATH", objPath);
+    lg2::info("Applied new settings: {OBJPATH} {UNIT_RUNNING_STATE}", "OBJPATH",
+              objPath, "UNIT_RUNNING_STATE", unitRunningState);
 
     queryAndUpdateProperties();
     return;
@@ -605,8 +623,6 @@
             return;
         }
         updateInProgress = true;
-        // Ensure our persistent files are updated with changes
-        writeStateFile();
         boost::asio::spawn(
             conn->get_io_context(),
             [this](boost::asio::yield_context yield) {