diff --git a/score/launch_manager/src/daemon/src/control/control_client_channel.cpp b/score/launch_manager/src/daemon/src/control/control_client_channel.cpp index fd9f1d57e..37dadadb8 100644 --- a/score/launch_manager/src/daemon/src/control/control_client_channel.cpp +++ b/score/launch_manager/src/daemon/src/control/control_client_channel.cpp @@ -81,6 +81,8 @@ bool ControlClientChannel::getResponse(ControlClientMessage& msg) LM_LOG_DEBUG() << "Response retrieved."; } + nudgeControlClientHandler(); + return result; } @@ -89,25 +91,7 @@ void ControlClientChannel::sendRequest(ControlClientMessage& msg) request_.msg_ = msg; request_.empty_ = false; - // now map the semaphore and post on it - // Attempt to map the semaphore - auto* nudgeLM = mmap( - NULL, sizeof(osal::Semaphore), PROT_WRITE, MAP_SHARED, osal::IpcCommsSync::control_client_handler_nudge_fd, 0); - - // RULECHECKER_comment(1, 1, check_c_style_cast, "This is the definition provided by the OS and does a C-style - // cast.", true) - if (nudgeLM != MAP_FAILED) - { - LM_LOG_DEBUG() << "Request sent. Waiting for acknowledgment..."; - auto* semaphore = static_cast(nudgeLM); - - // coverity[cert_mem52_cpp_violation:FALSE] The allocated memory is checked by the containing if statement. - const auto result = semaphore->post(); - SCORE_LANGUAGE_FUTURECPP_ASSERT_MESSAGE(result == osal::OsalReturnType::kSuccess, - "ControlClientChannel semaphore post failed"); - - munmap(nudgeLM, sizeof(osal::Semaphore)); // Unmap the semaphore - } + nudgeControlClientHandler(); const auto result = nudge_LM_Handler_.wait(); SCORE_LANGUAGE_FUTURECPP_ASSERT_MESSAGE(result == osal::OsalReturnType::kSuccess, @@ -196,6 +180,9 @@ ControlClientChannelP ControlClientChannel::initializeControlClientChannel(int f lock.unlock(); init_cv_.notify_all(); } + + loadControlNudge(); + return result; } @@ -260,6 +247,29 @@ void ControlClientChannel::releaseParentMapping() ipc_parent_.reset(); } +bool ControlClientChannel::loadControlNudge() { + if (nudgeControlClientHandler_ != nullptr) { + LM_LOG_ERROR() << "Semaphore was already mapped!"; + return false; + } + + void* nudgeBuf = mmap( + NULL, sizeof(osal::Semaphore), PROT_WRITE, MAP_SHARED, + osal::IpcCommsSync::control_client_handler_nudge_fd, 0); + + // RULECHECKER_comment(1, 1, check_c_style_cast, "This is the definition provided by the OS and does a C-style cast.", true) + if (nudgeBuf == MAP_FAILED) + { + LM_LOG_ERROR() << "mmap of nudge semaphore failed in initializeControlClientChannel:" + << std::strerror(errno); + return false; + } + + nudgeControlClientHandler_ = static_cast(nudgeBuf); + return true; +} + + std::string_view ControlClientChannel::toString(ControlClientCode code) { for (const auto& mapping : stateArray) diff --git a/score/launch_manager/src/daemon/src/control/control_client_channel.hpp b/score/launch_manager/src/daemon/src/control/control_client_channel.hpp index 5d289683e..9b164ff0c 100644 --- a/score/launch_manager/src/daemon/src/control/control_client_channel.hpp +++ b/score/launch_manager/src/daemon/src/control/control_client_channel.hpp @@ -253,6 +253,9 @@ class ControlClientChannel final { private: + /// @brief loads the control channel's nudge semaphore. + static bool loadControlNudge(); + /// @brief Ensure that the ControlClientChannel was setup properly before /// accessing it static bool is_initialized_; diff --git a/score/launch_manager/src/daemon/src/process_group_manager/details/process_group_manager.cpp b/score/launch_manager/src/daemon/src/process_group_manager/details/process_group_manager.cpp index 40c94ba54..6fb3c8a1d 100644 --- a/score/launch_manager/src/daemon/src/process_group_manager/details/process_group_manager.cpp +++ b/score/launch_manager/src/daemon/src/process_group_manager/details/process_group_manager.cpp @@ -30,6 +30,7 @@ static std::atomic_bool em_cancelled{false}; static void my_signal_handler(int) { em_cancelled.store(true); + ControlClientChannel::nudgeControlClientHandler(); } void ProcessGroupManager::cancel() @@ -142,16 +143,13 @@ inline bool ProcessGroupManager::initializeControlClientHandler() // The name is removed from the file system after creation, memory // is mapped and a pointer stored, the FD is kept open. ControlClientChannel::nudgeControlClientHandler_ = nullptr; - char shm_name[static_cast(score::lcm::internal::ProcessLimits::maxLocalBuffSize)]; + constexpr static std::string_view shm_name{"/_nudge~._.~me_"}; - static_cast(snprintf(shm_name, - static_cast(score::lcm::internal::ProcessLimits::maxLocalBuffSize), - "/_nudge~._.~me_")); // random name - int fd = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, 0U); + int fd = shm_open(shm_name.data(), O_CREAT | O_EXCL | O_RDWR, 0U); if (fd >= 0) { - shm_unlink(shm_name); + shm_unlink(shm_name.data()); if (0 == ftruncate(fd, static_cast(sizeof(osal::Semaphore)))) { @@ -464,10 +462,6 @@ bool ProcessGroupManager::sendResponse(ControlClientMessage msg) << static_cast(msg.request_or_response_) << ") re state" << msg.process_group_state_.pg_state_name_ << "of PG" << msg.process_group_state_.pg_name_; ret = scc->sendResponse(msg); - if (!ret) - { - ControlClientChannel::nudgeControlClientHandler(); - } } }