Add sigtest to check signal mask
This commit is contained in:
parent
11e9135f06
commit
301365968a
66
sigtest.c
Normal file
66
sigtest.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user