From 8f39695140b711ec7cf18441f4d911f8ce7faba3 Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Mon, 12 Dec 2011 15:03:45 +0400 Subject: [PATCH] main loop locking and config re-reading on SIGHUP --- yasnd.c | 71 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/yasnd.c b/yasnd.c index e891326..aa5b772 100644 --- a/yasnd.c +++ b/yasnd.c @@ -21,6 +21,7 @@ char* phone_device=NULL; // phone device for gammu, for example: "/dev/ttyACM0" char* phone_model=NULL; // gammu phone model, for example: "at" char* phone_connection=NULL; // gammu phone connection type, for example: "at" char* recipient_number=NULL; // recipient of sms alerts +bool loop_locked=false ; // flag for locking main loop while config re-reading void log_debug(const char *message,int verbosity) { @@ -174,18 +175,43 @@ void loop_function() } } -void termination_handler(int signum) +void signal_handler(int signum) { - log_event("SIGTERM captured, daemon stopping"); - closelog(); - // free config structure - cfg_free(cfg); - // free gammu structure - GSM_FreeStateMachine(state_machine); - // free hosts structures memory - if (hosts!=NULL) - free(hosts); - exit(EXIT_SUCCESS); + void free_resources() + { + // free config structure + cfg_free(cfg); + // free gammu structure + GSM_FreeStateMachine(state_machine); + // free hosts structures memory + if (hosts!=NULL) + free(hosts); + } + + if (signum==SIGHUP) + { + log_event("SIGHUP captured, daemon re-read config"); + // lock Main Loop + loop_locked=true; + + // STUB: need to kill all childs here + + // free all resources + free_resources(); + // run init again + init(); + // sleep 5 seconds and unlock Main Loop + sleep(5); + loop_locked=false; + return; + } + if (signum==SIGTERM) + { + log_event("SIGTERM captured, daemon stopping"); + closelog(); + free_resources(); // free all other resources + exit(EXIT_SUCCESS); + } } int main(void) { @@ -226,18 +252,27 @@ int main(void) { close(STDOUT_FILENO); close(STDERR_FILENO); - // Set signal handler for SIGTERM - signal(SIGTERM, termination_handler); + // Set signal handlers + signal(SIGTERM, signal_handler); + signal(SIGHUP, signal_handler); // Init apropriate structures init(); - /* The Big Loop */ + /* The Main Loop */ while (1) { - loop_function(); -// sleep(60); /* wait 60 seconds */ - log_event("Exiting"); - exit(EXIT_SUCCESS); + if (!loop_locked) + { + loop_function(); +// sleep(60); /* wait 60 seconds */ + log_event("Exiting"); + exit(EXIT_SUCCESS); + } + else + { + // if loop is locked, just wait 1 second and start it again + sleep(1); + } } exit(EXIT_SUCCESS); }