move srvcfg-mgr.json file to /var
The srvcfg-mgr.json file is a file that tracks systemd services that the
srvcfg-mgr application monitors and controls. The file is created by the
srvcfg-mgr application on startup. The file is not meant to be modified
by users. Given that this file is not a config file modifiable by users,
move it out of /etc and into /var to better follow other OpenBMC
applications usage of the filesystem. In general, application data goes
under /var, config files which are editable by users/scripts go in /etc.
Future commits in this series will add additional configuration data
files so the goal with this commit is to first get the existing file in
the correct directory so that new files can go there as well.
Note that this is forward compatible in that when you update to code
with this commit in it, your file in /etc will be moved to /var. But if
you were to move backwards after that then the file in /var would not
be moved (the old firmware has no knowledge of it). In this case a new
file would be generated in /etc.
Tested:
- Started up fresh QEMU session, verified file was created in
/var/lib/service-config-manager/srvcfg-mgr.json and it looked correct
- After above test, stopped srvcfg-mgr.service, moved the file from /var
to /etc, removed the /var/lib/service-config-manager dir and then
started up srvcfg-mgr.service. Confirmed file was moved to correct
location under /var
Change-Id: Ida01ae960f0553bf0f4cf07fe8c3555a4a527e90
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/inc/utils.hpp b/inc/utils.hpp
index 1499588..d6dded6 100644
--- a/inc/utils.hpp
+++ b/inc/utils.hpp
@@ -45,6 +45,8 @@
static constexpr const char* subStateRunning = "running";
static constexpr const char* subStateListening = "listening";
static constexpr const char* loadStateNotFound = "not-found";
+static constexpr const char* srvDataBaseDir =
+ "/var/lib/service-config-manager/";
using ListUnitsType =
std::tuple<std::string, std::string, std::string, std::string, std::string,
diff --git a/src/main.cpp b/src/main.cpp
index ab706cb..d82f7b9 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,7 +31,8 @@
srvMgrObjects;
static bool unitQueryStarted = false;
-static constexpr const char* srvCfgMgrFile = "/etc/srvcfg-mgr.json";
+static constexpr const char* srvCfgMgrFileOld = "/etc/srvcfg-mgr.json";
+static constexpr const char* srvCfgMgrFile = "srvcfg-mgr.json";
static constexpr const char* tmpFileBad = "/tmp/srvcfg-mgr.json.bad";
// Base service name list. All instance of these services and
@@ -169,12 +170,32 @@
}
bool updateRequired = false;
- bool jsonExist = std::filesystem::exists(srvCfgMgrFile);
+
+ // Determine if we need to create our persistent config dir
+ if (!std::filesystem::exists(srvDataBaseDir))
+ {
+ std::filesystem::create_directories(srvDataBaseDir);
+ }
+
+ std::string srvCfgMgrFilePath = std::string(srvDataBaseDir) + srvCfgMgrFile;
+
+ // First check if our config manager file is in the old spot.
+ // If it is, then move it to the new spot
+ if ((std::filesystem::exists(srvCfgMgrFileOld)) &&
+ (!std::filesystem::exists(srvCfgMgrFilePath)))
+ {
+ lg2::info("Moving {OLDFILEPATH} to new location, {FILEPATH}",
+ "OLDFILEPATH", srvCfgMgrFileOld, "FILEPATH",
+ srvCfgMgrFilePath);
+ std::filesystem::rename(srvCfgMgrFileOld, srvCfgMgrFilePath);
+ }
+
+ bool jsonExist = std::filesystem::exists(srvCfgMgrFilePath);
if (jsonExist)
{
try
{
- std::ifstream file(srvCfgMgrFile);
+ std::ifstream file(srvCfgMgrFilePath);
cereal::JSONInputArchive archive(file);
MonitorListMap savedMonitorList;
archive(savedMonitorList);
@@ -199,7 +220,7 @@
{
lg2::error(
"Failed to load {FILEPATH} file, need to rewrite: {ERROR}.",
- "FILEPATH", srvCfgMgrFile, "ERROR", e);
+ "FILEPATH", srvCfgMgrFilePath, "ERROR", e);
// The "bad" files need to be moved to /tmp/ so that we can try to
// find out the cause of the file corruption. If we encounter this
@@ -207,12 +228,12 @@
// we don't accidentally fill up /tmp/.
std::error_code ec;
std::filesystem::copy_file(
- srvCfgMgrFile, tmpFileBad,
+ srvCfgMgrFilePath, tmpFileBad,
std::filesystem::copy_options::overwrite_existing, ec);
if (ec)
{
lg2::error("Failed to copy {SRCFILE} file to {DSTFILE}.",
- "SRCFILE", srvCfgMgrFile, "DSTFILE", tmpFileBad);
+ "SRCFILE", srvCfgMgrFilePath, "DSTFILE", tmpFileBad);
}
updateRequired = true;
@@ -220,7 +241,7 @@
}
if (!jsonExist || updateRequired)
{
- std::ofstream file(srvCfgMgrFile);
+ std::ofstream file(srvCfgMgrFilePath);
cereal::JSONOutputArchive archive(file);
archive(CEREAL_NVP(unitsToMonitor));
}