/*-------------------------------------------------------------------*/ /* testsigaction.c */ /* Signal handling via the sigaction system call. */ /*-------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include /*-------------------------------------------------------------------*/ void mySigAction(int iSignal, siginfo_t *psSigInfo, void *pvContext) /* This function is intended to be a handler for signals of various types. Print iSignal to stdout, along with some of the information about the signal contained in *psSigInfo, and some of the signal's context contained in *pvContext. */ { ucontext_t *psContext = (ucontext_t*)pvContext; printf("--------------------------------------------\n"); printf("In mySigAction with argument %d\n", iSignal); printf("\n"); printf("Signal number: %d\n", psSigInfo->si_signo); printf("Error code: %d\n", psSigInfo->si_errno); printf("Signal code: %d\n", psSigInfo->si_code); printf("\n"); printf("Register EAX: %x\n", psContext->uc_mcontext.gregs[REG_EAX]); printf("Register EBX: %x\n", psContext->uc_mcontext.gregs[REG_EBX]); printf("Register ECX: %x\n", psContext->uc_mcontext.gregs[REG_ECX]); printf("Register EDX: %x\n", psContext->uc_mcontext.gregs[REG_ECX]); printf("Register ESP: %x\n", psContext->uc_mcontext.gregs[REG_ESP]); printf("Register EBP: %x\n", psContext->uc_mcontext.gregs[REG_EBP]); printf("Register EIP: %x\n", psContext->uc_mcontext.gregs[REG_EIP]); printf("--------------------------------------------\n"); } /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* Illustrate the sigaction system call. */ { struct sigaction sSigAction; struct sigaction sOldSigAction; /* Set sSigAction to indicate that we want to register a more elaborate signal handler -- one which accepts a siginfo_t parameter and a ucontext_t parameter containing information about the signal. */ sSigAction.sa_flags = SA_SIGINFO; /* Set sSigAction to indicate that mySigAction is the signal handling function. */ sSigAction.sa_sigaction = mySigAction; /* Set sSigAction to indicate that no signals should be blocked during execution of the signal handler. */ sigemptyset(&sSigAction.sa_mask); /* Register sSigAction as the structure containing information to be used when this process receives a SIGINT signal. The current state is assigned to sOldSigAction, in case we must restore it later. */ sigaction(SIGINT, &sSigAction, &sOldSigAction); printf("Entering an infinite loop\n"); for (;;) ; return 0; } /* Sample execution: $ gcc -Wall -ansi -pedantic -o testsigaction testsigaction.c $ testsigaction Entering an infinite loop ^C-------------------------------------------- In mySigAction with argument 2 Signal number: 2 Error code: 0 Signal code: 128 Register EAX: 1a Register EBX: 421328d4 Register ECX: 42130d00 Register EDX: 42130d00 Register ESP: bfffe430 Register EBP: bfffe558 Register EIP: 8048538 -------------------------------------------- ^C-------------------------------------------- In mySigAction with argument 2 Signal number: 2 Error code: 0 Signal code: 128 Register EAX: 1a Register EBX: 421328d4 Register ECX: 42130d00 Register EDX: 42130d00 Register ESP: bfffe430 Register EBP: bfffe558 Register EIP: 8048538 -------------------------------------------- */