xref: /petsc/src/sys/error/signal.c (revision 390e1bf27627d887df99a9f4d0d0ad68037f55ec)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay       Routines to handle signals the program will receive.
4e5c89e4eSSatish Balay     Usually this will call the error handlers.
5e5c89e4eSSatish Balay */
6af0996ceSBarry Smith #include <petsc/private/petscimpl.h>             /*I   "petscsys.h"   I*/
79be4fee8SSatish Balay #include <signal.h>
8e5c89e4eSSatish Balay 
90700a824SBarry Smith static PetscClassId SIGNAL_CLASSID = 0;
10156ba1f8SBarry Smith 
11e5c89e4eSSatish Balay struct SH {
120700a824SBarry Smith   PetscClassId   classid;
13e5c89e4eSSatish Balay   PetscErrorCode (*handler)(int,void*);
14e5c89e4eSSatish Balay   void           *ctx;
15e5c89e4eSSatish Balay   struct SH      *previous;
16e5c89e4eSSatish Balay };
17e5c89e4eSSatish Balay static struct SH *sh       = 0;
18ace3abfcSBarry Smith static PetscBool SignalSet = PETSC_FALSE;
19e5c89e4eSSatish Balay 
20e5c89e4eSSatish Balay /*
21e5c89e4eSSatish Balay     PetscSignalHandler_Private - This is the signal handler called by the system. This calls
22e5c89e4eSSatish Balay              any signal handler set by PETSc or the application code.
23e5c89e4eSSatish Balay 
24e5c89e4eSSatish Balay    Input Parameters: (depends on system)
25e5c89e4eSSatish Balay .    sig - integer code indicating the type of signal
26e5c89e4eSSatish Balay .    code - ??
27e5c89e4eSSatish Balay .    sigcontext - ??
28e5c89e4eSSatish Balay .    addr - ??
29e5c89e4eSSatish Balay 
30e5c89e4eSSatish Balay     Note: this is declared extern "C" because it is passed to the system routine signal()
31e5c89e4eSSatish Balay           which is an extern "C" routine. The Solaris 2.7 OS compilers require that this be
32e5c89e4eSSatish Balay           extern "C".
33e5c89e4eSSatish Balay 
34e5c89e4eSSatish Balay */
35e5c89e4eSSatish Balay #if defined(PETSC_HAVE_4ARG_SIGNAL_HANDLER)
36e5c89e4eSSatish Balay static void PetscSignalHandler_Private(int sig,int code,struct sigcontext * scp,char *addr)
37e5c89e4eSSatish Balay #else
38e5c89e4eSSatish Balay static void PetscSignalHandler_Private(int sig)
39e5c89e4eSSatish Balay #endif
40e5c89e4eSSatish Balay {
41e5c89e4eSSatish Balay   PetscErrorCode ierr;
42e5c89e4eSSatish Balay 
43e5c89e4eSSatish Balay   PetscFunctionBegin;
448d359177SBarry Smith   if (!sh || !sh->handler) ierr = PetscSignalHandlerDefault(sig,(void*)0);
45a297a907SKarl Rupp   else {
460700a824SBarry Smith     if (sh->classid != SIGNAL_CLASSID) SETERRABORT(PETSC_COMM_WORLD,PETSC_ERR_COR,"Signal object has been corrupted");
47e5c89e4eSSatish Balay     ierr = (*sh->handler)(sig,sh->ctx);
48e5c89e4eSSatish Balay   }
49e5c89e4eSSatish Balay   if (ierr) MPI_Abort(PETSC_COMM_WORLD,0);
50e5c89e4eSSatish Balay }
51e5c89e4eSSatish Balay 
52e5c89e4eSSatish Balay /*@
538d359177SBarry Smith    PetscSignalHandlerDefault - Default signal handler.
54e5c89e4eSSatish Balay 
55e5c89e4eSSatish Balay    Not Collective
56e5c89e4eSSatish Balay 
57e5c89e4eSSatish Balay    Level: advanced
58e5c89e4eSSatish Balay 
59e5c89e4eSSatish Balay    Input Parameters:
60e5c89e4eSSatish Balay +  sig - signal value
61e5c89e4eSSatish Balay -  ptr - unused pointer
62e5c89e4eSSatish Balay 
63e5c89e4eSSatish Balay    Concepts: signal handler^default
64e5c89e4eSSatish Balay 
65e5c89e4eSSatish Balay @*/
668d359177SBarry Smith PetscErrorCode  PetscSignalHandlerDefault(int sig,void *ptr)
67e5c89e4eSSatish Balay {
68e5c89e4eSSatish Balay   PetscErrorCode ierr;
69e5c89e4eSSatish Balay   const char     *SIGNAME[64];
70e5c89e4eSSatish Balay 
71e5c89e4eSSatish Balay   PetscFunctionBegin;
72e5c89e4eSSatish Balay   SIGNAME[0]       = "Unknown signal";
73e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGABRT)
74e5c89e4eSSatish Balay   SIGNAME[SIGABRT] = "Abort";
75e5c89e4eSSatish Balay #endif
76e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
775fa1c062SBarry Smith   SIGNAME[SIGALRM] = "Alarm";
78e5c89e4eSSatish Balay #endif
79e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
80e5c89e4eSSatish Balay   SIGNAME[SIGBUS]  = "BUS: Bus Error, possibly illegal memory access";
81e5c89e4eSSatish Balay #endif
82e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCHLD)
83e5c89e4eSSatish Balay   SIGNAME[SIGCHLD] = "CHLD";
84e5c89e4eSSatish Balay #endif
85e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
86e5c89e4eSSatish Balay   SIGNAME[SIGCONT] = "CONT";
87e5c89e4eSSatish Balay #endif
88e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
89e5c89e4eSSatish Balay   SIGNAME[SIGFPE]  = "FPE: Floating Point Exception,probably divide by zero";
90e5c89e4eSSatish Balay #endif
91e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
925fa1c062SBarry Smith   SIGNAME[SIGHUP]  = "Hang up: Some other process (or the batch system) has told this process to end";
93e5c89e4eSSatish Balay #endif
94e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
955fa1c062SBarry Smith   SIGNAME[SIGILL]  = "Illegal instruction: Likely due to memory corruption";
96e5c89e4eSSatish Balay #endif
97e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
985fa1c062SBarry Smith   SIGNAME[SIGINT]  = "Interrupt";
99e5c89e4eSSatish Balay #endif
100e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGKILL)
1015fa1c062SBarry Smith   SIGNAME[SIGKILL] = "Kill: Some other process (or the batch system) has told this process to end";
102e5c89e4eSSatish Balay #endif
103e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
1045fa1c062SBarry Smith   SIGNAME[SIGPIPE] = "Broken Pipe: Likely while reading or writing to a socket";
105e5c89e4eSSatish Balay #endif
106e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
1075fa1c062SBarry Smith   SIGNAME[SIGQUIT] = "Quit: Some other process (or the batch system) has told this process to end";
108e5c89e4eSSatish Balay #endif
109e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
110e5c89e4eSSatish Balay   SIGNAME[SIGSEGV] = "SEGV: Segmentation Violation, probably memory access out of range";
111e5c89e4eSSatish Balay #endif
112e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
113e5c89e4eSSatish Balay   SIGNAME[SIGSYS]  = "SYS";
114e5c89e4eSSatish Balay #endif
115e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
1169df9fd51SWolfgang Bangerth   SIGNAME[SIGTERM] = "Terminate: Some process (or the batch system) has told this process to end";
117e5c89e4eSSatish Balay #endif
118e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
119e5c89e4eSSatish Balay   SIGNAME[SIGTRAP] = "TRAP";
120e5c89e4eSSatish Balay #endif
121e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
122e5c89e4eSSatish Balay   SIGNAME[SIGTSTP] = "TSTP";
123e5c89e4eSSatish Balay #endif
124e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
125e5c89e4eSSatish Balay   SIGNAME[SIGURG]  = "URG";
126e5c89e4eSSatish Balay #endif
127e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
128e5c89e4eSSatish Balay   SIGNAME[SIGUSR1] = "User 1";
129e5c89e4eSSatish Balay #endif
130e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
131e5c89e4eSSatish Balay   SIGNAME[SIGUSR2] = "User 2";
132e5c89e4eSSatish Balay #endif
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay   signal(sig,SIG_DFL);
1353f6e4ae9SSatish Balay   (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
136a297a907SKarl Rupp   if (sig >= 0 && sig <= 20) (*PetscErrorPrintf)("Caught signal number %d %s\n",sig,SIGNAME[sig]);
137a297a907SKarl Rupp   else (*PetscErrorPrintf)("Caught signal\n");
138a297a907SKarl Rupp 
139e5c89e4eSSatish Balay   (*PetscErrorPrintf)("Try option -start_in_debugger or -on_error_attach_debugger\n");
140c168af59SJason Sarich   (*PetscErrorPrintf)("or see http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind\n");
141e57ff13aSBarry Smith   (*PetscErrorPrintf)("or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors\n");
1428bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
143dbf62e16SBarry Smith   if (!PetscStackActive()) (*PetscErrorPrintf)("  or try option -log_stack\n");
144a297a907SKarl Rupp   else {
145e5c89e4eSSatish Balay     PetscStackPop;  /* remove stack frames for error handlers */
146e5c89e4eSSatish Balay     PetscStackPop;
147e5c89e4eSSatish Balay     (*PetscErrorPrintf)("likely location of problem given in stack below\n");
1483f6e4ae9SSatish Balay     (*PetscErrorPrintf)("---------------------  Stack Frames ------------------------------------\n");
149639ff905SBarry Smith     PetscStackView(PETSC_STDOUT);
150e5c89e4eSSatish Balay   }
151e5c89e4eSSatish Balay #endif
152e5c89e4eSSatish Balay #if !defined(PETSC_USE_DEBUG)
153e5c89e4eSSatish Balay   (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n");
154e5c89e4eSSatish Balay   (*PetscErrorPrintf)("to get more information on the crash.\n");
155e5c89e4eSSatish Balay #endif
156efca3c55SSatish Balay   ierr =  PetscError(PETSC_COMM_SELF,0,"User provided function"," unknown file",PETSC_ERR_SIG,PETSC_ERROR_INITIAL,NULL);
157e5c89e4eSSatish Balay   MPI_Abort(PETSC_COMM_WORLD,(int)ierr);
158e5c89e4eSSatish Balay   PetscFunctionReturn(0);
159e5c89e4eSSatish Balay }
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay #if !defined(PETSC_SIGNAL_CAST)
162e5c89e4eSSatish Balay #define PETSC_SIGNAL_CAST
163e5c89e4eSSatish Balay #endif
164e5c89e4eSSatish Balay 
165e5c89e4eSSatish Balay /*@C
166e5c89e4eSSatish Balay    PetscPushSignalHandler - Catches the usual fatal errors and
167e5c89e4eSSatish Balay    calls a user-provided routine.
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay    Not Collective
170e5c89e4eSSatish Balay 
171e5c89e4eSSatish Balay     Input Parameter:
172e5c89e4eSSatish Balay +  routine - routine to call when a signal is received
173e5c89e4eSSatish Balay -  ctx - optional context needed by the routine
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay   Level: developer
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay    Concepts: signal handler^setting
178e5c89e4eSSatish Balay 
1798d359177SBarry Smith .seealso: PetscPopSignalHandler(), PetscSignalHandlerDefault(), PetscPushErrorHandler()
180e5c89e4eSSatish Balay 
181e5c89e4eSSatish Balay @*/
1827087cfbeSBarry Smith PetscErrorCode  PetscPushSignalHandler(PetscErrorCode (*routine)(int,void*),void *ctx)
183e5c89e4eSSatish Balay {
184e5c89e4eSSatish Balay   struct  SH     *newsh;
185e5c89e4eSSatish Balay   PetscErrorCode ierr;
186e5c89e4eSSatish Balay 
187e5c89e4eSSatish Balay   PetscFunctionBegin;
1880700a824SBarry Smith   if (!SIGNAL_CLASSID) {
1890700a824SBarry Smith     /* ierr = PetscClassIdRegister("Signal",&SIGNAL_CLASSID);CHKERRQ(ierr); */
1900700a824SBarry Smith     SIGNAL_CLASSID = 19;
191156ba1f8SBarry Smith   }
192e5c89e4eSSatish Balay   if (!SignalSet && routine) {
193e5c89e4eSSatish Balay     /* Do not catch ABRT, CHLD, KILL */
194e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
195e5c89e4eSSatish Balay     /* signal(SIGALRM, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
196e5c89e4eSSatish Balay #endif
197e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
198e5c89e4eSSatish Balay     signal(SIGBUS, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
199e5c89e4eSSatish Balay #endif
200e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
201e5c89e4eSSatish Balay     /*signal(SIGCONT, PETSC_SIGNAL_CAST PetscSignalHandler_Private);*/
202e5c89e4eSSatish Balay #endif
203e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
204e5c89e4eSSatish Balay     signal(SIGFPE,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
205e5c89e4eSSatish Balay #endif
206e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
207e5c89e4eSSatish Balay     signal(SIGHUP, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
208e5c89e4eSSatish Balay #endif
209e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
210e5c89e4eSSatish Balay     signal(SIGILL,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
211e5c89e4eSSatish Balay #endif
212e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
213e5c89e4eSSatish Balay     /* signal(SIGINT, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
214e5c89e4eSSatish Balay #endif
215e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
216e5c89e4eSSatish Balay     signal(SIGPIPE, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
217e5c89e4eSSatish Balay #endif
218e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
219e5c89e4eSSatish Balay     signal(SIGQUIT, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
220e5c89e4eSSatish Balay #endif
221e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
222e5c89e4eSSatish Balay     signal(SIGSEGV, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
223e5c89e4eSSatish Balay #endif
224e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
225e5c89e4eSSatish Balay     signal(SIGSYS,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
226e5c89e4eSSatish Balay #endif
227e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
228e5c89e4eSSatish Balay     signal(SIGTERM,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
229e5c89e4eSSatish Balay #endif
230e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
231e5c89e4eSSatish Balay     signal(SIGTRAP,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
232e5c89e4eSSatish Balay #endif
233e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
234e5c89e4eSSatish Balay     /* signal(SIGTSTP,  PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
235e5c89e4eSSatish Balay #endif
236e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
237e5c89e4eSSatish Balay     signal(SIGURG,  PETSC_SIGNAL_CAST PetscSignalHandler_Private);
238e5c89e4eSSatish Balay #endif
239e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
240e5c89e4eSSatish Balay     /*    signal(SIGUSR1, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
241e5c89e4eSSatish Balay #endif
242e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
243e5c89e4eSSatish Balay     /* signal(SIGUSR2, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
244e5c89e4eSSatish Balay #endif
245e5c89e4eSSatish Balay     SignalSet = PETSC_TRUE;
246e5c89e4eSSatish Balay   }
247e5c89e4eSSatish Balay   if (!routine) {
248e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
249e5c89e4eSSatish Balay     /* signal(SIGALRM, 0); */
250e5c89e4eSSatish Balay #endif
251e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
252e5c89e4eSSatish Balay     signal(SIGBUS,  0);
253e5c89e4eSSatish Balay #endif
254e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
255e5c89e4eSSatish Balay     /* signal(SIGCONT, 0); */
256e5c89e4eSSatish Balay #endif
257e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
258e5c89e4eSSatish Balay     signal(SIGFPE,  0);
259e5c89e4eSSatish Balay #endif
260e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
261e5c89e4eSSatish Balay     signal(SIGHUP,  0);
262e5c89e4eSSatish Balay #endif
263e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
264e5c89e4eSSatish Balay     signal(SIGILL,  0);
265e5c89e4eSSatish Balay #endif
266e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
267e5c89e4eSSatish Balay     /* signal(SIGINT,  0); */
268e5c89e4eSSatish Balay #endif
269e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
270e5c89e4eSSatish Balay     signal(SIGPIPE, 0);
271e5c89e4eSSatish Balay #endif
272e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
273e5c89e4eSSatish Balay     signal(SIGQUIT, 0);
274e5c89e4eSSatish Balay #endif
275e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
276e5c89e4eSSatish Balay     signal(SIGSEGV, 0);
277e5c89e4eSSatish Balay #endif
278e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
279e5c89e4eSSatish Balay     signal(SIGSYS,  0);
280e5c89e4eSSatish Balay #endif
281e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
282e5c89e4eSSatish Balay     signal(SIGTERM, 0);
283e5c89e4eSSatish Balay #endif
284e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
285e5c89e4eSSatish Balay     signal(SIGTRAP, 0);
286e5c89e4eSSatish Balay #endif
287e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
288e5c89e4eSSatish Balay     /* signal(SIGTSTP, 0); */
289e5c89e4eSSatish Balay #endif
290e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
291e5c89e4eSSatish Balay     signal(SIGURG,  0);
292e5c89e4eSSatish Balay #endif
293e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
294e5c89e4eSSatish Balay     /*    signal(SIGUSR1, 0); */
295e5c89e4eSSatish Balay #endif
296e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
297e5c89e4eSSatish Balay     /* signal(SIGUSR2, 0); */
298e5c89e4eSSatish Balay #endif
299e5c89e4eSSatish Balay     SignalSet = PETSC_FALSE;
300e5c89e4eSSatish Balay   }
301b00a9115SJed Brown   ierr = PetscNew(&newsh);CHKERRQ(ierr);
302156ba1f8SBarry Smith   if (sh) {
303e32f2f54SBarry Smith     if (sh->classid != SIGNAL_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Signal object has been corrupted");
304156ba1f8SBarry Smith     newsh->previous = sh;
305a297a907SKarl Rupp   }  else newsh->previous = 0;
306e5c89e4eSSatish Balay   newsh->handler = routine;
307e5c89e4eSSatish Balay   newsh->ctx     = ctx;
3080700a824SBarry Smith   newsh->classid = SIGNAL_CLASSID;
309e5c89e4eSSatish Balay   sh             = newsh;
310e5c89e4eSSatish Balay   PetscFunctionReturn(0);
311e5c89e4eSSatish Balay }
312e5c89e4eSSatish Balay 
313e30d2299SSatish Balay /*@
314e5c89e4eSSatish Balay    PetscPopSignalHandler - Removes the most last signal handler that was pushed.
315e5c89e4eSSatish Balay        If no signal handlers are left on the stack it will remove the PETSc signal handler.
316e5c89e4eSSatish Balay        (That is PETSc will no longer catch signals).
317e5c89e4eSSatish Balay 
318e5c89e4eSSatish Balay    Not Collective
319e5c89e4eSSatish Balay 
320e5c89e4eSSatish Balay   Level: developer
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay    Concepts: signal handler^setting
323e5c89e4eSSatish Balay 
324e5c89e4eSSatish Balay .seealso: PetscPushSignalHandler()
325e5c89e4eSSatish Balay 
326e5c89e4eSSatish Balay @*/
3277087cfbeSBarry Smith PetscErrorCode  PetscPopSignalHandler(void)
328e5c89e4eSSatish Balay {
329e5c89e4eSSatish Balay   struct SH      *tmp;
330*390e1bf2SBarry Smith   PetscErrorCode ierr;
331e5c89e4eSSatish Balay 
332e5c89e4eSSatish Balay   PetscFunctionBegin;
333e5c89e4eSSatish Balay   if (!sh) PetscFunctionReturn(0);
334e32f2f54SBarry Smith   if (sh->classid != SIGNAL_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Signal object has been corrupted");
335156ba1f8SBarry Smith 
336e5c89e4eSSatish Balay   tmp = sh;
337e5c89e4eSSatish Balay   sh  = sh->previous;
338*390e1bf2SBarry Smith   ierr = PetscFree(tmp);CHKERRQ(ierr);
339e5c89e4eSSatish Balay   if (!sh || !sh->handler) {
340e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
341e5c89e4eSSatish Balay     /* signal(SIGALRM, 0); */
342e5c89e4eSSatish Balay #endif
343e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
344e5c89e4eSSatish Balay     signal(SIGBUS,  0);
345e5c89e4eSSatish Balay #endif
346e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
347e5c89e4eSSatish Balay     /* signal(SIGCONT, 0); */
348e5c89e4eSSatish Balay #endif
349e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
350e5c89e4eSSatish Balay     signal(SIGFPE,  0);
351e5c89e4eSSatish Balay #endif
352e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
353e5c89e4eSSatish Balay     signal(SIGHUP,  0);
354e5c89e4eSSatish Balay #endif
355e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
356e5c89e4eSSatish Balay     signal(SIGILL,  0);
357e5c89e4eSSatish Balay #endif
358e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
359e5c89e4eSSatish Balay     /* signal(SIGINT,  0); */
360e5c89e4eSSatish Balay #endif
361e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
362e5c89e4eSSatish Balay     signal(SIGPIPE, 0);
363e5c89e4eSSatish Balay #endif
364e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
365e5c89e4eSSatish Balay     signal(SIGQUIT, 0);
366e5c89e4eSSatish Balay #endif
367e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
368e5c89e4eSSatish Balay     signal(SIGSEGV, 0);
369e5c89e4eSSatish Balay #endif
370e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
371e5c89e4eSSatish Balay     signal(SIGSYS,  0);
372e5c89e4eSSatish Balay #endif
373e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
374e5c89e4eSSatish Balay     signal(SIGTERM, 0);
375e5c89e4eSSatish Balay #endif
376e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
377e5c89e4eSSatish Balay     signal(SIGTRAP, 0);
378e5c89e4eSSatish Balay #endif
379e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
380e5c89e4eSSatish Balay     /* signal(SIGTSTP, 0); */
381e5c89e4eSSatish Balay #endif
382e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
383e5c89e4eSSatish Balay     signal(SIGURG,  0);
384e5c89e4eSSatish Balay #endif
385e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
386e5c89e4eSSatish Balay     /*    signal(SIGUSR1, 0); */
387e5c89e4eSSatish Balay #endif
388e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
389e5c89e4eSSatish Balay     /* signal(SIGUSR2, 0); */
390e5c89e4eSSatish Balay #endif
391e5c89e4eSSatish Balay     SignalSet = PETSC_FALSE;
392e5c89e4eSSatish Balay   } else {
393e5c89e4eSSatish Balay     SignalSet = PETSC_TRUE;
394e5c89e4eSSatish Balay   }
395e5c89e4eSSatish Balay   PetscFunctionReturn(0);
396e5c89e4eSSatish Balay }
397e5c89e4eSSatish Balay 
398e5c89e4eSSatish Balay 
399f0c7afdeSSatish Balay #if defined(PETSC_HAVE_SETJMP_H) && defined(PETSC_HAVE_SIGINFO_T)
400a8b45ee7SBarry Smith #include <setjmp.h>
401d076d156SJed Brown PETSC_VISIBILITY_INTERNAL jmp_buf PetscSegvJumpBuf;
402a8b45ee7SBarry Smith /*
403a8b45ee7SBarry Smith     This routine is called if a segmentation violation, i.e. inaccessible memory access
404a8b45ee7SBarry Smith     is triggered when PETSc is testing for a buggy pointer with PetscCheckPointer()
405a8b45ee7SBarry Smith 
406a8b45ee7SBarry Smith     It simply unrolls the UNIX signal and returns to the location where setjump(PetscSeqvJumpBuf) is declared.
407a8b45ee7SBarry Smith */
408d076d156SJed Brown PETSC_INTERN void PetscSegv_sigaction(int signal, siginfo_t *si, void *arg)
409a8b45ee7SBarry Smith {
410a8b45ee7SBarry Smith   longjmp(PetscSegvJumpBuf,1);
411a8b45ee7SBarry Smith   return;
412a8b45ee7SBarry Smith }
413a8b45ee7SBarry Smith #endif
414e5c89e4eSSatish Balay 
415e5c89e4eSSatish Balay 
416