main loop locking and config re-reading on SIGHUP

This commit is contained in:
Sergey Popov 2011-12-12 15:03:45 +04:00
parent eeb0166fa7
commit 8f39695140

47
yasnd.c
View File

@ -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_model=NULL; // gammu phone model, for example: "at"
char* phone_connection=NULL; // gammu phone connection type, for example: "at" char* phone_connection=NULL; // gammu phone connection type, for example: "at"
char* recipient_number=NULL; // recipient of sms alerts 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) void log_debug(const char *message,int verbosity)
{ {
@ -174,10 +175,10 @@ void loop_function()
} }
} }
void termination_handler(int signum) void signal_handler(int signum)
{
void free_resources()
{ {
log_event("SIGTERM captured, daemon stopping");
closelog();
// free config structure // free config structure
cfg_free(cfg); cfg_free(cfg);
// free gammu structure // free gammu structure
@ -185,8 +186,33 @@ void termination_handler(int signum)
// free hosts structures memory // free hosts structures memory
if (hosts!=NULL) if (hosts!=NULL)
free(hosts); 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); exit(EXIT_SUCCESS);
} }
}
int main(void) { int main(void) {
/* Our process ID and Session ID */ /* Our process ID and Session ID */
@ -226,18 +252,27 @@ int main(void) {
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
// Set signal handler for SIGTERM // Set signal handlers
signal(SIGTERM, termination_handler); signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
// Init apropriate structures // Init apropriate structures
init(); init();
/* The Big Loop */ /* The Main Loop */
while (1) { while (1) {
if (!loop_locked)
{
loop_function(); loop_function();
// sleep(60); /* wait 60 seconds */ // sleep(60); /* wait 60 seconds */
log_event("Exiting"); log_event("Exiting");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else
{
// if loop is locked, just wait 1 second and start it again
sleep(1);
}
}
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }