xref: /petsc/src/sys/error/signal.c (revision ede9db9363e1fdaaa09befd664c8164883ccce80)
1e5c89e4eSSatish Balay /*
2e5c89e4eSSatish Balay       Routines to handle signals the program will receive.
3e5c89e4eSSatish Balay     Usually this will call the error handlers.
4e5c89e4eSSatish Balay */
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I   "petscsys.h"   I*/
69be4fee8SSatish Balay #include <signal.h>
715d1ca26SJunchao Zhang #include <stdlib.h> /* for _Exit() */
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 };
1702c9f0b5SLisandro Dalcin static struct SH *sh        = NULL;
18ace3abfcSBarry Smith static PetscBool  SignalSet = PETSC_FALSE;
19e5c89e4eSSatish Balay 
2015d1ca26SJunchao Zhang /* Called by MPI_Abort() to suppress user-registered atexit()/on_exit() functions.
2115d1ca26SJunchao Zhang    See discussion at https://gitlab.com/petsc/petsc/-/merge_requests/2745.
2215d1ca26SJunchao Zhang */
MyExit(void)23d71ae5a4SJacob Faibussowitsch static void MyExit(void)
24d71ae5a4SJacob Faibussowitsch {
2515d1ca26SJunchao Zhang   _Exit(MPI_ERR_OTHER);
2615d1ca26SJunchao Zhang }
2715d1ca26SJunchao Zhang 
28e5c89e4eSSatish Balay /*
29e5c89e4eSSatish Balay     PetscSignalHandler_Private - This is the signal handler called by the system. This calls
30e5c89e4eSSatish Balay              any signal handler set by PETSc or the application code.
31e5c89e4eSSatish Balay 
32e5c89e4eSSatish Balay    Input Parameters: (depends on system)
33e5c89e4eSSatish Balay .    sig - integer code indicating the type of signal
34e5c89e4eSSatish Balay .    code - ??
35e5c89e4eSSatish Balay .    sigcontext - ??
36e5c89e4eSSatish Balay .    addr - ??
37e5c89e4eSSatish Balay 
38e5c89e4eSSatish Balay */
39e5c89e4eSSatish Balay #if defined(PETSC_HAVE_4ARG_SIGNAL_HANDLER)
PetscSignalHandler_Private(int sig,int code,struct sigcontext * scp,char * addr)40e5c89e4eSSatish Balay static void PetscSignalHandler_Private(int sig, int code, struct sigcontext *scp, char *addr)
41e5c89e4eSSatish Balay #else
42e5c89e4eSSatish Balay static void PetscSignalHandler_Private(int sig)
43e5c89e4eSSatish Balay #endif
44e5c89e4eSSatish Balay {
45e5c89e4eSSatish Balay   PetscErrorCode ierr;
46e5c89e4eSSatish Balay 
47c8025a54SPierre Jolivet   if (!sh || !sh->handler) ierr = PetscSignalHandlerDefault(sig, NULL);
48a297a907SKarl Rupp   else {
490700a824SBarry Smith     if (sh->classid != SIGNAL_CLASSID) SETERRABORT(PETSC_COMM_WORLD, PETSC_ERR_COR, "Signal object has been corrupted");
50e5c89e4eSSatish Balay     ierr = (*sh->handler)(sig, sh->ctx);
51e5c89e4eSSatish Balay   }
5241e02c4dSJunchao Zhang   if (ierr) PETSCABORT(PETSC_COMM_WORLD, PETSC_ERR_COR);
53e5c89e4eSSatish Balay }
54e5c89e4eSSatish Balay 
55e5c89e4eSSatish Balay /*@
568d359177SBarry Smith   PetscSignalHandlerDefault - Default signal handler.
57e5c89e4eSSatish Balay 
58e5c89e4eSSatish Balay   Not Collective
59e5c89e4eSSatish Balay 
60e5c89e4eSSatish Balay   Input Parameters:
61e5c89e4eSSatish Balay + sig - signal value
62e5c89e4eSSatish Balay - ptr - unused pointer
63e5c89e4eSSatish Balay 
642fe279fdSBarry Smith   Level: advanced
652fe279fdSBarry Smith 
666e25c4a1SBarry Smith   Developer Note:
676e25c4a1SBarry Smith   This does not call `PetscError()`, it handles the entire error process, including possibly printing the traceback, directly
68660278c0SBarry Smith 
696e25c4a1SBarry Smith .seealso: [](sec_errors), `PetscPushSignalHandler()`
70e5c89e4eSSatish Balay @*/
PetscSignalHandlerDefault(int sig,void * ptr)71d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSignalHandlerDefault(int sig, void *ptr)
72d71ae5a4SJacob Faibussowitsch {
73e5c89e4eSSatish Balay   const char *SIGNAME[64];
74e5c89e4eSSatish Balay 
75c2a741eeSJunchao Zhang   if (sig == SIGSEGV) PetscSignalSegvCheckPointerOrMpi();
76e5c89e4eSSatish Balay   SIGNAME[0] = "Unknown signal";
77e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGABRT)
78e5c89e4eSSatish Balay   SIGNAME[SIGABRT] = "Abort";
79e5c89e4eSSatish Balay #endif
80e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
815fa1c062SBarry Smith   SIGNAME[SIGALRM] = "Alarm";
82e5c89e4eSSatish Balay #endif
83e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
84e5c89e4eSSatish Balay   SIGNAME[SIGBUS] = "BUS: Bus Error, possibly illegal memory access";
85e5c89e4eSSatish Balay #endif
86e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCHLD)
87e5c89e4eSSatish Balay   SIGNAME[SIGCHLD] = "CHLD";
88e5c89e4eSSatish Balay #endif
89e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
90e5c89e4eSSatish Balay   SIGNAME[SIGCONT] = "CONT";
91e5c89e4eSSatish Balay #endif
92e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
93e5c89e4eSSatish Balay   SIGNAME[SIGFPE] = "FPE: Floating Point Exception,probably divide by zero";
94e5c89e4eSSatish Balay #endif
95e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
965fa1c062SBarry Smith   SIGNAME[SIGHUP] = "Hang up: Some other process (or the batch system) has told this process to end";
97e5c89e4eSSatish Balay #endif
98e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
995fa1c062SBarry Smith   SIGNAME[SIGILL] = "Illegal instruction: Likely due to memory corruption";
100e5c89e4eSSatish Balay #endif
101e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
1025fa1c062SBarry Smith   SIGNAME[SIGINT] = "Interrupt";
103e5c89e4eSSatish Balay #endif
104e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGKILL)
1055fa1c062SBarry Smith   SIGNAME[SIGKILL] = "Kill: Some other process (or the batch system) has told this process to end";
106e5c89e4eSSatish Balay #endif
107e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
1085fa1c062SBarry Smith   SIGNAME[SIGPIPE] = "Broken Pipe: Likely while reading or writing to a socket";
109e5c89e4eSSatish Balay #endif
110e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
1115fa1c062SBarry Smith   SIGNAME[SIGQUIT] = "Quit: Some other process (or the batch system) has told this process to end";
112e5c89e4eSSatish Balay #endif
113e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
114e5c89e4eSSatish Balay   SIGNAME[SIGSEGV] = "SEGV: Segmentation Violation, probably memory access out of range";
115e5c89e4eSSatish Balay #endif
116e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
117e5c89e4eSSatish Balay   SIGNAME[SIGSYS] = "SYS";
118e5c89e4eSSatish Balay #endif
119e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
1209df9fd51SWolfgang Bangerth   SIGNAME[SIGTERM] = "Terminate: Some process (or the batch system) has told this process to end";
121e5c89e4eSSatish Balay #endif
122e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
123e5c89e4eSSatish Balay   SIGNAME[SIGTRAP] = "TRAP";
124e5c89e4eSSatish Balay #endif
125e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
126e5c89e4eSSatish Balay   SIGNAME[SIGTSTP] = "TSTP";
127e5c89e4eSSatish Balay #endif
128e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
129e5c89e4eSSatish Balay   SIGNAME[SIGURG] = "URG";
130e5c89e4eSSatish Balay #endif
131e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
132e5c89e4eSSatish Balay   SIGNAME[SIGUSR1] = "User 1";
133e5c89e4eSSatish Balay #endif
134e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
135e5c89e4eSSatish Balay   SIGNAME[SIGUSR2] = "User 2";
136e5c89e4eSSatish Balay #endif
137e5c89e4eSSatish Balay 
138e5c89e4eSSatish Balay   signal(sig, SIG_DFL);
139dd460d27SBarry Smith   (void)PetscSleep(PetscGlobalRank % 4); /* prevent some jumbling of error messages from different ranks */
140dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("------------------------------------------------------------------------\n");
141dd460d27SBarry Smith   if (sig >= 0 && sig <= 20) (void)(*PetscErrorPrintf)("Caught signal number %d %s\n", sig, SIGNAME[sig]);
142dd460d27SBarry Smith   else (void)(*PetscErrorPrintf)("Caught signal\n");
143a297a907SKarl Rupp 
144dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("Try option -start_in_debugger or -on_error_attach_debugger\n");
145dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/\n");
1465ed36255SBarry Smith #if defined(PETSC_HAVE_CUDA)
147*74df5e01SJunchao Zhang   (void)(*PetscErrorPrintf)("or try https://docs.nvidia.com/compute-sanitizer/ComputeSanitizer/index.html on NVIDIA CUDA systems to find memory corruption errors\n");
1485ed36255SBarry Smith #endif
14927104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG)
150dfb7d7afSStefano Zampini   #if !PetscDefined(HAVE_THREADSAFETY)
151dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("---------------------  Stack Frames ------------------------------------\n");
152dd460d27SBarry Smith   (void)PetscStackView(PETSC_STDOUT);
153dfb7d7afSStefano Zampini   #endif
15427104ee2SJacob Faibussowitsch #else
155dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n");
156dd460d27SBarry Smith   (void)(*PetscErrorPrintf)("to get more information on the crash.\n");
15727104ee2SJacob Faibussowitsch #endif
15823fff9afSBarry Smith #if !defined(PETSC_MISSING_SIGBUS)
159f0b7f91aSBarry Smith   if (sig == SIGSEGV || sig == SIGBUS) {
16023fff9afSBarry Smith #else
16123fff9afSBarry Smith   if (sig == SIGSEGV) {
16223fff9afSBarry Smith #endif
163f0b7f91aSBarry Smith     PetscBool debug;
164f0b7f91aSBarry Smith 
165dd460d27SBarry Smith     (void)PetscMallocGetDebug(&debug, NULL, NULL);
166dd460d27SBarry Smith     if (debug) (void)PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__);
167dd460d27SBarry Smith     else (void)(*PetscErrorPrintf)("Run with -malloc_debug to check if memory corruption is causing the crash.\n");
168f0b7f91aSBarry Smith   }
16915d1ca26SJunchao Zhang   atexit(MyExit);
1703ba16761SJacob Faibussowitsch   PETSCABORT(PETSC_COMM_WORLD, PETSC_ERR_SIG);
1713ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
172e5c89e4eSSatish Balay }
173e5c89e4eSSatish Balay 
174e5c89e4eSSatish Balay #if !defined(PETSC_SIGNAL_CAST)
175e5c89e4eSSatish Balay   #define PETSC_SIGNAL_CAST
176e5c89e4eSSatish Balay #endif
177e5c89e4eSSatish Balay 
178e5c89e4eSSatish Balay /*@C
179e5c89e4eSSatish Balay   PetscPushSignalHandler - Catches the usual fatal errors and
180e5c89e4eSSatish Balay   calls a user-provided routine.
181e5c89e4eSSatish Balay 
182cc4c1da9SBarry Smith   Not Collective, No Fortran Support
183e5c89e4eSSatish Balay 
184d8d19677SJose E. Roman   Input Parameters:
185e5c89e4eSSatish Balay + routine - routine to call when a signal is received
186e5c89e4eSSatish Balay - ctx     - optional context needed by the routine
187e5c89e4eSSatish Balay 
188e5c89e4eSSatish Balay   Level: developer
189e5c89e4eSSatish Balay 
1908e010fa1SBarry Smith   Note:
1918e010fa1SBarry Smith   There is no way to return to a signal handler that was set directly by the user with the UNIX signal handler API or by
1928e010fa1SBarry Smith   the loader. That information is lost with the first call to `PetscPushSignalHandler()`
1938e010fa1SBarry Smith 
1948e010fa1SBarry Smith .seealso: [](sec_errors), `PetscPopSignalHandler()`, `PetscSignalHandlerDefault()`, `PetscPushErrorHandler()`
195e5c89e4eSSatish Balay @*/
1962a8381b2SBarry Smith PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*routine)(int, void *), PetscCtx ctx)
197d71ae5a4SJacob Faibussowitsch {
198e5c89e4eSSatish Balay   struct SH *newsh;
199e5c89e4eSSatish Balay 
200e5c89e4eSSatish Balay   PetscFunctionBegin;
2010700a824SBarry Smith   if (!SIGNAL_CLASSID) {
2029566063dSJacob Faibussowitsch     /* PetscCall(PetscClassIdRegister("Signal",&SIGNAL_CLASSID)); */
2030700a824SBarry Smith     SIGNAL_CLASSID = 19;
204156ba1f8SBarry Smith   }
205e5c89e4eSSatish Balay   if (!SignalSet && routine) {
206e5c89e4eSSatish Balay     /* Do not catch ABRT, CHLD, KILL */
207e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
208e5c89e4eSSatish Balay     /* signal(SIGALRM, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
209e5c89e4eSSatish Balay #endif
210e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
211e5c89e4eSSatish Balay     signal(SIGBUS, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
212e5c89e4eSSatish Balay #endif
213e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
214e5c89e4eSSatish Balay     /*signal(SIGCONT, PETSC_SIGNAL_CAST PetscSignalHandler_Private);*/
215e5c89e4eSSatish Balay #endif
216e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
217e5c89e4eSSatish Balay     signal(SIGFPE, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
218e5c89e4eSSatish Balay #endif
219e5cc20b0SSatish Balay #if !defined(PETSC_MISSING_SIGHUP) && defined(PETSC_HAVE_STRUCT_SIGACTION)
2205afd8efcSBarry Smith     {
2215afd8efcSBarry Smith       struct sigaction action;
2225afd8efcSBarry Smith       sigaction(SIGHUP, NULL, &action);
2235afd8efcSBarry Smith       if (action.sa_handler == SIG_IGN) {
224be87f6c0SPierre Jolivet         PetscCall(PetscInfo(NULL, "SIGHUP previously set to ignore, therefore not changing its signal handler\n"));
2255afd8efcSBarry Smith       } else {
226e5c89e4eSSatish Balay         signal(SIGHUP, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
2275afd8efcSBarry Smith       }
2285afd8efcSBarry Smith     }
229e5c89e4eSSatish Balay #endif
230e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
231e5c89e4eSSatish Balay     signal(SIGILL, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
232e5c89e4eSSatish Balay #endif
233e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
234e5c89e4eSSatish Balay     /* signal(SIGINT, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
235e5c89e4eSSatish Balay #endif
236e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
237e5c89e4eSSatish Balay     signal(SIGPIPE, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
238e5c89e4eSSatish Balay #endif
239e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
240e5c89e4eSSatish Balay     signal(SIGQUIT, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
241e5c89e4eSSatish Balay #endif
242e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
243e5c89e4eSSatish Balay     signal(SIGSEGV, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
244e5c89e4eSSatish Balay #endif
245e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
246e5c89e4eSSatish Balay     signal(SIGSYS, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
247e5c89e4eSSatish Balay #endif
248e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
249100ffedbSJunchao Zhang   #if !defined(PETSC_HAVE_OPENMPI)
25065f093c6SBarry Smith     /* Open MPI may use SIGTERM to close down all its ranks; we don't want to generate many confusing PETSc error messages in that case */
251e5c89e4eSSatish Balay     signal(SIGTERM, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
252e5c89e4eSSatish Balay   #endif
25365f093c6SBarry Smith #endif
254e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
255e5c89e4eSSatish Balay     signal(SIGTRAP, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
256e5c89e4eSSatish Balay #endif
257e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
258e5c89e4eSSatish Balay     /* signal(SIGTSTP,  PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
259e5c89e4eSSatish Balay #endif
260e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
261e5c89e4eSSatish Balay     signal(SIGURG, PETSC_SIGNAL_CAST PetscSignalHandler_Private);
262e5c89e4eSSatish Balay #endif
263e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
264e5c89e4eSSatish Balay     /* signal(SIGUSR1, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
265e5c89e4eSSatish Balay #endif
266e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
267e5c89e4eSSatish Balay     /* signal(SIGUSR2, PETSC_SIGNAL_CAST PetscSignalHandler_Private); */
268e5c89e4eSSatish Balay #endif
269e5c89e4eSSatish Balay     SignalSet = PETSC_TRUE;
270e5c89e4eSSatish Balay   }
271e5c89e4eSSatish Balay   if (!routine) {
272e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
27302c9f0b5SLisandro Dalcin     /* signal(SIGALRM, SIG_DFL); */
274e5c89e4eSSatish Balay #endif
275e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
27602c9f0b5SLisandro Dalcin     signal(SIGBUS, SIG_DFL);
277e5c89e4eSSatish Balay #endif
278e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
27902c9f0b5SLisandro Dalcin     /* signal(SIGCONT, SIG_DFL); */
280e5c89e4eSSatish Balay #endif
281e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
28202c9f0b5SLisandro Dalcin     signal(SIGFPE, SIG_DFL);
283e5c89e4eSSatish Balay #endif
284e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
28502c9f0b5SLisandro Dalcin     signal(SIGHUP, SIG_DFL);
286e5c89e4eSSatish Balay #endif
287e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
28802c9f0b5SLisandro Dalcin     signal(SIGILL, SIG_DFL);
289e5c89e4eSSatish Balay #endif
290e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
29102c9f0b5SLisandro Dalcin     /* signal(SIGINT,  SIG_DFL); */
292e5c89e4eSSatish Balay #endif
293e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
29402c9f0b5SLisandro Dalcin     signal(SIGPIPE, SIG_DFL);
295e5c89e4eSSatish Balay #endif
296e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
29702c9f0b5SLisandro Dalcin     signal(SIGQUIT, SIG_DFL);
298e5c89e4eSSatish Balay #endif
299e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
30002c9f0b5SLisandro Dalcin     signal(SIGSEGV, SIG_DFL);
301e5c89e4eSSatish Balay #endif
302e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
30302c9f0b5SLisandro Dalcin     signal(SIGSYS, SIG_DFL);
304e5c89e4eSSatish Balay #endif
305e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
30602c9f0b5SLisandro Dalcin     signal(SIGTERM, SIG_DFL);
307e5c89e4eSSatish Balay #endif
308e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
30902c9f0b5SLisandro Dalcin     signal(SIGTRAP, SIG_DFL);
310e5c89e4eSSatish Balay #endif
311e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
31202c9f0b5SLisandro Dalcin     /* signal(SIGTSTP, SIG_DFL); */
313e5c89e4eSSatish Balay #endif
314e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
31502c9f0b5SLisandro Dalcin     signal(SIGURG, SIG_DFL);
316e5c89e4eSSatish Balay #endif
317e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
31802c9f0b5SLisandro Dalcin     /* signal(SIGUSR1, SIG_DFL); */
319e5c89e4eSSatish Balay #endif
320e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
32102c9f0b5SLisandro Dalcin     /* signal(SIGUSR2, SIG_DFL); */
322e5c89e4eSSatish Balay #endif
323e5c89e4eSSatish Balay     SignalSet = PETSC_FALSE;
324e5c89e4eSSatish Balay   }
3259566063dSJacob Faibussowitsch   PetscCall(PetscNew(&newsh));
326156ba1f8SBarry Smith   if (sh) {
32708401ef6SPierre Jolivet     PetscCheck(sh->classid == SIGNAL_CLASSID, PETSC_COMM_SELF, PETSC_ERR_COR, "Signal object has been corrupted");
328156ba1f8SBarry Smith     newsh->previous = sh;
32902c9f0b5SLisandro Dalcin   } else newsh->previous = NULL;
330e5c89e4eSSatish Balay   newsh->handler = routine;
331e5c89e4eSSatish Balay   newsh->ctx     = ctx;
3320700a824SBarry Smith   newsh->classid = SIGNAL_CLASSID;
333e5c89e4eSSatish Balay   sh             = newsh;
3343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
335e5c89e4eSSatish Balay }
336e5c89e4eSSatish Balay 
337e30d2299SSatish Balay /*@
3388e010fa1SBarry Smith   PetscPopSignalHandler - Removes the last signal handler that was pushed.
339e5c89e4eSSatish Balay   If no signal handlers are left on the stack it will remove the PETSc signal handler.
340e5c89e4eSSatish Balay   (That is PETSc will no longer catch signals).
341e5c89e4eSSatish Balay 
342e5c89e4eSSatish Balay   Not Collective
343e5c89e4eSSatish Balay 
344e5c89e4eSSatish Balay   Level: developer
345e5c89e4eSSatish Balay 
3468e010fa1SBarry Smith   Note:
3478e010fa1SBarry Smith   There is no way to return to a signal handler that was set directly by the user with the UNIX signal handler API or by
3488e010fa1SBarry Smith   the loader. That information is lost with the first call to `PetscPushSignalHandler()`
3498e010fa1SBarry Smith 
3508e010fa1SBarry Smith .seealso: [](sec_errors), `PetscPushSignalHandler()`
351e5c89e4eSSatish Balay @*/
352d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscPopSignalHandler(void)
353d71ae5a4SJacob Faibussowitsch {
354e5c89e4eSSatish Balay   struct SH *tmp;
355e5c89e4eSSatish Balay 
356e5c89e4eSSatish Balay   PetscFunctionBegin;
3573ba16761SJacob Faibussowitsch   if (!sh) PetscFunctionReturn(PETSC_SUCCESS);
35808401ef6SPierre Jolivet   PetscCheck(sh->classid == SIGNAL_CLASSID, PETSC_COMM_SELF, PETSC_ERR_COR, "Signal object has been corrupted");
359156ba1f8SBarry Smith 
360e5c89e4eSSatish Balay   tmp = sh;
361e5c89e4eSSatish Balay   sh  = sh->previous;
3629566063dSJacob Faibussowitsch   PetscCall(PetscFree(tmp));
363e5c89e4eSSatish Balay   if (!sh || !sh->handler) {
364e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGALRM)
36502c9f0b5SLisandro Dalcin     /* signal(SIGALRM, SIG_DFL); */
366e5c89e4eSSatish Balay #endif
367e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGBUS)
36802c9f0b5SLisandro Dalcin     signal(SIGBUS, SIG_DFL);
369e5c89e4eSSatish Balay #endif
370e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGCONT)
37102c9f0b5SLisandro Dalcin     /* signal(SIGCONT, SIG_DFL); */
372e5c89e4eSSatish Balay #endif
373e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGFPE)
37402c9f0b5SLisandro Dalcin     signal(SIGFPE, SIG_DFL);
375e5c89e4eSSatish Balay #endif
376e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGHUP)
37702c9f0b5SLisandro Dalcin     signal(SIGHUP, SIG_DFL);
378e5c89e4eSSatish Balay #endif
379e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGILL)
38002c9f0b5SLisandro Dalcin     signal(SIGILL, SIG_DFL);
381e5c89e4eSSatish Balay #endif
382e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGINT)
38302c9f0b5SLisandro Dalcin     /* signal(SIGINT,  SIG_DFL); */
384e5c89e4eSSatish Balay #endif
385e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGPIPE)
38602c9f0b5SLisandro Dalcin     signal(SIGPIPE, SIG_DFL);
387e5c89e4eSSatish Balay #endif
388e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGQUIT)
38902c9f0b5SLisandro Dalcin     signal(SIGQUIT, SIG_DFL);
390e5c89e4eSSatish Balay #endif
391e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSEGV)
39202c9f0b5SLisandro Dalcin     signal(SIGSEGV, SIG_DFL);
393e5c89e4eSSatish Balay #endif
394e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGSYS)
39502c9f0b5SLisandro Dalcin     signal(SIGSYS, SIG_DFL);
396e5c89e4eSSatish Balay #endif
397e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTERM)
39802c9f0b5SLisandro Dalcin     signal(SIGTERM, SIG_DFL);
399e5c89e4eSSatish Balay #endif
400e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTRAP)
40102c9f0b5SLisandro Dalcin     signal(SIGTRAP, SIG_DFL);
402e5c89e4eSSatish Balay #endif
403e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGTSTP)
40402c9f0b5SLisandro Dalcin     /* signal(SIGTSTP, SIG_DFL); */
405e5c89e4eSSatish Balay #endif
406e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGURG)
40702c9f0b5SLisandro Dalcin     signal(SIGURG, SIG_DFL);
408e5c89e4eSSatish Balay #endif
409e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR1)
41002c9f0b5SLisandro Dalcin     /* signal(SIGUSR1, SIG_DFL); */
411e5c89e4eSSatish Balay #endif
412e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_SIGUSR2)
41302c9f0b5SLisandro Dalcin     /* signal(SIGUSR2, SIG_DFL); */
414e5c89e4eSSatish Balay #endif
415e5c89e4eSSatish Balay     SignalSet = PETSC_FALSE;
416e5c89e4eSSatish Balay   } else {
417e5c89e4eSSatish Balay     SignalSet = PETSC_TRUE;
418e5c89e4eSSatish Balay   }
4193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
420e5c89e4eSSatish Balay }
421