summaryrefslogtreecommitdiffstats
path: root/user_channel/channel_mgmt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'user_channel/channel_mgmt.cpp')
-rw-r--r--user_channel/channel_mgmt.cpp142
1 files changed, 64 insertions, 78 deletions
diff --git a/user_channel/channel_mgmt.cpp b/user_channel/channel_mgmt.cpp
index 3fb19b2..759de43 100644
--- a/user_channel/channel_mgmt.cpp
+++ b/user_channel/channel_mgmt.cpp
@@ -34,7 +34,6 @@
namespace ipmi
{
-namespace variant_ns = sdbusplus::message::variant_ns;
using namespace phosphor::logging;
static constexpr const char* channelAccessDefaultFilename =
@@ -143,7 +142,7 @@ std::string ChannelConfig::getChannelName(const uint8_t chNum)
if (!isValidChannel(chNum))
{
log<level::ERR>("Invalid channel number.",
- entry("ChannelID:%d", chNum));
+ entry("CHANNEL_ID=%d", chNum));
throw std::invalid_argument("Invalid channel number");
}
@@ -161,7 +160,7 @@ int ChannelConfig::convertToChannelNumberFromChannelName(
}
}
log<level::ERR>("Invalid channel name.",
- entry("Channel:%s", chName.c_str()));
+ entry("CHANNEL=%s", chName.c_str()));
throw std::invalid_argument("Invalid channel name");
return -1;
@@ -169,15 +168,15 @@ int ChannelConfig::convertToChannelNumberFromChannelName(
std::string ChannelConfig::getChannelNameFromPath(const std::string& path)
{
- std::size_t pos = path.find(networkIntfObjectBasePath);
- if (pos == std::string::npos)
+
+ constexpr size_t length = strlen(networkIntfObjectBasePath);
+ if (((length + 1) >= path.size()) ||
+ path.compare(0, length, networkIntfObjectBasePath))
{
- log<level::ERR>("Invalid interface path.",
- entry("PATH:%s", path.c_str()));
- throw std::invalid_argument("Invalid interface path");
+ log<level::ERR>("Invalid object path.", entry("PATH=%s", path.c_str()));
+ throw std::invalid_argument("Invalid object path");
}
- std::string chName =
- path.substr(pos + strlen(networkIntfObjectBasePath) + 1);
+ std::string chName(path, length + 1);
return chName;
}
@@ -192,7 +191,7 @@ void ChannelConfig::processChAccessPropChange(
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Exception: ", entry("MSG: %s", e.what()));
+ log<level::ERR>("Exception: ", entry("MSG=%s", e.what()));
return;
}
@@ -204,7 +203,7 @@ void ChannelConfig::processChAccessPropChange(
if (prop.first == privilegePropertyString)
{
propName = privilegePropertyString;
- intfPrivStr = variant_ns::get<std::string>(prop.second);
+ intfPrivStr = std::get<std::string>(prop.second);
break;
}
}
@@ -218,7 +217,7 @@ void ChannelConfig::processChAccessPropChange(
if (intfPrivStr.empty())
{
log<level::ERR>("Invalid privilege string.",
- entry("INTF:%s", chName.c_str()));
+ entry("INTF=%s", chName.c_str()));
return;
}
@@ -231,7 +230,7 @@ void ChannelConfig::processChAccessPropChange(
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Exception: ", entry("MSG: %s", e.what()));
+ log<level::ERR>("Exception: ", entry("MSG=%s", e.what()));
return;
}
@@ -614,7 +613,7 @@ ipmi_ret_t ChannelConfig::setChannelAccessPersistData(
{
log<level::DEBUG>(
"Network interface does not exist",
- entry("INTERFACE:%s", channelData[chNum].chName.c_str()));
+ entry("INTERFACE=%s", channelData[chNum].chName.c_str()));
return IPMI_CC_UNSPECIFIED_ERROR;
}
}
@@ -787,45 +786,13 @@ EChannelProtocolType
return static_cast<EChannelProtocolType>(it->second);
}
-uint8_t ChannelConfig::convertToChannelIndexNumber(const uint8_t chNum)
-{
-
- // TODO: There is limitation in current design. we cannot detect exact
- // LAN interface(eth0 or eth1) so Implementation may be updated
- // when there is any design update to figure out all the interfaces
- // independently based on the message.
-
- static uint8_t curChannel = 0xFF;
-
- if (curChannel == 0xFF)
- {
- auto it = interfaceMap.find(getInterfaceIndex());
- if (it == interfaceMap.end())
- {
- log<level::ERR>("Invalid Interface type ",
- entry("InterfaceIndex: %d", getInterfaceIndex()));
- throw std::invalid_argument("Invalid interface type.");
- }
-
- for (auto& channel : channelData)
- {
- std::string& interfaceName = it->second;
- if (channel.chName == interfaceName)
- {
- curChannel = channel.chID;
- break;
- }
- }
- }
- return ((chNum == currentChNum) ? curChannel : chNum);
-}
-
Json ChannelConfig::readJsonFile(const std::string& configFile)
{
std::ifstream jsonFile(configFile);
if (!jsonFile.good())
{
- log<level::ERR>("JSON file not found");
+ log<level::INFO>("JSON file not found",
+ entry("FILE_NAME=%s", configFile.c_str()));
return nullptr;
}
@@ -837,7 +804,7 @@ Json ChannelConfig::readJsonFile(const std::string& configFile)
catch (Json::parse_error& e)
{
log<level::DEBUG>("Corrupted channel config.",
- entry("MSG: %s", e.what()));
+ entry("MSG=%s", e.what()));
throw std::runtime_error("Corrupted channel config file");
}
@@ -847,17 +814,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>("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()))
{
- log<level::ERR>("JSON file not found");
+ 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;
}
@@ -900,7 +883,7 @@ int ChannelConfig::loadChannelConfig()
{
log<level::WARNING>(
"Channel not configured so loading default.",
- entry("CHANNEL_NUM:%d", chNum));
+ entry("CHANNEL_NUM=%d", chNum));
// If user didn't want to configure specific channel (say
// reserved channel), then load that index with default values.
setDefaultChannelConfig(chNum, defaultChannelName);
@@ -938,12 +921,12 @@ int ChannelConfig::loadChannelConfig()
catch (const Json::exception& e)
{
log<level::DEBUG>("Json Exception caught.",
- entry("MSG:%s", e.what()));
+ entry("MSG=%s", e.what()));
return -EBADMSG;
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Corrupted config.", entry("MSG:%s", e.what()));
+ log<level::ERR>("Corrupted config.", entry("MSG=%s", e.what()));
return -EBADMSG;
}
}
@@ -997,7 +980,7 @@ int ChannelConfig::readChannelVolatileData()
{
log<level::ERR>(
"Invalid/corrupted volatile channel access file",
- entry("FILE: %s", channelVolatileDataFilename));
+ entry("FILE=%s", channelVolatileDataFilename));
throw std::runtime_error(
"Corrupted volatile channel access file");
}
@@ -1005,12 +988,12 @@ int ChannelConfig::readChannelVolatileData()
}
catch (const Json::exception& e)
{
- log<level::DEBUG>("Json Exception caught.", entry("MSG:%s", e.what()));
+ log<level::DEBUG>("Json Exception caught.", entry("MSG=%s", e.what()));
throw std::runtime_error("Corrupted volatile channel access file");
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Corrupted config.", entry("MSG:%s", e.what()));
+ log<level::ERR>("Corrupted config.", entry("MSG=%s", e.what()));
throw std::runtime_error("Corrupted volatile channel access file");
}
@@ -1065,19 +1048,19 @@ int ChannelConfig::readChannelPersistData()
else
{
log<level::ERR>("Invalid/corrupted nv channel access file",
- entry("FILE:%s", channelNvDataFilename));
+ entry("FILE=%s", channelNvDataFilename));
throw std::runtime_error("Corrupted nv channel access file");
}
}
}
catch (const Json::exception& e)
{
- log<level::DEBUG>("Json Exception caught.", entry("MSG:%s", e.what()));
+ log<level::DEBUG>("Json Exception caught.", entry("MSG=%s", e.what()));
throw std::runtime_error("Corrupted nv channel access file");
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
+ log<level::ERR>("Corrupted config.", entry("MSG=%s", e.what()));
throw std::runtime_error("Corrupted nv channel access file");
}
@@ -1120,7 +1103,7 @@ int ChannelConfig::writeChannelVolatileData()
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
+ log<level::ERR>("Corrupted config.", entry("MSG=%s", e.what()));
return -EINVAL;
}
@@ -1171,7 +1154,7 @@ int ChannelConfig::writeChannelPersistData()
}
catch (const std::invalid_argument& e)
{
- log<level::ERR>("Corrupted config.", entry("MSG: %s", e.what()));
+ log<level::ERR>("Corrupted config.", entry("MSG=%s", e.what()));
return -EINVAL;
}
@@ -1245,10 +1228,10 @@ int ChannelConfig::setDbusProperty(const std::string& service,
catch (const sdbusplus::exception::SdBusError& e)
{
log<level::DEBUG>("set-property failed",
- entry("SERVICE:%s", service.c_str()),
- entry("OBJPATH:%s", objPath.c_str()),
- entry("INTERFACE:%s", interface.c_str()),
- entry("PROP:%s", property.c_str()));
+ entry("SERVICE=%s", service.c_str()),
+ entry("OBJPATH=%s", objPath.c_str()),
+ entry("INTERFACE=%s", interface.c_str()),
+ entry("PROP=%s", property.c_str()));
return -EIO;
}
@@ -1275,10 +1258,10 @@ int ChannelConfig::getDbusProperty(const std::string& service,
catch (const sdbusplus::exception::SdBusError& e)
{
log<level::DEBUG>("get-property failed",
- entry("SERVICE:%s", service.c_str()),
- entry("OBJPATH:%s", objPath.c_str()),
- entry("INTERFACE:%s", interface.c_str()),
- entry("PROP:%s", property.c_str()));
+ entry("SERVICE=%s", service.c_str()),
+ entry("OBJPATH=%s", objPath.c_str()),
+ entry("INTERFACE=%s", interface.c_str()),
+ entry("PROP=%s", property.c_str()));
return -EIO;
}
return 0;
@@ -1305,13 +1288,13 @@ int ChannelConfig::syncNetworkChannelConfig()
privilegePropertyString, variant))
{
log<level::DEBUG>("Network interface does not exist",
- entry("INTERFACE:%s",
+ entry("INTERFACE=%s",
channelData[chNum].chName.c_str()));
continue;
}
- intfPrivStr = variant_ns::get<std::string>(variant);
+ intfPrivStr = std::get<std::string>(variant);
}
- catch (const variant_ns::bad_variant_access& e)
+ catch (const std::bad_variant_access& e)
{
log<level::DEBUG>(
"exception: Network interface does not exist");
@@ -1358,6 +1341,9 @@ int ChannelConfig::syncNetworkChannelConfig()
void ChannelConfig::initChannelPersistData()
{
+ boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
+ channelLock{*channelMutex};
+
/* Always read the channel config */
if (loadChannelConfig() != 0)
{
OpenPOWER on IntegriCloud