Repository : ssh://git@open-mesh.org/batctl
On branch : master
commit ef7cfaa81f72292f67afe50303ac657f41280853 Author: Sven Eckelmann sven@narfation.org Date: Tue Jul 19 22:44:09 2016 +0200
batctl: Use monotonic time source for icmp timing
gettimeofday is not monotonic. It would give wrong results in some situation like leap seconds, when switching between daylight saving time and non-DST, NTP changes the time or when the administrator of the system changes it manually. The function is also obsolete since POSIX.1-2008.
clock_gettime is recommended to get a monotonic timesource and as general replacement of gettimeofday.
Signed-off-by: Sven Eckelmann sven@narfation.org
ef7cfaa81f72292f67afe50303ac657f41280853 Makefile | 2 +- functions.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile index 87851e6..5aad8fe 100755 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ MANPAGE = man/batctl.8 # batctl flags and options CFLAGS += -Wall -W -std=gnu99 -fno-strict-aliasing -MD -MP CPPFLAGS += -D_GNU_SOURCE -LDLIBS += -lm +LDLIBS += -lm -lrt
# disable verbose output ifneq ($(findstring $(MAKEFLAGS),s),s) diff --git a/functions.c b/functions.c index d236d64..92de8e3 100644 --- a/functions.c +++ b/functions.c @@ -46,6 +46,7 @@ #include <netlink/handlers.h> #include <netlink/msg.h> #include <netlink/attr.h> +#include <time.h>
#include "main.h" #include "functions.h" @@ -55,7 +56,7 @@ #include "debugfs.h" #include "netlink.h"
-static struct timeval start_time; +static struct timespec start_time; static char *host_name; char *line_ptr = NULL;
@@ -76,23 +77,23 @@ const char *fs_compile_out_param[] = {
void start_timer(void) { - gettimeofday(&start_time, NULL); + clock_gettime(CLOCK_MONOTONIC, &start_time); }
double end_timer(void) { - struct timeval end_time, diff; + struct timespec end_time, diff;
- gettimeofday(&end_time, NULL); + clock_gettime(CLOCK_MONOTONIC, &end_time); diff.tv_sec = end_time.tv_sec - start_time.tv_sec; - diff.tv_usec = end_time.tv_usec - start_time.tv_usec; + diff.tv_nsec = end_time.tv_nsec - start_time.tv_nsec;
- if (diff.tv_usec < 0) { + if (diff.tv_nsec < 0) { diff.tv_sec--; - diff.tv_usec += 1000000; + diff.tv_nsec += 1000000000; }
- return (((double)diff.tv_sec * 1000) + ((double)diff.tv_usec / 1000)); + return (((double)diff.tv_sec * 1000) + ((double)diff.tv_nsec / 1000000)); }
char *ether_ntoa_long(const struct ether_addr *addr)