19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* General fatal signal handling code for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGNAL_H
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_fatal.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This installs some signal handlers for the more common fatal signals,
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   so that if the programmer is lazy, the app doesn't die so horribly if
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   the program crashes.
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_Parachute(int sig)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	signal(sig, SIG_DFL);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	raise(sig);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_fatal_signals[] = {
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGSEGV,
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGBUS
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGBUS,
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGFPE
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGFPE,
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGQUIT
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SIGQUIT,
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_InstallParachute(void)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set a handler for any fatal signal not already handled */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGACTION
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct sigaction action;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; SDL_fatal_signals[i]; ++i ) {
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sigaction(SDL_fatal_signals[i], NULL, &action);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( action.sa_handler == SIG_DFL ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			action.sa_handler = SDL_Parachute;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sigaction(SDL_fatal_signals[i], &action, NULL);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SIGALRM
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set SIGALRM to be ignored -- necessary on Solaris */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sigaction(SIGALRM, NULL, &action);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( action.sa_handler == SIG_DFL ) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		action.sa_handler = SIG_IGN;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sigaction(SIGALRM, &action, NULL);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void (*ohandler)(int);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; SDL_fatal_signals[i]; ++i ) {
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ohandler != SIG_DFL ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			signal(SDL_fatal_signals[i], ohandler);
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGACTION */
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_UninstallParachute(void)
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Remove a handler for any fatal signal handled */
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGACTION
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct sigaction action;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; SDL_fatal_signals[i]; ++i ) {
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sigaction(SDL_fatal_signals[i], NULL, &action);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( action.sa_handler == SDL_Parachute ) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			action.sa_handler = SIG_DFL;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sigaction(SDL_fatal_signals[i], &action, NULL);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void (*ohandler)(int);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; SDL_fatal_signals[i]; ++i ) {
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ohandler != SDL_Parachute ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			signal(SDL_fatal_signals[i], ohandler);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGACTION */
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* No signals on this platform, nothing to do.. */
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_InstallParachute(void)
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_UninstallParachute(void)
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_SIGNAL_H */
135