Install a no-op TERM handler at first then replace later with one that

just exits main loop.  Do cleanup/file saving on exit from mainloop.
Fixes occasional crash where TERM came in before we were setup to
cleanup.
This commit is contained in:
Andy2 2010-09-20 04:55:35 -07:00
parent df1ec1628a
commit 00cdbc97d3
3 changed files with 46 additions and 22 deletions

View file

@ -475,22 +475,6 @@ showStatus( CursesAppGlobals* globals )
static XP_Bool
handleQuit( CursesAppGlobals* globals )
{
if ( !!globals->cGlobals.params->fileName ) {
XWStreamCtxt* outStream;
outStream = mem_stream_make(
MPPARM(globals->cGlobals.params->util->mpool)
globals->cGlobals.params->vtMgr,
&globals->cGlobals, 0, writeToFile );
stream_open( outStream );
game_saveToStream( &globals->cGlobals.game,
&globals->cGlobals.params->gi,
outStream );
stream_destroy( outStream );
}
globals->timeToExit = XP_TRUE;
return XP_TRUE;
} /* handleQuit */
@ -1531,6 +1515,10 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
&g_globals.cp, &procs );
stream_destroy( stream );
if ( !isServer && params->gi.serverRole == SERVER_ISSERVER ) {
isServer = XP_TRUE;
}
} else {
game_makeNewGame( MEMPOOL &g_globals.cGlobals.game, &params->gi,
params->util, (DrawCtx*)g_globals.draw,
@ -1620,6 +1608,22 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
}
}
if ( !!g_globals.cGlobals.params->fileName ) {
XWStreamCtxt* outStream;
outStream = mem_stream_make(
MPPARM(g_globals.cGlobals.params->util->mpool)
g_globals.cGlobals.params->vtMgr,
&g_globals.cGlobals, 0, writeToFile );
stream_open( outStream );
game_saveToStream( &g_globals.cGlobals.game,
&g_globals.cGlobals.params->gi,
outStream );
stream_destroy( outStream );
}
game_dispose( &g_globals.cGlobals.game ); /* takes care of the dict */
gi_disposePlayerInfo( MEMPOOL &g_globals.cGlobals.params->gi );

View file

@ -649,7 +649,13 @@ handle_client_event( GtkWidget *widget, GdkEventClient *event,
#endif
static void
quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals )
quit( void )
{
gtk_main_quit();
}
static void
cleanup( GtkAppGlobals* globals )
{
if ( !!globals->cGlobals.params->fileName ) {
XWStreamCtxt* outStream;
@ -680,9 +686,7 @@ quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals )
#ifdef XWFEATURE_RELAY
linux_close_socket( &globals->cGlobals );
#endif
gtk_main_quit();
} /* quit */
} /* cleanup */
GtkWidget*
makeAddSubmenu( GtkWidget* menubar, gchar* label )
@ -1328,7 +1332,7 @@ gtk_util_notifyGameOver( XW_UtilCtxt* uc )
if ( cGlobals->params->quitAfter >= 0 ) {
sleep( cGlobals->params->quitAfter );
quit( NULL, globals );
quit();
} else if ( cGlobals->params->undoWhenDone ) {
server_handleUndo( cGlobals->game.server );
board_draw( cGlobals->game.board );
@ -2060,7 +2064,8 @@ static GtkAppGlobals* g_globals_for_signal;
static void
handle_sigintterm( int XP_UNUSED(sig) )
{
quit( NULL, g_globals_for_signal );
LOG_FUNC();
gtk_main_quit();
}
int
@ -2218,6 +2223,8 @@ gtkmain( LaunchParams* params, int argc, char *argv[] )
/* MONCONTROL(1); */
cleanup( &globals );
return 0;
} /* gtkmain */

View file

@ -728,6 +728,12 @@ parsePair( const char* optarg, XP_U16* min, XP_U16* max )
}
#endif
static void
tmp_noop_sigintterm( int XP_UNUSED(sig) )
{
LOG_FUNC();
}
int
main( int argc, char** argv )
{
@ -741,6 +747,13 @@ main( int argc, char** argv )
unsigned int seed = defaultRandomSeed();
LaunchParams mainParams;
XP_U16 robotCount = 0;
/* install a no-op signal handler. Later curses- or gtk-specific code
will install one that does the right thing in that context */
struct sigaction act = { .sa_handler = tmp_noop_sigintterm };
sigaction( SIGINT, &act, NULL );
sigaction( SIGTERM, &act, NULL );
CommsConnType conType = COMMS_CONN_NONE;
#ifdef XWFEATURE_SMS