Signal.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/config.h"
00012
00013 #ifndef WIN32
00014 #include "lib/python.h"
00015
00016 #include <stdlib.h>
00017 #include <signal.h>
00018 #include <string.h>
00019
00020 #include "lib/io.h"
00021 #include "lib/Signal.h"
00022 #include "lib/matlab.h"
00023 #include "lib/octave.h"
00024 #include "lib/python.h"
00025 #include "lib/r.h"
00026
00027 int CSignal::signals[NUMTRAPPEDSIGS]={SIGINT, SIGURG};
00028 struct sigaction CSignal::oldsigaction[NUMTRAPPEDSIGS];
00029 bool CSignal::active=false;
00030 bool CSignal::cancel_computation=false;
00031
00032 CSignal::CSignal()
00033 : CSGObject()
00034 {
00035 }
00036
00037 CSignal::~CSignal()
00038 {
00039 if (!unset_handler())
00040 SG_SERROR("error uninitalizing signal handler\n");
00041 }
00042
00043 void CSignal::handler(int signal)
00044 {
00045 #if !defined(HAVE_SWIG) && (defined(HAVE_MATLAB) || defined(HAVE_OCTAVE) || defined(HAVE_R))
00046 if (signal == SIGINT)
00047 {
00048 SG_SPRINT("\nImmediately return to prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
00049 char answer=fgetc(stdin);
00050
00051 if (answer == 'I')
00052 {
00053 unset_handler();
00054 #if defined(HAVE_MATLAB)
00055 mexErrMsgTxt("sg stopped by SIGINT\n");
00056 #elif defined(HAVE_OCTAVE)
00057 SG_PRINT("sg stopped by SIGINT\n");
00058 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00059 octave_throw_interrupt_exception();
00060 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00061 #elif defined(HAVE_PYTHON)
00062 SG_PRINT("sg stopped by SIGINT\n");
00063 PyErr_SetInterrupt();
00064 PyErr_CheckSignals();
00065 #elif defined(HAVE_R)
00066 R_Suicide((char*) "sg stopped by SIGINT\n");
00067 #endif
00068 }
00069 else
00070 {
00071 if (answer == 'P')
00072 cancel_computation=true;
00073 else
00074 SG_SPRINT("\n");
00075 }
00076 }
00077 else if (signal == SIGURG)
00078 cancel_computation=true;
00079 else
00080 SG_SERROR("unknown signal %d received\n", signal);
00081 #else
00082 SG_SPRINT("\n");
00083 SG_SERROR("sg stopped by SIGINT\n");
00084 unset_handler();
00085 exit(0);
00086 #endif
00087 }
00088
00089 bool CSignal::set_handler()
00090 {
00091 if (!active)
00092 {
00093 struct sigaction act;
00094 sigset_t st;
00095
00096 sigemptyset(&st);
00097 for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
00098 sigaddset(&st, signals[i]);
00099
00100 #ifndef __INTERIX
00101 act.sa_sigaction=NULL;
00102 #endif
00103 act.sa_handler=CSignal::handler;
00104 act.sa_mask = st;
00105 act.sa_flags = 0;
00106
00107 for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
00108 {
00109 if (sigaction(signals[i], &act, &oldsigaction[i]))
00110 {
00111 SG_SWARNING("Error trapping signals!\n");
00112 for (int32_t j=i-1; j>=0; j--)
00113 sigaction(signals[i], &oldsigaction[i], NULL);
00114
00115 clear();
00116 return false;
00117 }
00118 }
00119
00120 active=true;
00121 return true;
00122 }
00123 else
00124 return false;
00125 }
00126
00127 bool CSignal::unset_handler()
00128 {
00129 if (active)
00130 {
00131 bool result=true;
00132
00133 for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
00134 {
00135 if (sigaction(signals[i], &oldsigaction[i], NULL))
00136 {
00137 SG_SERROR("error uninitalizing signal handler for signal %d\n", signals[i]);
00138 result=false;
00139 }
00140 }
00141
00142 if (result)
00143 clear();
00144
00145 return result;
00146 }
00147 else
00148 return false;
00149 }
00150
00151 void CSignal::clear_cancel()
00152 {
00153 cancel_computation=false;
00154 }
00155
00156 void CSignal::clear()
00157 {
00158 clear_cancel();
00159 active=false;
00160 memset(&CSignal::oldsigaction, 0, sizeof(CSignal::oldsigaction));
00161 }
00162 #endif //WIN32