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