From 1320e29aaff2b3e2580863b0416b3577ed32153f Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 13 Jan 2019 07:07:39 +0100 Subject: [PATCH] Implement Signal-Handlers for POSIX systems, incl. SIGTTIN/SIGTTOU handling SIGTTIN/OU allows running Doom3 in the background (or even sending it to the background with Ctrl-Z + bg) by disabling TTY input (before it would get stuck when run in background without +set in_tty 0, see #215) While at it, I also added signal handlers for some common crash signals (SIGILL, SIGABRT, SIGFPE, SIGSEGV) to print a backtrace before exiting the game (partly based on Yamagi Quake II code). --- .gitignore | 3 + neo/sys/linux/main.cpp | 2 + neo/sys/osx/DOOMController.mm | 2 + neo/sys/posix/posix_main.cpp | 112 +++++++++++++++++++++++++++++++++- neo/sys/posix/posix_public.h | 1 + 5 files changed, 118 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index cf1c10f6..8f05fb06 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ .cproject .project .settings/ + +/dhewm* +/*-releases/ diff --git a/neo/sys/linux/main.cpp b/neo/sys/linux/main.cpp index c349b754..5e08e2dc 100644 --- a/neo/sys/linux/main.cpp +++ b/neo/sys/linux/main.cpp @@ -301,6 +301,8 @@ int main(int argc, char **argv) { // so set $LC_ALL to "C". setenv("LC_ALL", "C", 1); + Posix_InitSignalHandlers(); + if ( argc > 1 ) { common->Init( argc-1, &argv[1] ); } else { diff --git a/neo/sys/osx/DOOMController.mm b/neo/sys/osx/DOOMController.mm index 54319fa6..70e31628 100644 --- a/neo/sys/osx/DOOMController.mm +++ b/neo/sys/osx/DOOMController.mm @@ -180,6 +180,8 @@ int SDL_main( int argc, char *argv[] ) { if (![[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]]) Sys_Error("Could not access application resources"); + Posix_InitSignalHandlers(); // DG: added signal handlers for POSIX platforms + if (argc > 1) common->Init(argc - 1, &argv[1]); else diff --git a/neo/sys/posix/posix_main.cpp b/neo/sys/posix/posix_main.cpp index 15cca0dc..80b7745a 100644 --- a/neo/sys/posix/posix_main.cpp +++ b/neo/sys/posix/posix_main.cpp @@ -44,6 +44,7 @@ If you have questions concerning this license or the applicable additional terms #include "framework/FileSystem.h" #include "framework/KeyInput.h" #include "framework/EditField.h" +#include "framework/Licensee.h" #include "sys/sys_local.h" #include "sys/posix/posix_public.h" @@ -355,6 +356,105 @@ int Sys_GetDriveFreeSpace( const char *path ) { return 1000 * 1024; } + +// ----------- lots of signal handling stuff ------------ + +static const int crashSigs[] = { SIGILL, SIGABRT, SIGFPE, SIGSEGV }; +static const char* crashSigNames[] = { "SIGILL", "SIGABRT", "SIGFPE", "SIGSEGV" }; + +#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) + // TODO: https://github.com/ianlancetaylor/libbacktrace looks interesting and also supports windows apparently + #define D3_HAVE_BACKTRACE + #include +#endif + +static void signalhandlerCrash(int sig) +{ + const char* name = ""; + for(int i=0; i running in the background (started with `./dhewm3 &` or similar) + // so we shouldn't take any console input + Sys_Printf( "Running in background, disabling terminal support.\n" ); + in_tty.SetBool( false ); + return; + } else { + Sys_Printf( "tcsetattr failed: %s (%d)\n", strerror( errno ), errno ); + Sys_Printf( "terminal support may not work correctly. Use +set in_tty 0 to disable it\n" ); + } } #if 0 // make the output non blocking diff --git a/neo/sys/posix/posix_public.h b/neo/sys/posix/posix_public.h index f6ccaf3d..6a5ff883 100644 --- a/neo/sys/posix/posix_public.h +++ b/neo/sys/posix/posix_public.h @@ -39,6 +39,7 @@ void Posix_Exit( int ret ); void Posix_SetExit(int ret); // override the exit code void Posix_SetExitSpawn( const char *exeName ); // set the process to be spawned when we quit +void Posix_InitSignalHandlers( void ); void Posix_InitConsoleInput( void ); void Posix_Shutdown( void );