From d693ea09b1cd42eb277fd69161f064aeee524080 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Wed, 17 Sep 2014 11:32:29 +0200 Subject: termination in foreground on Unix instead of sleep --- INSTALL | 14 ++++++++++++++ README | 2 ++ src/biruda.c | 28 ++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/INSTALL b/INSTALL index f302346..6664408 100644 --- a/INSTALL +++ b/INSTALL @@ -1,3 +1,17 @@ +Building on Linux +----------------- + +Archlinux +--------- + +Install the following packages from the official repos or build them +from the AUR: + * gengetopt + * confuse + * nanomsg + * json-c + * libdaemon + Building on Windows ------------------- diff --git a/README b/README index 132f1e0..4b02c16 100644 --- a/README +++ b/README @@ -29,6 +29,8 @@ Requirements http://sourceware.org/pthreads-win32/ * json-c: JSON for message payload https://github.com/json-c/json-c/wiki +* libdaemon: for daemonizing support on Unix + Other projects -------------- diff --git a/src/biruda.c b/src/biruda.c index 6511812..d95530e 100644 --- a/src/biruda.c +++ b/src/biruda.c @@ -4,6 +4,7 @@ #ifndef _WIN32 #include #include +#include #endif #include "biruda_cmdline.h" @@ -146,6 +147,13 @@ static int create_coordinator( cfg_t *cfg ) return coordinator_init( control ); } +static volatile int got_terminate = 0; + +static void terminate_foreground_func( int sig ) +{ + got_terminate = 1; +} + int main( int argc, char *argv[] ) { struct gengetopt_args_info args_info; @@ -194,8 +202,24 @@ int main( int argc, char *argv[] ) exit( EXIT_FAILURE ); } } - - sleep( 10 ); + + if( args_info.foreground_given ) { +#ifdef _WIN32 +#else + struct sigaction sa; + + memset( &sa, 0, sizeof( struct sigaction ) ); + sa.sa_handler = terminate_foreground_func; + sa.sa_flags = SA_RESTART; + + (void)sigaction( SIGINT, &sa, NULL ); + (void)sigaction( SIGTERM, &sa, NULL ); + + while( !got_terminate ) { + sleep( 1 ); + } +#endif + } if( has_coordinator ) { coordinator_terminate( ); -- cgit v1.2.3-54-g00ecf