AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 1 | /*
|
| 2 | // Copyright (c) 2018 Intel Corporation
|
| 3 | //
|
| 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 | // you may not use this file except in compliance with the License.
|
| 6 | // You may obtain a copy of the License at
|
| 7 | //
|
| 8 | // http://www.apache.org/licenses/LICENSE-2.0
|
| 9 | //
|
| 10 | // Unless required by applicable law or agreed to in writing, software
|
| 11 | // distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 | // See the License for the specific language governing permissions and
|
| 14 | // limitations under the License.
|
| 15 | */
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 16 | #include "srvcfg_manager.hpp"
|
| 17 |
|
| 18 | #include <boost/asio/spawn.hpp>
|
| 19 |
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 20 | #include <fstream>
|
| 21 | #include <regex>
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 22 |
|
Richard Marian Thomaiyar | 33816cf | 2019-06-14 13:54:50 +0530 | [diff] [blame] | 23 | extern std::unique_ptr<boost::asio::steady_timer> timer;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 24 | extern std::map<std::string, std::shared_ptr<phosphor::service::ServiceConfig>>
|
| 25 | srvMgrObjects;
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 26 | static bool updateInProgress = false;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 27 |
|
| 28 | namespace phosphor
|
| 29 | {
|
| 30 | namespace service
|
| 31 | {
|
| 32 |
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 33 | static constexpr const char* overrideConfFileName = "override.conf";
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 34 | static constexpr const size_t restartTimeout = 15; // seconds
|
| 35 |
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 36 | static constexpr const char* systemd1UnitBasePath =
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 37 | "/org/freedesktop/systemd1/unit/";
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 38 | static constexpr const char* systemdOverrideUnitBasePath =
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 39 | "/etc/systemd/system/";
|
| 40 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 41 | void ServiceConfig::updateSocketProperties(
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 42 | const boost::container::flat_map<std::string, VariantType>& propertyMap)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 43 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 44 | auto listenIt = propertyMap.find("Listen");
|
| 45 | if (listenIt != propertyMap.end())
|
| 46 | {
|
| 47 | auto listenVal =
|
| 48 | std::get<std::vector<std::tuple<std::string, std::string>>>(
|
| 49 | listenIt->second);
|
| 50 | if (listenVal.size())
|
| 51 | {
|
| 52 | protocol = std::get<0>(listenVal[0]);
|
| 53 | std::string port = std::get<1>(listenVal[0]);
|
| 54 | auto tmp = std::stoul(port.substr(port.find_last_of(":") + 1),
|
| 55 | nullptr, 10);
|
| 56 | if (tmp > std::numeric_limits<uint16_t>::max())
|
| 57 | {
|
| 58 | throw std::out_of_range("Out of range");
|
| 59 | }
|
| 60 | portNum = tmp;
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 61 | if (sockAttrIface && sockAttrIface->is_initialized())
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 62 | {
|
| 63 | internalSet = true;
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 64 | sockAttrIface->set_property(sockAttrPropPort, portNum);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 65 | internalSet = false;
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | void ServiceConfig::updateServiceProperties(
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 72 | const boost::container::flat_map<std::string, VariantType>& propertyMap)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 73 | {
|
| 74 | auto stateIt = propertyMap.find("UnitFileState");
|
| 75 | if (stateIt != propertyMap.end())
|
| 76 | {
|
| 77 | stateValue = std::get<std::string>(stateIt->second);
|
| 78 | unitEnabledState = unitMaskedState = false;
|
| 79 | if (stateValue == stateMasked)
|
| 80 | {
|
| 81 | unitMaskedState = true;
|
| 82 | }
|
| 83 | else if (stateValue == stateEnabled)
|
| 84 | {
|
| 85 | unitEnabledState = true;
|
| 86 | }
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 87 | if (srvCfgIface && srvCfgIface->is_initialized())
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 88 | {
|
| 89 | internalSet = true;
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 90 | srvCfgIface->set_property(srvCfgPropMasked, unitMaskedState);
|
| 91 | srvCfgIface->set_property(srvCfgPropEnabled, unitEnabledState);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 92 | internalSet = false;
|
| 93 | }
|
| 94 | }
|
| 95 | auto subStateIt = propertyMap.find("SubState");
|
| 96 | if (subStateIt != propertyMap.end())
|
| 97 | {
|
| 98 | subStateValue = std::get<std::string>(subStateIt->second);
|
| 99 | if (subStateValue == subStateRunning)
|
| 100 | {
|
| 101 | unitRunningState = true;
|
| 102 | }
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 103 | if (srvCfgIface && srvCfgIface->is_initialized())
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 104 | {
|
| 105 | internalSet = true;
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 106 | srvCfgIface->set_property(srvCfgPropRunning, unitRunningState);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 107 | internalSet = false;
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | void ServiceConfig::queryAndUpdateProperties()
|
| 113 | {
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 114 | conn->async_method_call(
|
| 115 | [this](boost::system::error_code ec,
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 116 | const boost::container::flat_map<std::string, VariantType>&
|
| 117 | propertyMap) {
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 118 | if (ec)
|
| 119 | {
|
| 120 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 121 | "async_method_call error: Failed to service unit "
|
| 122 | "properties");
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 123 | return;
|
| 124 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 125 | try
|
| 126 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 127 | updateServiceProperties(propertyMap);
|
| 128 | if (!socketObjectPath.empty())
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 129 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 130 | conn->async_method_call(
|
| 131 | [this](boost::system::error_code ec,
|
| 132 | const boost::container::flat_map<
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 133 | std::string, VariantType>& propertyMap) {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 134 | if (ec)
|
| 135 | {
|
| 136 | phosphor::logging::log<
|
| 137 | phosphor::logging::level::ERR>(
|
| 138 | "async_method_call error: Failed to get "
|
| 139 | "all property");
|
| 140 | return;
|
| 141 | }
|
| 142 | try
|
| 143 | {
|
| 144 | updateSocketProperties(propertyMap);
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 145 | if (!srvCfgIface)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 146 | {
|
| 147 | registerProperties();
|
| 148 | }
|
| 149 | }
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 150 | catch (const std::exception& e)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 151 | {
|
| 152 | phosphor::logging::log<
|
| 153 | phosphor::logging::level::ERR>(
|
| 154 | "Exception in getting socket properties",
|
| 155 | phosphor::logging::entry("WHAT=%s",
|
| 156 | e.what()));
|
| 157 | return;
|
| 158 | }
|
| 159 | },
|
| 160 | sysdService, socketObjectPath, dBusPropIntf,
|
| 161 | dBusGetAllMethod, sysdSocketIntf);
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 162 | }
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 163 | else if (!srvCfgIface)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 164 | {
|
| 165 | registerProperties();
|
| 166 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 167 | }
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 168 | catch (const std::exception& e)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 169 | {
|
| 170 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 171 | "Exception in getting socket properties",
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 172 | phosphor::logging::entry("WHAT=%s", e.what()));
|
| 173 | return;
|
| 174 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 175 | },
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 176 | sysdService, serviceObjectPath, dBusPropIntf, dBusGetAllMethod,
|
| 177 | sysdUnitIntf);
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 178 | return;
|
| 179 | }
|
| 180 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 181 | void ServiceConfig::createSocketOverrideConf()
|
| 182 | {
|
| 183 | if (!socketObjectPath.empty())
|
| 184 | {
|
| 185 | std::string socketUnitName(instantiatedUnitName + ".socket");
|
| 186 | /// Check override socket directory exist, if not create it.
|
Tom Joseph | 82e9557 | 2020-05-07 20:28:05 +0530 | [diff] [blame] | 187 | std::filesystem::path ovrUnitFileDir(systemdOverrideUnitBasePath);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 188 | ovrUnitFileDir += socketUnitName;
|
| 189 | ovrUnitFileDir += ".d";
|
Tom Joseph | 82e9557 | 2020-05-07 20:28:05 +0530 | [diff] [blame] | 190 | if (!std::filesystem::exists(ovrUnitFileDir))
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 191 | {
|
Tom Joseph | 82e9557 | 2020-05-07 20:28:05 +0530 | [diff] [blame] | 192 | if (!std::filesystem::create_directories(ovrUnitFileDir))
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 193 | {
|
| 194 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 195 | "Unable to create the directory.",
|
| 196 | phosphor::logging::entry("DIR=%s", ovrUnitFileDir.c_str()));
|
| 197 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::
|
| 198 | Common::Error::InternalFailure>();
|
| 199 | }
|
| 200 | }
|
| 201 | overrideConfDir = std::string(ovrUnitFileDir);
|
| 202 | }
|
| 203 | }
|
| 204 |
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 205 | ServiceConfig::ServiceConfig(
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 206 | sdbusplus::asio::object_server& srv_,
|
| 207 | std::shared_ptr<sdbusplus::asio::connection>& conn_,
|
| 208 | const std::string& objPath_, const std::string& baseUnitName_,
|
| 209 | const std::string& instanceName_, const std::string& serviceObjPath_,
|
| 210 | const std::string& socketObjPath_) :
|
Tom Joseph | 82e9557 | 2020-05-07 20:28:05 +0530 | [diff] [blame] | 211 | conn(conn_),
|
| 212 | server(srv_), objPath(objPath_), baseUnitName(baseUnitName_),
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 213 | instanceName(instanceName_), serviceObjectPath(serviceObjPath_),
|
| 214 | socketObjectPath(socketObjPath_)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 215 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 216 | instantiatedUnitName = baseUnitName + addInstanceName(instanceName, "@");
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 217 | updatedFlag = 0;
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 218 | queryAndUpdateProperties();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 219 | return;
|
| 220 | }
|
| 221 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 222 | std::string ServiceConfig::getSocketUnitName()
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 223 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 224 | return instantiatedUnitName + ".socket";
|
| 225 | }
|
| 226 |
|
| 227 | std::string ServiceConfig::getServiceUnitName()
|
| 228 | {
|
| 229 | return instantiatedUnitName + ".service";
|
| 230 | }
|
| 231 |
|
| 232 | bool ServiceConfig::isMaskedOut()
|
| 233 | {
|
| 234 | // return true if state is masked & no request to update the maskedState
|
| 235 | return (
|
| 236 | stateValue == "masked" &&
|
| 237 | !(updatedFlag & (1 << static_cast<uint8_t>(UpdatedProp::maskedState))));
|
| 238 | }
|
| 239 |
|
| 240 | void ServiceConfig::stopAndApplyUnitConfig(boost::asio::yield_context yield)
|
| 241 | {
|
| 242 | if (!updatedFlag || isMaskedOut())
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 243 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 244 | // No updates / masked - Just return.
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 245 | return;
|
| 246 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 247 | phosphor::logging::log<phosphor::logging::level::INFO>(
|
| 248 | "Applying new settings.",
|
| 249 | phosphor::logging::entry("OBJPATH=%s", objPath.c_str()));
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 250 | if (subStateValue == "running")
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 251 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 252 | if (!socketObjectPath.empty())
|
| 253 | {
|
| 254 | systemdUnitAction(conn, yield, getSocketUnitName(), sysdStopUnit);
|
| 255 | }
|
| 256 | systemdUnitAction(conn, yield, getServiceUnitName(), sysdStopUnit);
|
| 257 | }
|
| 258 |
|
| 259 | if (updatedFlag & (1 << static_cast<uint8_t>(UpdatedProp::port)))
|
| 260 | {
|
| 261 | createSocketOverrideConf();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 262 | // Create override config file and write data.
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 263 | std::string ovrCfgFile{overrideConfDir + "/" + overrideConfFileName};
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 264 | std::string tmpFile{ovrCfgFile + "_tmp"};
|
| 265 | std::ofstream cfgFile(tmpFile, std::ios::out);
|
| 266 | if (!cfgFile.good())
|
| 267 | {
|
| 268 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 269 | "Failed to open override.conf_tmp file");
|
| 270 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
|
| 271 | Error::InternalFailure>();
|
| 272 | }
|
| 273 |
|
| 274 | // Write the socket header
|
| 275 | cfgFile << "[Socket]\n";
|
| 276 | // Listen
|
| 277 | cfgFile << "Listen" << protocol << "="
|
| 278 | << "\n";
|
| 279 | cfgFile << "Listen" << protocol << "=" << portNum << "\n";
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 280 | cfgFile.close();
|
| 281 |
|
| 282 | if (std::rename(tmpFile.c_str(), ovrCfgFile.c_str()) != 0)
|
| 283 | {
|
| 284 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 285 | "Failed to rename tmp file as override.conf");
|
| 286 | std::remove(tmpFile.c_str());
|
| 287 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common::
|
| 288 | Error::InternalFailure>();
|
| 289 | }
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 290 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 291 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 292 | if (updatedFlag & ((1 << static_cast<uint8_t>(UpdatedProp::maskedState)) |
|
| 293 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState))))
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 294 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 295 | std::vector<std::string> unitFiles;
|
| 296 | if (socketObjectPath.empty())
|
| 297 | {
|
| 298 | unitFiles = {getServiceUnitName()};
|
| 299 | }
|
| 300 | else
|
| 301 | {
|
| 302 | unitFiles = {getSocketUnitName(), getServiceUnitName()};
|
| 303 | }
|
| 304 | systemdUnitFilesStateChange(conn, yield, unitFiles, stateValue,
|
| 305 | unitMaskedState, unitEnabledState);
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 306 | }
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 307 | return;
|
| 308 | }
|
| 309 | void ServiceConfig::restartUnitConfig(boost::asio::yield_context yield)
|
| 310 | {
|
| 311 | if (!updatedFlag || isMaskedOut())
|
AppaRao Puli | e55cfd6 | 2019-02-15 15:35:29 +0530 | [diff] [blame] | 312 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 313 | // No updates. Just return.
|
| 314 | return;
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 315 | }
|
| 316 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 317 | if (unitRunningState)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 318 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 319 | if (!socketObjectPath.empty())
|
| 320 | {
|
| 321 | systemdUnitAction(conn, yield, getSocketUnitName(),
|
| 322 | sysdRestartUnit);
|
| 323 | }
|
| 324 | systemdUnitAction(conn, yield, getServiceUnitName(), sysdRestartUnit);
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 325 | }
|
| 326 |
|
| 327 | // Reset the flag
|
| 328 | updatedFlag = 0;
|
| 329 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 330 | phosphor::logging::log<phosphor::logging::level::INFO>(
|
| 331 | "Applied new settings",
|
| 332 | phosphor::logging::entry("OBJPATH=%s", objPath.c_str()));
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 333 |
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 334 | queryAndUpdateProperties();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 335 | return;
|
| 336 | }
|
| 337 |
|
| 338 | void ServiceConfig::startServiceRestartTimer()
|
| 339 | {
|
Richard Marian Thomaiyar | 33816cf | 2019-06-14 13:54:50 +0530 | [diff] [blame] | 340 | timer->expires_after(std::chrono::seconds(restartTimeout));
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 341 | timer->async_wait([this](const boost::system::error_code& ec) {
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 342 | if (ec == boost::asio::error::operation_aborted)
|
| 343 | {
|
| 344 | // Timer reset.
|
| 345 | return;
|
| 346 | }
|
| 347 | else if (ec)
|
| 348 | {
|
| 349 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 350 | "async wait error.");
|
| 351 | return;
|
| 352 | }
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 353 | updateInProgress = true;
|
| 354 | boost::asio::spawn(conn->get_io_context(),
|
| 355 | [this](boost::asio::yield_context yield) {
|
| 356 | // Stop and apply configuration for all objects
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 357 | for (auto& srvMgrObj : srvMgrObjects)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 358 | {
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 359 | auto& srvObj = srvMgrObj.second;
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 360 | if (srvObj->updatedFlag)
|
| 361 | {
|
| 362 | srvObj->stopAndApplyUnitConfig(yield);
|
| 363 | }
|
| 364 | }
|
| 365 | // Do system reload
|
| 366 | systemdDaemonReload(conn, yield);
|
| 367 | // restart unit config.
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 368 | for (auto& srvMgrObj : srvMgrObjects)
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 369 | {
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 370 | auto& srvObj = srvMgrObj.second;
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 371 | if (srvObj->updatedFlag)
|
| 372 | {
|
| 373 | srvObj->restartUnitConfig(yield);
|
| 374 | }
|
| 375 | }
|
| 376 | updateInProgress = false;
|
| 377 | });
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 378 | });
|
| 379 | }
|
| 380 |
|
| 381 | void ServiceConfig::registerProperties()
|
| 382 | {
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 383 | srvCfgIface = server.add_interface(objPath, serviceConfigIntfName);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 384 |
|
| 385 | if (!socketObjectPath.empty())
|
| 386 | {
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 387 | sockAttrIface = server.add_interface(objPath, sockAttrIntfName);
|
| 388 | sockAttrIface->register_property(
|
| 389 | sockAttrPropPort, portNum,
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 390 | [this](const uint16_t& req, uint16_t& res) {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 391 | if (!internalSet)
|
| 392 | {
|
| 393 | if (req == res)
|
| 394 | {
|
| 395 | return 1;
|
| 396 | }
|
| 397 | if (updateInProgress)
|
| 398 | {
|
| 399 | return 0;
|
| 400 | }
|
| 401 | portNum = req;
|
| 402 | updatedFlag |=
|
| 403 | (1 << static_cast<uint8_t>(UpdatedProp::port));
|
| 404 | startServiceRestartTimer();
|
| 405 | }
|
| 406 | res = req;
|
| 407 | return 1;
|
| 408 | });
|
| 409 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 410 |
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 411 | srvCfgIface->register_property(
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 412 | srvCfgPropMasked, unitMaskedState, [this](const bool& req, bool& res) {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 413 | if (!internalSet)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 414 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 415 | if (req == res)
|
| 416 | {
|
| 417 | return 1;
|
| 418 | }
|
| 419 | if (updateInProgress)
|
| 420 | {
|
| 421 | return 0;
|
| 422 | }
|
| 423 | unitMaskedState = req;
|
| 424 | unitEnabledState = !unitMaskedState;
|
| 425 | unitRunningState = !unitMaskedState;
|
| 426 | updatedFlag |=
|
| 427 | (1 << static_cast<uint8_t>(UpdatedProp::maskedState)) |
|
| 428 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState)) |
|
| 429 | (1 << static_cast<uint8_t>(UpdatedProp::runningState));
|
| 430 | internalSet = true;
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 431 | srvCfgIface->set_property(srvCfgPropEnabled, unitEnabledState);
|
| 432 | srvCfgIface->set_property(srvCfgPropRunning, unitRunningState);
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 433 | internalSet = false;
|
| 434 | startServiceRestartTimer();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 435 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 436 | res = req;
|
| 437 | return 1;
|
| 438 | });
|
| 439 |
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 440 | srvCfgIface->register_property(
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 441 | srvCfgPropEnabled, unitEnabledState,
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 442 | [this](const bool& req, bool& res) {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 443 | if (!internalSet)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 444 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 445 | if (req == res)
|
| 446 | {
|
| 447 | return 1;
|
| 448 | }
|
| 449 | if (updateInProgress)
|
| 450 | {
|
| 451 | return 0;
|
| 452 | }
|
| 453 | if (unitMaskedState)
|
| 454 | { // block updating if masked
|
| 455 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 456 | "Invalid value specified");
|
| 457 | return -EINVAL;
|
| 458 | }
|
| 459 | unitEnabledState = req;
|
| 460 | updatedFlag |=
|
| 461 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState));
|
| 462 | startServiceRestartTimer();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 463 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 464 | res = req;
|
| 465 | return 1;
|
| 466 | });
|
| 467 |
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 468 | srvCfgIface->register_property(
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 469 | srvCfgPropRunning, unitRunningState,
|
AppaRao Puli | ee853eb | 2020-05-29 00:53:20 +0530 | [diff] [blame] | 470 | [this](const bool& req, bool& res) {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 471 | if (!internalSet)
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 472 | {
|
Richard Marian Thomaiyar | 90f2da3 | 2019-05-23 05:37:50 +0530 | [diff] [blame] | 473 | if (req == res)
|
| 474 | {
|
| 475 | return 1;
|
| 476 | }
|
| 477 | if (updateInProgress)
|
| 478 | {
|
| 479 | return 0;
|
| 480 | }
|
| 481 | if (unitMaskedState)
|
| 482 | { // block updating if masked
|
| 483 | phosphor::logging::log<phosphor::logging::level::ERR>(
|
| 484 | "Invalid value specified");
|
| 485 | return -EINVAL;
|
| 486 | }
|
| 487 | unitRunningState = req;
|
| 488 | updatedFlag |=
|
| 489 | (1 << static_cast<uint8_t>(UpdatedProp::runningState));
|
| 490 | startServiceRestartTimer();
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 491 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 492 | res = req;
|
| 493 | return 1;
|
| 494 | });
|
| 495 |
|
Tom Joseph | 83241c0 | 2020-05-30 11:30:20 +0530 | [diff] [blame] | 496 | srvCfgIface->initialize();
|
| 497 | if (!socketObjectPath.empty())
|
| 498 | {
|
| 499 | sockAttrIface->initialize();
|
| 500 | }
|
AppaRao Puli | 0084047 | 2018-10-03 19:37:46 +0530 | [diff] [blame] | 501 | return;
|
| 502 | }
|
| 503 |
|
| 504 | } // namespace service
|
| 505 | } // namespace phosphor
|