00001 #include "copyright.h" 00002 /*============================================================================*/ 00003 /*! \file ath_signal.c 00004 * \brief Implements very simple signal handling. 00005 * 00006 * PURPOSE: Implements very simple signal handling. Since signals can 00007 * come in at any time, these functions set a static global 00008 * variable which will be checked and reset if necessary -- TAG 8/19/2004 00009 * 00010 * 00011 * CONTAINS PUBLIC FUNCTIONS: 00012 * - ath_sig_init() 00013 * - ath_sig_act() 00014 * 00015 * PRIVATE FUNCTION PROTOTYPES: 00016 * - handler() 00017 * */ 00018 /*============================================================================*/ 00019 00020 #include <signal.h> 00021 #include <stdio.h> 00022 #include "defs.h" 00023 #include "athena.h" 00024 #include "prototypes.h" 00025 00026 static volatile int sig_caught = 0; /* caught signal */ 00027 00028 /*============================================================================== 00029 * PRIVATE FUNCTION PROTOTYPES: 00030 * - handler() 00031 *============================================================================*/ 00032 00033 static void handler(int s); 00034 00035 /*=========================== PUBLIC FUNCTIONS ===============================*/ 00036 /*----------------------------------------------------------------------------*/ 00037 /*! \fn void ath_sig_init(void) 00038 * \brief Defines the signal handler function. */ 00039 void ath_sig_init(void) 00040 { 00041 signal(SIGTERM, handler); /* Define the signal handler function */ 00042 return; 00043 } 00044 00045 /*----------------------------------------------------------------------------*/ 00046 /*! \fn int ath_sig_act(int *piquit) 00047 * \brief Handles response to any received signals. 00048 * 00049 * At the moment, only response to SIGTERM is implemented. */ 00050 int ath_sig_act(int *piquit) 00051 { 00052 00053 #ifdef MPI_PARALLEL 00054 int ierr, sig = sig_caught > *piquit ? sig_caught : *piquit; 00055 00056 ierr = MPI_Allreduce(&sig, piquit, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); 00057 00058 #else /* SERIAL */ 00059 00060 *piquit = sig_caught > *piquit ? sig_caught : *piquit; 00061 00062 #endif /* MPI_PARALLEL */ 00063 00064 if(sig_caught == SIGTERM) 00065 ath_pout(0,"Caught SIGTERM: Terminating program execution\n"); 00066 00067 sig_caught = 0; /* Reset the signal */ 00068 00069 return *piquit; 00070 } 00071 00072 /*=========================== PRIVATE FUNCTIONS ==============================*/ 00073 00074 /*----------------------------------------------------------------------------*/ 00075 /*! \fn static void handler(int s) 00076 * \brief Reinstalls the signal handler function. */ 00077 static void handler(int s){ 00078 sig_caught = s; 00079 signal(s, handler); /* Reinstall the signal handler function */ 00080 return; 00081 }