src/SDL_fatal.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 1 cf2af46e9e2a
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@0
    20
    slouken@devolution.com
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
slouken@0
    28
/* General fatal signal handling code for SDL */
slouken@0
    29
slouken@0
    30
#ifdef NO_SIGNAL_H
slouken@0
    31
slouken@0
    32
/* No signals on this platform, nothing to do.. */
slouken@0
    33
slouken@0
    34
void SDL_InstallParachute(void)
slouken@0
    35
{
slouken@0
    36
	return;
slouken@0
    37
}
slouken@0
    38
slouken@0
    39
void SDL_UninstallParachute(void)
slouken@0
    40
{
slouken@0
    41
	return;
slouken@0
    42
}
slouken@0
    43
slouken@0
    44
#else
slouken@0
    45
slouken@0
    46
#include <stdlib.h>
slouken@0
    47
#include <stdio.h>
slouken@0
    48
#include <signal.h>
slouken@0
    49
#include <string.h>
slouken@0
    50
slouken@0
    51
#include "SDL.h"
slouken@0
    52
#include "SDL_fatal.h"
slouken@0
    53
slouken@0
    54
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    55
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    56
   the program crashes.
slouken@0
    57
*/
slouken@0
    58
slouken@0
    59
static void SDL_Parachute(int sig)
slouken@0
    60
{
slouken@0
    61
	signal(sig, SIG_DFL);
slouken@0
    62
	fprintf(stderr, "Fatal signal: ");
slouken@0
    63
	switch (sig) {
slouken@0
    64
		case SIGSEGV:
slouken@0
    65
			fprintf(stderr, "Segmentation Fault");
slouken@0
    66
			break;
slouken@0
    67
#ifdef SIGBUS
slouken@0
    68
#if SIGBUS != SIGSEGV
slouken@0
    69
		case SIGBUS:
slouken@0
    70
			fprintf(stderr, "Bus Error");
slouken@0
    71
			break;
slouken@0
    72
#endif
slouken@0
    73
#endif /* SIGBUS */
slouken@0
    74
#ifdef SIGFPE
slouken@0
    75
		case SIGFPE:
slouken@0
    76
			fprintf(stderr, "Floating Point Exception");
slouken@0
    77
			break;
slouken@0
    78
#endif /* SIGFPE */
slouken@0
    79
#ifdef SIGQUIT
slouken@0
    80
		case SIGQUIT:
slouken@0
    81
			fprintf(stderr, "Keyboard Quit");
slouken@0
    82
			break;
slouken@0
    83
#endif /* SIGQUIT */
slouken@0
    84
#ifdef SIGPIPE
slouken@0
    85
		case SIGPIPE:
slouken@0
    86
			fprintf(stderr, "Broken Pipe");
slouken@0
    87
			break;
slouken@0
    88
#endif /* SIGPIPE */
slouken@0
    89
		default:
slouken@0
    90
			fprintf(stderr, "# %d", sig);
slouken@0
    91
			break;
slouken@0
    92
	}
slouken@0
    93
	fprintf(stderr, " (SDL Parachute Deployed)\n");
slouken@0
    94
	SDL_Quit();
slouken@0
    95
	exit(-sig);
slouken@0
    96
}
slouken@0
    97
slouken@0
    98
static int SDL_fatal_signals[] = {
slouken@0
    99
	SIGSEGV,
slouken@0
   100
#ifdef SIGBUS
slouken@0
   101
	SIGBUS,
slouken@0
   102
#endif
slouken@0
   103
#ifdef SIGFPE
slouken@0
   104
	SIGFPE,
slouken@0
   105
#endif
slouken@0
   106
#ifdef SIGQUIT
slouken@0
   107
	SIGQUIT,
slouken@0
   108
#endif
slouken@0
   109
#ifdef SIGPIPE
slouken@0
   110
	SIGPIPE,
slouken@0
   111
#endif
slouken@0
   112
	0
slouken@0
   113
};
slouken@0
   114
slouken@0
   115
void SDL_InstallParachute(void)
slouken@0
   116
{
slouken@0
   117
	int i;
slouken@0
   118
	void (*ohandler)(int);
slouken@0
   119
slouken@0
   120
	/* Set a handler for any fatal signal not already handled */
slouken@0
   121
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   122
		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@0
   123
		if ( ohandler != SIG_DFL ) {
slouken@0
   124
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   125
		}
slouken@0
   126
	}
slouken@0
   127
#ifdef SIGALRM
slouken@0
   128
	/* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@0
   129
	{
slouken@0
   130
		struct sigaction action, oaction;
slouken@0
   131
slouken@0
   132
		/* Set SIG_IGN action */
slouken@0
   133
		memset(&action, 0, (sizeof action));
slouken@0
   134
		action.sa_handler = SIG_IGN;
slouken@0
   135
		sigaction(SIGALRM, &action, &oaction);
slouken@0
   136
slouken@0
   137
		/* Reset original action if it was already being handled */
slouken@0
   138
		if ( oaction.sa_handler != SIG_DFL ) {
slouken@0
   139
			sigaction(SIGALRM, &oaction, NULL);
slouken@0
   140
		}
slouken@0
   141
	}
slouken@0
   142
#endif
slouken@0
   143
	return;
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
void SDL_UninstallParachute(void)
slouken@0
   147
{
slouken@0
   148
	int i;
slouken@0
   149
	void (*ohandler)(int);
slouken@0
   150
slouken@0
   151
	/* Remove a handler for any fatal signal handled */
slouken@0
   152
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   153
		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@0
   154
		if ( ohandler != SDL_Parachute ) {
slouken@0
   155
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   156
		}
slouken@0
   157
	}
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
#endif /* NO_SIGNAL_H */