summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndriy Byzhynar <abyzhynar@luxoft.com>2018-07-28 20:36:10 +0300
committerAlexander <akutsan@luxoft.com>2018-08-21 12:30:03 +0300
commit6829d454e3c8fa8b42a9c8a7dc824d38f4ad3dd6 (patch)
tree87b4d155825618d5a2e6eb5f9112a12f97027211
parent79b4ac8e5659afe08cbc5a7e322a7621119664c6 (diff)
downloadsdl_core-6829d454e3c8fa8b42a9c8a7dc824d38f4ad3dd6.tar.gz
Implement Low Voltage Handling in separate process
Implemented Low Voltage functionality handling in separate process in order to have possibility to suspend entire SDL process in case of Low Voltage signal After receipt of WakeUp signal SDL process is beeing waken up and resumes all its activities.
-rw-r--r--src/appMain/life_cycle_impl.cc2
-rw-r--r--src/appMain/low_voltage_signals_handler.cc128
-rw-r--r--src/appMain/low_voltage_signals_handler.h23
-rw-r--r--src/appMain/main.cc4
-rw-r--r--src/appMain/test/CMakeLists.txt44
-rw-r--r--src/appMain/test/low_voltage_signals_handler_test.cc125
-rw-r--r--src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/hmi_notifications_test.cc4
-rw-r--r--src/components/config_profile/src/profile.cc2
-rw-r--r--src/components/utils/include/utils/signals.h55
-rw-r--r--src/components/utils/src/signals_posix.cc29
10 files changed, 143 insertions, 273 deletions
diff --git a/src/appMain/life_cycle_impl.cc b/src/appMain/life_cycle_impl.cc
index d211771a39..186be3c829 100644
--- a/src/appMain/life_cycle_impl.cc
+++ b/src/appMain/life_cycle_impl.cc
@@ -237,7 +237,7 @@ void LifeCycleImpl::Run() {
LOG4CXX_AUTO_TRACE(logger_);
// Register signal handlers and wait sys signals
// from OS
- if (!utils::WaitTerminationSignals(&sig_handler)) {
+ if (!utils::Signals::WaitTerminationSignals(&sig_handler)) {
LOG4CXX_FATAL(logger_, "Fail to catch system signal!");
}
}
diff --git a/src/appMain/low_voltage_signals_handler.cc b/src/appMain/low_voltage_signals_handler.cc
index b514ac6d55..936d91ba18 100644
--- a/src/appMain/low_voltage_signals_handler.cc
+++ b/src/appMain/low_voltage_signals_handler.cc
@@ -33,7 +33,13 @@
#include "appMain/low_voltage_signals_handler.h"
#include <signal.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <iostream>
#include "appMain/life_cycle.h"
+#include "utils/signals.h"
#include "utils/logger.h"
#include "utils/typed_enum_print.h"
#include "config_profile/profile.h"
@@ -44,19 +50,21 @@ CREATE_LOGGERPTR_GLOBAL(logger_, "LowVoltageSignalsHandler")
LowVoltageSignalsHandler::LowVoltageSignalsHandler(
LifeCycle& life_cycle, const LowVoltageSignalsOffset& offset_data)
- : state_(SDLState::kRun)
- , notifications_delegate_(new NotificationThreadDelegate(*this))
+ : notifications_delegate_(new NotificationThreadDelegate(*this))
, signals_handler_thread_(threads::CreateThread(
"LV_SIGNALS_HANDLER_THREAD", notifications_delegate_.get()))
, life_cycle_(life_cycle)
, SIGLOWVOLTAGE_(offset_data.low_voltage_signal_offset + SIGRTMIN)
, SIGWAKEUP_(offset_data.wake_up_signal_offset + SIGRTMIN)
- , SIGIGNOFF_(offset_data.ignition_off_signal_offset + SIGRTMIN) {
+ , SIGIGNOFF_(offset_data.ignition_off_signal_offset + SIGRTMIN)
+ , cpid_(-1) {
+ sigemptyset(&lv_mask_);
+ sigaddset(&lv_mask_, SIGLOWVOLTAGE_);
signals_handler_thread_->start();
}
-SDLState LowVoltageSignalsHandler::get_current_sdl_state() const {
- return state_;
+sigset_t LowVoltageSignalsHandler::LowVoltageSignalsMask() const {
+ return lv_mask_;
}
int LowVoltageSignalsHandler::low_voltage_signo() const {
@@ -72,7 +80,6 @@ int LowVoltageSignalsHandler::ignition_off_signo() const {
}
void LowVoltageSignalsHandler::Destroy() {
- state_ = SDLState::kStop;
if (signals_handler_thread_) {
signals_handler_thread_->join();
}
@@ -85,87 +92,60 @@ LowVoltageSignalsHandler::~LowVoltageSignalsHandler() {
}
void LowVoltageSignalsHandler::HandleSignal(const int signo) {
- using utils::operator<<;
- LOG4CXX_DEBUG(logger_, "Received Signal: " << signo);
- LOG4CXX_DEBUG(logger_, "Current state is : " << get_current_sdl_state());
+ if (SIGLOWVOLTAGE_ == signo) {
+ LOG4CXX_DEBUG(logger_, "Received LOW_VOLTAGE signal");
- auto handle_run_state = [this, signo]() {
+ life_cycle_.LowVoltage();
+ cpid_ = utils::Signals::Fork();
- if (SIGLOWVOLTAGE_ == signo) {
- LOG4CXX_DEBUG(logger_, "Received LOW_VOLTAGE signal");
- state_ = SDLState::kSleep;
- life_cycle_.LowVoltage();
- return;
+ if (0 > cpid_) {
+ LOG4CXX_FATAL(logger_,
+ "Error due fork() call. Error: " << strerror(errno));
+ utils::Signals::ExitProcess(EXIT_FAILURE);
}
- if (SIGIGNOFF_ == signo) {
- LOG4CXX_DEBUG(logger_,
- "Received IGNITION_OFF signal. But SDL is in active state");
- // Do nothing
- return;
- }
-
- if (SIGWAKEUP_ == signo) {
- LOG4CXX_DEBUG(logger_,
- "Received WAKE_UP signal. But SDL is in active state");
- // Do nothing
- return;
- }
- LOG4CXX_DEBUG(logger_, "Received UNKNOWN signal");
- };
-
- auto handle_sleep_state = [this, signo]() {
-
- if (SIGWAKEUP_ == signo) {
- LOG4CXX_DEBUG(logger_, "Received WAKE UP signal");
- state_ = SDLState::kRun;
+ if (0 != cpid_) {
+ // In Parent process
+ LOG4CXX_DEBUG(logger_, "Child PID: " << cpid_);
+ utils::Signals::WaitPid(cpid_, nullptr, 0);
+ LOG4CXX_DEBUG(logger_, "Child process: " << cpid_ << " is stopped");
life_cycle_.WakeUp();
- return;
- }
-
- if (SIGIGNOFF_ == signo) {
- LOG4CXX_DEBUG(logger_, "Received IGNITION_OFF signal");
- state_ = SDLState::kStop;
- life_cycle_.IgnitionOff();
- return;
+ } else {
+ // In Child process
+ sigset_t signal_set;
+ sigfillset(&signal_set);
+ pthread_sigmask(SIG_BLOCK, &signal_set, nullptr);
+ sigemptyset(&lv_mask_);
+ sigaddset(&lv_mask_, SIGWAKEUP_);
+ sigaddset(&lv_mask_, SIGIGNOFF_);
+ std::cout << "Stopping parent process: " << getppid() << std::endl;
+ utils::Signals::SendSignal(SIGSTOP, getppid());
+ std::cout << "SIGSTOP signal sent to " << getppid() << std::endl;
}
+ return;
+ }
- if (SIGLOWVOLTAGE_ == signo) {
- LOG4CXX_DEBUG(
- logger_,
- "Received LOW VOLTAGE signal. But SDL is already in sleep state");
- // Do nothing
- return;
- }
- LOG4CXX_DEBUG(logger_, "Received UNKNOWN signal");
- };
-
- switch (state_) {
- case SDLState::kRun:
- handle_run_state();
- break;
- case SDLState::kSleep:
- handle_sleep_state();
- break;
- case SDLState::kStop: /* nothing to do here */
- LOG4CXX_DEBUG(logger_, "SDL is in stopping state");
- break;
+ if (SIGWAKEUP_ == signo) {
+ std::cout << "Received WAKE UP signal" << std::endl;
+ std::cout << "Waking Up parent process: " << getppid() << std::endl;
+ utils::Signals::SendSignal(SIGCONT, getppid());
+ std::cout << "Stopping child process: " << getpid() << std::endl;
+ utils::Signals::ExitProcess(0);
}
-}
-bool LowVoltageSignalsHandler::IsActive() const {
- return SDLState::kStop != state_;
+ if (SIGIGNOFF_ == signo) {
+ std::cout << "Received IGNITION_OFF signal" << std::endl;
+ std::cout << "Stopping all SDL processes..." << std::endl;
+ utils::Signals::SendSignal(SIGKILL, getppid());
+ utils::Signals::ExitProcess(0);
+ }
}
void NotificationThreadDelegate::threadMain() {
- sigset_t lv_mask;
- sigemptyset(&lv_mask);
- sigaddset(&lv_mask, low_voltage_signals_handler_.low_voltage_signo());
- sigaddset(&lv_mask, low_voltage_signals_handler_.wake_up_signo());
- sigaddset(&lv_mask, low_voltage_signals_handler_.ignition_off_signo());
-
- while (low_voltage_signals_handler_.IsActive()) {
+ while (true) {
int signo = 0;
+ const sigset_t lv_mask =
+ low_voltage_signals_handler_.LowVoltageSignalsMask();
const int err = sigwait(&lv_mask, &signo);
if (0 != err) {
LOG4CXX_ERROR(
diff --git a/src/appMain/low_voltage_signals_handler.h b/src/appMain/low_voltage_signals_handler.h
index 4b2902659e..8c139c6836 100644
--- a/src/appMain/low_voltage_signals_handler.h
+++ b/src/appMain/low_voltage_signals_handler.h
@@ -50,14 +50,6 @@ class LifeCycle;
class NotificationThreadDelegate;
/**
- * @brief The SDLState enum defines current Smartdevicelink app state
- * which can be changed due to received UNIX RT signals dedicated for
- * Low Voltage functionality handling
- * e.g. LowVoltage, WakeUp, IgnitionOff signals
- */
-enum class SDLState { kRun, kSleep, kStop };
-
-/**
* @brief Class which handles real-time POSIX signals
* dedicated for LOW VOLTAGE functionality
*/
@@ -74,19 +66,15 @@ class LowVoltageSignalsHandler {
const LowVoltageSignalsOffset& offset_data);
/**
* @brief Handles RT signals related to Low Voltage functionality
+ * @param signal number to handle
*/
void HandleSignal(const int signo);
/**
- * @brief Checks if SDL is in active state
- * therefore not yet in sleep state due to Low Voltage event
- */
- bool IsActive() const;
-
- /**
- * @brief Returns current LOW VOLTAGE SDL state
+ * @brief Returns signals mask required for handling
+ * LOW VOLTAGE functionality
*/
- SDLState get_current_sdl_state() const;
+ sigset_t LowVoltageSignalsMask() const;
/**
* @brief Returns LOW VOLTAGE signal number
@@ -114,13 +102,14 @@ class LowVoltageSignalsHandler {
* Invoked from destructor
*/
void Destroy();
- SDLState state_;
std::unique_ptr<NotificationThreadDelegate> notifications_delegate_;
threads::Thread* signals_handler_thread_;
LifeCycle& life_cycle_;
int SIGLOWVOLTAGE_;
int SIGWAKEUP_;
int SIGIGNOFF_;
+ pid_t cpid_;
+ sigset_t lv_mask_;
};
class NotificationThreadDelegate : public threads::ThreadDelegate {
diff --git a/src/appMain/main.cc b/src/appMain/main.cc
index d1f0594daf..752cfb7bb9 100644
--- a/src/appMain/main.cc
+++ b/src/appMain/main.cc
@@ -96,7 +96,7 @@ bool InitHmi(std::string hmi_link) {
*/
int32_t main(int32_t argc, char** argv) {
// Unsubscribe once for all threads
- if (!utils::UnsubscribeFromTermination()) {
+ if (!utils::Signals::UnsubscribeFromTermination()) {
// Can't use internal logger here
exit(EXIT_FAILURE);
}
@@ -124,7 +124,7 @@ int32_t main(int32_t argc, char** argv) {
// except specific thread dedicated for
// Low Voltage signals handling
// Thread will be created later
- if (!utils::UnsubscribeFromLowVoltageSignals(signals_offset)) {
+ if (!utils::Signals::UnsubscribeFromLowVoltageSignals(signals_offset)) {
// Can't use internal logger here
exit(EXIT_FAILURE);
}
diff --git a/src/appMain/test/CMakeLists.txt b/src/appMain/test/CMakeLists.txt
deleted file mode 100644
index c90cad203b..0000000000
--- a/src/appMain/test/CMakeLists.txt
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright (c) 2018, Ford Motor Company
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
-#
-# Redistributions of source code must retain the above copyright notice, this
-# list of conditions and the following disclaimer.
-#
-# Redistributions in binary form must reproduce the above copyright notice,
-# this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided with the
-# distribution.
-#
-# Neither the name of the Ford Motor Company nor the names of its contributors
-# may be used to endorse or promote products derived from this software
-# without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
-
-include_directories (
- ${GMOCK_INCLUDE_DIRECTORY}
-)
-
-set(testSources
- $<TARGET_OBJECTS:LowVoltageHandlerObjLibrary>
- ${CMAKE_SOURCE_DIR}/src/appMain/test/low_voltage_signals_handler_test.cc
-)
-
-set(LIBRARIES
- gmock
-)
-
-create_test(low_voltage_signals_handler_test "${testSources}" "${LIBRARIES}")
diff --git a/src/appMain/test/low_voltage_signals_handler_test.cc b/src/appMain/test/low_voltage_signals_handler_test.cc
deleted file mode 100644
index c6be92e558..0000000000
--- a/src/appMain/test/low_voltage_signals_handler_test.cc
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
-* Copyright (c) 2018, Ford Motor Company
-* All rights reserved.
-*
-* Redistribution and use in source and binary forms, with or without
-* modification, are permitted provided that the following conditions are met:
-*
-* Redistributions of source code must retain the above copyright notice, this
-* list of conditions and the following disclaimer.
-*
-* Redistributions in binary form must reproduce the above copyright notice,
-* this list of conditions and the following
-* disclaimer in the documentation and/or other materials provided with the
-* distribution.
-*
-* Neither the name of the Ford Motor Company nor the names of its contributors
-* may be used to endorse or promote products derived from this software
-* without specific prior written permission.
-*
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-* POSSIBILITY OF SUCH DAMAGE.
-*/
-
-#include "appMain/low_voltage_signals_handler.h"
-
-#include <memory>
-#include "gtest/gtest.h"
-#include "appMain/test/mock_life_cycle.h"
-#include "config_profile/profile.h"
-#include "utils/macro.h"
-
-namespace test {
-
-class LowVoltageSignalsHandlerTest : public ::testing::Test {
- protected:
- void SetUp() OVERRIDE {
- profile_.set_config_file_name("smartDeviceLink.ini");
- signals_offset_ = {profile_.low_voltage_signal_offset(),
- profile_.wake_up_signal_offset(),
- profile_.ignition_off_signal_offset()};
-
- low_voltage_signals_handler_ =
- std::unique_ptr<main_namespace::LowVoltageSignalsHandler>(
- new main_namespace::LowVoltageSignalsHandler(mock_life_cycle_,
- signals_offset_));
- }
-
- profile::Profile profile_;
- main_namespace::LowVoltageSignalsOffset signals_offset_;
- std::unique_ptr<main_namespace::LowVoltageSignalsHandler>
- low_voltage_signals_handler_;
- main_namespace::MockLifeCycle mock_life_cycle_;
-};
-
-TEST_F(LowVoltageSignalsHandlerTest,
- LowVoltageSignalReceived_ExpectLifeCycleLowVoltageCall) {
- // Check that initial SDL state is running
- EXPECT_EQ(main_namespace::SDLState::kRun,
- low_voltage_signals_handler_->get_current_sdl_state());
- // Set expectation after LOW VOLTAGE signal
- EXPECT_CALL(mock_life_cycle_, LowVoltage());
- const int low_voltage_signo =
- low_voltage_signals_handler_->low_voltage_signo();
- // Emulate LOW VOLTAGE signal receipt and handling
- low_voltage_signals_handler_->HandleSignal(low_voltage_signo);
- // Check that SDL is in sleep state after LOW VOLTAGE
- EXPECT_EQ(main_namespace::SDLState::kSleep,
- low_voltage_signals_handler_->get_current_sdl_state());
-}
-
-TEST_F(LowVoltageSignalsHandlerTest,
- WakeUpSignalReceived_ExpectLifeCycleWakeUpCall) {
- // Check that initial SDL state is running
- EXPECT_EQ(main_namespace::SDLState::kRun,
- low_voltage_signals_handler_->get_current_sdl_state());
- EXPECT_CALL(mock_life_cycle_, LowVoltage());
- const int low_voltage_signo =
- low_voltage_signals_handler_->low_voltage_signo();
- // Emulate LOW VOLTAGE signals receipt and handling
- low_voltage_signals_handler_->HandleSignal(low_voltage_signo);
- // Check that SDL is in sleep state after LOW VOLTAGE
- EXPECT_EQ(main_namespace::SDLState::kSleep,
- low_voltage_signals_handler_->get_current_sdl_state());
- EXPECT_CALL(mock_life_cycle_, WakeUp());
- const int wake_up_signo = low_voltage_signals_handler_->wake_up_signo();
- // Emulate WAKE UP signal receipt and handling
- low_voltage_signals_handler_->HandleSignal(wake_up_signo);
- // Check that SDL is in running state after WAKE UP
- EXPECT_EQ(main_namespace::SDLState::kRun,
- low_voltage_signals_handler_->get_current_sdl_state());
-}
-
-TEST_F(LowVoltageSignalsHandlerTest,
- IgnitionOffSignalReceived_ExpectLifeCycleIgnitionOffCall) {
- // Check that initial SDL state is running
- EXPECT_EQ(main_namespace::SDLState::kRun,
- low_voltage_signals_handler_->get_current_sdl_state());
- EXPECT_CALL(mock_life_cycle_, LowVoltage());
- const int low_voltage_signo =
- low_voltage_signals_handler_->low_voltage_signo();
- // Emulate LOW VOLTAGE signals receipt and handling
- low_voltage_signals_handler_->HandleSignal(low_voltage_signo);
- // Check that SDL is in sleep state after LOW VOLTAGE
- EXPECT_EQ(main_namespace::SDLState::kSleep,
- low_voltage_signals_handler_->get_current_sdl_state());
- EXPECT_CALL(mock_life_cycle_, IgnitionOff());
- const int ignition_off_signo =
- low_voltage_signals_handler_->ignition_off_signo();
-
- // Emulate IGNITION OFF signal receipt and handling
- low_voltage_signals_handler_->HandleSignal(ignition_off_signo);
- // Check that SDL is in stopped state after IGNITION OFF
- EXPECT_EQ(main_namespace::SDLState::kStop,
- low_voltage_signals_handler_->get_current_sdl_state());
-}
-} // namespace test
diff --git a/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/hmi_notifications_test.cc b/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/hmi_notifications_test.cc
index dd51078614..fe82b16f3f 100644
--- a/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/hmi_notifications_test.cc
+++ b/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/test/commands/hmi/hmi_notifications_test.cc
@@ -872,7 +872,7 @@ TEST_F(HMICommandsNotificationsTest,
SubscribeForSignal();
command->Run();
- utils::WaitTerminationSignals(sig_handler);
+ utils::Signals::WaitTerminationSignals(sig_handler);
EXPECT_EQ(am::mobile_api::AppInterfaceUnregisteredReason::IGNITION_OFF,
mob_reason);
@@ -914,7 +914,7 @@ TEST_F(HMICommandsNotificationsTest,
SubscribeForSignal();
command->Run();
- utils::WaitTerminationSignals(sig_handler);
+ utils::Signals::WaitTerminationSignals(sig_handler);
#endif
}
}
diff --git a/src/components/config_profile/src/profile.cc b/src/components/config_profile/src/profile.cc
index d869d6b65e..3db4dda816 100644
--- a/src/components/config_profile/src/profile.cc
+++ b/src/components/config_profile/src/profile.cc
@@ -2169,7 +2169,7 @@ void Profile::UpdateValues() {
LOG_UPDATED_VALUE(
ignition_off_signal_offset_, kIgnitionOffSignalOffsetKey, kMainSection);
- ReadBoolValue(&multiple_transports_enabled_,
+ ReadBoolValue(&multiple_transports_enabled_,
kDefaultMultipleTransportsEnabled,
kMultipleTransportsSection,
kMultipleTransportsEnabledKey);
diff --git a/src/components/utils/include/utils/signals.h b/src/components/utils/include/utils/signals.h
index 4efdaaa4e8..bda83f315f 100644
--- a/src/components/utils/include/utils/signals.h
+++ b/src/components/utils/include/utils/signals.h
@@ -42,10 +42,57 @@ typedef void (*sighandler_t)(int);
namespace utils {
-bool UnsubscribeFromTermination();
-bool WaitTerminationSignals(sighandler_t sig_handler);
-bool UnsubscribeFromLowVoltageSignals(
- const main_namespace::LowVoltageSignalsOffset& offset_data);
+class Signals {
+ public:
+ /**
+ * @brief Unsubscribe thread from termination signals SIGINT and SIGTERM
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromTermination();
+
+ /**
+ * @brief Triggers thread to wait for termination signals SIGINT and SIGTERM
+ * @param sig_handler - handler to work with signals specidied above
+ * @return True if handler handles signals successfuly, otherwise false
+ */
+ static bool WaitTerminationSignals(sighandler_t sig_handler);
+
+ /**
+ * @brief Unsubscribe thread from low voltage signals
+ * SIGLOWVOLTAGE, SIGWAKEUP and SIGIGNOFF
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromLowVoltageSignals(
+ const main_namespace::LowVoltageSignalsOffset& offset_data);
+
+ /**
+ * @brief Sends signal to specified process
+ * @param signal to send
+ * @param destination process signal to be sent to
+ */
+ static void SendSignal(const int signo, const pid_t pid);
+
+ /**
+ * @brief Creates child process
+ * @return created process id or -1 in case of error
+ */
+ static pid_t Fork();
+
+ /**
+ * @brief Wait for child process to be terminated
+ * @param cpid - process to wait for termination
+ * @param status store status information in the int to which it points
+ * @param options - options for exit form function
+ * detailed options can be found here: https://linux.die.net/man/2/waitpid
+ */
+ static void WaitPid(pid_t cpid, int* status, int options);
+
+ /**
+ * @brief Exits for process
+ * @param status - exit status code
+ */
+ static void ExitProcess(const int status);
+};
} // namespace utils
diff --git a/src/components/utils/src/signals_posix.cc b/src/components/utils/src/signals_posix.cc
index dd569a96d6..e13dc04f40 100644
--- a/src/components/utils/src/signals_posix.cc
+++ b/src/components/utils/src/signals_posix.cc
@@ -29,13 +29,16 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <sys/wait.h>
#include <csignal>
#include <cstdlib>
#include <stdint.h>
+#include <iostream>
#include "utils/signals.h"
-bool utils::UnsubscribeFromTermination() {
+namespace utils {
+bool Signals::UnsubscribeFromTermination() {
// Disable some system signals receiving in thread
// by blocking those signals
// (system signals processes only in the main thread)
@@ -51,7 +54,7 @@ bool utils::UnsubscribeFromTermination() {
return !pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
}
-bool utils::UnsubscribeFromLowVoltageSignals(
+bool Signals::UnsubscribeFromLowVoltageSignals(
const main_namespace::LowVoltageSignalsOffset& offset_data) {
// Disable Low Voltage signals in main thread
// due to all further threads will inherit signals mask
@@ -68,6 +71,25 @@ bool utils::UnsubscribeFromLowVoltageSignals(
return !pthread_sigmask(SIG_BLOCK, &signal_set, nullptr);
}
+void Signals::SendSignal(const int signo, const pid_t pid) {
+ if (kill(pid, signo) == -1) {
+ std::cerr << "Error sending signal: " << strsignal(signo) << " to " << pid
+ << " .Error: " << strerror(errno) << std::endl;
+ }
+}
+
+pid_t Signals::Fork() {
+ return fork();
+}
+
+void Signals::ExitProcess(const int status) {
+ exit(status);
+}
+
+void Signals::WaitPid(pid_t cpid, int* status, int options) {
+ waitpid(cpid, status, options);
+}
+
namespace {
bool CatchSIGSEGV(sighandler_t handler) {
struct sigaction act;
@@ -80,7 +102,7 @@ bool CatchSIGSEGV(sighandler_t handler) {
}
} // namespace
-bool utils::WaitTerminationSignals(sighandler_t sig_handler) {
+bool Signals::WaitTerminationSignals(sighandler_t sig_handler) {
sigset_t signal_set;
int sig = -1;
@@ -98,3 +120,4 @@ bool utils::WaitTerminationSignals(sighandler_t sig_handler) {
}
return false;
}
+} // namespace utils