From f6a8b97e3935d934c776dcfc7081b20dcd1a0639 Mon Sep 17 00:00:00 2001 From: George McCabe <23407799+georgemccabe@users.noreply.github.com> Date: Thu, 3 Nov 2022 15:17:51 -0600 Subject: [PATCH] per dtcenter/METplus-Internal#21, added signal handling for all MET tools to log information on why the application ended unsuccessfully --- src/basic/vx_util/main.cc | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/basic/vx_util/main.cc b/src/basic/vx_util/main.cc index 96b574390b..c70ed1fe1f 100644 --- a/src/basic/vx_util/main.cc +++ b/src/basic/vx_util/main.cc @@ -65,6 +65,7 @@ void do_pre_process(int argc, char *argv[]); void set_handlers(); void set_user_id(); void store_arguments(int argc, char **argv); +void tidy_and_exit(int signal); //////////////////////////////////////////////////////////////////////// @@ -124,7 +125,6 @@ string get_current_time() { //////////////////////////////////////////////////////////////////////// -/* not working at Docker // based on blog at http://www.alexonlinux.com/how-to-handle-sigsegv-but-also-generate-core-dump // NOTE: that comments on the blog indicate the core file generated on red hat or on multi-threaded programs // might contain unhelpful information. @@ -134,22 +134,23 @@ void segv_handler(int signum) { getcwd(cwdbuffer,MET_BUF_SIZE+1); - fprintf(stderr, "FATAL ERROR (SEGFAULT): Process %d got signal %d @ local time = %s\n", getpid(), signum, timebuffer); + fprintf(stderr, "FATAL ERROR (SEGFAULT): Process %d got signal %d @ local time = %s\n", getpid(), signum, timebuffer.c_str()); fprintf(stderr, "FATAL ERROR (SEGFAULT): Look for a core file in %s\n",cwdbuffer); fprintf(stderr, "FATAL ERROR (SEGFAULT): Process command line: %s\n",met_cmdline.c_str()); signal(signum, SIG_DFL); kill(getpid(), signum); } -*/ //////////////////////////////////////////////////////////////////////// -// Need signal handlers for SIGINT, SIGHUP, SIGTERM, SIGPIPE, and SIGSEGV -// PORTsignal(SIGPIPE, (PORTsigfunc)SIG_IGN); -// PORTsignal(SIGSEGV, segv_handler); void set_handlers() { - - set_new_handler(oom); + set_new_handler(oom); + signal(SIGSEGV, segv_handler); + signal(SIGINT, tidy_and_exit); + signal(SIGTERM, tidy_and_exit); + signal(SIGABRT, tidy_and_exit); + signal(SIGFPE, tidy_and_exit); + signal(SIGILL, tidy_and_exit); } //////////////////////////////////////////////////////////////////////// @@ -173,6 +174,18 @@ void store_arguments(int argc, char **argv) { //////////////////////////////////////////////////////////////////////// void tidy_and_exit(int signal) { + printf("FATAL: "); + if(signal == SIGINT) { + printf("Received Signal Interrupt. "); + } else if(signal == SIGTERM){ + printf("Received Signal Terminate. "); + } else if(signal == SIGABRT){ + printf("Received Signal Abort. "); + } else if(signal == SIGFPE){ + printf("Received Signal Floating-Point Exception. "); + } else if(signal == SIGILL){ + printf("Received Signal Illegal Instruction. "); + } printf("Exiting %d\n", signal); exit(signal); }