A simple test for the signal handling in C and on Linux:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <setjmp.h>
#define LOOP 5
static int* ptrs[LOOP];
static struct sigaction act;
static sigjmp_buf env;
static void sigsegvhandler(int sig, siginfo_t *siginfo, void *ptr) {
siglongjmp(env, 1);
}
void func(int x) {
printf("func(%d)...\n", x);
if (sigsetjmp(env, 1) == 0) {
*ptrs[x] = x;
if (x == 4) {
raise (SIGSEGV);
}
} else {
printf("SIGSEGV recovery.\n");
}
printf("func(%d) done.\n", x);
}
int main(int argc, char *argv[]) {
int x = 0;
int a = 0;
int b = 0;
int c = 0;
ptrs[0] = &a;
ptrs[1] = NULL;
ptrs[2] = &b;
ptrs[3] = NULL;
ptrs[4] = &c;
printf("Hello SIGSEGV test!\n");
printf("a=%d b=%d c=%d x=%d\n", a, b, c, x);
memset(&act, 0, sizeof(act));
act.sa_sigaction = sigsegvhandler;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGSEGV, &act, NULL)) {
perror("sigaction");
return 1;
}
while (x < 5) {
printf("\nLoop x=%d...\n", x);
func(x);
x++;
}
printf("\na=%d b=%d c=%d x=%d\n", a, b, c, x);
printf("Done.\n");
return 0;
}
Output:
Hello SIGSEGV test!
a=0 b=0 c=0 x=0
Loop x=0...
func(0)...
func(0) done.
Loop x=1...
func(1)...
SIGSEGV recovery.
func(1) done.
Loop x=2...
func(2)...
func(2) done.
Loop x=3...
func(3)...
SIGSEGV recovery.
func(3) done.
Loop x=4...
func(4)...
SIGSEGV recovery.
func(4) done.
a=0 b=2 c=4 x=5
Done.
No comments:
Post a Comment