main loop locking and config re-reading on SIGHUP
This commit is contained in:
parent
eeb0166fa7
commit
8f39695140
71
yasnd.c
71
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user