src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1402 d910939febfa
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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 
    23 /* General fatal signal handling code for SDL */
    24 
    25 #include "SDL_config.h"
    26 
    27 #ifdef HAVE_SIGNAL_H
    28 
    29 #include <signal.h>
    30 
    31 #include "SDL.h"
    32 #include "SDL_fatal.h"
    33 
    34 /* This installs some signal handlers for the more common fatal signals,
    35    so that if the programmer is lazy, the app doesn't die so horribly if
    36    the program crashes.
    37 */
    38 
    39 static void SDL_Parachute(int sig)
    40 {
    41 	signal(sig, SIG_DFL);
    42 	SDL_Quit();
    43 	raise(sig);
    44 }
    45 
    46 static int SDL_fatal_signals[] = {
    47 	SIGSEGV,
    48 #ifdef SIGBUS
    49 	SIGBUS,
    50 #endif
    51 #ifdef SIGFPE
    52 	SIGFPE,
    53 #endif
    54 #ifdef SIGQUIT
    55 	SIGQUIT,
    56 #endif
    57 	0
    58 };
    59 
    60 void SDL_InstallParachute(void)
    61 {
    62 	/* Set a handler for any fatal signal not already handled */
    63 	int i;
    64 #ifdef HAVE_SIGACTION
    65 	struct sigaction action;
    66 
    67 	for ( i=0; SDL_fatal_signals[i]; ++i ) {
    68 		sigaction(SDL_fatal_signals[i], NULL, &action);
    69 		if ( action.sa_handler == SIG_DFL ) {
    70 			action.sa_handler = SDL_Parachute;
    71 			sigaction(SDL_fatal_signals[i], &action, NULL);
    72 		}
    73 	}
    74 #ifdef SIGALRM
    75 	/* Set SIGALRM to be ignored -- necessary on Solaris */
    76 	sigaction(SIGALRM, NULL, &action);
    77 	if ( action.sa_handler == SIG_DFL ) {
    78 		action.sa_handler = SIG_IGN;
    79 		sigaction(SIGALRM, &action, NULL);
    80 	}
    81 #endif
    82 #else
    83 	void (*ohandler)(int);
    84 
    85 	for ( i=0; SDL_fatal_signals[i]; ++i ) {
    86 		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    87 		if ( ohandler != SIG_DFL ) {
    88 			signal(SDL_fatal_signals[i], ohandler);
    89 		}
    90 	}
    91 #endif /* HAVE_SIGACTION */
    92 	return;
    93 }
    94 
    95 void SDL_UninstallParachute(void)
    96 {
    97 	/* Remove a handler for any fatal signal handled */
    98 	int i;
    99 #ifdef HAVE_SIGACTION
   100 	struct sigaction action;
   101 
   102 	for ( i=0; SDL_fatal_signals[i]; ++i ) {
   103 		sigaction(SDL_fatal_signals[i], NULL, &action);
   104 		if ( action.sa_handler == SDL_Parachute ) {
   105 			action.sa_handler = SIG_DFL;
   106 			sigaction(SDL_fatal_signals[i], &action, NULL);
   107 		}
   108 	}
   109 #else
   110 	void (*ohandler)(int);
   111 
   112 	for ( i=0; SDL_fatal_signals[i]; ++i ) {
   113 		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
   114 		if ( ohandler != SDL_Parachute ) {
   115 			signal(SDL_fatal_signals[i], ohandler);
   116 		}
   117 	}
   118 #endif /* HAVE_SIGACTION */
   119 }
   120 
   121 #else
   122 
   123 /* No signals on this platform, nothing to do.. */
   124 
   125 void SDL_InstallParachute(void)
   126 {
   127 	return;
   128 }
   129 
   130 void SDL_UninstallParachute(void)
   131 {
   132 	return;
   133 }
   134 
   135 #endif /* HAVE_SIGNAL_H */