Skip to content

Commit

Permalink
iox-#2330 Add sendSDNotifySignalHelper to ServiceManagement
Browse files Browse the repository at this point in the history
Introduced a new virtual function enable sending notifications  sd_notify
  • Loading branch information
khromenokroman committed Sep 1, 2024
1 parent 999a603 commit d8829c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
45 changes: 17 additions & 28 deletions iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ class ServiceManagement
static constexpr const uint16_t SIZE_STRING = 4096; ///< maximum size of string // 2
static constexpr const uint8_t SIZE_THREAD_NAME = 15; ///< max size for thread name // 1

/// dbus signal handler
virtual void processNotify() = 0;
/// Sets a shutdown flag
virtual void shutdown() = 0;
/// Sets a thread name
virtual bool setThreadNameHelper(iox::string<SIZE_THREAD_NAME>& threadName) = 0;
/// Get environment variable
virtual std::string getEnvironmentVariable(const char* const env_var) = 0;

virtual void processNotify() = 0; /// dbus signal handler
virtual void shutdown() = 0; /// Sets a shutdown flag
virtual bool setThreadNameHelper(iox::string<SIZE_THREAD_NAME>& threadName) = 0; /// Sets a thread name
virtual std::string getEnvironmentVariable(const char* const env_var) = 0; /// Get environment variable
virtual bool sendSDNotifySignalHelper(const std::string_view state) = 0; /// Send notify

protected:
ServiceManagement() = default;
Expand Down Expand Up @@ -137,31 +135,13 @@ class ServiceManagementSystemd final : public ServiceManagement
**/
bool setThreadNameHelper(iox::string<SIZE_THREAD_NAME>& threadName) final;

#ifdef USE_SYSTEMD
/**
* @brief Helper function to send SDNotify signals
* @param state SDNotify state to be sent
* @return True if signal sending is successful, otherwise false
**/
static bool sendSDNotifySignalHelper(const std::string_view state)
{
auto result = IOX_POSIX_CALL(sd_notify)(0, state.data()).successReturnValue(1).evaluate();
if (result.has_error())
{
IOX_LOG(ERROR,
"Failed to send " << state.data()
<< " signal. Error: " << result.get_error().getHumanReadableErrnum());
return false;
}
return true;
}
#else
static bool sendSDNotifySignalHelper([[maybe_unused]] const std::string_view state)
{
// empty implementation
return true;
}
#endif
bool sendSDNotifySignalHelper(const std::string_view state);

/**
* @brief Function to manage the watchdog loop
**/
Expand Down Expand Up @@ -224,6 +204,15 @@ class NoServiceManagementSystemd final : public ServiceManagement
// empty implementation
return true;
}

/**
* @brief Empty implementation send SDNotify signals
**/
bool sendSDNotifySignalHelper([[maybe_unused]] const std::string_view state) final
{
// empty implementation
return true;
}
};
} // namespace service_management

Expand Down
12 changes: 12 additions & 0 deletions iceoryx_posh/source/roudi/roudi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,18 @@ void ServiceManagementSystemd::processNotify()
}
}

bool ServiceManagementSystemd::sendSDNotifySignalHelper(const std::string_view state)
{
auto result = IOX_POSIX_CALL(sd_notify)(0, state.data()).successReturnValue(1).evaluate();
if (result.has_error())
{
IOX_LOG(ERROR,
"Failed to send " << state.data() << " signal. Error: " << result.get_error().getHumanReadableErrnum());
return false;
}
return true;
}

} // namespace service_management
} // namespace roudi
} // namespace iox

0 comments on commit d8829c4

Please sign in to comment.