From 9869f6dc5af85caf2e0fd4c56713a7f2049ecfff Mon Sep 17 00:00:00 2001 From: Marc Hoersken Date: Sat, 11 Apr 2020 08:35:05 +0200 Subject: tests/server: move all signal handling routines to util.[ch] Avoid code duplication to prepare for portability enhancements. --- tests/server/rtspd.c | 136 +--------------------------------------------- tests/server/sockfilt.c | 136 +--------------------------------------------- tests/server/socksd.c | 121 +---------------------------------------- tests/server/sws.c | 136 +--------------------------------------------- tests/server/tftpd.c | 128 +------------------------------------------- tests/server/util.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/server/util.h | 9 ++++ 7 files changed, 158 insertions(+), 647 deletions(-) (limited to 'tests') diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 7563fd22a..d3f0fe2f5 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -201,138 +201,6 @@ static const char *doc404_RTSP = "RTSP/1.0 404 Not Found\r\n" #define RTP_DATA_SIZE 12 static const char *RTP_DATA = "$_1234\n\0asdf"; -/* 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 RETSIGTYPE (*SIGHANDLER_T)(int); - -#ifdef SIGHUP -static SIGHANDLER_T old_sighup_handler = SIG_ERR; -#endif - -#ifdef SIGPIPE -static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; -#endif - -#ifdef SIGALRM -static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; -#endif - -#ifdef SIGINT -static SIGHANDLER_T old_sigint_handler = SIG_ERR; -#endif - -#ifdef SIGTERM -static SIGHANDLER_T old_sigterm_handler = SIG_ERR; -#endif - -#if defined(SIGBREAK) && defined(WIN32) -static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; -#endif - -/* var which if set indicates that the program should finish execution */ - -SIG_ATOMIC_T got_exit_signal = 0; - -/* if next is set indicates the first signal handled in exit_signal_handler */ - -static volatile int exit_signal = 0; - -/* signal handler that will be triggered to indicate that the program - should finish its execution in a controlled manner as soon as possible. - The first time this is called it will set got_exit_signal to one and - store in exit_signal the signal that triggered its execution. */ - -static RETSIGTYPE exit_signal_handler(int signum) -{ - int old_errno = errno; - if(got_exit_signal == 0) { - got_exit_signal = 1; - exit_signal = signum; - } - (void)signal(signum, exit_signal_handler); - errno = old_errno; -} - -static void install_signal_handlers(void) -{ -#ifdef SIGHUP - /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); - 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); - if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); -#endif -#ifdef SIGALRM - /* ignore SIGALRM signal */ - old_sigalrm_handler = signal(SIGALRM, SIG_IGN); - if(old_sigalrm_handler == SIG_ERR) - logmsg("cannot install SIGALRM handler: %s", strerror(errno)); -#endif -#ifdef SIGINT - /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); - 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); - 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); - if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); -#endif -} - -static void restore_signal_handlers(void) -{ -#ifdef SIGHUP - if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); -#endif -#ifdef SIGPIPE - if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); -#endif -#ifdef SIGALRM - if(SIG_ERR != old_sigalrm_handler) - (void)signal(SIGALRM, old_sigalrm_handler); -#endif -#ifdef SIGINT - if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); -#endif -#ifdef SIGTERM - if(SIG_ERR != old_sigterm_handler) - (void)signal(SIGTERM, old_sigterm_handler); -#endif -#if defined(SIGBREAK) && defined(WIN32) - if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); -#endif -} - static int ProcessRequest(struct httprequest *req) { char *line = &req->reqbuf[req->checkindex]; @@ -1275,7 +1143,7 @@ int main(int argc, char *argv[]) atexit(win32_cleanup); #endif - install_signal_handlers(); + install_signal_handlers(false); pid = (long)getpid(); @@ -1465,7 +1333,7 @@ server_cleanup: clear_advisor_read_lock(SERVERLOGS_LOCK); } - restore_signal_handlers(); + restore_signal_handlers(false); if(got_exit_signal) { logmsg("========> %s rtspd (port: %d pid: %ld) exits with signal (%d)", diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 83ba08a98..166b75b38 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -147,138 +147,6 @@ enum sockmode { ACTIVE_DISCONNECT /* as a client, disconnected from server */ }; -/* 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 RETSIGTYPE (*SIGHANDLER_T)(int); - -#ifdef SIGHUP -static SIGHANDLER_T old_sighup_handler = SIG_ERR; -#endif - -#ifdef SIGPIPE -static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; -#endif - -#ifdef SIGALRM -static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; -#endif - -#ifdef SIGINT -static SIGHANDLER_T old_sigint_handler = SIG_ERR; -#endif - -#ifdef SIGTERM -static SIGHANDLER_T old_sigterm_handler = SIG_ERR; -#endif - -#if defined(SIGBREAK) && defined(WIN32) -static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; -#endif - -/* var which if set indicates that the program should finish execution */ - -SIG_ATOMIC_T got_exit_signal = 0; - -/* if next is set indicates the first signal handled in exit_signal_handler */ - -static volatile int exit_signal = 0; - -/* signal handler that will be triggered to indicate that the program - should finish its execution in a controlled manner as soon as possible. - The first time this is called it will set got_exit_signal to one and - store in exit_signal the signal that triggered its execution. */ - -static RETSIGTYPE exit_signal_handler(int signum) -{ - int old_errno = errno; - if(got_exit_signal == 0) { - got_exit_signal = 1; - exit_signal = signum; - } - (void)signal(signum, exit_signal_handler); - errno = old_errno; -} - -static void install_signal_handlers(void) -{ -#ifdef SIGHUP - /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); - 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); - if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); -#endif -#ifdef SIGALRM - /* ignore SIGALRM signal */ - old_sigalrm_handler = signal(SIGALRM, SIG_IGN); - if(old_sigalrm_handler == SIG_ERR) - logmsg("cannot install SIGALRM handler: %s", strerror(errno)); -#endif -#ifdef SIGINT - /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); - 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); - 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); - if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); -#endif -} - -static void restore_signal_handlers(void) -{ -#ifdef SIGHUP - if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); -#endif -#ifdef SIGPIPE - if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); -#endif -#ifdef SIGALRM - if(SIG_ERR != old_sigalrm_handler) - (void)signal(SIGALRM, old_sigalrm_handler); -#endif -#ifdef SIGINT - if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); -#endif -#ifdef SIGTERM - if(SIG_ERR != old_sigterm_handler) - (void)signal(SIGTERM, old_sigterm_handler); -#endif -#if defined(SIGBREAK) && defined(WIN32) - if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); -#endif -} - #ifdef WIN32 /* * read-wrapper to support reading from stdin on Windows. @@ -1550,7 +1418,7 @@ int main(int argc, char *argv[]) setmode(fileno(stderr), O_BINARY); #endif - install_signal_handlers(); + install_signal_handlers(false); #ifdef ENABLE_IPV6 if(!use_ipv6) @@ -1647,7 +1515,7 @@ sockfilt_cleanup: if(wrotepidfile) unlink(pidname); - restore_signal_handlers(); + restore_signal_handlers(false); if(got_exit_signal) { logmsg("============> sockfilt exits with signal (%d)", exit_signal); diff --git a/tests/server/socksd.c b/tests/server/socksd.c index 5e32bc904..390ccaab0 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -230,123 +230,6 @@ static void getconfig(void) } } - -/* 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 RETSIGTYPE (*SIGHANDLER_T)(int); - -#ifdef SIGHUP -static SIGHANDLER_T old_sighup_handler = SIG_ERR; -#endif - -#ifdef SIGPIPE -static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; -#endif - -#ifdef SIGALRM -static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; -#endif - -#ifdef SIGINT -static SIGHANDLER_T old_sigint_handler = SIG_ERR; -#endif - -#if defined(SIGBREAK) && defined(WIN32) -static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; -#endif - -/* var which if set indicates that the program should finish execution */ - -SIG_ATOMIC_T got_exit_signal = 0; - -/* if next is set indicates the first signal handled in exit_signal_handler */ - -static volatile int exit_signal = 0; - -/* signal handler that will be triggered to indicate that the program - should finish its execution in a controlled manner as soon as possible. - The first time this is called it will set got_exit_signal to one and - store in exit_signal the signal that triggered its execution. */ - -static RETSIGTYPE exit_signal_handler(int signum) -{ - int old_errno = errno; - if(got_exit_signal == 0) { - got_exit_signal = 1; - exit_signal = signum; - } - (void)signal(signum, exit_signal_handler); - errno = old_errno; -} - -static void install_signal_handlers(void) -{ -#ifdef SIGHUP - /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); - 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); - if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); -#endif -#ifdef SIGALRM - /* ignore SIGALRM signal */ - old_sigalrm_handler = signal(SIGALRM, SIG_IGN); - if(old_sigalrm_handler == SIG_ERR) - logmsg("cannot install SIGALRM handler: %s", strerror(errno)); -#endif -#ifdef SIGINT - /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); - if(old_sigint_handler == SIG_ERR) - logmsg("cannot install SIGINT handler: %s", strerror(errno)); - else - siginterrupt(SIGINT, 1); -#endif -#if defined(SIGBREAK) && defined(WIN32) - /* handle SIGBREAK signal with our exit_signal_handler */ - old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler); - if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); -#endif -} - -static void restore_signal_handlers(void) -{ -#ifdef SIGHUP - if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); -#endif -#ifdef SIGPIPE - if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); -#endif -#ifdef SIGALRM - if(SIG_ERR != old_sigalrm_handler) - (void)signal(SIGALRM, old_sigalrm_handler); -#endif -#ifdef SIGINT - if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); -#endif -#if defined(SIGBREAK) && defined(WIN32) - if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); -#endif -} - static void loghex(unsigned char *buffer, ssize_t len) { char data[1200]; @@ -1095,7 +978,7 @@ int main(int argc, char *argv[]) setmode(fileno(stderr), O_BINARY); #endif - install_signal_handlers(); + install_signal_handlers(false); #ifdef ENABLE_IPV6 if(!use_ipv6) @@ -1145,7 +1028,7 @@ socks5_cleanup: if(wrotepidfile) unlink(pidname); - restore_signal_handlers(); + restore_signal_handlers(false); if(got_exit_signal) { logmsg("============> socksd exits with signal (%d)", exit_signal); diff --git a/tests/server/sws.c b/tests/server/sws.c index b397ed19b..427bd6e8d 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -213,141 +213,9 @@ static const char *doc404 = "HTTP/1.1 404 Not Found\r\n" "The requested URL was not found on this server.\n" "


