This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_fatal.c
139 lines (118 loc) · 3.34 KB
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
*/
21
#include "SDL_config.h"
22
23
24
/* General fatal signal handling code for SDL */
25
#ifdef HAVE_SIGNAL_H
26
27
28
#include <signal.h>
29
#include "SDL.h"
30
31
32
33
34
35
36
#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.
*/
37
38
static void
SDL_Parachute(int sig)
39
{
40
41
42
signal(sig, SIG_DFL);
SDL_Quit();
raise(sig);
43
44
}
45
static const int SDL_fatal_signals[] = {
46
SIGSEGV,
47
#ifdef SIGBUS
48
SIGBUS,
49
50
#endif
#ifdef SIGFPE
51
SIGFPE,
52
53
#endif
#ifdef SIGQUIT
54
SIGQUIT,
55
#endif
56
0
57
58
};
59
60
void
SDL_InstallParachute(void)
61
{
62
63
/* Set a handler for any fatal signal not already handled */
int i;
64
#ifdef HAVE_SIGACTION
65
66
67
68
69
70
71
72
73
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);
}
}
74
#ifdef SIGALRM
75
76
77
78
79
80
/* 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);
}
81
82
#endif
#else
83
84
85
86
87
88
89
90
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);
}
}
91
#endif /* HAVE_SIGACTION */
92
return;
93
94
}
95
96
void
SDL_UninstallParachute(void)
97
{
98
99
/* Remove a handler for any fatal signal handled */
int i;
100
#ifdef HAVE_SIGACTION
101
102
103
104
105
106
107
108
109
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);
}
}
110
#else
111
112
113
114
115
116
117
118
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);
}
}
119
#endif /* HAVE_SIGACTION */
120
121
}
122
123
124
125
#else
/* No signals on this platform, nothing to do.. */
126
127
void
SDL_InstallParachute(void)
128
{
129
return;
130
131
}
132
133
void
SDL_UninstallParachute(void)
134
{
135
return;
136
137
138
}
#endif /* HAVE_SIGNAL_H */
139
/* vi: set ts=4 sw=4 expandtab: */