diff options
author | /C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org </C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org> | 2008-01-09 22:52:31 +0000 |
---|---|---|
committer | /C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org </C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org> | 2008-01-09 22:52:31 +0000 |
commit | 920b90f2b03c60b6940e83cdce8c4b4bfbbc4268 (patch) | |
tree | 1dc9fe70ee9304ea0ca72ceb6a2a07537d302cb8 /src/alarm.c | |
parent | 6023de67c84e531939b77454783835c65f694bff (diff) | |
download | conntrack-tools-920b90f2b03c60b6940e83cdce8c4b4bfbbc4268.tar.gz conntrack-tools-920b90f2b03c60b6940e83cdce8c4b4bfbbc4268.zip |
wake up the daemon iff there are real events to handle instead of polling (Based on comments from Max Kellerman)
Diffstat (limited to 'src/alarm.c')
-rw-r--r-- | src/alarm.c | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/src/alarm.c b/src/alarm.c index b4db167..e1d2e24 100644 --- a/src/alarm.c +++ b/src/alarm.c @@ -18,6 +18,7 @@ #include <unistd.h> #include <stdlib.h> +#include <sys/time.h> #include "linux_list.h" #include "conntrackd.h" #include "alarm.h" @@ -25,23 +26,16 @@ #include <time.h> #include <errno.h> -/* alarm cascade */ -#define ALARM_CASCADE_SIZE STEPS_PER_SECONDS -static struct list_head *alarm_cascade; +static LIST_HEAD(alarm_list); -struct alarm_list *create_alarm() -{ - return (struct alarm_list *) malloc(sizeof(struct alarm_list)); -} - -void destroy_alarm(struct alarm_list *t) +void set_alarm_expiration_secs(struct alarm_list *t, unsigned long expires) { - free(t); + t->tv.tv_sec = expires; } -void set_alarm_expiration(struct alarm_list *t, unsigned long expires) +void set_alarm_expiration_usecs(struct alarm_list *t, unsigned long expires) { - t->expires = expires; + t->tv.tv_usec = expires; } void set_alarm_function(struct alarm_list *t, @@ -59,16 +53,18 @@ void init_alarm(struct alarm_list *t) { INIT_LIST_HEAD(&t->head); - t->expires = 0; + timerclear(&t->tv); t->data = 0; t->function = NULL; } void add_alarm(struct alarm_list *alarm) { - unsigned int pos = jhash(alarm, sizeof(alarm), 0) % ALARM_CASCADE_SIZE; + struct timeval tv; - list_add(&alarm->head, &alarm_cascade[pos]); + gettimeofday(&tv, NULL); + alarm->tv.tv_sec += tv.tv_sec; + list_add_tail(&alarm->head, &alarm_list); } void del_alarm(struct alarm_list *alarm) @@ -76,41 +72,35 @@ void del_alarm(struct alarm_list *alarm) list_del(&alarm->head); } -int mod_alarm(struct alarm_list *alarm, unsigned long expires) +void mod_alarm(struct alarm_list *alarm, unsigned long sc, unsigned long usc) { - alarm->expires = expires; - return 0; + struct timeval tv; + + list_del(&alarm->head); + INIT_LIST_HEAD(&alarm->head); + gettimeofday(&tv, NULL); + alarm->tv.tv_sec = tv.tv_sec + sc; + alarm->tv.tv_usec = tv.tv_usec + usc; + list_add_tail(&alarm->head, &alarm_list); } -void do_alarm_run(int step) +void do_alarm_run(struct timeval *next_alarm) { struct list_head *i, *tmp; struct alarm_list *t; + struct timeval tv; - list_for_each_safe(i, tmp, &alarm_cascade[step]) { - t = (struct alarm_list *) i; - - t->expires--; - if (t->expires == 0) - t->function(t, t->data); - } -} + gettimeofday(&tv, NULL); -int init_alarm_scheduler() -{ - int i; - - alarm_cascade = malloc(sizeof(struct list_head) * ALARM_CASCADE_SIZE); - if (alarm_cascade == NULL) - return -1; + list_for_each_safe(i, tmp, &alarm_list) { + t = (struct alarm_list *) i; - for (i=0; i<ALARM_CASCADE_SIZE; i++) - INIT_LIST_HEAD(&alarm_cascade[i]); + if (timercmp(&t->tv, &tv, >)) { + timersub(&t->tv, &tv, next_alarm); + break; + } - return 0; -} - -void destroy_alarm_scheduler() -{ - free(alarm_cascade); + del_alarm(t); + t->function(t, t->data); + } } |