x86UNIXProcessControl.cpp
Engine/source/platformX86UNIX/x86UNIXProcessControl.cpp
Public Functions
CheckExitCode(S64 exitCode)
ImmediateShutdown(S32 exitCode, S32 signalNum)
SignalHandler(int sigtype)
Detailed Description
Public Functions
CheckExitCode(S64 exitCode)
Cleanup(bool minimal)
ImmediateShutdown(S32 exitCode, S32 signalNum)
ProcessControlInit()
SignalHandler(int sigtype)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platformX86UNIX/platformX86UNIX.h" 25#include "platformX86UNIX/x86UNIXState.h" 26#include "platformX86UNIX/x86UNIXStdConsole.h" 27#include "platform/platformInput.h" 28#include "console/console.h" 29 30#include <stdlib.h> 31#include <unistd.h> 32#include <signal.h> 33#include "console/engineAPI.h" 34#ifndef TORQUE_DEDICATED 35#include <SDL.h> 36#endif 37 38//----------------------------------------------------------------------------- 39// This is a mainly a debugging function for intercepting a nonzero exit code 40// and generating a core dump for a stack trace. 41// Need an S64 here because postQuitMessage uses a U32, and 42// forceshutdown uses an S32. So S64 is needed to 43// accomodate them both 44static void CheckExitCode(S64 exitCode) 45{ 46 if (exitCode != 0) 47 { 48 Con::errorf(ConsoleLogEntry::General, 49 "Nonzero exit code: %d, triggering SIGSEGV for core dump", 50 exitCode); 51 kill(getpid(), SIGSEGV); 52 } 53} 54 55//----------------------------------------------------------------------------- 56static void SignalHandler(int sigtype) 57{ 58 if (sigtype == SIGSEGV || sigtype == SIGTRAP) 59 { 60 signal(SIGSEGV, SIG_DFL); 61 signal(SIGTRAP, SIG_DFL); 62 // restore the signal handling to default so that we don't get into 63 // a crash loop with ImmediateShutdown 64 ImmediateShutdown(-sigtype, sigtype); 65 } 66 else 67 { 68 signal(sigtype, SIG_DFL); 69 dPrintf("Unknown signal caught by SignalHandler: %d\n", sigtype); 70 // exit to be safe 71 ImmediateShutdown(1); 72 } 73} 74 75//----------------------------------------------------------------------------- 76void Cleanup(bool minimal) 77{ 78 if (!minimal) 79 { 80 Input::destroy(); 81 } 82 83 StdConsole::destroy(); 84 85#ifndef TORQUE_DEDICATED 86 SDL_Quit(); 87#endif 88} 89 90//----------------------------------------------------------------------------- 91void ImmediateShutdown(S32 exitCode, S32 signalNum) 92{ 93 bool segfault = signalNum > 0; 94 95 Cleanup(segfault); 96 97 if (!segfault) 98 { 99 dPrintf("Exiting\n"); 100 // exit (doesn't call destructors) 101 _exit(exitCode); 102 } 103 else 104 { 105// there is a problem in kernel 2.4.17 which causes a hang when a segfault 106// occurs. also subsequent runs of "ps" will hang and the machine has to be 107// hard reset to clear up the problem 108// JMQ: this bug appears to be fixed in 2.4.18 109//#define KERNEL_2_4_WORKAROUND 110#ifdef KERNEL_2_4_WORKAROUND 111 dPrintf("Segmentation Fault (Exiting without core dump due to #define KERNEL_2_4_WORKAROUND)\n"); 112 dFflushStdout(); 113 _exit(exitCode); 114#else 115 // kill with signal 116 kill(getpid(), signalNum); 117#endif 118 } 119 120} 121 122//----------------------------------------------------------------------------- 123void ProcessControlInit() 124{ 125 // JMQ: ignore IO signals background read/write terminal (so that we don't 126 // get suspended in daemon mode) 127 signal(SIGTTIN, SIG_IGN); 128 signal(SIGTTOU, SIG_IGN); 129 130 // we're not interested in the exit status of child processes, so this 131 // prevents zombies from accumulating. 132#if defined(__FreeBSD__) 133 signal(SIGCHLD, SIG_IGN); 134#else 135 signal(SIGCLD, SIG_IGN); 136#endif 137 138 // install signal handler for SIGSEGV, so that we can attempt 139 // clean shutdown 140 signal(SIGSEGV, &SignalHandler); 141 signal(SIGTRAP, &SignalHandler); 142} 143 144//----------------------------------------------------------------------------- 145void Platform::postQuitMessage(const S32 in_quitVal) 146{ 147 // if we have a window send a quit event, otherwise just force shutdown 148#if 0 149 if (windowCreated()) 150 { 151 CheckExitCode(in_quitVal); 152 SendQuitEvent(); 153 } 154 else 155#endif 156 { 157 forceShutdown(in_quitVal); 158 } 159} 160 161//----------------------------------------------------------------------------- 162void Platform::debugBreak() 163{ 164 // in windows, "Calling DebugBreak causes the program to display 165 // a dialog box as if it had crashed." So we segfault. 166 Con::errorf(ConsoleLogEntry::General, 167 "Platform::debugBreak: triggering SIGSEGV for core dump"); 168 //kill(getpid(), SIGSEGV); 169 kill(getpid(), SIGTRAP); 170} 171 172//----------------------------------------------------------------------------- 173void Platform::forceShutdown(S32 returnValue) 174{ 175#if 0 176 // if a dedicated server is running, turn it off 177 if (isDedicated() && Game->isRunning()) 178 Game->setRunning(false); 179 else 180#endif 181 ImmediateShutdown(returnValue); 182} 183 184//----------------------------------------------------------------------------- 185void Platform::outputDebugString(const char *string, ...) 186{ 187 char buffer[2048]; 188 189 va_list args; 190 va_start( args, string ); 191 192 dVsprintf( buffer, sizeof(buffer), string, args ); 193 va_end( args ); 194 195 U32 length = dStrlen(buffer); 196 if( length == (sizeof(buffer) - 1 ) ) 197 length--; 198 199 buffer[length++] = '\n'; 200 buffer[length] = '\0'; 201 202 fwrite(buffer, sizeof(char), length, stderr); 203} 204 205//----------------------------------------------------------------------------- 206// testing function 207//DefineEngineFunction(debug_debugbreak, void, () , , "debug_debugbreak();"); 208 209//----------------------------------------------------------------------------- 210void Platform::restartInstance() 211{ 212/* 213 if (Game->isRunning() ) 214 { 215 //Con::errorf( "Error restarting Instance. Game is Still running!"); 216 return; 217 } 218 219 char cmd[2048]; 220 sprintf(cmd, "%s &", x86UNIXState->getExePathName()); 221 system(cmd); 222*/ 223 exit(0); 224} 225