From c93b612597f45469c0f9d67dc0dbb36cb0d7ae2b Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Fri, 25 Nov 2011 17:04:49 +0400 Subject: [PATCH] hosts' list are now defined via config file --- Makefile.am | 1 + yasnd.c | 61 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/Makefile.am b/Makefile.am index 1ac4bc8..bb08eec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,5 @@ INCLUDES=${gammu_CFLAGS} +yasnd_LDADD = ${libconfuse_LIBS} bin_PROGRAMS = yasnd yasnd_SOURCES = yasnd.c diff --git a/yasnd.c b/yasnd.c index 679f098..d5a342c 100644 --- a/yasnd.c +++ b/yasnd.c @@ -24,17 +24,7 @@ typedef struct { int fail_count; // how many times in a row host was unreachable } host_decl; -host_decl hosts[NUM_HOSTS]; - -void init() -{ - hosts[0].hostname="10.0.0.1"; - hosts[0].fail_count=0; - hosts[1].hostname="10.0.0.2"; - hosts[1].fail_count=0; - hosts[2].hostname="10.0.0.254"; - hosts[2].fail_count=0; -} +host_decl* hosts=NULL; void log_event(const char *message) { @@ -43,30 +33,66 @@ void log_event(const char *message) #endif } -void check_host(int num,host_decl host) +void* allocate_memory(int bytes) +{ + void* result=malloc(bytes); + if (result==NULL) + { + log_event("Error: malloc failed"); + exit(EXIT_FAILURE); + } +} + +void init() +{ + // Initialize config parsing library + cfg_opt_t opts[] = { + CFG_STR_LIST("hosts", "{}", CFGF_NONE), + CFG_END() + }; + cfg_t *cfg; + cfg = cfg_init(opts, CFGF_NONE); + // check if config file is valid and parse it + if (cfg_parse(cfg, "/etc/yasnd.conf") == CFG_PARSE_ERROR) + { + log_event("error parsing config file"); + exit(EXIT_FAILURE); + } + // allocate memory for hosts array... + hosts=allocate_memory(cfg_size(cfg,"hosts")*sizeof(host_decl)); + // ...and fill it with config file content + for (int i=0;ihostname); log_event(tmp); - execl("/bin/ping","/bin/ping","-c 4","-n",host.hostname,(char*) 0); + execl("/bin/ping","/bin/ping","-c 4","-n",host->hostname,(char*) 0); // STUB: check result of exec call exit(EXIT_SUCCESS); } // Save child's pid - host.helper_pid=pid; + host->helper_pid=pid; } void loop_function() { for (int i=0;i