diff --git a/src/Makefile.am b/src/Makefile.am index 35b2a50b4..029cbada9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -35,6 +35,7 @@ zm_SOURCES = \ zm_mpeg.cpp \ zm_jpeg.c \ zm_regexp.cpp \ + zm_signal.cpp \ zm_buffer.cpp \ zm_debug.c @@ -71,6 +72,7 @@ noinst_HEADERS = \ zm_jpeg.h \ zm_mpeg.h \ zm_regexp.h \ + zm_signal.h \ zm_buffer.h EXTRA_DIST = \ diff --git a/src/zma.cpp b/src/zma.cpp index 7c2ccaaf9..59a041ca9 100644 --- a/src/zma.cpp +++ b/src/zma.cpp @@ -22,60 +22,9 @@ #include "zm.h" #include "zm_db.h" +#include "zm_signal.h" #include "zm_monitor.h" -void zm_die_handler( int signal ) -{ -#if HAVE_DECL_STRSIGNAL - char * error = strsignal(signal); - size_t errorStringSize = strlen(error) + strlen("Got signal (), crashing."); - char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char. - (void) snprintf(errorString, errorStringSize, "Got signal (%s), crashing.", error); - - Error(( (const char *)errorString )); - free(errorString); -#else /* HAVE_DECL_STRSIGNAL */ - Error(( "Got signal %d, crashing", signal )); -#endif /* HAVE_DECL_STRSIGNAL */ - exit( signal ); -} - -bool zma_terminate = false; - -void zm_term_handler( int signal ) -{ -#if HAVE_DECL_STRSIGNAL - char * error = strsignal(signal); - size_t errorStringSize = strlen(error) + strlen("Got signal (), exiting."); - char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char. - (void) snprintf(errorString, errorStringSize, "Got signal (%s), exiting.", error); - - Info(( (const char *)errorString )); - free(errorString); -#else /* HAVE_DECL_STRSIGNAL */ - Info(( "Got TERM signal, exiting" )); -#endif /* HAVE_DECL_STRSIGNAL */ - zma_terminate = true; -} - -bool zma_reload = false; - -void zm_hup_handler( int signal ) -{ -#if HAVE_DECL_STRSIGNAL - char * error = strsignal(signal); - size_t errorStringSize = strlen(error) + strlen("Got signal (), reloading."); - char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char. - (void) snprintf(errorString, errorStringSize, "Got signal (%s), reloading.", error); - - Info(( (const char *)errorString )); - free(errorString); -#else /* HAVE_DECL_STRSIGNAL */ - Info(( "Got HUP signal, reloading" )); -#endif /* HAVE_DECL_STRSIGNAL */ - zma_reload = true; -} - void Usage() { fprintf( stderr, "zma -m \n" ); @@ -154,27 +103,15 @@ int main( int argc, char *argv[] ) Event::OpenFrameSocket( monitor->Id() ); } + zmSetDefaultHupHandler(); + zmSetDefaultTermHandler(); + zmSetDefaultDieHandler(); + sigset_t block_set; sigemptyset( &block_set ); struct sigaction action, old_action; - action.sa_handler = zm_hup_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGHUP, &action, &old_action ); - - action.sa_handler = zm_term_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGTERM, &action, &old_action ); - - action.sa_handler = zm_die_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGBUS, &action, &old_action ); - sigaction( SIGSEGV, &action, &old_action ); - - while( !zma_terminate ) + while( !zm_terminate ) { // Process the next image sigprocmask( SIG_BLOCK, &block_set, 0 ); @@ -182,10 +119,10 @@ int main( int argc, char *argv[] ) { usleep( monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE ); } - if ( zma_reload ) + if ( zm_reload ) { monitor->Reload(); - zma_reload = false; + zm_reload = false; } sigprocmask( SIG_UNBLOCK, &block_set, 0 ); } diff --git a/src/zmc.cpp b/src/zmc.cpp index f6d1e498b..6e3c55ad4 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -23,16 +23,9 @@ #include "zm.h" #include "zm_db.h" +#include "zm_signal.h" #include "zm_monitor.h" -bool zmc_terminate = false; - -void zmc_term_handler( int /* signal */ ) -{ - Info(( "Got TERM signal, exiting" )); - zmc_terminate = true; -} - void Usage() { fprintf( stderr, "zmc -d or -H -P -p or -f or -m \n" ); @@ -188,15 +181,14 @@ int main( int argc, char *argv[] ) } Info(( "Starting Capture" )); + + zmSetDefaultTermHandler(); + zmSetDefaultDieHandler(); + sigset_t block_set; sigemptyset( &block_set ); struct sigaction action, old_action; - action.sa_handler = zmc_term_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGTERM, &action, &old_action ); - sigaddset( &block_set, SIGUSR1 ); sigaddset( &block_set, SIGUSR2 ); if ( device[0] && n_monitors == 1 ) @@ -220,7 +212,7 @@ int main( int argc, char *argv[] ) struct timeval now; struct DeltaTimeval delta_time; - while( !zmc_terminate ) + while( !zm_terminate ) { /* grab a new one */ sigprocmask( SIG_BLOCK, &block_set, 0 ); diff --git a/src/zmf.cpp b/src/zmf.cpp index 96f4822f9..adc2f41c8 100644 --- a/src/zmf.cpp +++ b/src/zmf.cpp @@ -33,43 +33,11 @@ #include "zm.h" #include "zm_db.h" -//#include "zm_debug.h" +#include "zm_signal.h" #include "zm_monitor.h" #include "zmf.h" -void zm_die_handler( int signal ) -{ -#if HAVE_DECL_STRSIGNAL - char * error = strsignal(signal); - size_t errorStringSize = strlen(error) + strlen("Got signal (), crashing."); - char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char. - (void) snprintf(errorString, errorStringSize, "Got signal (%s), crashing.", error); - - Error(( (const char *)errorString )); - free(errorString); -#else /* HAVE_DECL_STRSIGNAL */ - Error(( "Got signal %d, crashing", signal )); -#endif /* HAVE_DECL_STRSIGNAL */ - exit( signal ); -} - -void zm_term_handler( int signal ) -{ -#if HAVE_DECL_STRSIGNAL - char * error = strsignal(signal); - size_t errorStringSize = strlen(error) + strlen("Got signal (), exiting."); - char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char. - (void) snprintf(errorString, errorStringSize, "Got signal (%s), exiting.", error); - - Info(( (const char *)errorString )); - free(errorString); -#else /* HAVE_DECL_STRSIGNAL */ - Info(( "Got TERM signal, exiting" )); -#endif /* HAVE_DECL_STRSIGNAL */ - exit( 0 ); -} - int OpenSocket( int monitor_id ) { int sd = socket( AF_UNIX, SOCK_STREAM, 0); @@ -205,21 +173,13 @@ int main( int argc, char *argv[] ) snprintf( capt_path, sizeof(capt_path), "%s/%d/%%d/%%0%dd-capture.jpg", config.dir_events, monitor->Id(), config.event_image_digits ); snprintf( anal_path, sizeof(anal_path), "%s/%d/%%d/%%0%dd-analyse.jpg", config.dir_events, monitor->Id(), config.event_image_digits ); + zmSetDefaultTermHandler(); + zmSetDefaultDieHandler(); + sigset_t block_set; sigemptyset( &block_set ); struct sigaction action, old_action; - action.sa_handler = zm_term_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGTERM, &action, &old_action ); - - action.sa_handler = zm_die_handler; - action.sa_mask = block_set; - action.sa_flags = 0; - sigaction( SIGBUS, &action, &old_action ); - sigaction( SIGSEGV, &action, &old_action ); - int sd = OpenSocket( monitor->Id() ); FrameHeader frame_header = { 0, 0, false, 0 }; diff --git a/src/zms.cpp b/src/zms.cpp index 66415f88b..25c5b77d5 100644 --- a/src/zms.cpp +++ b/src/zms.cpp @@ -20,6 +20,7 @@ #include "zm.h" #include "zm_db.h" #include "zm_user.h" +#include "zm_signal.h" #include "zm_monitor.h" bool ValidateAccess( User *user, int mon_id ) @@ -74,6 +75,9 @@ int main( int argc, const char *argv[] ) zmLoadConfig(); + zmSetDefaultTermHandler(); + zmSetDefaultDieHandler(); + const char *query = getenv( "QUERY_STRING" ); if ( query ) { diff --git a/src/zmu.cpp b/src/zmu.cpp index be84e0e64..244076900 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -22,6 +22,7 @@ #include "zm.h" #include "zm_db.h" #include "zm_user.h" +#include "zm_signal.h" #include "zm_monitor.h" #include "zm_local_camera.h" @@ -348,6 +349,9 @@ int main( int argc, char *argv[] ) zmLoadConfig(); + zmSetDefaultTermHandler(); + zmSetDefaultDieHandler(); + User *user = 0; if ( config.opt_use_auth )