Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1030 De-register signal handler at the end of mai…
Browse files Browse the repository at this point in the history
…n in examples to prevent access to deinitialized resources
  • Loading branch information
elBoberido committed Sep 6, 2023
1 parent 3dd97a0 commit 589be6f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 8 deletions.
4 changes: 4 additions & 0 deletions iceoryx_examples/icediscovery/iox_wait_for_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ int main()
<< std::endl;
}

// de-register signal handler before destroying the Discovery
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

return (EXIT_SUCCESS);
}
15 changes: 10 additions & 5 deletions iceoryx_examples/request_response/client_cxx_waitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ void signalHandler(int)

int main()
{
auto sigTermGuard =
iox::posix::registerSignalHandler(iox::posix::Signal::TERM, signalHandler).expect("failed to register SIGTERM");
auto sigIntGuard =
iox::posix::registerSignalHandler(iox::posix::Signal::INT, signalHandler).expect("failed to register SIGINT");

//! [initialize runtime]
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
//! [initialize runtime]
Expand All @@ -63,6 +58,12 @@ int main()
//! [create waitset]
waitset.emplace();

// since the waitset is used in signal handler, this must happen after the waitset initialization
auto sigTermGuard =
iox::posix::registerSignalHandler(iox::posix::Signal::TERM, signalHandler).expect("failed to register SIGTERM");
auto sigIntGuard =
iox::posix::registerSignalHandler(iox::posix::Signal::INT, signalHandler).expect("failed to register SIGINT");

//! [create client]
iox::popo::ClientOptions options;
options.responseQueueCapacity = 2U;
Expand Down Expand Up @@ -131,6 +132,10 @@ int main()
}
//! [mainloop]

// de-register signal handler before destroying the waitset
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

waitset.reset();
std::cout << "shutting down" << std::endl;

Expand Down
10 changes: 7 additions & 3 deletions iceoryx_examples/request_response_in_c/client_c_waitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ void sigHandler(int signalValue)

int main(void)
{
signal(SIGINT, sigHandler);
signal(SIGTERM, sigHandler);

iox_runtime_init(APP_NAME);

iox_client_storage_t clientStorage;
Expand All @@ -72,6 +69,10 @@ int main(void)
}
//! [create waitset and attach client]

// since the waitset is used in signal handler, this must happen after the waitset initialization
signal(SIGINT, sigHandler);
signal(SIGTERM, sigHandler);

while (keepRunning)
{
struct AddRequest* request = NULL;
Expand Down Expand Up @@ -145,6 +146,9 @@ int main(void)
}

//! [cleanup]
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

iox_ws_detach_client_state(waitset, client, ClientState_HAS_RESPONSE);
iox_ws_deinit(waitset);
iox_client_deinit(client);
Expand Down
4 changes: 4 additions & 0 deletions iceoryx_examples/waitset/ice_waitset_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ int main()

std::cout << "shutting down" << std::endl;

// de-register signal handler before destroying the waitset
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

waitset.reset();
return (EXIT_SUCCESS);
}
3 changes: 3 additions & 0 deletions iceoryx_examples/waitset_in_c/ice_c_waitset_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ int main(void)
//! [event loop]

//! [cleanup all resources]
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

for (uint64_t i = 0U; i < NUMBER_OF_SUBSCRIBERS; ++i)
{
// not mandatory since iox_sub_deinit will detach the subscriber automatically
Expand Down
3 changes: 3 additions & 0 deletions iceoryx_examples/waitset_in_c/ice_c_waitset_grouping.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ int main(void)
//! [event loop]

//! [cleanup all resources]
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

for (uint64_t i = 0U; i < NUMBER_OF_SUBSCRIBERS; ++i)
{
iox_sub_deinit(subscriber[i]);
Expand Down
3 changes: 3 additions & 0 deletions iceoryx_examples/waitset_in_c/ice_c_waitset_individual.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ int main(void)
//! [event loop]

//! [cleanup all resources]
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

for (uint64_t i = 0U; i < NUMBER_OF_SUBSCRIBERS; ++i)
{
iox_sub_deinit(subscriber[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ int main(void)
//! [event loop]

//! [cleanup all resources]
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);

joinThread(cyclicTriggerThread);
iox_ws_deinit(waitSet);
iox_user_trigger_deinit(shutdownTrigger);
Expand Down

0 comments on commit 589be6f

Please sign in to comment.