diff options
Diffstat (limited to 'src/sys/sys_main.c')
-rw-r--r-- | src/sys/sys_main.c | 105 |
1 files changed, 80 insertions, 25 deletions
diff --git a/src/sys/sys_main.c b/src/sys/sys_main.c index 460e8a22..53fc57fb 100644 --- a/src/sys/sys_main.c +++ b/src/sys/sys_main.c @@ -128,6 +128,65 @@ char *Sys_ConsoleInput(void) return CON_Input( ); } +#ifdef DEDICATED +# define PID_FILENAME PRODUCT_NAME "_server.pid" +#else +# define PID_FILENAME PRODUCT_NAME ".pid" +#endif + +/* +================= +Sys_PIDFileName +================= +*/ +static char *Sys_PIDFileName( void ) +{ + return va( "%s/%s", Sys_TempPath( ), PID_FILENAME ); +} + +/* +================= +Sys_WritePIDFile + +Return qtrue if there is an existing stale PID file +================= +*/ +qboolean Sys_WritePIDFile( void ) +{ + char *pidFile = Sys_PIDFileName( ); + FILE *f; + qboolean stale = qfalse; + + // First, check if the pid file is already there + if( ( f = fopen( pidFile, "r" ) ) != NULL ) + { + char pidBuffer[ 64 ] = { 0 }; + int pid; + + pid = fread( pidBuffer, sizeof( char ), sizeof( pidBuffer ) - 1, f ); + fclose( f ); + + if(pid > 0) + { + pid = atoi( pidBuffer ); + if( !Sys_PIDIsRunning( pid ) ) + stale = qtrue; + } + else + stale = qtrue; + } + + if( ( f = fopen( pidFile, "w" ) ) != NULL ) + { + fprintf( f, "%d", Sys_PID( ) ); + fclose( f ); + } + else + Com_Printf( S_COLOR_YELLOW "Couldn't write %s.\n", pidFile ); + + return stale; +} + /* ================= Sys_Exit @@ -135,7 +194,7 @@ Sys_Exit Single exit point (regular exit or in case of error) ================= */ -void Sys_Exit( int ex ) +static void Sys_Exit( int exitCode ) { CON_Shutdown( ); @@ -143,13 +202,13 @@ void Sys_Exit( int ex ) SDL_Quit( ); #endif -#ifdef NDEBUG - exit( ex ); -#else - // Cause a backtrace on error exits - assert( ex == 0 ); - exit( ex ); -#endif + if( exitCode < 2 ) + { + // Normal exit + remove( Sys_PIDFileName( ) ); + } + + exit( exitCode ); } /* @@ -159,7 +218,6 @@ Sys_Quit */ void Sys_Quit( void ) { - CL_Shutdown( ); Sys_Exit( 0 ); } @@ -288,15 +346,14 @@ void Sys_Error( const char *error, ... ) va_list argptr; char string[1024]; - CL_Shutdown (); - va_start (argptr,error); Q_vsnprintf (string, sizeof(string), error, argptr); va_end (argptr); + CL_Shutdown( string ); Sys_ErrorDialog( string ); - Sys_Exit( 1 ); + Sys_Exit( 3 ); } /* @@ -451,7 +508,7 @@ void Sys_ParseArgs( int argc, char **argv ) #else fprintf( stdout, Q3_VERSION " client (%s)\n", date ); #endif - Sys_Exit(0); + Sys_Exit( 0 ); } } } @@ -481,14 +538,16 @@ void Sys_SigHandler( int signal ) else { signalcaught = qtrue; - fprintf( stderr, "Received signal %d, exiting...\n", signal ); #ifndef DEDICATED - CL_Shutdown(); + CL_Shutdown( va( "Received signal %d", signal ) ); #endif - SV_Shutdown( "Signal caught" ); + SV_Shutdown( va( "Received signal %d", signal ) ); } - Sys_Exit( 0 ); // Exit with 0 to avoid recursive signals + if( signal == SIGTERM || signal == SIGINT ) + Sys_Exit( 1 ); + else + Sys_Exit( 2 ); } /* @@ -520,7 +579,10 @@ int main( int argc, char **argv ) if( SDL_VERSIONNUM( ver->major, ver->minor, ver->patch ) < SDL_VERSIONNUM( MINSDL_MAJOR, MINSDL_MINOR, MINSDL_PATCH ) ) { - Sys_Print( "SDL version " MINSDL_VERSION " or greater required\n" ); + Sys_Dialog( DT_ERROR, va( "SDL version " MINSDL_VERSION " or greater is required, " + "but only version %d.%d.%d was found. You may be able to obtain a more recent copy " + "from http://www.libsdl.org/.", ver->major, ver->minor, ver->patch ), "SDL Library Too Old" ); + Sys_Exit( 1 ); } #endif @@ -562,13 +624,6 @@ int main( int argc, char **argv ) while( 1 ) { -#ifndef DEDICATED - int appState = SDL_GetAppState( ); - - Cvar_SetValue( "com_unfocused", !( appState & SDL_APPINPUTFOCUS ) ); - Cvar_SetValue( "com_minimized", !( appState & SDL_APPACTIVE ) ); -#endif - IN_Frame( ); Com_Frame( ); } |