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 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