Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [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 | */ |
| 16 | #include "srvcfg_manager.hpp" |
| 17 | |
Ed Tanous | c828db7 | 2025-04-01 16:42:24 -0700 | [diff] [blame] | 18 | #include <boost/asio/detached.hpp> |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 19 | #include <boost/asio/spawn.hpp> |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 20 | #ifdef USB_CODE_UPDATE |
| 21 | #include <cereal/archives/json.hpp> |
| 22 | #include <cereal/types/tuple.hpp> |
| 23 | #include <cereal/types/unordered_map.hpp> |
| 24 | |
| 25 | #include <cstdio> |
| 26 | #endif |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 27 | #include <fstream> |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 28 | #ifdef PERSIST_SETTINGS |
| 29 | #include <nlohmann/json.hpp> |
| 30 | #endif |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 31 | #include <regex> |
| 32 | |
| 33 | extern std::unique_ptr<boost::asio::steady_timer> timer; |
| 34 | extern std::map<std::string, std::shared_ptr<phosphor::service::ServiceConfig>> |
| 35 | srvMgrObjects; |
| 36 | static bool updateInProgress = false; |
| 37 | |
| 38 | namespace phosphor |
| 39 | { |
| 40 | namespace service |
| 41 | { |
| 42 | |
| 43 | static constexpr const char* overrideConfFileName = "override.conf"; |
| 44 | static constexpr const size_t restartTimeout = 15; // seconds |
| 45 | |
| 46 | static constexpr const char* systemd1UnitBasePath = |
| 47 | "/org/freedesktop/systemd1/unit/"; |
| 48 | static constexpr const char* systemdOverrideUnitBasePath = |
| 49 | "/etc/systemd/system/"; |
| 50 | |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 51 | #ifdef PERSIST_SETTINGS |
| 52 | static constexpr const char* persistDataFileVersionStr = "Version"; |
| 53 | static constexpr const size_t persistDataFileVersion = 1; |
| 54 | #endif |
| 55 | |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 56 | #ifdef USB_CODE_UPDATE |
| 57 | static constexpr const char* usbCodeUpdateStateFilePath = |
| 58 | "/var/lib/srvcfg_manager"; |
| 59 | static constexpr const char* usbCodeUpdateStateFile = |
| 60 | "/var/lib/srvcfg_manager/usb-code-update-state"; |
| 61 | static constexpr const char* emptyUsbCodeUpdateRulesFile = |
| 62 | "/etc/udev/rules.d/70-bmc-usb.rules"; |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 63 | |
| 64 | using UsbCodeUpdateStateMap = std::unordered_map<std::string, bool>; |
| 65 | |
| 66 | void ServiceConfig::setUSBCodeUpdateState(const bool& state) |
| 67 | { |
| 68 | // Enable usb code update |
| 69 | if (state) |
| 70 | { |
| 71 | if (std::filesystem::exists(emptyUsbCodeUpdateRulesFile)) |
| 72 | { |
Jiaqing Zhao | f27f431 | 2022-04-02 17:42:46 +0800 | [diff] [blame] | 73 | lg2::info("Enable usb code update"); |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 74 | std::filesystem::remove(emptyUsbCodeUpdateRulesFile); |
| 75 | } |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Disable usb code update |
| 80 | if (std::filesystem::exists(emptyUsbCodeUpdateRulesFile)) |
| 81 | { |
| 82 | std::filesystem::remove(emptyUsbCodeUpdateRulesFile); |
| 83 | } |
| 84 | std::error_code ec; |
| 85 | std::filesystem::create_symlink("/dev/null", emptyUsbCodeUpdateRulesFile, |
| 86 | ec); |
| 87 | if (ec) |
| 88 | { |
Jiaqing Zhao | f27f431 | 2022-04-02 17:42:46 +0800 | [diff] [blame] | 89 | lg2::error("Disable usb code update failed"); |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 90 | return; |
| 91 | } |
Jiaqing Zhao | f27f431 | 2022-04-02 17:42:46 +0800 | [diff] [blame] | 92 | lg2::info("Disable usb code update"); |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void ServiceConfig::saveUSBCodeUpdateStateToFile(const bool& maskedState, |
| 96 | const bool& enabledState) |
| 97 | { |
| 98 | if (!std::filesystem::exists(usbCodeUpdateStateFilePath)) |
| 99 | { |
| 100 | std::filesystem::create_directories(usbCodeUpdateStateFilePath); |
| 101 | } |
| 102 | |
| 103 | UsbCodeUpdateStateMap usbCodeUpdateState; |
| 104 | usbCodeUpdateState[srvCfgPropMasked] = maskedState; |
| 105 | usbCodeUpdateState[srvCfgPropEnabled] = enabledState; |
| 106 | |
| 107 | std::ofstream file(usbCodeUpdateStateFile, std::ios::out); |
| 108 | cereal::JSONOutputArchive archive(file); |
| 109 | archive(CEREAL_NVP(usbCodeUpdateState)); |
| 110 | } |
| 111 | |
| 112 | void ServiceConfig::getUSBCodeUpdateStateFromFile() |
| 113 | { |
| 114 | if (!std::filesystem::exists(usbCodeUpdateStateFile)) |
| 115 | { |
Jiaqing Zhao | f27f431 | 2022-04-02 17:42:46 +0800 | [diff] [blame] | 116 | lg2::info("usb-code-update-state file does not exist"); |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 117 | |
| 118 | unitMaskedState = false; |
| 119 | unitEnabledState = true; |
| 120 | unitRunningState = true; |
| 121 | setUSBCodeUpdateState(unitEnabledState); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | std::ifstream file(usbCodeUpdateStateFile); |
| 126 | cereal::JSONInputArchive archive(file); |
| 127 | UsbCodeUpdateStateMap usbCodeUpdateState; |
| 128 | archive(usbCodeUpdateState); |
| 129 | |
| 130 | auto iterMask = usbCodeUpdateState.find(srvCfgPropMasked); |
| 131 | if (iterMask != usbCodeUpdateState.end()) |
| 132 | { |
| 133 | unitMaskedState = iterMask->second; |
| 134 | if (unitMaskedState) |
| 135 | { |
| 136 | unitEnabledState = !unitMaskedState; |
| 137 | unitRunningState = !unitMaskedState; |
| 138 | setUSBCodeUpdateState(unitEnabledState); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | auto iterEnable = usbCodeUpdateState.find(srvCfgPropEnabled); |
| 143 | if (iterEnable != usbCodeUpdateState.end()) |
| 144 | { |
| 145 | unitEnabledState = iterEnable->second; |
| 146 | unitRunningState = iterEnable->second; |
| 147 | setUSBCodeUpdateState(unitEnabledState); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | #endif |
| 152 | |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 153 | void ServiceConfig::updateSocketProperties( |
| 154 | const boost::container::flat_map<std::string, VariantType>& propertyMap) |
| 155 | { |
| 156 | auto listenIt = propertyMap.find("Listen"); |
| 157 | if (listenIt != propertyMap.end()) |
| 158 | { |
| 159 | auto listenVal = |
| 160 | std::get<std::vector<std::tuple<std::string, std::string>>>( |
| 161 | listenIt->second); |
| 162 | if (listenVal.size()) |
| 163 | { |
| 164 | protocol = std::get<0>(listenVal[0]); |
| 165 | std::string port = std::get<1>(listenVal[0]); |
| 166 | auto tmp = std::stoul(port.substr(port.find_last_of(":") + 1), |
| 167 | nullptr, 10); |
| 168 | if (tmp > std::numeric_limits<uint16_t>::max()) |
| 169 | { |
| 170 | throw std::out_of_range("Out of range"); |
| 171 | } |
| 172 | portNum = tmp; |
| 173 | if (sockAttrIface && sockAttrIface->is_initialized()) |
| 174 | { |
| 175 | internalSet = true; |
| 176 | sockAttrIface->set_property(sockAttrPropPort, portNum); |
| 177 | internalSet = false; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void ServiceConfig::updateServiceProperties( |
| 184 | const boost::container::flat_map<std::string, VariantType>& propertyMap) |
| 185 | { |
| 186 | auto stateIt = propertyMap.find("UnitFileState"); |
| 187 | if (stateIt != propertyMap.end()) |
| 188 | { |
| 189 | stateValue = std::get<std::string>(stateIt->second); |
| 190 | unitEnabledState = unitMaskedState = false; |
| 191 | if (stateValue == stateMasked) |
| 192 | { |
| 193 | unitMaskedState = true; |
| 194 | } |
| 195 | else if (stateValue == stateEnabled) |
| 196 | { |
| 197 | unitEnabledState = true; |
| 198 | } |
| 199 | if (srvCfgIface && srvCfgIface->is_initialized()) |
| 200 | { |
| 201 | internalSet = true; |
| 202 | srvCfgIface->set_property(srvCfgPropMasked, unitMaskedState); |
| 203 | srvCfgIface->set_property(srvCfgPropEnabled, unitEnabledState); |
| 204 | internalSet = false; |
| 205 | } |
| 206 | } |
| 207 | auto subStateIt = propertyMap.find("SubState"); |
| 208 | if (subStateIt != propertyMap.end()) |
| 209 | { |
| 210 | subStateValue = std::get<std::string>(subStateIt->second); |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 211 | if (subStateValue == subStateRunning || |
| 212 | subStateValue == subStateListening) |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 213 | { |
| 214 | unitRunningState = true; |
| 215 | } |
| 216 | if (srvCfgIface && srvCfgIface->is_initialized()) |
| 217 | { |
| 218 | internalSet = true; |
| 219 | srvCfgIface->set_property(srvCfgPropRunning, unitRunningState); |
| 220 | internalSet = false; |
| 221 | } |
| 222 | } |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 223 | |
| 224 | #ifdef USB_CODE_UPDATE |
Jiaqing Zhao | 430d7ea | 2022-04-01 23:23:25 +0800 | [diff] [blame] | 225 | if (baseUnitName == usbCodeUpdateUnitName) |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 226 | { |
| 227 | getUSBCodeUpdateStateFromFile(); |
| 228 | } |
| 229 | #endif |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void ServiceConfig::queryAndUpdateProperties() |
| 233 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 234 | std::string objectPath = |
| 235 | isSocketActivatedService ? socketObjectPath : serviceObjectPath; |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 236 | if (objectPath.empty()) |
| 237 | { |
| 238 | return; |
| 239 | } |
| 240 | |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 241 | conn->async_method_call( |
| 242 | [this](boost::system::error_code ec, |
| 243 | const boost::container::flat_map<std::string, VariantType>& |
| 244 | propertyMap) { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 245 | if (ec) |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 246 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 247 | lg2::error( |
| 248 | "async_method_call error: Failed to service unit properties: {EC}", |
| 249 | "EC", ec.value()); |
| 250 | return; |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 251 | } |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 252 | try |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 253 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 254 | updateServiceProperties(propertyMap); |
| 255 | if (!socketObjectPath.empty()) |
| 256 | { |
| 257 | conn->async_method_call( |
| 258 | [this](boost::system::error_code ec, |
| 259 | const boost::container::flat_map< |
| 260 | std::string, VariantType>& propertyMap) { |
| 261 | if (ec) |
| 262 | { |
| 263 | lg2::error( |
| 264 | "async_method_call error: Failed to get all property: {EC}", |
| 265 | "EC", ec.value()); |
| 266 | return; |
| 267 | } |
| 268 | try |
| 269 | { |
| 270 | updateSocketProperties(propertyMap); |
| 271 | if (!srvCfgIface) |
| 272 | { |
| 273 | registerProperties(); |
| 274 | } |
| 275 | } |
| 276 | catch (const std::exception& e) |
| 277 | { |
| 278 | lg2::error( |
| 279 | "Exception in getting socket properties: {ERROR}", |
| 280 | "ERROR", e); |
| 281 | return; |
| 282 | } |
| 283 | }, |
| 284 | sysdService, socketObjectPath, dBusPropIntf, |
| 285 | dBusGetAllMethod, sysdSocketIntf); |
| 286 | } |
| 287 | else if (!srvCfgIface) |
| 288 | { |
| 289 | registerProperties(); |
| 290 | } |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 291 | writeStateFile(); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 292 | } |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 293 | catch (const std::exception& e) |
| 294 | { |
| 295 | lg2::error("Exception in getting socket properties: {ERROR}", |
| 296 | "ERROR", e); |
| 297 | return; |
| 298 | } |
| 299 | }, |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 300 | sysdService, objectPath, dBusPropIntf, dBusGetAllMethod, sysdUnitIntf); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
| 304 | void ServiceConfig::createSocketOverrideConf() |
| 305 | { |
| 306 | if (!socketObjectPath.empty()) |
| 307 | { |
| 308 | std::string socketUnitName(instantiatedUnitName + ".socket"); |
| 309 | /// Check override socket directory exist, if not create it. |
| 310 | std::filesystem::path ovrUnitFileDir(systemdOverrideUnitBasePath); |
| 311 | ovrUnitFileDir += socketUnitName; |
| 312 | ovrUnitFileDir += ".d"; |
| 313 | if (!std::filesystem::exists(ovrUnitFileDir)) |
| 314 | { |
| 315 | if (!std::filesystem::create_directories(ovrUnitFileDir)) |
| 316 | { |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 317 | lg2::error("Unable to create the {DIR} directory.", "DIR", |
| 318 | ovrUnitFileDir); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 319 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project:: |
| 320 | Common::Error::InternalFailure>(); |
| 321 | } |
| 322 | } |
| 323 | overrideConfDir = std::string(ovrUnitFileDir); |
| 324 | } |
| 325 | } |
| 326 | |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 327 | void ServiceConfig::writeStateFile() |
| 328 | { |
| 329 | #ifdef PERSIST_SETTINGS |
| 330 | nlohmann::json stateMap; |
| 331 | stateMap[persistDataFileVersionStr] = persistDataFileVersion; |
| 332 | stateMap[srvCfgPropMasked] = unitMaskedState; |
| 333 | stateMap[srvCfgPropEnabled] = unitEnabledState; |
| 334 | stateMap[srvCfgPropRunning] = unitRunningState; |
| 335 | |
| 336 | std::ofstream file(stateFile); |
| 337 | file << stateMap; |
| 338 | file.close(); |
| 339 | #endif |
| 340 | } |
| 341 | |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 342 | ServiceConfig::ServiceConfig( |
| 343 | sdbusplus::asio::object_server& srv_, |
| 344 | std::shared_ptr<sdbusplus::asio::connection>& conn_, |
| 345 | const std::string& objPath_, const std::string& baseUnitName_, |
| 346 | const std::string& instanceName_, const std::string& serviceObjPath_, |
| 347 | const std::string& socketObjPath_) : |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 348 | conn(conn_), server(srv_), objPath(objPath_), baseUnitName(baseUnitName_), |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 349 | instanceName(instanceName_), serviceObjectPath(serviceObjPath_), |
| 350 | socketObjectPath(socketObjPath_) |
| 351 | { |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 352 | isSocketActivatedService = serviceObjectPath.empty(); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 353 | instantiatedUnitName = baseUnitName + addInstanceName(instanceName, "@"); |
| 354 | updatedFlag = 0; |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 355 | stateFile = srvDataBaseDir + instantiatedUnitName; |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 356 | queryAndUpdateProperties(); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | std::string ServiceConfig::getSocketUnitName() |
| 361 | { |
| 362 | return instantiatedUnitName + ".socket"; |
| 363 | } |
| 364 | |
| 365 | std::string ServiceConfig::getServiceUnitName() |
| 366 | { |
| 367 | return instantiatedUnitName + ".service"; |
| 368 | } |
| 369 | |
| 370 | bool ServiceConfig::isMaskedOut() |
| 371 | { |
| 372 | // return true if state is masked & no request to update the maskedState |
| 373 | return ( |
| 374 | stateValue == "masked" && |
| 375 | !(updatedFlag & (1 << static_cast<uint8_t>(UpdatedProp::maskedState)))); |
| 376 | } |
| 377 | |
| 378 | void ServiceConfig::stopAndApplyUnitConfig(boost::asio::yield_context yield) |
| 379 | { |
| 380 | if (!updatedFlag || isMaskedOut()) |
| 381 | { |
| 382 | // No updates / masked - Just return. |
| 383 | return; |
| 384 | } |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 385 | lg2::info("Applying new settings: {OBJPATH}", "OBJPATH", objPath); |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 386 | if (subStateValue == subStateRunning || subStateValue == subStateListening) |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 387 | { |
| 388 | if (!socketObjectPath.empty()) |
| 389 | { |
| 390 | systemdUnitAction(conn, yield, getSocketUnitName(), sysdStopUnit); |
| 391 | } |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 392 | if (!isSocketActivatedService) |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 393 | { |
| 394 | systemdUnitAction(conn, yield, getServiceUnitName(), sysdStopUnit); |
| 395 | } |
| 396 | else |
| 397 | { |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 398 | // For socket-activated service, each connection will spawn a |
| 399 | // service instance from template. Need to find all spawned service |
| 400 | // `<unitName>@<attribute>.service` and stop them through the |
| 401 | // systemdUnitAction method |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 402 | boost::system::error_code ec; |
| 403 | auto listUnits = |
| 404 | conn->yield_method_call<std::vector<ListUnitsType>>( |
| 405 | yield, ec, sysdService, sysdObjPath, sysdMgrIntf, |
| 406 | "ListUnits"); |
| 407 | |
| 408 | checkAndThrowInternalFailure( |
| 409 | ec, "yield_method_call error: ListUnits failed"); |
| 410 | |
| 411 | for (const auto& unit : listUnits) |
| 412 | { |
| 413 | const auto& service = |
| 414 | std::get<static_cast<int>(ListUnitElements::name)>(unit); |
| 415 | const auto& status = |
| 416 | std::get<static_cast<int>(ListUnitElements::subState)>( |
| 417 | unit); |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 418 | if (service.find(baseUnitName + "@") != std::string::npos && |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 419 | service.find(".service") != std::string::npos && |
| 420 | status == subStateRunning) |
| 421 | { |
| 422 | systemdUnitAction(conn, yield, service, sysdStopUnit); |
| 423 | } |
| 424 | } |
| 425 | } |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (updatedFlag & (1 << static_cast<uint8_t>(UpdatedProp::port))) |
| 429 | { |
| 430 | createSocketOverrideConf(); |
| 431 | // Create override config file and write data. |
| 432 | std::string ovrCfgFile{overrideConfDir + "/" + overrideConfFileName}; |
| 433 | std::string tmpFile{ovrCfgFile + "_tmp"}; |
| 434 | std::ofstream cfgFile(tmpFile, std::ios::out); |
| 435 | if (!cfgFile.good()) |
| 436 | { |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 437 | lg2::error("Failed to open the {TMPFILE} file.", "TMPFILE", |
| 438 | tmpFile); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 439 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common:: |
| 440 | Error::InternalFailure>(); |
| 441 | } |
| 442 | |
| 443 | // Write the socket header |
| 444 | cfgFile << "[Socket]\n"; |
| 445 | // Listen |
| 446 | cfgFile << "Listen" << protocol << "=" |
| 447 | << "\n"; |
| 448 | cfgFile << "Listen" << protocol << "=" << portNum << "\n"; |
| 449 | cfgFile.close(); |
| 450 | |
| 451 | if (std::rename(tmpFile.c_str(), ovrCfgFile.c_str()) != 0) |
| 452 | { |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 453 | lg2::error("Failed to rename {TMPFILE} file as {OVERCFGFILE} file.", |
| 454 | "TMPFILE", tmpFile, "OVERCFGFILE", ovrCfgFile); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 455 | std::remove(tmpFile.c_str()); |
| 456 | phosphor::logging::elog<sdbusplus::xyz::openbmc_project::Common:: |
| 457 | Error::InternalFailure>(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (updatedFlag & ((1 << static_cast<uint8_t>(UpdatedProp::maskedState)) | |
| 462 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState)))) |
| 463 | { |
| 464 | std::vector<std::string> unitFiles; |
| 465 | if (socketObjectPath.empty()) |
| 466 | { |
| 467 | unitFiles = {getServiceUnitName()}; |
| 468 | } |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 469 | else if (serviceObjectPath.empty()) |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 470 | { |
| 471 | unitFiles = {getSocketUnitName()}; |
| 472 | } |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 473 | else |
| 474 | { |
| 475 | unitFiles = {getSocketUnitName(), getServiceUnitName()}; |
| 476 | } |
| 477 | systemdUnitFilesStateChange(conn, yield, unitFiles, stateValue, |
| 478 | unitMaskedState, unitEnabledState); |
| 479 | } |
| 480 | return; |
| 481 | } |
| 482 | void ServiceConfig::restartUnitConfig(boost::asio::yield_context yield) |
| 483 | { |
| 484 | if (!updatedFlag || isMaskedOut()) |
| 485 | { |
| 486 | // No updates. Just return. |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (unitRunningState) |
| 491 | { |
| 492 | if (!socketObjectPath.empty()) |
| 493 | { |
| 494 | systemdUnitAction(conn, yield, getSocketUnitName(), |
| 495 | sysdRestartUnit); |
| 496 | } |
Jiaqing Zhao | f476683 | 2022-02-28 14:28:18 +0800 | [diff] [blame] | 497 | if (!serviceObjectPath.empty()) |
George Liu | a19b509 | 2021-05-24 15:54:02 +0800 | [diff] [blame] | 498 | { |
| 499 | systemdUnitAction(conn, yield, getServiceUnitName(), |
| 500 | sysdRestartUnit); |
| 501 | } |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Reset the flag |
| 505 | updatedFlag = 0; |
| 506 | |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 507 | lg2::info("Applied new settings: {OBJPATH}", "OBJPATH", objPath); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 508 | |
| 509 | queryAndUpdateProperties(); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | void ServiceConfig::startServiceRestartTimer() |
| 514 | { |
| 515 | timer->expires_after(std::chrono::seconds(restartTimeout)); |
| 516 | timer->async_wait([this](const boost::system::error_code& ec) { |
| 517 | if (ec == boost::asio::error::operation_aborted) |
| 518 | { |
| 519 | // Timer reset. |
| 520 | return; |
| 521 | } |
| 522 | else if (ec) |
| 523 | { |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 524 | lg2::error("async wait error: {EC}", "EC", ec.value()); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | updateInProgress = true; |
Andrew Geissler | cb0f134 | 2025-04-22 16:08:27 -0500 | [diff] [blame^] | 528 | // Ensure our persistent files are updated with changes |
| 529 | writeStateFile(); |
Ed Tanous | c828db7 | 2025-04-01 16:42:24 -0700 | [diff] [blame] | 530 | boost::asio::spawn( |
Jason M. Bills | 543e072 | 2025-02-26 10:33:58 -0800 | [diff] [blame] | 531 | conn->get_io_context(), |
| 532 | [this](boost::asio::yield_context yield) { |
| 533 | // Stop and apply configuration for all objects |
| 534 | for (auto& srvMgrObj : srvMgrObjects) |
| 535 | { |
| 536 | auto& srvObj = srvMgrObj.second; |
| 537 | if (srvObj->updatedFlag) |
| 538 | { |
| 539 | srvObj->stopAndApplyUnitConfig(yield); |
| 540 | } |
| 541 | } |
| 542 | // Do system reload |
| 543 | systemdDaemonReload(conn, yield); |
| 544 | // restart unit config. |
| 545 | for (auto& srvMgrObj : srvMgrObjects) |
| 546 | { |
| 547 | auto& srvObj = srvMgrObj.second; |
| 548 | if (srvObj->updatedFlag) |
| 549 | { |
| 550 | srvObj->restartUnitConfig(yield); |
| 551 | } |
| 552 | } |
| 553 | updateInProgress = false; |
| 554 | }, |
Ed Tanous | c828db7 | 2025-04-01 16:42:24 -0700 | [diff] [blame] | 555 | boost::asio::detached); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 556 | }); |
| 557 | } |
| 558 | |
| 559 | void ServiceConfig::registerProperties() |
| 560 | { |
| 561 | srvCfgIface = server.add_interface(objPath, serviceConfigIntfName); |
| 562 | |
| 563 | if (!socketObjectPath.empty()) |
| 564 | { |
| 565 | sockAttrIface = server.add_interface(objPath, sockAttrIntfName); |
| 566 | sockAttrIface->register_property( |
| 567 | sockAttrPropPort, portNum, |
| 568 | [this](const uint16_t& req, uint16_t& res) { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 569 | if (!internalSet) |
| 570 | { |
| 571 | if (req == res) |
| 572 | { |
| 573 | return 1; |
| 574 | } |
| 575 | if (updateInProgress) |
| 576 | { |
| 577 | return 0; |
| 578 | } |
| 579 | portNum = req; |
| 580 | updatedFlag |= |
| 581 | (1 << static_cast<uint8_t>(UpdatedProp::port)); |
| 582 | startServiceRestartTimer(); |
| 583 | } |
| 584 | res = req; |
| 585 | return 1; |
| 586 | }); |
| 587 | } |
| 588 | |
| 589 | srvCfgIface->register_property( |
| 590 | srvCfgPropMasked, unitMaskedState, [this](const bool& req, bool& res) { |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 591 | if (!internalSet) |
| 592 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 593 | #ifdef USB_CODE_UPDATE |
| 594 | if (baseUnitName == usbCodeUpdateUnitName) |
| 595 | { |
| 596 | unitMaskedState = req; |
| 597 | unitEnabledState = !unitMaskedState; |
| 598 | unitRunningState = !unitMaskedState; |
| 599 | internalSet = true; |
| 600 | srvCfgIface->set_property(srvCfgPropEnabled, |
| 601 | unitEnabledState); |
| 602 | srvCfgIface->set_property(srvCfgPropRunning, |
| 603 | unitRunningState); |
| 604 | srvCfgIface->set_property(srvCfgPropMasked, |
| 605 | unitMaskedState); |
| 606 | internalSet = false; |
| 607 | setUSBCodeUpdateState(unitEnabledState); |
| 608 | saveUSBCodeUpdateStateToFile(unitMaskedState, |
| 609 | unitEnabledState); |
| 610 | return 1; |
| 611 | } |
| 612 | #endif |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 613 | if (req == res) |
| 614 | { |
| 615 | return 1; |
| 616 | } |
| 617 | if (updateInProgress) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 621 | unitMaskedState = req; |
| 622 | unitEnabledState = !unitMaskedState; |
| 623 | unitRunningState = !unitMaskedState; |
| 624 | updatedFlag |= |
| 625 | (1 << static_cast<uint8_t>(UpdatedProp::maskedState)) | |
| 626 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState)) | |
| 627 | (1 << static_cast<uint8_t>(UpdatedProp::runningState)); |
| 628 | internalSet = true; |
| 629 | srvCfgIface->set_property(srvCfgPropEnabled, unitEnabledState); |
| 630 | srvCfgIface->set_property(srvCfgPropRunning, unitRunningState); |
| 631 | internalSet = false; |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 632 | startServiceRestartTimer(); |
| 633 | } |
| 634 | res = req; |
| 635 | return 1; |
Patrick Williams | d8effd6 | 2023-10-20 11:19:46 -0500 | [diff] [blame] | 636 | }); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 637 | |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 638 | srvCfgIface->register_property( |
| 639 | srvCfgPropEnabled, unitEnabledState, |
| 640 | [this](const bool& req, bool& res) { |
| 641 | if (!internalSet) |
Patrick Williams | dfc7270 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 642 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 643 | #ifdef USB_CODE_UPDATE |
| 644 | if (baseUnitName == usbCodeUpdateUnitName) |
| 645 | { |
| 646 | if (unitMaskedState) |
| 647 | { // block updating if masked |
| 648 | lg2::error("Invalid value specified"); |
| 649 | return -EINVAL; |
| 650 | } |
| 651 | unitEnabledState = req; |
| 652 | unitRunningState = req; |
| 653 | internalSet = true; |
| 654 | srvCfgIface->set_property(srvCfgPropEnabled, |
| 655 | unitEnabledState); |
| 656 | srvCfgIface->set_property(srvCfgPropRunning, |
| 657 | unitRunningState); |
| 658 | internalSet = false; |
| 659 | setUSBCodeUpdateState(unitEnabledState); |
| 660 | saveUSBCodeUpdateStateToFile(unitMaskedState, |
| 661 | unitEnabledState); |
| 662 | res = req; |
| 663 | return 1; |
| 664 | } |
Chicago Duan | 25a0f63 | 2021-11-11 16:32:07 +0800 | [diff] [blame] | 665 | #endif |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 666 | if (req == res) |
| 667 | { |
| 668 | return 1; |
| 669 | } |
| 670 | if (updateInProgress) |
| 671 | { |
| 672 | return 0; |
| 673 | } |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 674 | if (unitMaskedState) |
| 675 | { // block updating if masked |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 676 | lg2::error("Invalid value specified"); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | } |
| 679 | unitEnabledState = req; |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 680 | updatedFlag |= |
| 681 | (1 << static_cast<uint8_t>(UpdatedProp::enabledState)); |
| 682 | startServiceRestartTimer(); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 683 | } |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 684 | res = req; |
| 685 | return 1; |
| 686 | }); |
Patrick Williams | dfc7270 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 687 | |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 688 | srvCfgIface->register_property( |
| 689 | srvCfgPropRunning, unitRunningState, |
| 690 | [this](const bool& req, bool& res) { |
| 691 | if (!internalSet) |
Patrick Williams | dfc7270 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 692 | { |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 693 | #ifdef USB_CODE_UPDATE |
| 694 | if (baseUnitName == usbCodeUpdateUnitName) |
| 695 | { |
| 696 | if (unitMaskedState) |
| 697 | { // block updating if masked |
| 698 | lg2::error("Invalid value specified"); |
| 699 | return -EINVAL; |
| 700 | } |
| 701 | unitEnabledState = req; |
| 702 | unitRunningState = req; |
| 703 | internalSet = true; |
| 704 | srvCfgIface->set_property(srvCfgPropEnabled, |
| 705 | unitEnabledState); |
| 706 | srvCfgIface->set_property(srvCfgPropRunning, |
| 707 | unitRunningState); |
| 708 | internalSet = false; |
| 709 | setUSBCodeUpdateState(unitEnabledState); |
| 710 | saveUSBCodeUpdateStateToFile(unitMaskedState, |
| 711 | unitEnabledState); |
| 712 | res = req; |
| 713 | return 1; |
| 714 | } |
| 715 | #endif |
| 716 | if (req == res) |
| 717 | { |
| 718 | return 1; |
| 719 | } |
| 720 | if (updateInProgress) |
| 721 | { |
| 722 | return 0; |
| 723 | } |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 724 | if (unitMaskedState) |
| 725 | { // block updating if masked |
George Liu | cb267c8 | 2022-01-05 17:53:28 +0800 | [diff] [blame] | 726 | lg2::error("Invalid value specified"); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | } |
| 729 | unitRunningState = req; |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 730 | updatedFlag |= |
| 731 | (1 << static_cast<uint8_t>(UpdatedProp::runningState)); |
| 732 | startServiceRestartTimer(); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 733 | } |
Patrick Williams | de87972 | 2024-08-16 15:21:46 -0400 | [diff] [blame] | 734 | res = req; |
| 735 | return 1; |
| 736 | }); |
Vernon Mauery | ba2c083 | 2020-07-15 10:02:38 -0700 | [diff] [blame] | 737 | |
| 738 | srvCfgIface->initialize(); |
| 739 | if (!socketObjectPath.empty()) |
| 740 | { |
| 741 | sockAttrIface->initialize(); |
| 742 | } |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | } // namespace service |
| 747 | } // namespace phosphor |