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 19, 2024
1 parent 6fea9c9 commit d63c6b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion appsec/src/helper/network/acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ socket::ptr acceptor::accept()
int s = ::accept(sock_, reinterpret_cast<struct sockaddr *>(&addr), &len);
if (s == -1) {
if (errno == EINTR || errno == EAGAIN) {
throw dds::timeout_error();
return {};
}

throw std::system_error(errno, std::generic_category());
Expand Down
40 changes: 25 additions & 15 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,18 +89,15 @@ void runner::run()
{
try {
SPDLOG_INFO("Runner running");
unblock_sigusr1();
handle_sigusr1();

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

if (!socket) {
SPDLOG_CRITICAL("Acceptor returned invalid socket. Bug.");
break;
continue; // interrupted / timeout
}

if (interrupted()) {
Expand Down

0 comments on commit d63c6b3

Please sign in to comment.