Signal.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
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     unset_handler();
00046 
00047 #if !defined(HAVE_SWIG) && (defined(HAVE_MATLAB) || defined(HAVE_OCTAVE) || defined(HAVE_R))
00048     if (signal == SIGINT)
00049     {
00050         SG_SPRINT("\nImmediately return to matlab prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
00051         char answer=fgetc(stdin);
00052 
00053         if (answer == 'I')
00054         {
00055 #if defined(HAVE_MATLAB)
00056             mexErrMsgTxt("sg stopped by SIGINT\n");
00057 #elif defined(HAVE_OCTAVE)
00058             SG_PRINT("sg stopped by SIGINT\n");
00059             BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00060             octave_throw_interrupt_exception();
00061             END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00062 #elif defined(HAVE_PYTHON)
00063             SG_PRINT("sg stopped by SIGINT\n");
00064             PyErr_SetInterrupt();
00065             PyErr_CheckSignals();
00066 #elif defined(HAVE_R)
00067             R_Suicide((char*) "sg stopped by SIGINT\n");
00068 #endif
00069 
00070         }
00071         else
00072         {
00073             set_handler();
00074 
00075             if (answer == 'P')
00076                 cancel_computation=true;
00077             else
00078                 SG_SPRINT("\n");
00079         }
00080     }
00081     else if (signal == SIGURG)
00082         cancel_computation=true;
00083     else
00084         SG_SERROR("unknown signal %d received\n", signal);
00085 #else
00086     SG_SPRINT("\n");
00087     SG_SERROR("sg stopped by SIGINT\n");
00088     unset_handler();
00089     exit(0);
00090 #endif
00091 }
00092 
00093 bool CSignal::set_handler()
00094 {
00095     if (!active)
00096     {
00097         struct sigaction act;
00098         sigset_t st;
00099 
00100         sigemptyset(&st);
00101 
00102 #ifndef __INTERIX
00103         act.sa_sigaction=NULL; //just in case
00104 #endif
00105         act.sa_handler=CSignal::handler;
00106         act.sa_mask = st;
00107         act.sa_flags = 0;
00108 
00109         for (INT i=0; i<NUMTRAPPEDSIGS; i++)
00110         {
00111             if (sigaction(signals[i], &act, &oldsigaction[i]))
00112             {
00113                 for (INT j=i-1; j>=0; j--)
00114                     sigaction(signals[i], &oldsigaction[i], NULL);
00115 
00116                 clear();
00117                 return false;
00118             }
00119         }
00120 
00121         active=true;
00122         return true;
00123     }
00124     else
00125         return false;
00126 }
00127 
00128 bool CSignal::unset_handler()
00129 {
00130     if (active)
00131     {
00132         bool result=true;
00133 
00134         for (INT i=0; i<NUMTRAPPEDSIGS; i++)
00135         {
00136             if (sigaction(signals[i], &oldsigaction[i], NULL))
00137             {
00138                 SG_SERROR("error uninitalizing signal handler for signal %d\n", signals[i]);
00139                 result=false;
00140             }
00141         }
00142 
00143         if (result)
00144             clear();
00145 
00146         return result;
00147     }
00148     else
00149         return false;
00150 }
00151 
00152 void CSignal::clear()
00153 {
00154     cancel_computation=false;
00155     active=false;
00156     memset(&CSignal::oldsigaction, 0, sizeof(CSignal::oldsigaction));
00157 }
00158 #endif //WIN32

SHOGUN Machine Learning Toolbox - Documentation