From 3fb6e5a01001b8c7dfdc33a89041178aac381a27 Mon Sep 17 00:00:00 2001 From: Patrick Monnerat Date: Tue, 26 Jan 2021 03:10:48 +0100 Subject: [PATCH] test server: take care of siginterrupt() deprecation Closes #6529 --- tests/server/util.c | 66 ++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/tests/server/util.c b/tests/server/util.c index c4f16e451..7fc547068 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -535,12 +535,6 @@ long timediff(struct timeval newer, struct timeval older) (long)(newer.tv_usec-older.tv_usec)/1000; } -/* do-nothing macro replacement for systems which lack siginterrupt() */ - -#ifndef HAVE_SIGINTERRUPT -#define siginterrupt(x,y) do {} while(0) -#endif - /* vars used to keep around previous signal handlers */ typedef void (*SIGHANDLER_T)(int); @@ -721,6 +715,36 @@ static DWORD WINAPI main_window_loop(LPVOID lpParameter) } #endif +static SIGHANDLER_T set_signal(int signum, SIGHANDLER_T handler, + bool restartable) +{ +#if defined(HAVE_SIGACTION) && defined(SA_RESTART) + struct sigaction sa, oldsa; + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = handler; + sigemptyset(&sa.sa_mask); + sigaddset(&sa.sa_mask, signum); + sa.sa_flags = restartable? SA_RESTART: 0; + + if(sigaction(signum, &sa, &oldsa)) + return SIG_ERR; + + return oldsa.sa_handler; +#else + SIGHANDLER_T oldhdlr = signal(signum, handler); + +#ifdef HAVE_SIGINTERRUPT + if(oldhdlr != SIG_ERR) + siginterrupt(signum, (int) restartable); +#else + (void) restartable; +#endif + + return oldhdlr; +#endif +} + void install_signal_handlers(bool keep_sigalrm) { #ifdef WIN32 @@ -731,20 +755,20 @@ void install_signal_handlers(bool keep_sigalrm) #endif #ifdef SIGHUP /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); + old_sighup_handler = set_signal(SIGHUP, SIG_IGN, FALSE); if(old_sighup_handler == SIG_ERR) logmsg("cannot install SIGHUP handler: %s", strerror(errno)); #endif #ifdef SIGPIPE /* ignore SIGPIPE signal */ - old_sigpipe_handler = signal(SIGPIPE, SIG_IGN); + old_sigpipe_handler = set_signal(SIGPIPE, SIG_IGN, FALSE); if(old_sigpipe_handler == SIG_ERR) logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); #endif #ifdef SIGALRM if(!keep_sigalrm) { /* ignore SIGALRM signal */ - old_sigalrm_handler = signal(SIGALRM, SIG_IGN); + old_sigalrm_handler = set_signal(SIGALRM, SIG_IGN, FALSE); if(old_sigalrm_handler == SIG_ERR) logmsg("cannot install SIGALRM handler: %s", strerror(errno)); } @@ -753,27 +777,21 @@ void install_signal_handlers(bool keep_sigalrm) #endif #ifdef SIGINT /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); + old_sigint_handler = set_signal(SIGINT, exit_signal_handler, TRUE); if(old_sigint_handler == SIG_ERR) logmsg("cannot install SIGINT handler: %s", strerror(errno)); - else - siginterrupt(SIGINT, 1); #endif #ifdef SIGTERM /* handle SIGTERM signal with our exit_signal_handler */ - old_sigterm_handler = signal(SIGTERM, exit_signal_handler); + old_sigterm_handler = set_signal(SIGTERM, exit_signal_handler, TRUE); if(old_sigterm_handler == SIG_ERR) logmsg("cannot install SIGTERM handler: %s", strerror(errno)); - else - siginterrupt(SIGTERM, 1); #endif #if defined(SIGBREAK) && defined(WIN32) /* handle SIGBREAK signal with our exit_signal_handler */ - old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler); + old_sigbreak_handler = set_signal(SIGBREAK, exit_signal_handler, TRUE); if(old_sigbreak_handler == SIG_ERR) logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); #endif #ifdef WIN32 if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE)) @@ -791,31 +809,31 @@ void restore_signal_handlers(bool keep_sigalrm) { #ifdef SIGHUP if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); + (void) set_signal(SIGHUP, old_sighup_handler, FALSE); #endif #ifdef SIGPIPE if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); + (void) set_signal(SIGPIPE, old_sigpipe_handler, FALSE); #endif #ifdef SIGALRM if(!keep_sigalrm) { if(SIG_ERR != old_sigalrm_handler) - (void)signal(SIGALRM, old_sigalrm_handler); + (void) set_signal(SIGALRM, old_sigalrm_handler, FALSE); } #else (void)keep_sigalrm; #endif #ifdef SIGINT if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); + (void) set_signal(SIGINT, old_sigint_handler, FALSE); #endif #ifdef SIGTERM if(SIG_ERR != old_sigterm_handler) - (void)signal(SIGTERM, old_sigterm_handler); + (void) set_signal(SIGTERM, old_sigterm_handler, FALSE); #endif #if defined(SIGBREAK) && defined(WIN32) if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); + (void) set_signal(SIGBREAK, old_sigbreak_handler, FALSE); #endif #ifdef WIN32 (void)SetConsoleCtrlHandler(ctrl_event_handler, FALSE);