Skip to content

Commit

Permalink
Make signal handling race safe again
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelortmann committed Jul 4, 2024
1 parent b02bd00 commit b2585a5
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ int notify_users_at = 0; /* Minutes past the hour to notify users of notes? */
char version[81]; /* Version info (long form) */
char ver[41]; /* Version info (short form) */

volatile sig_atomic_t do_restart = 0; /* .restart has been called, restart ASAP */
volatile sig_atomic_t sig_quit, sig_hup,
do_restart = 0; /* .restart has been called, restart ASAP */
int resolve_timeout = RES_TIMEOUT; /* Hostname/address lookup timeout */
char quit_msg[1024]; /* Quit message */

Expand Down Expand Up @@ -372,21 +373,12 @@ static void got_term(int z)

static void got_quit(int z)
{
if (check_tcl_signal("sigquit"))
return;
putlog(LOG_MISC, "*", "Received QUIT signal: restarting...");
do_restart = -1;
return;
sig_quit = 1;
}

static void got_hup(int z)
{
write_userfile(-1);
if (check_tcl_signal("sighup"))
return;
putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
do_restart = -2;
return;
sig_hup = 1;
}

/* A call to resolver (gethostbyname, etc) timed out
Expand All @@ -410,6 +402,24 @@ static void got_ill(int z)
#endif
}

static void check_signals() {
if (sig_quit) {
if (check_tcl_signal("sigquit"))
return;
putlog(LOG_MISC, "*", "Received QUIT signal: restarting...");
do_restart = -1;
sig_quit = 0;
}
if (sig_hup) {
write_userfile(-1);
if (check_tcl_signal("sighup"))
return;
putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
do_restart = -2;
sig_hup = 0;
}
}

#ifdef DEBUG_ASSERT
/* Called from the Assert macro.
*/
Expand Down Expand Up @@ -753,6 +763,7 @@ static void mainloop(int toplevel)
cleanup--;

xx = sockgets(buf, &i);
check_signals();
if (xx >= 0) { /* Non-error */
int idx;

Expand Down

0 comments on commit b2585a5

Please sign in to comment.