implement IPC messaging with queue loop
This commit is contained in:
36
yasnd.c
36
yasnd.c
@ -8,7 +8,6 @@
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <signal.h>
|
||||
#include <confuse.h>
|
||||
|
||||
#include "yasnd.h"
|
||||
@ -16,6 +15,7 @@
|
||||
cfg_t *cfg; // pointer to configuration structure
|
||||
host_decl* hosts=NULL; // structure with hosts' definitions
|
||||
int hosts_count=0; // count of hosts
|
||||
int qid=-1; // IPC queue id
|
||||
int debug_flag=0;
|
||||
bool sms_send_enable=false; // send or not send alerts via SMS
|
||||
char* phone_device=NULL; // phone device for gammu, for example: "/dev/ttyACM0"
|
||||
@ -109,7 +109,6 @@ void init()
|
||||
}
|
||||
// initialize gammu structures
|
||||
gammu_init();
|
||||
|
||||
}
|
||||
|
||||
void check_host(int num,host_decl* host)
|
||||
@ -121,6 +120,10 @@ void check_host(int num,host_decl* host)
|
||||
}
|
||||
// If pid == 0, then we are in the child
|
||||
if (pid == 0) {
|
||||
// Set default signal handlers
|
||||
signal(SIGTERM, SIG_DFL);
|
||||
signal(SIGHUP, SIG_DFL);
|
||||
//
|
||||
char tmp[50];
|
||||
sprintf(tmp, "Pinging host %s", host->hostname);
|
||||
log_debug(tmp,DEBUG_BASE);
|
||||
@ -205,6 +208,10 @@ void signal_handler(int signum)
|
||||
{
|
||||
void free_resources()
|
||||
{
|
||||
// Stop IPC loop and remove IPC queue
|
||||
kill(ipc_clone_pid,SIGTERM);
|
||||
waitpid(ipc_clone_pid,NULL,__WCLONE);
|
||||
remove_queue(qid);
|
||||
// free config structure
|
||||
cfg_free(cfg);
|
||||
// free gammu structure
|
||||
@ -224,6 +231,8 @@ void signal_handler(int signum)
|
||||
|
||||
// free all resources
|
||||
free_resources();
|
||||
// Init IPC loop again
|
||||
ipc_init();
|
||||
// run init again
|
||||
init();
|
||||
// sleep 5 seconds and unlock Main Loop
|
||||
@ -234,8 +243,13 @@ void signal_handler(int signum)
|
||||
if (signum==SIGTERM)
|
||||
{
|
||||
log_event("SIGTERM captured, daemon stopping");
|
||||
// close log decriptor
|
||||
closelog();
|
||||
free_resources(); // free all other resources
|
||||
// free resources
|
||||
free_resources();
|
||||
// remove IPC message queue
|
||||
remove_queue(qid);
|
||||
//
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
@ -278,21 +292,25 @@ int main(void) {
|
||||
close(STDOUT_FILENO);
|
||||
close(STDERR_FILENO);
|
||||
|
||||
// Set signal handlers
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGHUP, signal_handler);
|
||||
// initialize IPC queue and create process
|
||||
// that will watch for new messages in it
|
||||
ipc_init();
|
||||
|
||||
// Init apropriate structures
|
||||
init();
|
||||
|
||||
// Set signal handlers
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGHUP, signal_handler);
|
||||
|
||||
/* The Main Loop */
|
||||
while (1) {
|
||||
if (!loop_locked)
|
||||
{
|
||||
loop_function();
|
||||
// sleep(60); /* wait 60 seconds */
|
||||
log_event("Exiting");
|
||||
exit(EXIT_SUCCESS);
|
||||
sleep(60); /* wait 60 seconds */
|
||||
// log_event("Exiting");
|
||||
// exit(EXIT_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user