c - Cannot handle consecutive SIGSEGV signal -
i trying handle sigsegv signal code. have written following code in c under fedora 15. problem signal handler not getting called second segmentation fault. can kindly point out doing wrong.
typedef struct _st_demo { int m_iunused; _st_demo() { m_iunused = 0; } } st_demo; jmp_buf ex_buf__; static void sig_hdl (int sig, siginfo_t *siginfo, void *context) { cout<<"inside signal handler."<<endl; longjmp(ex_buf__, 1); } int main (int argc, char *argv[]) { st_demo* pstdemo = 0; struct sigaction act; memset (&act, '\0', sizeof(act)); /* use sa_sigaction field because handles has 2 additional parameters */ act.sa_sigaction = &sig_hdl; /* sa_siginfo flag tells sigaction() use sa_sigaction field, not sa_handler. */ act.sa_flags = sa_siginfo; if (sigaction(sigsegv, &act, null) < 0) { perror ("sigaction"); return 1; } if(!setjmp(ex_buf__)) { cout<<"before first seg fault."<<endl; cout<<pstdemo->m_iunused<<endl; } else { cout<<"after jump."<<endl; } cout<<"before second seg fault."<<endl; cout<<pstdemo->m_iunused<<endl; while(1) { sleep(1); } return 0; }
your longjmp
cause jump location, not have returned signal handler. means signal still blocked (this default behavior signals, masked until have returned signal handler).
you can fix indicating want signal occur again clearing signal mask in handler before longjmp
.
- use
sa_nodefer
flag inact.sa_flags
prevent being masked in first place. - use
siglongjmp
/sigsetjmp
functions, saves mask you
or
- call sigprocmask either before or after longjmp unmask yourself.
a warning: dangerous thing (catch sigsegv, , longjmp out of signal handler) , impossible useful it.
if memory access error occurs in function not async signal safe , reentrant not able continue in kind of sane way anyway.
but since there multiple similar questions on site guess kind of exercise.
related question: catching segmentation violations , getting on life
also useful
Comments
Post a Comment