Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Enhance signal handling #1635

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ AX_CREATE_STDINT_H([eggint.h])

# Checks for functions and their arguments.
AC_CHECK_FUNCS([clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton memset_s snprintf strlcpy vsnprintf])
AC_CHECK_DECL([SA_RESETHAND], [], [], [[#include <signal.h>]])
AC_FUNC_SELECT_ARGTYPES
EGG_FUNC_B64_NTOP
AC_FUNC_MMAP
Expand Down
1 change: 0 additions & 1 deletion src/bg.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#include "main.h"
#include <signal.h>
#include "bg.h"

extern char pid_file[];
Expand Down
53 changes: 24 additions & 29 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ int notify_users_at = 0; /* Minutes past the hour to notify users of notes? */
char version[128]; /* 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 @@ -168,7 +169,7 @@ void fatal(const char *s, int recoverable)

putlog(LOG_MISC, "*", "* %s", s);
for (i = 0; i < dcc_total; i++)
if (dcc[i].sock >= 0)
if (dcc[i].sock >= 0 && dcc[i].sock != STDOUT)
killsock(dcc[i].sock);
#ifdef TLS
ssl_cleanup();
Expand Down Expand Up @@ -340,24 +341,14 @@ static void got_bus(int z)
{
write_debug();
fatal("BUS ERROR -- CRASHING!", 1);
#ifdef SA_RESETHAND
kill(getpid(), SIGBUS);
#else
bg_send_quit(BG_ABORT);
exit(1);
#endif
}

static void got_segv(int z)
{
write_debug();
fatal("SEGMENT VIOLATION -- CRASHING!", 1);
#ifdef SA_RESETHAND
kill(getpid(), SIGSEGV);
#else
bg_send_quit(BG_ABORT);
exit(1);
#endif
}

static void got_fpe(int z)
Expand All @@ -378,21 +369,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 @@ -416,6 +398,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 @@ -759,6 +759,7 @@ static void mainloop(int toplevel)
cleanup--;

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

Expand Down Expand Up @@ -1000,17 +1001,11 @@ int main(int arg_c, char **arg_v)
/* Set up error traps: */
sv.sa_handler = got_bus;
sigemptyset(&sv.sa_mask);
#ifdef SA_RESETHAND
sv.sa_flags = SA_RESETHAND;
#else
sv.sa_flags = 0;
#endif
sigaction(SIGBUS, &sv, NULL);
sv.sa_handler = got_segv;
sigaction(SIGSEGV, &sv, NULL);
#ifdef SA_RESETHAND
sv.sa_flags = 0;
#endif
sv.sa_handler = got_fpe;
sigaction(SIGFPE, &sv, NULL);
sv.sa_handler = got_term;
Expand Down
3 changes: 1 addition & 2 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
maxfd = maxfd_e;

/* select() may modify the timeval argument - copy it */
t.tv_sec = td->blocktime.tv_sec;
t.tv_usec = td->blocktime.tv_usec;
t = td->blocktime;

call_hook(HOOK_PRE_SELECT);
x = select((SELECT_TYPE_ARG1) maxfd + 1,
Expand Down