monitor:SIGHUP: Handle reloading JSON config thru SIGHUP
Enable capturing the HUP signal to reload the JSON configuration. This
will reload the appropriate JSON configuration file found and update the
trust groups and fan definitions configured.
Tested:
JSON configuration is reloaded and updated after SIGHUP
Single instance of trust groups exist that match the JSON config
Single instance of fan definitions exist that match the JSON config
Change-Id: If55ca583a67fd76f0733009707bd5c4b5eda3e63
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/json_config.hpp b/json_config.hpp
index 55ca85a..d25c04a 100644
--- a/json_config.hpp
+++ b/json_config.hpp
@@ -127,6 +127,8 @@
if (fs::exists(confFile))
{
+ log<level::INFO>("Loading configuration",
+ entry("JSON_FILE=%s", confFile.c_str()));
file.open(confFile);
try
{
diff --git a/monitor/Makefile.am b/monitor/Makefile.am
index 6c3c435..f488859 100644
--- a/monitor/Makefile.am
+++ b/monitor/Makefile.am
@@ -17,14 +17,16 @@
$(SDEVENTPLUS_LIBS) \
$(PHOSPHOR_LOGGING_LIBS) \
${PHOSPHOR_DBUS_INTERFACES_LIBS} \
- -lstdc++fs
+ -lstdc++fs \
+ $(STDPLUS_LIBS)
phosphor_fan_monitor_CXXFLAGS = \
$(SDBUSPLUS_CFLAGS) \
$(SDEVENTPLUS_CFLAGS) \
$(PHOSPHOR_LOGGING_CFLAGS) \
${PHOSPHOR_DBUS_INTERFACES_CFLAGS} \
- -flto
+ -flto \
+ $(STDPLUS_CFLAGS)
if WANT_JSON
phosphor_fan_monitor_SOURCES += json_parser.cpp
diff --git a/monitor/main.cpp b/monitor/main.cpp
index 7517d6d..f0458f9 100644
--- a/monitor/main.cpp
+++ b/monitor/main.cpp
@@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include "config.h"
+
#include "argument.hpp"
#include "fan.hpp"
#include "system.hpp"
@@ -21,6 +23,8 @@
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
+#include <stdplus/signal.hpp>
using namespace phosphor::fan::monitor;
using namespace phosphor::logging;
@@ -58,6 +62,15 @@
System system(mode, bus, event);
+#ifdef MONITOR_USE_JSON
+ // Enable SIGHUP handling to reload JSON config
+ stdplus::signal::block(SIGHUP);
+ sdeventplus::source::Signal signal(event, SIGHUP,
+ std::bind(&System::sighupHandler,
+ &system, std::placeholders::_1,
+ std::placeholders::_2));
+#endif
+
if (mode == Mode::init)
{
// Fans were initialized to be functional, exit
diff --git a/monitor/system.cpp b/monitor/system.cpp
index 032291f..632b0b2 100644
--- a/monitor/system.cpp
+++ b/monitor/system.cpp
@@ -27,13 +27,16 @@
#endif
#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
namespace phosphor::fan::monitor
{
using json = nlohmann::json;
+using namespace phosphor::logging;
System::System(Mode mode, sdbusplus::bus::bus& bus,
const sdeventplus::Event& event) :
@@ -45,23 +48,34 @@
jsonObj = getJsonObj(bus);
#endif
// Retrieve and set trust groups within the trust manager
- _trust = std::make_unique<trust::Manager>(getTrustGroups(jsonObj));
-
+ setTrustMgr(getTrustGroups(jsonObj));
// Retrieve fan definitions and create fan objects to be monitored
- for (const auto& fanDef : getFanDefinitions(jsonObj))
+ setFans(getFanDefinitions(jsonObj));
+ log<level::INFO>("Configuration loaded");
+}
+
+void System::sighupHandler(sdeventplus::source::Signal&,
+ const struct signalfd_siginfo*)
+{
+ try
{
- // Check if a condition exists on the fan
- auto condition = std::get<conditionField>(fanDef);
- if (condition)
- {
- // Condition exists, skip adding fan if it fails
- if (!(*condition)(bus))
- {
- continue;
- }
- }
- _fans.emplace_back(
- std::make_unique<Fan>(mode, bus, event, _trust, fanDef));
+ json jsonObj = json::object();
+#ifdef MONITOR_USE_JSON
+ jsonObj = getJsonObj(_bus);
+#endif
+ auto trustGrps = getTrustGroups(jsonObj);
+ auto fanDefs = getFanDefinitions(jsonObj);
+ // Set configured trust groups
+ setTrustMgr(trustGrps);
+ // Clear/set configured fan definitions
+ _fans.clear();
+ setFans(fanDefs);
+ log<level::INFO>("Configuration reloaded successfully");
+ }
+ catch (std::runtime_error& re)
+ {
+ log<level::ERR>("Error reloading config, no config changes made",
+ entry("LOAD_ERROR=%s", re.what()));
}
}
@@ -75,6 +89,11 @@
#endif
}
+void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs)
+{
+ _trust = std::make_unique<trust::Manager>(groupFuncs);
+}
+
const std::vector<FanDefinition> System::getFanDefinitions(const json& jsonObj)
{
#ifdef MONITOR_USE_JSON
@@ -84,4 +103,23 @@
#endif
}
+void System::setFans(const std::vector<FanDefinition>& fanDefs)
+{
+ for (const auto& fanDef : fanDefs)
+ {
+ // Check if a condition exists on the fan
+ auto condition = std::get<conditionField>(fanDef);
+ if (condition)
+ {
+ // Condition exists, skip adding fan if it fails
+ if (!(*condition)(_bus))
+ {
+ continue;
+ }
+ }
+ _fans.emplace_back(
+ std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef));
+ }
+}
+
} // namespace phosphor::fan::monitor
diff --git a/monitor/system.hpp b/monitor/system.hpp
index c72f0d5..9757082 100644
--- a/monitor/system.hpp
+++ b/monitor/system.hpp
@@ -23,6 +23,7 @@
#include <nlohmann/json.hpp>
#include <sdbusplus/bus.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
#include <memory>
#include <optional>
@@ -54,6 +55,13 @@
System(Mode mode, sdbusplus::bus::bus& bus,
const sdeventplus::Event& event);
+ /**
+ * @brief Callback function to handle receiving a HUP signal to reload the
+ * JSON configuration.
+ */
+ void sighupHandler(sdeventplus::source::Signal&,
+ const struct signalfd_siginfo*);
+
private:
/* The mode of fan monitor */
Mode _mode;
@@ -80,6 +88,13 @@
const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj);
/**
+ * @brief Set the trust manager's list of trust group functions
+ *
+ * @param[in] groupFuncs - list of trust group functions
+ */
+ void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs);
+
+ /**
* @brief Retrieve the configured fan definitions
*
* @param[in] jsonObj - JSON object to parse from
@@ -87,6 +102,13 @@
* @return List of fan definition data on the fans configured
*/
const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj);
+
+ /**
+ * @brief Set the list of fans to be monitored
+ *
+ * @param[in] fanDefs - list of fan definitions to create fans monitored
+ */
+ void setFans(const std::vector<FanDefinition>& fanDefs);
};
} // namespace phosphor::fan::monitor