From ec0ee88d13987e2953b7c532c2e8e1e5506967ac Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 12 Jul 2016 13:57:23 +0300 Subject: [PATCH] Move trigger into its own subsystem Also fixes a bug where enabling monitor without trigger would crash. --- main.c | 86 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/main.c b/main.c index 38624d0..00b26de 100644 --- a/main.c +++ b/main.c @@ -44,6 +44,16 @@ typedef struct Time connection_down_time; } Monitor; +typedef struct +{ + Monitor *monitor; + Time delay; + Time interval; + const char *command; + Time next; + pid_t child_pid; +} Trigger; + typedef struct { const char *target_name; @@ -62,6 +72,8 @@ Time current_time(); int monitor_init(Monitor *); void monitor_check(Monitor *, Time); void capture_handler(uint8_t *, const struct pcap_pkthdr *, const uint8_t *); +int trigger_init(Trigger *); +void trigger_check(Trigger *, Time); int get_inet_scope(uint32_t, const Address *); int get_inet6_scope(const struct in6_addr *, const Address *); int pinger_init(Pinger *); @@ -76,17 +88,18 @@ int verbose; int main(int argc, char **argv) { Monitor monitor; + Trigger trigger; Pinger pinger; int o; char *endp; Time stats_interval = 1800000000ULL; - Time trigger_delay = 60000000ULL; - Time trigger_interval = 600000000ULL; - const char *trigger_cmd = NULL; no_daemon = 0; verbose = 0; monitor.interface_name = NULL; + trigger.monitor = &monitor; + trigger.delay = 60000000ULL; + trigger.interval = 600000000ULL; pinger.target_name = NULL; /* Parse options */ @@ -103,14 +116,14 @@ int main(int argc, char **argv) stats_interval = strtoul(optarg, NULL, 10)*1000000; break; case 't': - trigger_delay = strtoul(optarg, &endp, 10)*1000000; + trigger.delay = strtoul(optarg, &endp, 10)*1000000; if(*endp==',') - trigger_interval = strtoul(endp+1, NULL, 10)*1000000; + trigger.interval = strtoul(endp+1, NULL, 10)*1000000; else - trigger_interval = trigger_delay; + trigger.interval = trigger.delay; break; case 'c': - trigger_cmd = optarg; + trigger.command = optarg; break; case 'i': monitor.interface_name = strdup(optarg); @@ -121,8 +134,13 @@ int main(int argc, char **argv) } if(monitor.interface_name) + { monitor_init(&monitor); + if(trigger.command) + trigger_init(&trigger); + } + if(pinger.target_name) pinger_init(&pinger); @@ -132,8 +150,6 @@ int main(int argc, char **argv) openlog("netmon", 0, LOG_LOCAL7); Time next_stats = current_time()+stats_interval; - Time next_trigger = trigger_delay; - pid_t child_pid = 0; while(1) { Time time = current_time(); @@ -142,17 +158,8 @@ int main(int argc, char **argv) { monitor_check(&monitor, time); - if(monitor.connection_status) - next_trigger = trigger_delay; - else if(time>=monitor.last_receive+next_trigger) - { - if(no_daemon) - printf("Running %s\n", trigger_cmd); - else - syslog(LOG_INFO, "Running %s", trigger_cmd); - child_pid = run_command(trigger_cmd); - next_trigger += trigger_interval; - } + if(trigger.command) + trigger_check(&trigger, time); } if(pinger.target_name) @@ -175,13 +182,6 @@ int main(int argc, char **argv) next_stats += stats_interval; } } - - /* Reap any finished child process */ - if(child_pid) - { - if(waitpid(child_pid, NULL, WNOHANG)==child_pid) - child_pid = 0; - } } closelog(); @@ -322,6 +322,38 @@ void capture_handler(uint8_t *user, const struct pcap_pkthdr *header, const uint monitor->last_transmit = timestamp; } +int trigger_init(Trigger *trigger) +{ + trigger->next = trigger->delay; + trigger->child_pid = 0; + + return 0; +} + +void trigger_check(Trigger *trigger, Time time) +{ + Monitor *monitor = trigger->monitor; + + if(monitor->connection_status) + trigger->next = trigger->delay; + else if(time>=monitor->last_receive+trigger->next) + { + if(no_daemon) + printf("Running %s\n", trigger->command); + else + syslog(LOG_INFO, "Running %s", trigger->command); + trigger->child_pid = run_command(trigger->command); + trigger->next += trigger->interval; + } + + /* Reap any finished child process */ + if(trigger->child_pid) + { + if(waitpid(trigger->child_pid, NULL, WNOHANG)==trigger->child_pid) + trigger->child_pid = 0; + } +} + int get_inet_scope(uint32_t addr, const Address *local_addr) { uint32_t diff = addr^((struct sockaddr_in *)&local_addr->address)->sin_addr.s_addr; -- 2.45.2