From 86a8f6f0a151956122316482342707732f03135e Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Mon, 27 May 2019 22:52:14 +0100 Subject: [PATCH] wmail: added signal-handler for SIGINT and SIGTERM. --- wmail/src/config.h | 2 ++ wmail/src/wmail.c | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/wmail/src/config.h b/wmail/src/config.h index ec3928b..3199d1f 100644 --- a/wmail/src/config.h +++ b/wmail/src/config.h @@ -101,4 +101,6 @@ extern config_t config; // config manipulation functions void ReadConfigFile( bool resetConfigStrings ); +void ResetConfigStrings( void ); + #endif diff --git a/wmail/src/wmail.c b/wmail/src/wmail.c index 149bda6..71f40ea 100644 --- a/wmail/src/wmail.c +++ b/wmail/src/wmail.c @@ -102,6 +102,7 @@ typedef enum { // data static unsigned long lastTimeOut; +static sig_atomic_t caughtSig; mail_state_t state = STATE_NOMAIL; int numMails = 0; bool namesChanged = false; @@ -162,6 +163,7 @@ static DAProgramOption options[] = { // prototypes void PreparePixmaps( bool freeThemFirst ); +static void ExitHandler( int sig ); void TimedOut( void ); void CheckTimeOut( bool force ); void CheckMBox( void ); @@ -201,6 +203,7 @@ bool HasTickerWork( void ); int main( int argc, char **argv ) { char *usersHome; + struct sigaction sa = { .sa_handler = ExitHandler }; struct stat fileStat; XTextProperty windowName; char *name = argv[0]; @@ -213,7 +216,7 @@ int main( int argc, char **argv ) if( config.checksumFileName == NULL ) { if(( usersHome = getenv( "HOME" )) == NULL ) { WARNING( "HOME environment-variable is not set, placing %s in current directory!\n", WMAIL_CHECKSUM_FILE ); - config.checksumFileName = WMAIL_CHECKSUM_FILE; + config.checksumFileName = strdup( WMAIL_CHECKSUM_FILE ); } else config.checksumFileName = MakePathName( usersHome, WMAIL_CHECKSUM_FILE ); } @@ -291,6 +294,16 @@ int main( int argc, char **argv ) outPixmap = DAMakePixmap(); PreparePixmaps( false ); + if( sigaction( SIGINT, &sa, NULL ) == -1 ) { + perror( "wmail error: sigaction" ); + exit( EXIT_FAILURE ); + } + + if( sigaction( SIGTERM, &sa, NULL ) == -1 ) { + perror( "wmail error: sigaction" ); + exit( EXIT_FAILURE ); + } + DASetCallbacks( &callbacks ); DASetTimeout (1000 / config.fps); @@ -520,8 +533,19 @@ void RemoveChecksumFile( void ) remove( config.checksumFileName ); } +static void ExitHandler( int sig ) +{ + caughtSig = 1; +} + void TimedOut( void ) { + if (caughtSig) { + ClearAllNames(); + ResetConfigStrings(); + exit( EXIT_SUCCESS ); + } + CheckTimeOut( true ); } -- 2.11.4.GIT