src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1330 450721ad5436
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General fatal signal handling code for SDL */
slouken@0
    25
slouken@1330
    26
#ifdef HAVE_SIGNAL_H
slouken@0
    27
slouken@0
    28
#include <signal.h>
slouken@0
    29
slouken@0
    30
#include "SDL.h"
slouken@0
    31
#include "SDL_fatal.h"
slouken@0
    32
slouken@0
    33
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    34
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    35
   the program crashes.
slouken@0
    36
*/
slouken@0
    37
slouken@0
    38
static void SDL_Parachute(int sig)
slouken@0
    39
{
slouken@0
    40
	signal(sig, SIG_DFL);
slouken@0
    41
	SDL_Quit();
slouken@1330
    42
	raise(sig);
slouken@0
    43
}
slouken@0
    44
slouken@0
    45
static int SDL_fatal_signals[] = {
slouken@0
    46
	SIGSEGV,
slouken@0
    47
#ifdef SIGBUS
slouken@0
    48
	SIGBUS,
slouken@0
    49
#endif
slouken@0
    50
#ifdef SIGFPE
slouken@0
    51
	SIGFPE,
slouken@0
    52
#endif
slouken@0
    53
#ifdef SIGQUIT
slouken@0
    54
	SIGQUIT,
slouken@0
    55
#endif
slouken@0
    56
	0
slouken@0
    57
};
slouken@0
    58
slouken@0
    59
void SDL_InstallParachute(void)
slouken@0
    60
{
slouken@814
    61
	/* Set a handler for any fatal signal not already handled */
slouken@0
    62
	int i;
slouken@814
    63
#ifdef HAVE_SIGACTION
slouken@814
    64
	struct sigaction action;
slouken@814
    65
slouken@814
    66
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
    67
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
    68
		if ( action.sa_handler == SIG_DFL ) {
slouken@814
    69
			action.sa_handler = SDL_Parachute;
slouken@814
    70
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
    71
		}
slouken@814
    72
	}
slouken@814
    73
#ifdef SIGALRM
slouken@814
    74
	/* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@814
    75
	sigaction(SIGALRM, NULL, &action);
slouken@814
    76
	if ( action.sa_handler == SIG_DFL ) {
slouken@814
    77
		action.sa_handler = SIG_IGN;
slouken@861
    78
		sigaction(SIGALRM, &action, NULL);
slouken@814
    79
	}
slouken@814
    80
#endif
slouken@814
    81
#else
slouken@0
    82
	void (*ohandler)(int);
slouken@0
    83
slouken@0
    84
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
    85
		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@0
    86
		if ( ohandler != SIG_DFL ) {
slouken@0
    87
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
    88
		}
slouken@0
    89
	}
slouken@814
    90
#endif /* HAVE_SIGACTION */
slouken@0
    91
	return;
slouken@0
    92
}
slouken@0
    93
slouken@0
    94
void SDL_UninstallParachute(void)
slouken@0
    95
{
slouken@814
    96
	/* Remove a handler for any fatal signal handled */
slouken@0
    97
	int i;
slouken@814
    98
#ifdef HAVE_SIGACTION
slouken@814
    99
	struct sigaction action;
slouken@814
   100
slouken@814
   101
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
   102
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
   103
		if ( action.sa_handler == SDL_Parachute ) {
slouken@814
   104
			action.sa_handler = SIG_DFL;
slouken@814
   105
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
   106
		}
slouken@814
   107
	}
slouken@814
   108
#else
slouken@0
   109
	void (*ohandler)(int);
slouken@0
   110
slouken@0
   111
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   112
		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@0
   113
		if ( ohandler != SDL_Parachute ) {
slouken@0
   114
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   115
		}
slouken@0
   116
	}
slouken@814
   117
#endif /* HAVE_SIGACTION */
slouken@0
   118
}
slouken@0
   119
slouken@1330
   120
#else
slouken@1330
   121
slouken@1330
   122
/* No signals on this platform, nothing to do.. */
slouken@1330
   123
slouken@1330
   124
void SDL_InstallParachute(void)
slouken@1330
   125
{
slouken@1330
   126
	return;
slouken@1330
   127
}
slouken@1330
   128
slouken@1330
   129
void SDL_UninstallParachute(void)
slouken@1330
   130
{
slouken@1330
   131
	return;
slouken@1330
   132
}
slouken@1330
   133
slouken@1330
   134
#endif /* HAVE_SIGNAL_H */