Skip to content

Commit

Permalink
unblock SIGUSR1 only around accept()
Browse files Browse the repository at this point in the history
  • Loading branch information
cataphract committed Sep 17, 2024
1 parent 6fea9c9 commit bc83e5c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions appsec/src/helper/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ network::base_acceptor::ptr acceptor_from_config(const config::config &cfg)
return std::make_unique<network::local::acceptor>(sock_path);
}

void unblock_sigusr1()
void block_sigusr1()
{
// the signal handler need not do anything (just interrupt accept())
struct sigaction alarmer = {};
alarmer.sa_handler = [](int) {};
if (sigaction(SIGUSR1, &alarmer, nullptr) < 0) {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
if (pthread_sigmask(SIG_UNBLOCK, &mask, nullptr) != 0) {
throw std::runtime_error{
"Failed to set SIGUSR1 handler: errno " + std::to_string(errno)};
"Failed to block SIGUSR1: errno " + std::to_string(errno)};
}
}

void unblock_sigusr1()
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
Expand All @@ -53,6 +56,16 @@ void unblock_sigusr1()
}
}

void handle_sigusr1()
{
// the signal handler need not do anything (just interrupt accept())
struct sigaction alarmer = {};
alarmer.sa_handler = [](int) {};
if (sigaction(SIGUSR1, &alarmer, nullptr) < 0) {
throw std::runtime_error{
"Failed to set SIGUSR1 handler: errno " + std::to_string(errno)};
}
}
} // namespace

runner::runner(const config::config &cfg, std::atomic<bool> &interrupted)
Expand All @@ -76,12 +89,16 @@ void runner::run()
{
try {
SPDLOG_INFO("Runner running");
unblock_sigusr1();
handle_sigusr1();

while (!interrupted()) {
network::base_socket::ptr socket;
try {
unblock_sigusr1();
socket = acceptor_->accept();
block_sigusr1();
} catch (const timeout_error &e) {
block_sigusr1();
continue;
}

Expand Down

0 comments on commit bc83e5c

Please sign in to comment.