src/SDL_fatal.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 13 Jan 2010 07:00:20 +0000
changeset 3648 a9d830c05998
parent 3644 09ce26cedfd2
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed build problems with gcc __attribute__.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General fatal signal handling code for SDL */
slouken@0
    25
slouken@1330
    26
#ifdef HAVE_SIGNAL_H
slouken@0
    27
slouken@0
    28
#include <signal.h>
slouken@0
    29
slouken@3644
    30
#include "SDL.h"
slouken@0
    31
#include "SDL_fatal.h"
slouken@0
    32
slouken@0
    33
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    34
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    35
   the program crashes.
slouken@0
    36
*/
slouken@0
    37
slouken@1895
    38
static void
slouken@1895
    39
SDL_Parachute(int sig)
slouken@0
    40
{
slouken@1895
    41
    signal(sig, SIG_DFL);
slouken@1895
    42
    SDL_Quit();
slouken@1895
    43
    raise(sig);
slouken@0
    44
}
slouken@0
    45
slouken@3162
    46
static const int SDL_fatal_signals[] = {
slouken@1895
    47
    SIGSEGV,
slouken@0
    48
#ifdef SIGBUS
slouken@1895
    49
    SIGBUS,
slouken@0
    50
#endif
slouken@0
    51
#ifdef SIGFPE
slouken@1895
    52
    SIGFPE,
slouken@0
    53
#endif
slouken@0
    54
#ifdef SIGQUIT
slouken@1895
    55
    SIGQUIT,
slouken@0
    56
#endif
slouken@1895
    57
    0
slouken@0
    58
};
slouken@0
    59
slouken@1895
    60
void
slouken@1895
    61
SDL_InstallParachute(void)
slouken@0
    62
{
slouken@1895
    63
    /* Set a handler for any fatal signal not already handled */
slouken@1895
    64
    int i;
slouken@814
    65
#ifdef HAVE_SIGACTION
slouken@1895
    66
    struct sigaction action;
slouken@814
    67
slouken@1895
    68
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
    69
        sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@1895
    70
        if (action.sa_handler == SIG_DFL) {
slouken@1895
    71
            action.sa_handler = SDL_Parachute;
slouken@1895
    72
            sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@1895
    73
        }
slouken@1895
    74
    }
slouken@814
    75
#ifdef SIGALRM
slouken@1895
    76
    /* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@1895
    77
    sigaction(SIGALRM, NULL, &action);
slouken@1895
    78
    if (action.sa_handler == SIG_DFL) {
slouken@1895
    79
        action.sa_handler = SIG_IGN;
slouken@1895
    80
        sigaction(SIGALRM, &action, NULL);
slouken@1895
    81
    }
slouken@814
    82
#endif
slouken@814
    83
#else
slouken@1895
    84
    void (*ohandler) (int);
slouken@0
    85
slouken@1895
    86
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
    87
        ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@1895
    88
        if (ohandler != SIG_DFL) {
slouken@1895
    89
            signal(SDL_fatal_signals[i], ohandler);
slouken@1895
    90
        }
slouken@1895
    91
    }
slouken@814
    92
#endif /* HAVE_SIGACTION */
slouken@1895
    93
    return;
slouken@0
    94
}
slouken@0
    95
slouken@1895
    96
void
slouken@1895
    97
SDL_UninstallParachute(void)
slouken@0
    98
{
slouken@1895
    99
    /* Remove a handler for any fatal signal handled */
slouken@1895
   100
    int i;
slouken@814
   101
#ifdef HAVE_SIGACTION
slouken@1895
   102
    struct sigaction action;
slouken@814
   103
slouken@1895
   104
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
   105
        sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@1895
   106
        if (action.sa_handler == SDL_Parachute) {
slouken@1895
   107
            action.sa_handler = SIG_DFL;
slouken@1895
   108
            sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@1895
   109
        }
slouken@1895
   110
    }
slouken@814
   111
#else
slouken@1895
   112
    void (*ohandler) (int);
slouken@0
   113
slouken@1895
   114
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
   115
        ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@1895
   116
        if (ohandler != SDL_Parachute) {
slouken@1895
   117
            signal(SDL_fatal_signals[i], ohandler);
slouken@1895
   118
        }
slouken@1895
   119
    }
slouken@814
   120
#endif /* HAVE_SIGACTION */
slouken@0
   121
}
slouken@0
   122
slouken@1330
   123
#else
slouken@1330
   124
slouken@1330
   125
/* No signals on this platform, nothing to do.. */
slouken@1330
   126
slouken@1895
   127
void
slouken@1895
   128
SDL_InstallParachute(void)
slouken@1330
   129
{
slouken@1895
   130
    return;
slouken@1330
   131
}
slouken@1330
   132
slouken@1895
   133
void
slouken@1895
   134
SDL_UninstallParachute(void)
slouken@1330
   135
{
slouken@1895
   136
    return;
slouken@1330
   137
}
slouken@1330
   138
slouken@1330
   139
#endif /* HAVE_SIGNAL_H */
slouken@1895
   140
/* vi: set ts=4 sw=4 expandtab: */