src/SDL_fatal.c
changeset 0 74212992fb08
child 1 cf2af46e9e2a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_fatal.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* General fatal signal handling code for SDL */
    1.32 +
    1.33 +#ifdef NO_SIGNAL_H
    1.34 +
    1.35 +/* No signals on this platform, nothing to do.. */
    1.36 +
    1.37 +void SDL_InstallParachute(void)
    1.38 +{
    1.39 +	return;
    1.40 +}
    1.41 +
    1.42 +void SDL_UninstallParachute(void)
    1.43 +{
    1.44 +	return;
    1.45 +}
    1.46 +
    1.47 +#else
    1.48 +
    1.49 +#include <stdlib.h>
    1.50 +#include <stdio.h>
    1.51 +#include <signal.h>
    1.52 +#include <string.h>
    1.53 +
    1.54 +#include "SDL.h"
    1.55 +#include "SDL_fatal.h"
    1.56 +
    1.57 +/* This installs some signal handlers for the more common fatal signals,
    1.58 +   so that if the programmer is lazy, the app doesn't die so horribly if
    1.59 +   the program crashes.
    1.60 +*/
    1.61 +
    1.62 +static void SDL_Parachute(int sig)
    1.63 +{
    1.64 +	signal(sig, SIG_DFL);
    1.65 +	fprintf(stderr, "Fatal signal: ");
    1.66 +	switch (sig) {
    1.67 +		case SIGSEGV:
    1.68 +			fprintf(stderr, "Segmentation Fault");
    1.69 +			break;
    1.70 +#ifdef SIGBUS
    1.71 +#if SIGBUS != SIGSEGV
    1.72 +		case SIGBUS:
    1.73 +			fprintf(stderr, "Bus Error");
    1.74 +			break;
    1.75 +#endif
    1.76 +#endif /* SIGBUS */
    1.77 +#ifdef SIGFPE
    1.78 +		case SIGFPE:
    1.79 +			fprintf(stderr, "Floating Point Exception");
    1.80 +			break;
    1.81 +#endif /* SIGFPE */
    1.82 +#ifdef SIGQUIT
    1.83 +		case SIGQUIT:
    1.84 +			fprintf(stderr, "Keyboard Quit");
    1.85 +			break;
    1.86 +#endif /* SIGQUIT */
    1.87 +#ifdef SIGPIPE
    1.88 +		case SIGPIPE:
    1.89 +			fprintf(stderr, "Broken Pipe");
    1.90 +			break;
    1.91 +#endif /* SIGPIPE */
    1.92 +		default:
    1.93 +			fprintf(stderr, "# %d", sig);
    1.94 +			break;
    1.95 +	}
    1.96 +	fprintf(stderr, " (SDL Parachute Deployed)\n");
    1.97 +	SDL_Quit();
    1.98 +	exit(-sig);
    1.99 +}
   1.100 +
   1.101 +static int SDL_fatal_signals[] = {
   1.102 +	SIGSEGV,
   1.103 +#ifdef SIGBUS
   1.104 +	SIGBUS,
   1.105 +#endif
   1.106 +#ifdef SIGFPE
   1.107 +	SIGFPE,
   1.108 +#endif
   1.109 +#ifdef SIGQUIT
   1.110 +	SIGQUIT,
   1.111 +#endif
   1.112 +#ifdef SIGPIPE
   1.113 +	SIGPIPE,
   1.114 +#endif
   1.115 +	0
   1.116 +};
   1.117 +
   1.118 +void SDL_InstallParachute(void)
   1.119 +{
   1.120 +	int i;
   1.121 +	void (*ohandler)(int);
   1.122 +
   1.123 +	/* Set a handler for any fatal signal not already handled */
   1.124 +	for ( i=0; SDL_fatal_signals[i]; ++i ) {
   1.125 +		ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
   1.126 +		if ( ohandler != SIG_DFL ) {
   1.127 +			signal(SDL_fatal_signals[i], ohandler);
   1.128 +		}
   1.129 +	}
   1.130 +#ifdef SIGALRM
   1.131 +	/* Set SIGALRM to be ignored -- necessary on Solaris */
   1.132 +	{
   1.133 +		struct sigaction action, oaction;
   1.134 +
   1.135 +		/* Set SIG_IGN action */
   1.136 +		memset(&action, 0, (sizeof action));
   1.137 +		action.sa_handler = SIG_IGN;
   1.138 +		sigaction(SIGALRM, &action, &oaction);
   1.139 +
   1.140 +		/* Reset original action if it was already being handled */
   1.141 +		if ( oaction.sa_handler != SIG_DFL ) {
   1.142 +			sigaction(SIGALRM, &oaction, NULL);
   1.143 +		}
   1.144 +	}
   1.145 +#endif
   1.146 +	return;
   1.147 +}
   1.148 +
   1.149 +void SDL_UninstallParachute(void)
   1.150 +{
   1.151 +	int i;
   1.152 +	void (*ohandler)(int);
   1.153 +
   1.154 +	/* Remove a handler for any fatal signal handled */
   1.155 +	for ( i=0; SDL_fatal_signals[i]; ++i ) {
   1.156 +		ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
   1.157 +		if ( ohandler != SDL_Parachute ) {
   1.158 +			signal(SDL_fatal_signals[i], ohandler);
   1.159 +		}
   1.160 +	}
   1.161 +}
   1.162 +
   1.163 +#endif /* NO_SIGNAL_H */