" SWSVERSION "
\n" "\n"; -/* 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 RETSIGTYPE (*SIGHANDLER_T)(int); - -#ifdef SIGHUP -static SIGHANDLER_T old_sighup_handler = SIG_ERR; -#endif - -#ifdef SIGPIPE -static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; -#endif - -#ifdef SIGALRM -static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; -#endif - -#ifdef SIGINT -static SIGHANDLER_T old_sigint_handler = SIG_ERR; -#endif - -#ifdef SIGTERM -static SIGHANDLER_T old_sigterm_handler = SIG_ERR; -#endif - -#if defined(SIGBREAK) && defined(WIN32) -static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; -#endif - -/* var which if set indicates that the program should finish execution */ - -SIG_ATOMIC_T got_exit_signal = 0; - -/* if next is set indicates the first signal handled in exit_signal_handler */ - -static volatile int exit_signal = 0; - /* work around for handling trailing headers */ static int already_recv_zeroed_chunk = FALSE; -/* signal handler that will be triggered to indicate that the program - should finish its execution in a controlled manner as soon as possible. - The first time this is called it will set got_exit_signal to one and - store in exit_signal the signal that triggered its execution. */ - -static RETSIGTYPE exit_signal_handler(int signum) -{ - int old_errno = errno; - if(got_exit_signal == 0) { - got_exit_signal = 1; - exit_signal = signum; - } - (void)signal(signum, exit_signal_handler); - errno = old_errno; -} - -static void install_signal_handlers(void) -{ -#ifdef SIGHUP - /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); - 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); - if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); -#endif -#ifdef SIGALRM - /* ignore SIGALRM signal */ - old_sigalrm_handler = signal(SIGALRM, SIG_IGN); - if(old_sigalrm_handler == SIG_ERR) - logmsg("cannot install SIGALRM handler: %s", strerror(errno)); -#endif -#ifdef SIGINT - /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); - 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); - 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); - if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); -#endif -} - -static void restore_signal_handlers(void) -{ -#ifdef SIGHUP - if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); -#endif -#ifdef SIGPIPE - if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); -#endif -#ifdef SIGALRM - if(SIG_ERR != old_sigalrm_handler) - (void)signal(SIGALRM, old_sigalrm_handler); -#endif -#ifdef SIGINT - if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); -#endif -#ifdef SIGTERM - if(SIG_ERR != old_sigterm_handler) - (void)signal(SIGTERM, old_sigterm_handler); -#endif -#if defined(SIGBREAK) && defined(WIN32) - if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); -#endif -} - /* returns true if the current socket is an IP one */ static bool socket_domain_is_ip(void) { @@ -2111,7 +1979,7 @@ int main(int argc, char *argv[]) atexit(win32_cleanup); #endif - install_signal_handlers(); + install_signal_handlers(false); pid = (long)getpid(); @@ -2394,7 +2262,7 @@ sws_cleanup: clear_advisor_read_lock(SERVERLOGS_LOCK); } - restore_signal_handlers(); + restore_signal_handlers(false); if(got_exit_signal) { logmsg("========> %s sws (%s pid: %ld) exits with signal (%d)", diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index cac281c1e..0c94e1cb7 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -221,44 +221,6 @@ static sigjmp_buf timeoutbuf; static const unsigned int rexmtval = TIMEOUT; #endif -/* 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 RETSIGTYPE (*SIGHANDLER_T)(int); - -#ifdef SIGHUP -static SIGHANDLER_T old_sighup_handler = SIG_ERR; -#endif - -#ifdef SIGPIPE -static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; -#endif - -#ifdef SIGINT -static SIGHANDLER_T old_sigint_handler = SIG_ERR; -#endif - -#ifdef SIGTERM -static SIGHANDLER_T old_sigterm_handler = SIG_ERR; -#endif - -#if defined(SIGBREAK) && defined(WIN32) -static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; -#endif - -/* var which if set indicates that the program should finish execution */ - -SIG_ATOMIC_T got_exit_signal = 0; - -/* if next is set indicates the first signal handled in exit_signal_handler */ - -static volatile int exit_signal = 0; - /***************************************************************************** * FUNCTION PROTOTYPES * *****************************************************************************/ @@ -295,12 +257,6 @@ static void justtimeout(int signum); #endif /* HAVE_ALARM && SIGALRM */ -static RETSIGTYPE exit_signal_handler(int signum); - -static void install_signal_handlers(void); - -static void restore_signal_handlers(void); - /***************************************************************************** * FUNCTION IMPLEMENTATIONS * *****************************************************************************/ @@ -348,86 +304,6 @@ static void justtimeout(int signum) #endif /* HAVE_ALARM && SIGALRM */ -/* signal handler that will be triggered to indicate that the program - should finish its execution in a controlled manner as soon as possible. - The first time this is called it will set got_exit_signal to one and - store in exit_signal the signal that triggered its execution. */ - -static RETSIGTYPE exit_signal_handler(int signum) -{ - int old_errno = errno; - if(got_exit_signal == 0) { - got_exit_signal = 1; - exit_signal = signum; - } - (void)signal(signum, exit_signal_handler); - errno = old_errno; -} - -static void install_signal_handlers(void) -{ -#ifdef SIGHUP - /* ignore SIGHUP signal */ - old_sighup_handler = signal(SIGHUP, SIG_IGN); - 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); - if(old_sigpipe_handler == SIG_ERR) - logmsg("cannot install SIGPIPE handler: %s", strerror(errno)); -#endif -#ifdef SIGINT - /* handle SIGINT signal with our exit_signal_handler */ - old_sigint_handler = signal(SIGINT, exit_signal_handler); - 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); - 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); - if(old_sigbreak_handler == SIG_ERR) - logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); - else - siginterrupt(SIGBREAK, 1); -#endif -} - -static void restore_signal_handlers(void) -{ -#ifdef SIGHUP - if(SIG_ERR != old_sighup_handler) - (void)signal(SIGHUP, old_sighup_handler); -#endif -#ifdef SIGPIPE - if(SIG_ERR != old_sigpipe_handler) - (void)signal(SIGPIPE, old_sigpipe_handler); -#endif -#ifdef SIGINT - if(SIG_ERR != old_sigint_handler) - (void)signal(SIGINT, old_sigint_handler); -#endif -#ifdef SIGTERM - if(SIG_ERR != old_sigterm_handler) - (void)signal(SIGTERM, old_sigterm_handler); -#endif -#if defined(SIGBREAK) && defined(WIN32) - if(SIG_ERR != old_sigbreak_handler) - (void)signal(SIGBREAK, old_sigbreak_handler); -#endif -} - /* * init for either read-ahead or write-behind. * zero for write-behind, one for read-head. @@ -757,7 +633,7 @@ int main(int argc, char **argv) atexit(win32_cleanup); #endif - install_signal_handlers(); + install_signal_handlers(true); pid = (long)getpid(); @@ -930,7 +806,7 @@ tftpd_cleanup: clear_advisor_read_lock(SERVERLOGS_LOCK); } - restore_signal_handlers(); + restore_signal_handlers(true); if(got_exit_signal) { logmsg("========> %s tftpd (port: %d pid: %ld) exits with signal (%d)", diff --git a/tests/server/util.c b/tests/server/util.c index 24f9497ae..5dd4e0a7f 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -509,3 +509,142 @@ long timediff(struct timeval newer, struct timeval older) return (long)(newer.tv_sec-older.tv_sec)*1000+ (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 RETSIGTYPE (*SIGHANDLER_T)(int); + +#ifdef SIGHUP +static SIGHANDLER_T old_sighup_handler = SIG_ERR; +#endif + +#ifdef SIGPIPE +static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; +#endif + +#ifdef SIGALRM +static SIGHANDLER_T old_sigalrm_handler = SIG_ERR; +#endif + +#ifdef SIGINT +static SIGHANDLER_T old_sigint_handler = SIG_ERR; +#endif + +#ifdef SIGTERM +static SIGHANDLER_T old_sigterm_handler = SIG_ERR; +#endif + +#if defined(SIGBREAK) && defined(WIN32) +static SIGHANDLER_T old_sigbreak_handler = SIG_ERR; +#endif + +/* var which if set indicates that the program should finish execution */ +volatile int got_exit_signal = 0; + +/* if next is set indicates the first signal handled in exit_signal_handler */ +volatile int exit_signal = 0; + +/* signal handler that will be triggered to indicate that the program + * should finish its execution in a controlled manner as soon as possible. + * The first time this is called it will set got_exit_signal to one and + * store in exit_signal the signal that triggered its execution. + */ +static RETSIGTYPE exit_signal_handler(int signum) +{ + int old_errno = errno; + logmsg("exit_signal_handler: %d", signum); + if(got_exit_signal == 0) { + got_exit_signal = 1; + exit_signal = signum; + } + (void)signal(signum, exit_signal_handler); + errno = old_errno; +} + +void install_signal_handlers(bool keep_sigalrm) +{ +#ifdef SIGHUP + /* ignore SIGHUP signal */ + old_sighup_handler = signal(SIGHUP, SIG_IGN); + 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); + 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); + if(old_sigalrm_handler == SIG_ERR) + logmsg("cannot install SIGALRM handler: %s", strerror(errno)); + } +#else + (void)keep_sigalrm; +#endif +#ifdef SIGINT + /* handle SIGINT signal with our exit_signal_handler */ + old_sigint_handler = signal(SIGINT, exit_signal_handler); + 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); + 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); + if(old_sigbreak_handler == SIG_ERR) + logmsg("cannot install SIGBREAK handler: %s", strerror(errno)); + else + siginterrupt(SIGBREAK, 1); +#endif +} + +void restore_signal_handlers(bool keep_sigalrm) +{ +#ifdef SIGHUP + if(SIG_ERR != old_sighup_handler) + (void)signal(SIGHUP, old_sighup_handler); +#endif +#ifdef SIGPIPE + if(SIG_ERR != old_sigpipe_handler) + (void)signal(SIGPIPE, old_sigpipe_handler); +#endif +#ifdef SIGALRM + if(!keep_sigalrm) { + if(SIG_ERR != old_sigalrm_handler) + (void)signal(SIGALRM, old_sigalrm_handler); + } +#else + (void)keep_sigalrm; +#endif +#ifdef SIGINT + if(SIG_ERR != old_sigint_handler) + (void)signal(SIGINT, old_sigint_handler); +#endif +#ifdef SIGTERM + if(SIG_ERR != old_sigterm_handler) + (void)signal(SIGTERM, old_sigterm_handler); +#endif +#if defined(SIGBREAK) && defined(WIN32) + if(SIG_ERR != old_sigbreak_handler) + (void)signal(SIGBREAK, old_sigbreak_handler); +#endif +} diff --git a/tests/server/util.h b/tests/server/util.h index 0ae61aee5..0446b6a84 100644 --- a/tests/server/util.h +++ b/tests/server/util.h @@ -66,4 +66,13 @@ void clear_advisor_read_lock(const char *filename); int strncasecompare(const char *first, const char *second, size_t max); +/* global variable which if set indicates that the program should finish */ +extern volatile int got_exit_signal; + +/* global variable which if set indicates the first signal handled */ +extern volatile int exit_signal; + +void install_signal_handlers(bool keep_sigalrm); +void restore_signal_handlers(bool keep_sigalrm); + #endif /* HEADER_CURL_SERVER_UTIL_H */ -- cgit v1.2.3