This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 2nd occurrence of signal not being handled?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Mark Geisert on 12/18/2009 10:23 PM:
> Or maybe there's some subtle
> mistake in my use of the signal functions.  Any advice would be appreciated.
> 
> ..mark
> 
> #include <setjmp.h>
> #include <signal.h>
> #include <stdio.h>
> 
> volatile int    step = 0;
> sigjmp_buf      trapoline;
> 
> void
> segv_handler(int sig)
> {
>     printf("case %d: SIGSEGV handled\n", step++);
>     siglongjmp(trapoline, step);

Within this handler, SIGSEGV is still blocked.  Why?  because...

> }
> 
> int
> main()
> {
>     struct sigaction    sa;
> 
>     sa.sa_handler = segv_handler;
>     sa.sa_flags   = 0;

...you didn't request SA_NODEFER.  But calling siglongjmp from within the
handler only restores the original mask (and thus unblocking SIGSEGV), if...

>     sigemptyset(&sa.sa_mask);
> 
>     sigaction(SIGSEGV, &sa, NULL);
> 
>     switch(sigsetjmp(trapoline, 0)) {

...you call sigsetjmp with a non-zero savemask argument.  Therefore...

>         case 0:
>             printf("case %d reached\n", step);
>             printf("case %d: %08X\n", *(int *) 42);
>             ++step;
> 
>         case 1:

...when you get here, your signal mask still includes SIGSEGV...

>             printf("case %d reached\n", step);
>             printf("case %d: %08X\n", *(int *) 42);

...and causing a fault when SIGSEGV is blocked is fatal.  Meanwhile, it
didn't help that your program has undefined behavior: since you didn't
pass enough arguments to printf, the program is allowed to do whatever it
wants.

>             ++step;
> 
>         default:
>             printf("case %d reached\n", step);
>             break;
>     }
> 
>     return 0;
> }

And as far as I can tell, your problem is not cygwin-specific.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkss8aAACgkQ84KuGfSFAYA1LgCggTQg11EsbFzySEz73JkNZLkM
/cAAoLsynutWk+vKcDY6OHzdGnj2/vDx
=FNAm
-----END PGP SIGNATURE-----

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]