1: static char help[] = "Tests signal handling.\n\n";
3: #include <petscsys.h>
4: #include <signal.h>
6: typedef struct _handlerCtx {
7: int exitHandler;
8: int signum;
9: } HandlerCtx;
11: PetscErrorCode handleSignal(int signum, void *ctx)
12: {
13: HandlerCtx *user = (HandlerCtx *)ctx;
15: user->signum = signum;
16: if (signum == SIGHUP) user->exitHandler = 1;
17: return PETSC_SUCCESS;
18: }
20: int main(int argc, char *args[])
21: {
22: HandlerCtx user;
24: user.exitHandler = 0;
26: PetscFunctionBeginUser;
27: PetscCall(PetscInitialize(&argc, &args, NULL, 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: }
40: /*TEST
42: build:
43: requires: !defined(PETSC_MISSING_SIGHUP)
45: test:
46: TODO: need to send a signal to the process to kill it from the test harness
48: TEST*/