xref: /petsc/src/sys/error/tests/ex1.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
1 static char help[] = "Tests signal handling.\n\n";
2 
3 #include <petscsys.h>
4 #include <signal.h>
5 
6 typedef struct _handlerCtx {
7   int exitHandler;
8   int signum;
9 } HandlerCtx;
10 
11 PetscErrorCode handleSignal(int signum, void *ctx)
12 {
13   HandlerCtx *user = (HandlerCtx *)ctx;
14 
15   user->signum = signum;
16   if (signum == SIGHUP) user->exitHandler = 1;
17   return PETSC_SUCCESS;
18 }
19 
20 int main(int argc, char *args[])
21 {
22   HandlerCtx user;
23 
24   user.exitHandler = 0;
25 
26   PetscFunctionBeginUser;
27   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
28   PetscCall(PetscPushSignalHandler(handleSignal, &user));
29   while (!user.exitHandler) {
30     if (user.signum > 0) {
31       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Caught signal %d\n", user.signum));
32       user.signum = -1;
33     }
34   }
35   PetscCall(PetscPopSignalHandler());
36   PetscCall(PetscFinalize());
37   return 0;
38 }
39 
40 /*TEST
41 
42    build:
43      requires: !defined(PETSC_MISSING_SIGHUP)
44 
45    test:
46      TODO: need to send a signal to the process to kill it from the test harness
47 
48 TEST*/
49