Control: fanctl should validate dumpfile output
fanctl is emitting "done" messages prematurely before all file contents
have been written. This change erases any existing json outputs and
proactively validates the output before reporting success.
Signed-off-by: Mike Capps <mikepcapps@gmail.com>
Change-Id: I364024675a468bc0dfb1035e3158e85c206b79d2
diff --git a/control/fanctl.cpp b/control/fanctl.cpp
index f069d29..80cf58b 100644
--- a/control/fanctl.cpp
+++ b/control/fanctl.cpp
@@ -532,10 +532,41 @@
*/
void dumpFanControl()
{
+ namespace fs = std::filesystem;
+
try
{
+ // delete existing file
+ if (fs::exists(dumpFile))
+ {
+ std::filesystem::remove(dumpFile);
+ }
+
SDBusPlus::callMethod(systemdService, systemdPath, systemdMgrIface,
"KillUnit", phosphorServiceName, "main", SIGUSR1);
+
+ bool done = false;
+
+ do
+ {
+ // wait for file to be detected
+ sleep(1);
+
+ if (fs::exists(dumpFile))
+ {
+ try
+ {
+ auto unused{nlohmann::json::parse(std::ifstream{dumpFile})};
+ done = true;
+ }
+ catch (...)
+ {
+ // TODO: maybe have a max-retries counter and fail after N
+ // tries
+ }
+ }
+ } while (!done);
+
std::cout << "Fan control dump written to: " << dumpFile << std::endl;
}
catch (const phosphor::fan::util::DBusPropertyError& e)
@@ -749,11 +780,17 @@
{
status();
}
-#ifdef CONTROL_USE_JSON
else if (app.got_subcommand("dump"))
{
+#ifdef CONTROL_USE_JSON
dumpFanControl();
+#else
+ std::ofstream(dumpFile)
+ << "{\n\"msg\": \"Unable to create dump on "
+ "non-JSON config based system\"\n}";
+#endif
}
+#ifdef CONTROL_USE_JSON
else if (app.got_subcommand("query_dump"))
{
queryDumpFile(dq);