Skip to content

Commit

Permalink
CA-407106: Avoid using deprecated sigset and sigignore functions
Browse files Browse the repository at this point in the history
Use `sigaction` and `sigprocmask` instead.
There is also `signal`, but the manpage says to not use it.
Indeed `sigaction` has more reliable semantics by default in POSIX (e.g. it blocks the signal while you are handling it,
so you don't reenter it while it is executing).

Unfortunately the new functions are more verbose, so introduce a loop for setting up the signal handlers.

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Mar 5, 2025
1 parent ae28dcc commit cf3c6d0
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions daemon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ main(
// logrotate may send SIGHUP regardless of the state of xha.
//

sigignore(SIGHUP);
struct sigaction ignore = { 0 };
ignore.sa_handler = SIG_IGN;
sigaction(SIGHUP, &ignore, NULL);

// Initialize ha_config in BSS.
// (static initialization causes a warning for unknown reason)
Expand Down Expand Up @@ -315,7 +317,7 @@ main(

for (sig = 1; sig < _NSIG; sig++)
{
sigignore(sig);
sigaction(sig, &ignore, NULL);
}

// #### Set resource limit of core size to max value
Expand Down Expand Up @@ -420,18 +422,19 @@ main(
//
// sigcatch -
//
// signal handler for sigset().
// signal handler for sigaction().
//
// sigcatch may be invoked in any thread context in any time.
// Acquiring any lock which does not support recursive in this function causes deadlock.
//
// action.sa_flags is 0, which means that the signal that triggerred the handler
// is automatically blocked already
//

MTC_STATIC void
sigcatch(
int signo)
{
sigset(signo, sigcatch);

switch (signo)
{
case SIGTERM:
Expand Down Expand Up @@ -491,10 +494,21 @@ post_phase_init(
}

// Now it's time to catch signals.
static const int signals[] = { SIGTERM, SIGCHLD, SIGHUP };
sigset_t mask;
unsigned i;
struct sigaction action = { 0 };
action.sa_handler = sigcatch;
sigemptyset(&mask);

for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) {
int sig = signals[i];
sigaddset(&mask, sig);

sigaction(sig, &action, NULL);
}

sigset(SIGTERM, sigcatch);
sigset(SIGCHLD, sigcatch);
sigset(SIGHUP, sigcatch);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
}

Expand Down

0 comments on commit cf3c6d0

Please sign in to comment.