src/SDL_fatal.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Sep 2005 09:43:00 +0000
changeset 1152 51a8702d8ecd
parent 861 863da1c38c7e
child 1312 c9b51268668f
permissions -rw-r--r--
Updates to PocketPC (WinCE) support, thanks to Dmitry Yakimov at
activekitten.com.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
icculus@1152
    28
#ifdef _WIN32_WCE
icculus@1152
    29
#define NO_SIGNAL_H
icculus@1152
    30
#endif
icculus@1152
    31
 
slouken@0
    32
/* General fatal signal handling code for SDL */
slouken@0
    33
slouken@0
    34
#ifdef NO_SIGNAL_H
slouken@0
    35
slouken@0
    36
/* No signals on this platform, nothing to do.. */
slouken@0
    37
slouken@0
    38
void SDL_InstallParachute(void)
slouken@0
    39
{
slouken@0
    40
	return;
slouken@0
    41
}
slouken@0
    42
slouken@0
    43
void SDL_UninstallParachute(void)
slouken@0
    44
{
slouken@0
    45
	return;
slouken@0
    46
}
slouken@0
    47
slouken@0
    48
#else
slouken@0
    49
slouken@0
    50
#include <stdlib.h>
slouken@0
    51
#include <stdio.h>
slouken@0
    52
#include <signal.h>
slouken@0
    53
#include <string.h>
slouken@0
    54
slouken@0
    55
#include "SDL.h"
slouken@0
    56
#include "SDL_fatal.h"
slouken@0
    57
slouken@1
    58
#ifdef __CYGWIN__
slouken@1
    59
#define DISABLE_STDIO
slouken@1
    60
#endif
slouken@1
    61
slouken@0
    62
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    63
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    64
   the program crashes.
slouken@0
    65
*/
slouken@0
    66
slouken@1
    67
static void print_msg(const char *text)
slouken@1
    68
{
slouken@1
    69
#ifndef DISABLE_STDIO
slouken@1
    70
	fprintf(stderr, "%s", text);
slouken@1
    71
#endif
slouken@1
    72
}
slouken@1
    73
slouken@0
    74
static void SDL_Parachute(int sig)
slouken@0
    75
{
slouken@0
    76
	signal(sig, SIG_DFL);
slouken@1
    77
	print_msg("Fatal signal: ");
slouken@0
    78
	switch (sig) {
slouken@0
    79
		case SIGSEGV:
slouken@1
    80
			print_msg("Segmentation Fault");
slouken@0
    81
			break;
slouken@0
    82
#ifdef SIGBUS
slouken@0
    83
#if SIGBUS != SIGSEGV
slouken@0
    84
		case SIGBUS:
slouken@1
    85
			print_msg("Bus Error");
slouken@0
    86
			break;
slouken@0
    87
#endif
slouken@0
    88
#endif /* SIGBUS */
slouken@0
    89
#ifdef SIGFPE
slouken@0
    90
		case SIGFPE:
slouken@1
    91
			print_msg("Floating Point Exception");
slouken@0
    92
			break;
slouken@0
    93
#endif /* SIGFPE */
slouken@0
    94
#ifdef SIGQUIT
slouken@0
    95
		case SIGQUIT:
slouken@1
    96
			print_msg("Keyboard Quit");
slouken@0
    97
			break;
slouken@0
    98
#endif /* SIGQUIT */
slouken@0
    99
#ifdef SIGPIPE
slouken@0
   100
		case SIGPIPE:
slouken@1
   101
			print_msg("Broken Pipe");
slouken@0
   102
			break;
slouken@0
   103
#endif /* SIGPIPE */
slouken@0
   104
		default:
slouken@1
   105
#ifndef DISABLE_STDIO
slouken@0
   106
			fprintf(stderr, "# %d", sig);
slouken@1
   107
#endif
slouken@0
   108
			break;
slouken@0
   109
	}
slouken@1
   110
	print_msg(" (SDL Parachute Deployed)\n");
slouken@0
   111
	SDL_Quit();
slouken@0
   112
	exit(-sig);
slouken@0
   113
}
slouken@0
   114
slouken@0
   115
static int SDL_fatal_signals[] = {
slouken@0
   116
	SIGSEGV,
slouken@0
   117
#ifdef SIGBUS
slouken@0
   118
	SIGBUS,
slouken@0
   119
#endif
slouken@0
   120
#ifdef SIGFPE
slouken@0
   121
	SIGFPE,
slouken@0
   122
#endif
slouken@0
   123
#ifdef SIGQUIT
slouken@0
   124
	SIGQUIT,
slouken@0
   125
#endif
slouken@0
   126
	0
slouken@0
   127
};
slouken@0
   128
slouken@0
   129
void SDL_InstallParachute(void)
slouken@0
   130
{
slouken@814
   131
	/* Set a handler for any fatal signal not already handled */
slouken@0
   132
	int i;
slouken@814
   133
#ifdef HAVE_SIGACTION
slouken@814
   134
	struct sigaction action;
slouken@814
   135
slouken@814
   136
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
   137
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
   138
		if ( action.sa_handler == SIG_DFL ) {
slouken@814
   139
			action.sa_handler = SDL_Parachute;
slouken@814
   140
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
   141
		}
slouken@814
   142
	}
slouken@814
   143
#ifdef SIGALRM
slouken@814
   144
	/* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@814
   145
	sigaction(SIGALRM, NULL, &action);
slouken@814
   146
	if ( action.sa_handler == SIG_DFL ) {
slouken@814
   147
		action.sa_handler = SIG_IGN;
slouken@861
   148
		sigaction(SIGALRM, &action, NULL);
slouken@814
   149
	}
slouken@814
   150
#endif
slouken@814
   151
#else
slouken@0
   152
	void (*ohandler)(int);
slouken@0
   153
slouken@0
   154
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   155
		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@0
   156
		if ( ohandler != SIG_DFL ) {
slouken@0
   157
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   158
		}
slouken@0
   159
	}
slouken@814
   160
#endif /* HAVE_SIGACTION */
slouken@0
   161
	return;
slouken@0
   162
}
slouken@0
   163
slouken@0
   164
void SDL_UninstallParachute(void)
slouken@0
   165
{
slouken@814
   166
	/* Remove a handler for any fatal signal handled */
slouken@0
   167
	int i;
slouken@814
   168
#ifdef HAVE_SIGACTION
slouken@814
   169
	struct sigaction action;
slouken@814
   170
slouken@814
   171
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
   172
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
   173
		if ( action.sa_handler == SDL_Parachute ) {
slouken@814
   174
			action.sa_handler = SIG_DFL;
slouken@814
   175
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
   176
		}
slouken@814
   177
	}
slouken@814
   178
#else
slouken@0
   179
	void (*ohandler)(int);
slouken@0
   180
slouken@0
   181
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   182
		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@0
   183
		if ( ohandler != SDL_Parachute ) {
slouken@0
   184
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   185
		}
slouken@0
   186
	}
slouken@814
   187
#endif /* HAVE_SIGACTION */
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
#endif /* NO_SIGNAL_H */