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