src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Mar 2002 19:55:32 +0000
changeset 292 eadc0746dfaf
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
Added SDL_LockRect() and SDL_UnlockRect()
Incorporated XFree86 extension libraries into the source
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@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
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@1
    54
#ifdef __CYGWIN__
slouken@1
    55
#define DISABLE_STDIO
slouken@1
    56
#endif
slouken@1
    57
slouken@0
    58
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    59
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    60
   the program crashes.
slouken@0
    61
*/
slouken@0
    62
slouken@1
    63
static void print_msg(const char *text)
slouken@1
    64
{
slouken@1
    65
#ifndef DISABLE_STDIO
slouken@1
    66
	fprintf(stderr, "%s", text);
slouken@1
    67
#endif
slouken@1
    68
}
slouken@1
    69
slouken@0
    70
static void SDL_Parachute(int sig)
slouken@0
    71
{
slouken@0
    72
	signal(sig, SIG_DFL);
slouken@1
    73
	print_msg("Fatal signal: ");
slouken@0
    74
	switch (sig) {
slouken@0
    75
		case SIGSEGV:
slouken@1
    76
			print_msg("Segmentation Fault");
slouken@0
    77
			break;
slouken@0
    78
#ifdef SIGBUS
slouken@0
    79
#if SIGBUS != SIGSEGV
slouken@0
    80
		case SIGBUS:
slouken@1
    81
			print_msg("Bus Error");
slouken@0
    82
			break;
slouken@0
    83
#endif
slouken@0
    84
#endif /* SIGBUS */
slouken@0
    85
#ifdef SIGFPE
slouken@0
    86
		case SIGFPE:
slouken@1
    87
			print_msg("Floating Point Exception");
slouken@0
    88
			break;
slouken@0
    89
#endif /* SIGFPE */
slouken@0
    90
#ifdef SIGQUIT
slouken@0
    91
		case SIGQUIT:
slouken@1
    92
			print_msg("Keyboard Quit");
slouken@0
    93
			break;
slouken@0
    94
#endif /* SIGQUIT */
slouken@0
    95
#ifdef SIGPIPE
slouken@0
    96
		case SIGPIPE:
slouken@1
    97
			print_msg("Broken Pipe");
slouken@0
    98
			break;
slouken@0
    99
#endif /* SIGPIPE */
slouken@0
   100
		default:
slouken@1
   101
#ifndef DISABLE_STDIO
slouken@0
   102
			fprintf(stderr, "# %d", sig);
slouken@1
   103
#endif
slouken@0
   104
			break;
slouken@0
   105
	}
slouken@1
   106
	print_msg(" (SDL Parachute Deployed)\n");
slouken@0
   107
	SDL_Quit();
slouken@0
   108
	exit(-sig);
slouken@0
   109
}
slouken@0
   110
slouken@0
   111
static int SDL_fatal_signals[] = {
slouken@0
   112
	SIGSEGV,
slouken@0
   113
#ifdef SIGBUS
slouken@0
   114
	SIGBUS,
slouken@0
   115
#endif
slouken@0
   116
#ifdef SIGFPE
slouken@0
   117
	SIGFPE,
slouken@0
   118
#endif
slouken@0
   119
#ifdef SIGQUIT
slouken@0
   120
	SIGQUIT,
slouken@0
   121
#endif
slouken@0
   122
#ifdef SIGPIPE
slouken@0
   123
	SIGPIPE,
slouken@0
   124
#endif
slouken@0
   125
	0
slouken@0
   126
};
slouken@0
   127
slouken@0
   128
void SDL_InstallParachute(void)
slouken@0
   129
{
slouken@0
   130
	int i;
slouken@0
   131
	void (*ohandler)(int);
slouken@0
   132
slouken@0
   133
	/* Set a handler for any fatal signal not already handled */
slouken@0
   134
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   135
		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@0
   136
		if ( ohandler != SIG_DFL ) {
slouken@0
   137
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   138
		}
slouken@0
   139
	}
slouken@0
   140
#ifdef SIGALRM
slouken@0
   141
	/* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@0
   142
	{
slouken@0
   143
		struct sigaction action, oaction;
slouken@0
   144
slouken@0
   145
		/* Set SIG_IGN action */
slouken@0
   146
		memset(&action, 0, (sizeof action));
slouken@0
   147
		action.sa_handler = SIG_IGN;
slouken@0
   148
		sigaction(SIGALRM, &action, &oaction);
slouken@0
   149
slouken@0
   150
		/* Reset original action if it was already being handled */
slouken@0
   151
		if ( oaction.sa_handler != SIG_DFL ) {
slouken@0
   152
			sigaction(SIGALRM, &oaction, NULL);
slouken@0
   153
		}
slouken@0
   154
	}
slouken@0
   155
#endif
slouken@0
   156
	return;
slouken@0
   157
}
slouken@0
   158
slouken@0
   159
void SDL_UninstallParachute(void)
slouken@0
   160
{
slouken@0
   161
	int i;
slouken@0
   162
	void (*ohandler)(int);
slouken@0
   163
slouken@0
   164
	/* Remove a handler for any fatal signal handled */
slouken@0
   165
	for ( i=0; SDL_fatal_signals[i]; ++i ) {
slouken@0
   166
		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@0
   167
		if ( ohandler != SDL_Parachute ) {
slouken@0
   168
			signal(SDL_fatal_signals[i], ohandler);
slouken@0
   169
		}
slouken@0
   170
	}
slouken@0
   171
}
slouken@0
   172
slouken@0
   173
#endif /* NO_SIGNAL_H */