Read state directory from systemd file
Reading the state dir from the systemd service file gives us
flexibility to define the bmcweb state from wherever we like, rather
than just using the current directory, which might not be writable.
Tested: bmcweb boots, shows state is persisted in the same location as
previously.
Change-Id: I9c048421fe249b73b1cae2ff5204ffd357cd3123
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/config/bmcweb.service.in b/config/bmcweb.service.in
index 7ea392f..a430317 100644
--- a/config/bmcweb.service.in
+++ b/config/bmcweb.service.in
@@ -8,6 +8,7 @@
ExecReload=kill -s HUP $MAINPID
ExecStart=@MESON_INSTALL_PREFIX@/bin/bmcweb daemon
Type=simple
+StateDirectory=/home/root
WorkingDirectory=/home/root
SyslogLevelPrefix=true
WatchdogSec=@BMCWEB_WATCHDOG_TIMEOUT_SECONDS@s
diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp
index d210800..bb24f77 100644
--- a/include/persistent_data.hpp
+++ b/include/persistent_data.hpp
@@ -32,8 +32,22 @@
uint64_t jsonRevision = 1;
public:
- // todo(ed) should read this from a fixed location somewhere, not CWD
- static constexpr const char* filename = "bmcweb_persistent_data.json";
+ static std::string getStateFile()
+ {
+ // NOLINTNEXTLINE(concurrency-mt-unsafe)
+ const char* stateDir = std::getenv("STATE_DIRECTORY");
+ if (stateDir == nullptr)
+ {
+ stateDir = ".";
+ }
+ return std::string(stateDir) + "/bmcweb_persistent_data.json";
+ }
+
+ static const std::string& filename()
+ {
+ const static std::string fname = getStateFile();
+ return fname;
+ }
ConfigFile()
{
@@ -60,7 +74,7 @@
// this application for the moment
void readData()
{
- std::ifstream persistentFile(filename);
+ std::ifstream persistentFile(filename());
uint64_t fileRevision = 0;
if (persistentFile.is_open())
{
@@ -248,7 +262,8 @@
void writeData()
{
- std::filesystem::path path(filename);
+ const std::string& fname = filename();
+ std::filesystem::path path(fname);
path = path.parent_path();
if (!path.empty())
{
@@ -263,7 +278,7 @@
}
boost::beast::file_posix persistentFile;
boost::system::error_code ec;
- persistentFile.open(filename, boost::beast::file_mode::write, ec);
+ persistentFile.open(fname.c_str(), boost::beast::file_mode::write, ec);
if (ec)
{
BMCWEB_LOG_CRITICAL("Unable to store persistent data to file {}",
@@ -276,7 +291,7 @@
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::group_read;
- std::filesystem::permissions(filename, permission, ec);
+ std::filesystem::permissions(fname, permission, ec);
if (ec)
{
BMCWEB_LOG_CRITICAL("Failed to set filesystem permissions {}",