diff --git a/sigtest.c b/sigtest.c new file mode 100644 index 0000000..9b07855 --- /dev/null +++ b/sigtest.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +const int SIGNAL_TYPE = SIGSEGV; + +// Global flag and jump buffer for recovery +static jmp_buf env_buffer; +static volatile int handled = 0; + +void handler(int signal_num) { + handled += 1; + longjmp(env_buffer, 1); +} + +int trigger() { + if(setjmp(env_buffer)) { + return 1; + } + //volatile char _ = *((char*)0); + raise(SIGNAL_TYPE); + return 0; +} + +int printSigMasks() { + sigset_t set; + // Get the current signal mask + if (sigprocmask(SIG_BLOCK, NULL, &set) == -1) { + perror("sigprocmask"); + return 1; + } + // Print blocked signal masks + int blocked = 0; + for (int i = 1; i < NSIG; ++i) { + if (sigismember(&set, i)) { + blocked += 1; + printf("Signal %d is blocked\n", i); + } + } + if(!blocked) { + printf("No blocked signals\n"); + } + return 0; +} + +int main(int argc, char *argv[]) { + printf("NSIG = %i\n", NSIG); + + void* sig_res = signal(SIGNAL_TYPE, handler); + if(sig_res == SIG_ERR) { + printf("Error setting signal handler\n"); + exit(EXIT_FAILURE); + } + + int result = 0; + printSigMasks(); + result = trigger(); + printf("Raised = %i\tHandled = %i\n", result, handled); + printSigMasks(); + result = trigger(); + printf("Raised = %i\tHandled = %i\n", result, handled); + printSigMasks(); + + return EXIT_SUCCESS; +}