summaryrefslogtreecommitdiffstats
path: root/user_channel
diff options
context:
space:
mode:
authorRichard Marian Thomaiyar <richard.marian.thomaiyar@linux.intel.com>2019-05-09 00:16:53 +0530
committerRichard Marian Thomaiyar <richard.marian.thomaiyar@linux.intel.com>2019-05-14 05:24:20 +0000
commit687df40108b0103f7539fafebcce63c1f8405335 (patch)
tree48bfd67eb9367cc75f69e3c01bcf029a10db7580 /user_channel
parent4e6d25719120b40a55db66f1ea13ddf285d5338c (diff)
downloadphosphor-host-ipmid-687df40108b0103f7539fafebcce63c1f8405335.tar.gz
phosphor-host-ipmid-687df40108b0103f7539fafebcce63c1f8405335.zip
user-mgmt: sync ipmi user & channel conf file
ipmi_user.json file is stored in non-volatile memory, and it is necessary to make sure that file is properly synced to the storage device, to avoid any corruption issue related to power loss. This fix makes sure that temporary file is fully synced with storage device and then renamed, such that the file is either in old state or in new updated state. Same is also performed for channel configuration file too. Tested: 1. Verified regular ipmi user list & channel works without any issue 2. Verifid that any power loss, immediately, once the file is written doesn't corrupt the entries. Change-Id: I9ef84573947ab6f85f66530ac4a20e9eeaddf283 Signed-off-by: Richard Marian Thomaiyar <richard.marian.thomaiyar@linux.intel.com>
Diffstat (limited to 'user_channel')
-rw-r--r--user_channel/channel_mgmt.cpp28
-rw-r--r--user_channel/user_mgmt.cpp31
2 files changed, 41 insertions, 18 deletions
diff --git a/user_channel/channel_mgmt.cpp b/user_channel/channel_mgmt.cpp
index 6be7d9a..932795c 100644
--- a/user_channel/channel_mgmt.cpp
+++ b/user_channel/channel_mgmt.cpp
@@ -846,17 +846,33 @@ Json ChannelConfig::readJsonFile(const std::string& configFile)
int ChannelConfig::writeJsonFile(const std::string& configFile,
const Json& jsonData)
{
- std::ofstream jsonFile(configFile);
- if (!jsonFile.good())
+ const std::string tmpFile = configFile + "_tmp";
+ int fd = open(tmpFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (fd < 0)
{
- log<level::ERR>("JSON file not found");
+ log<level::ERR>("Error in creating json file",
+ entry("FILE_NAME = %s", tmpFile.c_str()));
+ return -EIO;
+ }
+ const auto& writeData = jsonData.dump();
+ if (write(fd, writeData.c_str(), writeData.size()) !=
+ static_cast<ssize_t>(writeData.size()))
+ {
+ close(fd);
+ log<level::ERR>("Error in writing configuration file",
+ entry("FILE_NAME = %s", tmpFile.c_str()));
return -EIO;
}
+ close(fd);
- // Write JSON to file
- jsonFile << jsonData;
+ if (std::rename(tmpFile.c_str(), configFile.c_str()) != 0)
+ {
+ log<level::ERR>("Error in renaming temporary data file",
+ entry("FILE_NAME = %s", tmpFile.c_str()));
+ return -EIO;
+ }
- jsonFile.flush();
return 0;
}
diff --git a/user_channel/user_mgmt.cpp b/user_channel/user_mgmt.cpp
index 5ca2884..89ba142 100644
--- a/user_channel/user_mgmt.cpp
+++ b/user_channel/user_mgmt.cpp
@@ -1087,15 +1087,6 @@ void UserAccess::writeUserData()
boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
userLock{*userMutex};
- static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"};
- std::ofstream oUsrData(tmpFile, std::ios::out | std::ios::binary);
- if (!oUsrData.good())
- {
- log<level::ERR>("Error in creating temporary IPMI user data file");
- throw std::ios_base::failure(
- "Error in creating temporary IPMI user data file");
- }
-
Json jsonUsersTbl = Json::array();
// user index 0 is reserved, starts with 1
for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
@@ -1130,9 +1121,25 @@ void UserAccess::writeUserData()
jsonUsersTbl.push_back(jsonUserInfo);
}
- oUsrData << jsonUsersTbl;
- oUsrData.flush();
- oUsrData.close();
+ static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"};
+ int fd = open(tmpFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (fd < 0)
+ {
+ log<level::ERR>("Error in creating temporary IPMI user data file");
+ throw std::ios_base::failure(
+ "Error in creating temporary IPMI user data file");
+ }
+ const auto& writeStr = jsonUsersTbl.dump();
+ if (write(fd, writeStr.c_str(), writeStr.size()) !=
+ static_cast<ssize_t>(writeStr.size()))
+ {
+ close(fd);
+ log<level::ERR>("Error in writing temporary IPMI user data file");
+ throw std::ios_base::failure(
+ "Error in writing temporary IPMI user data file");
+ }
+ close(fd);
if (std::rename(tmpFile.c_str(), ipmiUserDataFile) != 0)
{
OpenPOWER on IntegriCloud