Reputation: 12074
I have written a segmentation fault handler, using sigsetjmp and siglongjmp. Once it goes to the signal handler, i invoke siglongjmp so that the faulty instruction is skipped.
Problem is, i again want to cause SIGSEGV and go to the same handler, but now sigsetjmp will return 1.
How to reset sigsetjmp?
Here is my code:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
sigjmp_buf env, env1;
void SIGSEGV_handler(int signal)
{
printf("Segmentation fault caught\n");
siglongjmp(env, 1);
}
int main()
{
void * allocation;
size_t size;
static int devZerofd = -1;
struct sigaction sa, sa1;
sa.sa_handler=(void*)SIGSEGV_handler;
sigaction(SIGSEGV, &sa, NULL);
if ( devZerofd == -1 ) {
devZerofd = open("/dev/zero", O_RDWR);
if ( devZerofd < 0 )
perror("open() on /dev/zero failed");
}
allocation = (caddr_t) mmap(0, 5000, PROT_READ|PROT_NONE, MAP_PRIVATE, devZerofd, 0);
if ( allocation == (caddr_t)-1 )
fprintf(stderr, "mmap() failed ");
if ( mprotect((caddr_t)allocation, 5000, PROT_NONE) < 0 )
fprintf(stderr, "mprotect failed");
else
printf("mprotect done: memory allocated at address %u\n",allocation);
if(sigsetjmp(env, 1)==0) {
printf("Causing SIGSEGV: 1\n");
strcpy(allocation,"Hello, how are you");
}
/****** This can't be done again as sigsetjmp won't return 0*****/
/*
if(sigsetjmp(env, 1)==0) {
printf("Causing SIGSEGV: 1\n");
strcpy(allocation,"Hello, how are you");
}
*/
}
Upvotes: 1
Views: 1850
Reputation: 140718
You have misunderstood how [sig]setjmp
works. If you uncomment the code that you think won't work, compile it, and run it, you will see that it does in fact work.
setjmp
cannot be made to return zero by calling longjmp
. If you call setjmp
itself a second time, even with the same jmp_buf
(as you do here), it will return zero a second time.
You have a bug, by the way: you didn't set up your sigaction
parameter structure correctly. You should have done this:
struct sigaction sa;
sa.sa_handler = SIGSEGV_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGSEGV, &sa, 0);
The use of mmap
is a little infelicitous, but not actually buggy. You don't need /dev/zero
on most current platforms, you can just use MAP_ANON
(some platforms spell it MAP_ANONYMOUS
) and a -1 fd argument. And you should be using getpagesize
and then asking for a whole number of pages.
Upvotes: 7