src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1152 51a8702d8ecd
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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
icculus@1152
    23
#ifdef _WIN32_WCE
icculus@1152
    24
#define NO_SIGNAL_H
icculus@1152
    25
#endif
icculus@1152
    26
 
slouken@0
    27
/* General fatal signal handling code for SDL */
slouken@0
    28
slouken@0
    29
#ifdef NO_SIGNAL_H
slouken@0
    30
slouken@0
    31
/* No signals on this platform, nothing to do.. */
slouken@0
    32
slouken@0
    33
void SDL_InstallParachute(void)
slouken@0
    34
{
slouken@0
    35
	return;
slouken@0
    36
}
slouken@0
    37
slouken@0
    38
void SDL_UninstallParachute(void)
slouken@0
    39
{
slouken@0
    40
	return;
slouken@0
    41
}
slouken@0
    42
slouken@0
    43
#else
slouken@0
    44
slouken@0
    45
#include <stdlib.h>
slouken@0
    46
#include <stdio.h>
slouken@0
    47
#include <signal.h>
slouken@0
    48
#include <string.h>
slouken@0
    49
slouken@0
    50
#include "SDL.h"
slouken@0
    51
#include "SDL_fatal.h"
slouken@0
    52
slouken@1
    53
#ifdef __CYGWIN__
slouken@1
    54
#define DISABLE_STDIO
slouken@1
    55
#endif
slouken@1
    56
slouken@0
    57
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    58
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    59
   the program crashes.
slouken@0
    60
*/
slouken@0
    61
slouken@1
    62
static void print_msg(const char *text)
slouken@1
    63
{
slouken@1
    64
#ifndef DISABLE_STDIO
slouken@1
    65
	fprintf(stderr, "%s", text);
slouken@1
    66
#endif
slouken@1
    67
}
slouken@1
    68
slouken@0
    69
static void SDL_Parachute(int sig)
slouken@0
    70
{
slouken@0
    71
	signal(sig, SIG_DFL);
slouken@1
    72
	print_msg("Fatal signal: ");
slouken@0
    73
	switch (sig) {
slouken@0
    74
		case SIGSEGV:
slouken@1
    75
			print_msg("Segmentation Fault");
slouken@0
    76
			break;
slouken@0
    77
#ifdef SIGBUS
slouken@0
    78
#if SIGBUS != SIGSEGV
slouken@0
    79
		case SIGBUS:
slouken@1
    80
			print_msg("Bus Error");
slouken@0
    81
			break;
slouken@0
    82
#endif
slouken@0
    83
#endif /* SIGBUS */
slouken@0
    84
#ifdef SIGFPE
slouken@0
    85
		case SIGFPE:
slouken@1
    86
			print_msg("Floating Point Exception");
slouken@0
    87
			break;
slouken@0
    88
#endif /* SIGFPE */
slouken@0
    89
#ifdef SIGQUIT
slouken@0
    90
		case SIGQUIT:
slouken@1
    91
			print_msg("Keyboard Quit");
slouken@0
    92
			break;
slouken@0
    93
#endif /* SIGQUIT */
slouken@0
    94
#ifdef SIGPIPE
slouken@0
    95
		case SIGPIPE:
slouken@1
    96
			print_msg("Broken Pipe");
slouken@0
    97
			break;
slouken@0
    98
#endif /* SIGPIPE */
slouken@0
    99
		default:
slouken@1
   100
#ifndef DISABLE_STDIO
slouken@0
   101
			fprintf(stderr, "# %d", sig);
slouken@1
   102
#endif
slouken@0
   103
			break;
slouken@0
   104
	}
slouken@1
   105
	print_msg(" (SDL Parachute Deployed)\n");
slouken@0
   106
	SDL_Quit();
slouken@0
   107
	exit(-sig);
slouken@0
   108
}
slouken@0
   109
slouken@0
   110
static int SDL_fatal_signals[] = {
slouken@0
   111
	SIGSEGV,
slouken@0
   112
#ifdef SIGBUS
slouken@0
   113
	SIGBUS,
slouken@0
   114
#endif
slouken@0
   115
#ifdef SIGFPE
slouken@0
   116
	SIGFPE,
slouken@0
   117
#endif
slouken@0
   118
#ifdef SIGQUIT
slouken@0
   119
	SIGQUIT,
slouken@0
   120
#endif
slouken@0
   121
	0
slouken@0
   122
};
slouken@0
   123
slouken@0
   124
void SDL_InstallParachute(void)
slouken@0
   125
{
slouken@814
   126
	/* Set a handler for any fatal signal not already handled */
slouken@0
   127
	int i;
slouken@814
   128
#ifdef HAVE_SIGACTION
slouken@814
   129
	struct sigaction action;
slouken@814
   130
slouken@814
   131
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
   132
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
   133
		if ( action.sa_handler == SIG_DFL ) {
slouken@814
   134
			action.sa_handler = SDL_Parachute;
slouken@814
   135
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
   136
		}
slouken@814
   137
	}
slouken@814
   138
#ifdef SIGALRM
slouken@814
   139
	/* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@814
   140
	sigaction(SIGALRM, NULL, &action);
slouken@814
   141
	if ( action.sa_handler == SIG_DFL ) {
slouken@814
   142
		action.sa_handler = SIG_IGN;
slouken@861
   143
		sigaction(SIGALRM, &action, NULL);
slouken@814
   144
	}
slouken@814
   145
#endif
slouken@814
   146
#else
slouken@0
   147
	void (*ohandler)(int);
slouken@0
   148
slouken@0
   149
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   150
		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@0
   151
		if ( ohandler != SIG_DFL ) {
slouken@0
   152
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   153
		}
slouken@0
   154
	}
slouken@814
   155
#endif /* HAVE_SIGACTION */
slouken@0
   156
	return;
slouken@0
   157
}
slouken@0
   158
slouken@0
   159
void SDL_UninstallParachute(void)
slouken@0
   160
{
slouken@814
   161
	/* Remove a handler for any fatal signal handled */
slouken@0
   162
	int i;
slouken@814
   163
#ifdef HAVE_SIGACTION
slouken@814
   164
	struct sigaction action;
slouken@814
   165
slouken@814
   166
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@814
   167
		sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@814
   168
		if ( action.sa_handler == SDL_Parachute ) {
slouken@814
   169
			action.sa_handler = SIG_DFL;
slouken@814
   170
			sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@814
   171
		}
slouken@814
   172
	}
slouken@814
   173
#else
slouken@0
   174
	void (*ohandler)(int);
slouken@0
   175
slouken@0
   176
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   177
		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@0
   178
		if ( ohandler != SDL_Parachute ) {
slouken@0
   179
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   180
		}
slouken@0
   181
	}
slouken@814
   182
#endif /* HAVE_SIGACTION */
slouken@0
   183
}
slouken@0
   184
slouken@0
   185
#endif /* NO_SIGNAL_H */