This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_fatal.c
140 lines (118 loc) · 3.27 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
/* General fatal signal handling code for SDL */
26
#ifdef HAVE_SIGNAL_H
27
28
29
#include <signal.h>
30
#include "SDL.h"
31
32
33
34
35
36
37
#include "SDL_fatal.h"
/* This installs some signal handlers for the more common fatal signals,
so that if the programmer is lazy, the app doesn't die so horribly if
the program crashes.
*/
38
39
static void
SDL_Parachute(int sig)
40
{
41
42
43
signal(sig, SIG_DFL);
SDL_Quit();
raise(sig);
44
45
}
46
static const int SDL_fatal_signals[] = {
47
SIGSEGV,
48
#ifdef SIGBUS
49
SIGBUS,
50
51
#endif
#ifdef SIGFPE
52
SIGFPE,
53
54
#endif
#ifdef SIGQUIT
55
SIGQUIT,
56
#endif
57
0
58
59
};
60
61
void
SDL_InstallParachute(void)
62
{
63
64
/* Set a handler for any fatal signal not already handled */
int i;
65
#ifdef HAVE_SIGACTION
66
67
68
69
70
71
72
73
74
struct sigaction action;
for (i = 0; SDL_fatal_signals[i]; ++i) {
sigaction(SDL_fatal_signals[i], NULL, &action);
if (action.sa_handler == SIG_DFL) {
action.sa_handler = SDL_Parachute;
sigaction(SDL_fatal_signals[i], &action, NULL);
}
}
75
#ifdef SIGALRM
76
77
78
79
80
81
/* Set SIGALRM to be ignored -- necessary on Solaris */
sigaction(SIGALRM, NULL, &action);
if (action.sa_handler == SIG_DFL) {
action.sa_handler = SIG_IGN;
sigaction(SIGALRM, &action, NULL);
}
82
83
#endif
#else
84
85
86
87
88
89
90
91
void (*ohandler) (int);
for (i = 0; SDL_fatal_signals[i]; ++i) {
ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
if (ohandler != SIG_DFL) {
signal(SDL_fatal_signals[i], ohandler);
}
}
92
#endif /* HAVE_SIGACTION */
93
return;
94
95
}
96
97
void
SDL_UninstallParachute(void)
98
{
99
100
/* Remove a handler for any fatal signal handled */
int i;
101
#ifdef HAVE_SIGACTION
102
103
104
105
106
107
108
109
110
struct sigaction action;
for (i = 0; SDL_fatal_signals[i]; ++i) {
sigaction(SDL_fatal_signals[i], NULL, &action);
if (action.sa_handler == SDL_Parachute) {
action.sa_handler = SIG_DFL;
sigaction(SDL_fatal_signals[i], &action, NULL);
}
}
111
#else
112
113
114
115
116
117
118
119
void (*ohandler) (int);
for (i = 0; SDL_fatal_signals[i]; ++i) {
ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
if (ohandler != SDL_Parachute) {
signal(SDL_fatal_signals[i], ohandler);
}
}
120
#endif /* HAVE_SIGACTION */
121
122
}
123
124
125
126
#else
/* No signals on this platform, nothing to do.. */
127
128
void
SDL_InstallParachute(void)
129
{
130
return;
131
132
}
133
134
void
SDL_UninstallParachute(void)
135
{
136
return;
137
138
139
}
#endif /* HAVE_SIGNAL_H */
140
/* vi: set ts=4 sw=4 expandtab: */