src/events/SDL_quit.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Mar 2011 21:36:36 -0700
branchSDL-1.2
changeset 5492 b214b7ce9b00
parent 4159 a1b03ba2fcd0
child 5500 27348c0ae529
permissions -rw-r--r--
Do not break application's signal handler installed with SA_SIGINFO

Gleb Natapov to sdl

If application installs SIGINT/SIGTERM signal handler with
sigaction(SA_SIGINFO) syscall before initializing SDL, after
initialization
of SDL signal handler will be reinstalled without SA_SIGINFO flag which
brings havoc when the signal handler is called. The breakage is done by
SDL_QuitInit()/SDL_QuitQuit() function. They use signal() to detect that
signal handler is already installed even in sigaction() is available.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* General quit handling code for SDL */
    25 
    26 #ifdef HAVE_SIGNAL_H
    27 #include <signal.h>
    28 #endif
    29 
    30 #include "SDL_events.h"
    31 #include "SDL_events_c.h"
    32 
    33 
    34 #ifdef HAVE_SIGNAL_H
    35 static void SDL_HandleSIG(int sig)
    36 {
    37 	/* Reset the signal handler */
    38 	signal(sig, SDL_HandleSIG);
    39 
    40 	/* Signal a quit interrupt */
    41 	SDL_PrivateQuit();
    42 }
    43 #endif /* HAVE_SIGNAL_H */
    44 
    45 /* Public functions */
    46 int SDL_QuitInit(void)
    47 {
    48 #ifdef HAVE_SIGACTION
    49 	struct sigaction action;
    50 	sigaction(SIGINT, NULL, &action);
    51 	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == SIG_DFL ) {
    52 		action.sa_handler = SDL_HandleSIG;
    53 		sigaction(SIGINT, &action, NULL);
    54 	}
    55 	sigaction(SIGTERM, NULL, &action);
    56 	if ( action.sa_handler == SIG_DFL && action.sa_sigaction == SIG_DFL ) {
    57 		action.sa_handler = SDL_HandleSIG;
    58 		sigaction(SIGTERM, &action, NULL);
    59 	}
    60 #elif HAVE_SIGNAL_H
    61 	void (*ohandler)(int);
    62 
    63 	/* Both SIGINT and SIGTERM are translated into quit interrupts */
    64 	ohandler = signal(SIGINT, SDL_HandleSIG);
    65 	if ( ohandler != SIG_DFL )
    66 		signal(SIGINT, ohandler);
    67 	ohandler = signal(SIGTERM, SDL_HandleSIG);
    68 	if ( ohandler != SIG_DFL )
    69 		signal(SIGTERM, ohandler);
    70 #endif /* HAVE_SIGNAL_H */
    71 
    72 	/* That's it! */
    73 	return(0);
    74 }
    75 void SDL_QuitQuit(void)
    76 {
    77 #ifdef HAVE_SIGACTION
    78 	struct sigaction action;
    79 	sigaction(SIGINT, NULL, &action);
    80 	if ( action.sa_handler == SDL_HandleSIG ) {
    81 		action.sa_handler = SIG_DFL;
    82 		sigaction(SIGINT, &action, NULL);
    83 	}
    84 	sigaction(SIGTERM, NULL, &action);
    85 	if ( action.sa_handler == SDL_HandleSIG ) {
    86 		action.sa_handler = SIG_DFL;
    87 		sigaction(SIGTERM, &action, NULL);
    88 	}
    89 #elif HAVE_SIGNAL_H
    90 	void (*ohandler)(int);
    91 
    92 	ohandler = signal(SIGINT, SIG_DFL);
    93 	if ( ohandler != SDL_HandleSIG )
    94 		signal(SIGINT, ohandler);
    95 	ohandler = signal(SIGTERM, SIG_DFL);
    96 	if ( ohandler != SDL_HandleSIG )
    97 		signal(SIGTERM, ohandler);
    98 #endif /* HAVE_SIGNAL_H */
    99 }
   100 
   101 /* This function returns 1 if it's okay to close the application window */
   102 int SDL_PrivateQuit(void)
   103 {
   104 	int posted;
   105 
   106 	posted = 0;
   107 	if ( SDL_ProcessEvents[SDL_QUIT] == SDL_ENABLE ) {
   108 		SDL_Event event;
   109 		event.type = SDL_QUIT;
   110 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   111 			posted = 1;
   112 			SDL_PushEvent(&event);
   113 		}
   114 	}
   115 	return(posted);
   116 }