Salome HOME
Move MEDWrapper to MED module
[modules/kernel.git] / src / Utils / Utils_SignalsHandler.cxx
1 //  KERNEL Utils : common utils for KERNEL
2 //  Copyright (C) 2003  CEA
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License.
8 //
9
10 //  This library is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with this library; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
20
21
22 #include <stdexcept>
23 #include <stdio.h>
24 #include <signal.h>
25
26 #include "Utils_SignalsHandler.h"
27
28
29 //============================================================================
30 //function : Handler 
31 //purpose  : univisal handler for signals
32 //============================================================================
33 static void Handler(int theSigId)
34 {
35   char aMessage[256] = "";
36   sprintf(aMessage,"Signal with ID = %d was cautch!",theSigId);
37   throw std::runtime_error(aMessage);
38 }
39
40
41 //=======================================================================
42 //function : SetSigHandler
43 //purpose  : Redefine signal handlers. If the handler of the signal is
44 //           set as SIG_IGN. That's why the shells often ignore some 
45 //           signal when starting child processes. We keep it.
46 //=======================================================================
47 static void SetSignalHandler(Utils_SignalsHandler::TSigHandlerCont& theSigHandlerCont,
48                                       int theSigId)
49 {
50   TSigHandler anOldHandler = signal(theSigId,&Handler);
51   if(anOldHandler == SIG_IGN)
52     signal(theSigId,SIG_IGN);  
53   theSigHandlerCont[theSigId] = anOldHandler;
54 }
55
56 static TSigHandler StoreSignalHandler(Utils_SignalsHandler::TSigHandlerCont& theSigHandlerCont,
57                                       int theSigId)
58 {
59   TSigHandler anOldHandler = signal(theSigId,&Handler);
60   signal(theSigId,anOldHandler);
61   if(anOldHandler == SIG_IGN)
62     signal(theSigId,SIG_IGN);  
63   theSigHandlerCont[theSigId] = anOldHandler;
64   return anOldHandler;
65 }
66
67 static void RestoreSigHandler(TSigHandler theSigHandler,
68                               int theSigId)
69 {
70   signal(theSigId,theSigHandler);
71 }
72
73
74 //=======================================================================
75 //function : Utils_SignalsHandler
76 //purpose  : Constructor
77 //=======================================================================
78 Utils_SignalsHandler::Utils_SignalsHandler()
79 {
80   // asv 28.02.05 : some signals are not defined on Windows.. why?  I don't know..
81 #ifndef WNT
82   StoreSignalHandler(mySigHandlerCont,SIGHUP); // floating point exception
83   StoreSignalHandler(mySigHandlerCont,SIGQUIT); // quit
84   StoreSignalHandler(mySigHandlerCont,SIGBUS); // bus error
85   StoreSignalHandler(mySigHandlerCont,SIGSTKFLT); // stack fault.
86 #endif
87   StoreSignalHandler(mySigHandlerCont,SIGFPE); // floating point exception  
88   StoreSignalHandler(mySigHandlerCont,SIGINT); // interrupt
89   StoreSignalHandler(mySigHandlerCont,SIGILL); // illegal instruction
90   StoreSignalHandler(mySigHandlerCont,SIGTERM); // termination
91   StoreSignalHandler(mySigHandlerCont,SIGSEGV); // segmentation 
92   //StoreSignalHandler(mySigHandlerCont,SIGABRT); // abort (ANSI).  
93   // portage CCRT
94   //  StoreSignalHandler(mySigHandlerCont,SIGSTKFLT); // stack fault.  
95 }
96
97
98 //=======================================================================
99 //function : Utils_SignalsHandler
100 //purpose  : destructor
101 //=======================================================================
102 Utils_SignalsHandler::~Utils_SignalsHandler() 
103 {
104   TSigHandlerCont::iterator anIter = mySigHandlerCont.begin();
105   TSigHandlerCont::iterator anIterEnd = mySigHandlerCont.end();
106   for(; anIter != anIterEnd; anIter++)
107     RestoreSigHandler(anIter->second,anIter->first);
108 }
109
110
111 //=======================================================================
112 //function : SetSigHandler
113 //purpose  : sets new handler for pointed signal
114 //=======================================================================
115 TSigHandler Utils_SignalsHandler::SetSigHandler(int theSigId, 
116                                                 TSigHandler theSigHandler)
117 {
118   TSigHandler anOldHandler = signal(theSigId,theSigHandler);
119   if(anOldHandler == SIG_IGN)
120     signal(theSigId,SIG_IGN);  
121   mySigHandlerCont[theSigId] = anOldHandler;
122   return anOldHandler;
123 }