Avoid some potential race conditions by blocking the signals we manage

They are atomically unblocked in the ppoll/epoll_pwait calls, avoiding any
race condition on the check of the “stop” or “reload” booleans.
This commit is contained in:
Florent Le Coz
2015-05-27 23:44:23 +02:00
parent 54f96debca
commit 897b281e67
2 changed files with 12 additions and 2 deletions
+11 -1
View File
@@ -99,9 +99,19 @@ int main(int ac, char** av)
if (hostname.empty()) if (hostname.empty())
return config_help("hostname"); return config_help("hostname");
// Block the signals we want to manage. They will be unblocked only during
// the epoll_pwait or ppoll calls. This avoids some race conditions,
// explained in man 2 pselect on linux
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR1);
sigaddset(&mask, SIGUSR2);
sigprocmask(SIG_BLOCK, &mask, nullptr);
// Install the signals used to exit the process cleanly, or reload the // Install the signals used to exit the process cleanly, or reload the
// config // config
sigset_t mask;
sigemptyset(&mask); sigemptyset(&mask);
struct sigaction on_sigint; struct sigaction on_sigint;
on_sigint.sa_sigaction = &sigint_handler; on_sigint.sa_sigaction = &sigint_handler;