From 22d936bf6b72cae350bd255f2e3d9a0b21ae948c Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Mon, 12 Feb 2024 19:20:01 +0100 Subject: [PATCH] iox-#2185 Use 'iox1' prefix for the ipc channels --- .../internal/runtime/ipc_interface_base.hpp | 6 +++ .../source/runtime/ipc_interface_base.cpp | 41 +++++++++++++++---- .../source/runtime/ipc_interface_creator.cpp | 11 ++--- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/internal/runtime/ipc_interface_base.hpp b/iceoryx_posh/include/iceoryx_posh/internal/runtime/ipc_interface_base.hpp index cd6c83b1ed..f43ff9ec15 100644 --- a/iceoryx_posh/include/iceoryx_posh/internal/runtime/ipc_interface_base.hpp +++ b/iceoryx_posh/include/iceoryx_posh/internal/runtime/ipc_interface_base.hpp @@ -141,6 +141,11 @@ IpcMessageErrorType stringToIpcMessageErrorType(const char* str) noexcept; /// @param[in] msg enum value to convert std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept; +/// @brief Transforms an IPC channel name to a prefixed interface name +/// @param[in] channelName the name of the channel without the 'iox1_#_' prefix +/// @return the interface name with the 'iox1_#_' prefix or a 'nullopt' if the resulting name would be too long +iox::optional ipcChannelNameToInterfaceName(RuntimeName_t channelName); + class IpcInterfaceUser; class IpcInterfaceCreator; @@ -263,6 +268,7 @@ class IpcInterface bool hasClosableIpcChannel() const noexcept; protected: + RuntimeName_t m_interfaceName; RuntimeName_t m_runtimeName; uint64_t m_maxMessageSize{0U}; uint64_t m_maxMessages{0U}; diff --git a/iceoryx_posh/source/runtime/ipc_interface_base.cpp b/iceoryx_posh/source/runtime/ipc_interface_base.cpp index c07fb8057c..e27b929dab 100644 --- a/iceoryx_posh/source/runtime/ipc_interface_base.cpp +++ b/iceoryx_posh/source/runtime/ipc_interface_base.cpp @@ -77,11 +77,34 @@ std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept return convert::toString(static_cast(msg)); } +iox::optional ipcChannelNameToInterfaceName(RuntimeName_t channelName) +{ + iox::string<1> uniqueRoudiIdString{TruncateToCapacity, + iox::convert::toString(roudi::DEFAULT_UNIQUE_ROUDI_ID).c_str()}; + RuntimeName_t interfaceName = concatenate(ICEORYX_RESOURCE_PREFIX, "_", uniqueRoudiIdString, "_"); + if (interfaceName.size() + channelName.size() > RuntimeName_t::capacity()) + { + return nullopt; + } + interfaceName.append(TruncateToCapacity, channelName); + return interfaceName; +} + template IpcInterface::IpcInterface(const RuntimeName_t& runtimeName, const uint64_t maxMessages, const uint64_t messageSize) noexcept - : m_runtimeName(runtimeName) + : m_interfaceName( + ipcChannelNameToInterfaceName(runtimeName) + .or_else([&runtimeName] { + IOX_LOG(FATAL, + "The runtime with the name '" + << runtimeName.size() + << "' would exceed the maximum allowed size when used with the 'iox1_#_' prefix!"); + IOX_PANIC("The runtime name exceeds the max size"); + }) + .value()) + , m_runtimeName(runtimeName) { m_maxMessages = maxMessages; m_maxMessageSize = messageSize; @@ -99,7 +122,8 @@ bool IpcInterface::receive(IpcMessage& answer) const noexcept { if (!m_ipcChannel.has_value()) { - IOX_LOG(WARN, "Trying to receive data on an non-initialized IPC interface! Interface name: " << m_runtimeName); + IOX_LOG(WARN, + "Trying to receive data on an non-initialized IPC interface! Interface name: " << m_interfaceName); return false; } @@ -117,7 +141,8 @@ bool IpcInterface::timedReceive(const units::Duration timeout, I { if (!m_ipcChannel.has_value()) { - IOX_LOG(WARN, "Trying to receive data on an non-initialized IPC interface! Interface name: " << m_runtimeName); + IOX_LOG(WARN, + "Trying to receive data on an non-initialized IPC interface! Interface name: " << m_interfaceName); return false; } @@ -146,7 +171,7 @@ bool IpcInterface::send(const IpcMessage& msg) const noexcept { if (!m_ipcChannel.has_value()) { - IOX_LOG(WARN, "Trying to send data on an non-initialized IPC interface! Interface name: " << m_runtimeName); + IOX_LOG(WARN, "Trying to send data on an non-initialized IPC interface! Interface name: " << m_interfaceName); return false; } @@ -173,7 +198,7 @@ bool IpcInterface::timedSend(const IpcMessage& msg, units::Durat { if (!m_ipcChannel.has_value()) { - IOX_LOG(WARN, "Trying to send data on an non-initialized IPC interface! Interface name: " << m_runtimeName); + IOX_LOG(WARN, "Trying to send data on an non-initialized IPC interface! Interface name: " << m_interfaceName); return false; } @@ -214,7 +239,7 @@ bool IpcInterface::openIpcChannel(const PosixIpcChannelSide chan using IpcChannelBuilder_t = typename IpcChannelType::Builder_t; IpcChannelBuilder_t() - .name(m_runtimeName) + .name(m_interfaceName) .channelSide(m_channelSide) .maxMsgSize(m_maxMessageSize) .maxMsgNumber(m_maxMessages) @@ -224,7 +249,7 @@ bool IpcInterface::openIpcChannel(const PosixIpcChannelSide chan if (this->m_channelSide == PosixIpcChannelSide::SERVER) { IOX_LOG(ERROR, - "Unable to create ipc channel '" << this->m_runtimeName + "Unable to create ipc channel '" << this->m_interfaceName << "'. Error code: " << static_cast(err)); } else @@ -233,7 +258,7 @@ bool IpcInterface::openIpcChannel(const PosixIpcChannelSide chan // therefore resulting in a wall of error messages on the console which leads to missing the important // one that roudi is not running if this would be LogLevel::ERROR instead of LogLevel::TRACE IOX_LOG(TRACE, - "Unable to open ipc channel '" << this->m_runtimeName + "Unable to open ipc channel '" << this->m_interfaceName << "'. Error code: " << static_cast(err)); } }); diff --git a/iceoryx_posh/source/runtime/ipc_interface_creator.cpp b/iceoryx_posh/source/runtime/ipc_interface_creator.cpp index 8cfa1c8980..d3c24aa381 100644 --- a/iceoryx_posh/source/runtime/ipc_interface_creator.cpp +++ b/iceoryx_posh/source/runtime/ipc_interface_creator.cpp @@ -28,21 +28,22 @@ IpcInterfaceCreator::IpcInterfaceCreator(const RuntimeName_t& runtimeName, const uint64_t messageSize) noexcept : IpcInterfaceBase(runtimeName, maxMessages, messageSize) , m_fileLock(std::move(FileLockBuilder() - .name(runtimeName) + .name(m_interfaceName) .permission(iox::perms::owner_read | iox::perms::owner_write) .create() - .or_else([&runtimeName](auto& error) { + .or_else([this](auto& error) { if (error == FileLockError::LOCKED_BY_OTHER_PROCESS) { IOX_LOG(FATAL, - "An application with the name " << runtimeName + "An application with the name " << m_runtimeName << " is still running. Using the " "same name twice is not supported."); IOX_REPORT_FATAL(PoshError::IPC_INTERFACE__APP_WITH_SAME_NAME_STILL_RUNNING); } else { - IOX_LOG(FATAL, "Error occurred while acquiring file lock named " << runtimeName); + IOX_LOG(FATAL, + "Error occurred while acquiring file lock named " << m_interfaceName); IOX_REPORT_FATAL(PoshError::IPC_INTERFACE__COULD_NOT_ACQUIRE_FILE_LOCK); } }) @@ -50,7 +51,7 @@ IpcInterfaceCreator::IpcInterfaceCreator(const RuntimeName_t& runtimeName, { // check if the IPC channel is still there (e.g. because of no proper termination // of the process) - cleanupOutdatedIpcChannel(runtimeName); + cleanupOutdatedIpcChannel(m_interfaceName); openIpcChannel(PosixIpcChannelSide::SERVER); }