Salome HOME
Porting to Mandrake 10.1 and new products:
[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 using namespace std;
28
29
30 //============================================================================
31 //function : Handler 
32 //purpose  : univisal handler for signals
33 //============================================================================
34 static void Handler(int theSigId)
35 {
36   char aMessage[256] = "";
37   sprintf(aMessage,"Signal with ID = %d was cautch!",theSigId);
38   throw std::runtime_error(aMessage);
39 }
40
41
42 //=======================================================================
43 //function : SetSigHandler
44 //purpose  : Redefine signal handlers. If the handler of the signal is
45 //           set as SIG_IGN. That's why the shells often ignore some 
46 //           signal when starting child processes. We keep it.
47 //=======================================================================
48 static void SetSignalHandler(Utils_SignalsHandler::TSigHandlerCont& theSigHandlerCont,
49                                       int theSigId)
50 {
51   TSigHandler anOldHandler = signal(theSigId,&Handler);
52   if(anOldHandler == SIG_IGN)
53     signal(theSigId,SIG_IGN);  
54   theSigHandlerCont[theSigId] = anOldHandler;
55 }
56
57 static TSigHandler StoreSignalHandler(Utils_SignalsHandler::TSigHandlerCont& theSigHandlerCont,
58                                       int theSigId)
59 {
60   TSigHandler anOldHandler = signal(theSigId,&Handler);
61   signal(theSigId,anOldHandler);
62   if(anOldHandler == SIG_IGN)
63     signal(theSigId,SIG_IGN);  
64   theSigHandlerCont[theSigId] = anOldHandler;
65   return anOldHandler;
66 }
67
68 static void RestoreSigHandler(TSigHandler theSigHandler,
69                               int theSigId)
70 {
71   signal(theSigId,theSigHandler);
72 }
73
74
75 //=======================================================================
76 //function : Utils_SignalsHandler
77 //purpose  : Constructor
78 //=======================================================================
79 Utils_SignalsHandler::Utils_SignalsHandler()
80 {
81   StoreSignalHandler(mySigHandlerCont,SIGHUP); // end of leader process
82   StoreSignalHandler(mySigHandlerCont,SIGFPE); // floating point exception
83   
84   StoreSignalHandler(mySigHandlerCont,SIGINT); // interrupt
85   StoreSignalHandler(mySigHandlerCont,SIGQUIT); // quit
86   StoreSignalHandler(mySigHandlerCont,SIGBUS); // bus error
87   StoreSignalHandler(mySigHandlerCont,SIGILL); // illegal instruction
88   StoreSignalHandler(mySigHandlerCont,SIGTERM); // termination
89   StoreSignalHandler(mySigHandlerCont,SIGSEGV); // segmentation 
90   //StoreSignalHandler(mySigHandlerCont,SIGABRT); // abort (ANSI).  
91   // portage CCRT
92   //  StoreSignalHandler(mySigHandlerCont,SIGSTKFLT); // stack fault.  
93 }
94
95
96 //=======================================================================
97 //function : Utils_SignalsHandler
98 //purpose  : destructor
99 //=======================================================================
100 Utils_SignalsHandler::~Utils_SignalsHandler() 
101 {
102   TSigHandlerCont::iterator anIter = mySigHandlerCont.begin();
103   TSigHandlerCont::iterator anIterEnd = mySigHandlerCont.end();
104   for(; anIter != anIterEnd; anIter++)
105     RestoreSigHandler(anIter->second,anIter->first);
106 }
107
108
109 //=======================================================================
110 //function : SetSigHandler
111 //purpose  : sets new handler for pointed signal
112 //=======================================================================
113 TSigHandler Utils_SignalsHandler::SetSigHandler(int theSigId, 
114                                                 TSigHandler theSigHandler)
115 {
116   TSigHandler anOldHandler = signal(theSigId,theSigHandler);
117   if(anOldHandler == SIG_IGN)
118     signal(theSigId,SIG_IGN);  
119   mySigHandlerCont[theSigId] = anOldHandler;
120   return anOldHandler;
121 }