Replacing signal(2) with sigaction(2)

Hello,

I’ve been experimenting with replacing the usage of signal(2) function
in SDL_fatal.h with sigaction(2), at least where it is important to
preserve pre-existing sigaction flags.

This problem was noticed by users of SBCL, which makes heavy usage of
signal handling and sigaction flags, and CL-SDL. While the original
code does restore the handler, it does not restore the sigaction flags.

I am proposing a change which uses sigaction(2) to query the current
handler, and only replace if necessary.

Also, signal(2) man page says that usage of sigaction(2) is preferable.

Patch attached, feel free to reformat if necessary. Please CC me on any
replies, I am not subscribed.

Thanks,–
; Matthew Danish <@Matthew_Danish>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; “There is no dark side of the moon really; matter of fact, it’s all dark.”
-------------- next part --------------
— SDL_fatal.c.old Fri Feb 7 08:02:17 2003
+++ SDL_fatal.c Fri Feb 7 08:01:03 2003
@@ -128,15 +128,20 @@
void SDL_InstallParachute(void)
{
int i;

  • void (*ohandler)(int);
  •    struct sigaction oldact;
    

    /* Set a handler for any fatal signal not already handled */

  • for ( i=0; SDL_fatal_signals[i]; ++i ) {
  •   ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    
  •   if ( ohandler != SIG_DFL ) {
    
  •   	signal(SDL_fatal_signals[i], ohandler);
    
  •   }
    
  • }
  •    /* sigaction properly preserves sigaction flags */
    
  •    for ( i=0; SDL_fatal_signals[i]; ++i ) {
    
  •      /* obtain current sigaction structure */
    
  •      sigaction(SDL_fatal_signals[i], NULL, &oldact);
    
  •      if ( oldact.sa_handler == SIG_DFL ) {
    
  •        oldact.sa_handler = SDL_Parachute;
    
  •        sigaction(SDL_fatal_signals[i], &oldact, NULL);
    
  •      }
    
  •    }
    

#ifdef SIGALRM
/* Set SIGALRM to be ignored – necessary on Solaris */
{
@@ -159,15 +164,19 @@
void SDL_UninstallParachute(void)
{
int i;

  • void (*ohandler)(int);
  •    struct sigaction oldact;
    

    /* Remove a handler for any fatal signal handled */

  • for ( i=0; SDL_fatal_signals[i]; ++i ) {
  •   ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
    
  •   if ( ohandler != SDL_Parachute ) {
    
  •   	signal(SDL_fatal_signals[i], ohandler);
    
  •   }
    
  • }
  •    for ( i=0; SDL_fatal_signals[i]; ++i ) {
    
  •      /* query signal handler */
    
  •      sigaction(SDL_fatal_signals[i], NULL, &oldact);
    
  •      if ( oldact.sa_handler == SDL_Parachute ) {
    
  •        /* reset to default */
    
  •        oldact.sa_handler = SIG_DFL;
    
  •        sigaction(SDL_fatal_signals[i], &oldact, NULL);
    
  •      }
    
  •    }
    

}

#endif /* NO_SIGNAL_H */

Hello,

I’ve been experimenting with replacing the usage of signal(2) function
in SDL_fatal.c with sigaction(2), at least where it is important to
preserve pre-existing sigaction flags.

This problem was noticed by users of SBCL, which makes heavy usage of
signal handling and sigaction flags, and CL-SDL. While the original
code does restore the handler, it does not restore the sigaction flags.

I am proposing a change which uses sigaction(2) to query the current
handler, and only replace if necessary.

Also, signal(2) man page says that usage of sigaction(2) is preferable.

Patch attached for SDL_fatal.c.

Thanks,

P.S. Hopefully this isn’t posted twice; originally I sent it without being
subscribed, then decided to subscribe and resend it.–
; Matthew Danish <@Matthew_Danish>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; “There is no dark side of the moon really; matter of fact, it’s all dark.”
-------------- next part --------------
— SDL_fatal.c.old Fri Feb 7 08:02:17 2003
+++ SDL_fatal.c Fri Feb 7 08:01:03 2003
@@ -128,15 +128,20 @@
void SDL_InstallParachute(void)
{
int i;

  • void (*ohandler)(int);
  •    struct sigaction oldact;
    

    /* Set a handler for any fatal signal not already handled */

  • for ( i=0; SDL_fatal_signals[i]; ++i ) {
  •   ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    
  •   if ( ohandler != SIG_DFL ) {
    
  •   	signal(SDL_fatal_signals[i], ohandler);
    
  •   }
    
  • }
  •    /* sigaction properly preserves sigaction flags */
    
  •    for ( i=0; SDL_fatal_signals[i]; ++i ) {
    
  •      /* obtain current sigaction structure */
    
  •      sigaction(SDL_fatal_signals[i], NULL, &oldact);
    
  •      if ( oldact.sa_handler == SIG_DFL ) {
    
  •        oldact.sa_handler = SDL_Parachute;
    
  •        sigaction(SDL_fatal_signals[i], &oldact, NULL);
    
  •      }
    
  •    }
    

#ifdef SIGALRM
/* Set SIGALRM to be ignored – necessary on Solaris */
{
@@ -159,15 +164,19 @@
void SDL_UninstallParachute(void)
{
int i;

  • void (*ohandler)(int);
  •    struct sigaction oldact;
    

    /* Remove a handler for any fatal signal handled */

  • for ( i=0; SDL_fatal_signals[i]; ++i ) {
  •   ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
    
  •   if ( ohandler != SDL_Parachute ) {
    
  •   	signal(SDL_fatal_signals[i], ohandler);
    
  •   }
    
  • }
  •    for ( i=0; SDL_fatal_signals[i]; ++i ) {
    
  •      /* query signal handler */
    
  •      sigaction(SDL_fatal_signals[i], NULL, &oldact);
    
  •      if ( oldact.sa_handler == SDL_Parachute ) {
    
  •        /* reset to default */
    
  •        oldact.sa_handler = SIG_DFL;
    
  •        sigaction(SDL_fatal_signals[i], &oldact, NULL);
    
  •      }
    
  •    }
    

}

#endif /* NO_SIGNAL_H */