src/SDL_fatal.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 09 Apr 2012 23:55:43 -0400
changeset 6305 601b0e251822
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rwxr-xr-x
SDL_ExitProcess() was ignoring exit code parameter.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* General fatal signal handling code for SDL */
slouken@0
    24
slouken@1330
    25
#ifdef HAVE_SIGNAL_H
slouken@0
    26
slouken@0
    27
#include <signal.h>
slouken@0
    28
slouken@3644
    29
#include "SDL.h"
slouken@0
    30
#include "SDL_fatal.h"
slouken@0
    31
slouken@0
    32
/* This installs some signal handlers for the more common fatal signals,
slouken@0
    33
   so that if the programmer is lazy, the app doesn't die so horribly if
slouken@0
    34
   the program crashes.
slouken@0
    35
*/
slouken@0
    36
slouken@1895
    37
static void
slouken@1895
    38
SDL_Parachute(int sig)
slouken@0
    39
{
slouken@1895
    40
    signal(sig, SIG_DFL);
slouken@1895
    41
    SDL_Quit();
slouken@1895
    42
    raise(sig);
slouken@0
    43
}
slouken@0
    44
slouken@3162
    45
static const int SDL_fatal_signals[] = {
slouken@1895
    46
    SIGSEGV,
slouken@0
    47
#ifdef SIGBUS
slouken@1895
    48
    SIGBUS,
slouken@0
    49
#endif
slouken@0
    50
#ifdef SIGFPE
slouken@1895
    51
    SIGFPE,
slouken@0
    52
#endif
slouken@0
    53
#ifdef SIGQUIT
slouken@1895
    54
    SIGQUIT,
slouken@0
    55
#endif
slouken@1895
    56
    0
slouken@0
    57
};
slouken@0
    58
slouken@1895
    59
void
slouken@1895
    60
SDL_InstallParachute(void)
slouken@0
    61
{
slouken@1895
    62
    /* Set a handler for any fatal signal not already handled */
slouken@1895
    63
    int i;
slouken@814
    64
#ifdef HAVE_SIGACTION
slouken@1895
    65
    struct sigaction action;
slouken@814
    66
slouken@1895
    67
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
    68
        sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@1895
    69
        if (action.sa_handler == SIG_DFL) {
slouken@1895
    70
            action.sa_handler = SDL_Parachute;
slouken@1895
    71
            sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@1895
    72
        }
slouken@1895
    73
    }
slouken@814
    74
#ifdef SIGALRM
slouken@1895
    75
    /* Set SIGALRM to be ignored -- necessary on Solaris */
slouken@1895
    76
    sigaction(SIGALRM, NULL, &action);
slouken@1895
    77
    if (action.sa_handler == SIG_DFL) {
slouken@1895
    78
        action.sa_handler = SIG_IGN;
slouken@1895
    79
        sigaction(SIGALRM, &action, NULL);
slouken@1895
    80
    }
slouken@814
    81
#endif
slouken@814
    82
#else
slouken@1895
    83
    void (*ohandler) (int);
slouken@0
    84
slouken@1895
    85
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
    86
        ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
slouken@1895
    87
        if (ohandler != SIG_DFL) {
slouken@1895
    88
            signal(SDL_fatal_signals[i], ohandler);
slouken@1895
    89
        }
slouken@1895
    90
    }
slouken@814
    91
#endif /* HAVE_SIGACTION */
slouken@1895
    92
    return;
slouken@0
    93
}
slouken@0
    94
slouken@1895
    95
void
slouken@1895
    96
SDL_UninstallParachute(void)
slouken@0
    97
{
slouken@1895
    98
    /* Remove a handler for any fatal signal handled */
slouken@1895
    99
    int i;
slouken@814
   100
#ifdef HAVE_SIGACTION
slouken@1895
   101
    struct sigaction action;
slouken@814
   102
slouken@1895
   103
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
   104
        sigaction(SDL_fatal_signals[i], NULL, &action);
slouken@1895
   105
        if (action.sa_handler == SDL_Parachute) {
slouken@1895
   106
            action.sa_handler = SIG_DFL;
slouken@1895
   107
            sigaction(SDL_fatal_signals[i], &action, NULL);
slouken@1895
   108
        }
slouken@1895
   109
    }
slouken@814
   110
#else
slouken@1895
   111
    void (*ohandler) (int);
slouken@0
   112
slouken@1895
   113
    for (i = 0; SDL_fatal_signals[i]; ++i) {
slouken@1895
   114
        ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
slouken@1895
   115
        if (ohandler != SDL_Parachute) {
slouken@1895
   116
            signal(SDL_fatal_signals[i], ohandler);
slouken@1895
   117
        }
slouken@1895
   118
    }
slouken@814
   119
#endif /* HAVE_SIGACTION */
slouken@0
   120
}
slouken@0
   121
slouken@1330
   122
#else
slouken@1330
   123
slouken@1330
   124
/* No signals on this platform, nothing to do.. */
slouken@1330
   125
slouken@1895
   126
void
slouken@1895
   127
SDL_InstallParachute(void)
slouken@1330
   128
{
slouken@1895
   129
    return;
slouken@1330
   130
}
slouken@1330
   131
slouken@1895
   132
void
slouken@1895
   133
SDL_UninstallParachute(void)
slouken@1330
   134
{
slouken@1895
   135
    return;
slouken@1330
   136
}
slouken@1330
   137
slouken@1330
   138
#endif /* HAVE_SIGNAL_H */
slouken@1895
   139
/* vi: set ts=4 sw=4 expandtab: */