Unify signal handlers that do same thing; clean memory leaks and a

double-dispose.
This commit is contained in:
ehouse 2009-08-30 15:23:05 +00:00
parent dd666472a3
commit 6827ab5d88
3 changed files with 37 additions and 27 deletions

View file

@ -338,7 +338,7 @@ curses_util_notifyGameOver( XW_UtilCtxt* uc )
if ( globals->cGlobals.params->quitAfter >= 0 ) {
sleep( globals->cGlobals.params->quitAfter );
globals->timeToExit = XP_TRUE;
handleQuit( globals );
} else if ( globals->cGlobals.params->undoWhenDone ) {
server_handleUndo( globals->cGlobals.game.server );
} else {
@ -844,12 +844,11 @@ SIGWINCH_handler( int signal )
} /* SIGWINCH_handler */
static void
SIGINT_handler( int XP_UNUSED(signal) )
SIGINTTERM_handler( int XP_UNUSED(signal) )
{
(void)handleQuit( &g_globals );
}
static void
cursesListenOnSocket( CursesAppGlobals* globals, int newSock )
{
@ -1439,8 +1438,9 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
/* reader pipe */
cursesListenOnSocket( &g_globals, g_globals.timepipe[0] );
struct sigaction act = { .sa_handler = SIGINT_handler };
struct sigaction act = { .sa_handler = SIGINTTERM_handler };
sigaction( SIGINT, &act, NULL );
sigaction( SIGTERM, &act, NULL );
struct sigaction act2 = { .sa_handler = SIGWINCH_handler };
sigaction( SIGWINCH, &act2, NULL );
@ -1452,7 +1452,8 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
if ( !!params->fileName && file_exists( params->fileName ) ) {
XWStreamCtxt* stream;
stream = streamFromFile( &g_globals.cGlobals, params->fileName, &g_globals );
stream = streamFromFile( &g_globals.cGlobals, params->fileName,
&g_globals );
(void)game_makeFromStream( MEMPOOL stream, &g_globals.cGlobals.game,
&params->gi, dict, params->util,

View file

@ -569,8 +569,6 @@ quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals )
linux_close_socket( &globals->cGlobals );
#endif
vtmgr_destroy( MEMPOOL globals->cGlobals.params->vtMgr );
gtk_main_quit();
} /* quit */
@ -1841,7 +1839,7 @@ drop_msg_toggle( GtkWidget* toggle, GtkAppGlobals* globals )
static GtkAppGlobals* g_globals_for_signal;
static void
handle_sigint( int XP_UNUSED(sig) )
handle_sigintterm( int XP_UNUSED(sig) )
{
quit( NULL, g_globals_for_signal );
}
@ -1862,8 +1860,9 @@ gtkmain( LaunchParams* params, int argc, char *argv[] )
#endif
g_globals_for_signal = &globals;
struct sigaction act = { .sa_handler = handle_sigint };
struct sigaction act = { .sa_handler = handle_sigintterm };
sigaction( SIGINT, &act, NULL );
sigaction( SIGTERM, &act, NULL );
memset( &globals, 0, sizeof(globals) );

View file

@ -113,10 +113,17 @@ writeToFile( XWStreamCtxt* stream, void* closure )
stream_getBytes( stream, buf, len );
file = fopen( cGlobals->params->fileName, "w" );
if ( 1 != fwrite( buf, len, 1, file ) ) {
XP_ASSERT( 0 );
if ( !file ) {
XP_LOGF( "%s: fopen => %d (%s)", __func__, errno, strerror(errno) );
} else {
if ( 1 == fwrite( buf, len, 1, file ) ) {
XP_LOGF( "%s: wrote %d bytes to %s", __func__, len,
cGlobals->params->fileName );
} else {
XP_ASSERT( 0 );
}
fclose( file );
}
fclose( file );
free( buf );
} /* writeToFile */
@ -328,7 +335,7 @@ linux_init_relay_socket( CommonGlobals* cGlobals, const CommsAddrRec* addrRec )
XP_STATUSF( "1: sending to port %d", addrRec->u.ip_relay.port );
host = gethostbyname( addrRec->u.ip_relay.hostName );
if ( NULL == host ) {
XP_WARNF( "gethostbyname(%s) returned -1",
XP_WARNF( "%s: gethostbyname(%s) returned -1", __func__,
addrRec->u.ip_relay.hostName );
sock = -1;
goto done;
@ -357,37 +364,38 @@ linux_tcp_send( const XP_U8* buf, XP_U16 buflen,
CommonGlobals* globals, const CommsAddrRec* addrRec )
{
XP_S16 result = 0;
int socket = globals->socket;
int sock = globals->socket;
if ( socket == -1 ) {
XP_STATUSF( "%s: socket uninitialized", __func__ );
socket = linux_init_relay_socket( globals, addrRec );
if ( socket != -1 ) {
assert( globals->socket == socket );
if ( sock == -1 ) {
XP_LOGF( "%s: socket uninitialized", __func__ );
sock = linux_init_relay_socket( globals, addrRec );
if ( sock != -1 ) {
assert( globals->socket == sock );
(*globals->socketChanged)( globals->socketChangedClosure,
-1, socket,
&globals->storage );
-1, sock, &globals->storage );
}
}
if ( socket != -1 ) {
if ( sock != -1 ) {
XP_U16 netLen = htons( buflen );
errno = 0;
result = send( socket, &netLen, sizeof(netLen), 0 );
result = send( sock, &netLen, sizeof(netLen), 0 );
if ( result == sizeof(netLen) ) {
result = send( socket, buf, buflen, 0 );
result = send( sock, buf, buflen, 0 );
}
if ( result <= 0 ) {
XP_STATUSF( "closing non-functional socket" );
close( socket );
close( sock );
(*globals->socketChanged)( globals->socketChangedClosure,
socket, -1, &globals->storage );
sock, -1, &globals->storage );
globals->socket = -1;
}
XP_STATUSF( "%s: send(sock=%d) returned %d of %d (err=%d)",
__func__, socket, result, buflen, errno );
__func__, sock, result, buflen, errno );
} else {
XP_LOGF( "%s: socket still -1", __func__ );
}
return result;
@ -1113,6 +1121,8 @@ main( int argc, char** argv )
mpool_destroy( mainParams.util->mpool );
free( mainParams.util );
XP_LOGF( "exiting main" );
return 0;
} /* main */