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