From patchwork Tue Jul 19 20:44:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 16559 X-Patchwork-Delegate: sven@narfation.org Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id A0A7D82DB1; Tue, 19 Jul 2016 22:44:27 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=narfation.org Authentication-Results: open-mesh.org; dkim=fail reason="verification failed; unprotected key" header.d=narfation.org header.i=@narfation.org header.b=qbJAYESr; dkim-adsp=fail (unprotected policy); dkim-atps=neutral Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2001:4d88:2000:7::2; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Authentication-Results: open-mesh.org; dmarc=pass header.from=narfation.org Received: from v3-1039.vlinux.de (narfation.org [IPv6:2001:4d88:2000:7::2]) by open-mesh.org (Postfix) with ESMTPS id CDC6F82DAB for ; Tue, 19 Jul 2016 22:44:18 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p200300C593C156F90000000000002E16.dip0.t-ipconnect.de [IPv6:2003:c5:93c1:56f9::2e16]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 227601100F1; Tue, 19 Jul 2016 22:44:18 +0200 (CEST) Authentication-Results: v3-1039.vlinux.de; dmarc=none header.from=narfation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1468961058; bh=Dbacu7SzzbIx/bV1GqUWlfcdKblz6A9XMjTPL1apm+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qbJAYESr5yYcgQWVgFmlETFIg7+Qf80Eyp1Q1C2hDhJcNF2ONCpsUa+gc+nFTcsCA /m2R0eKG6BCrBQhmd9Usguw9ZxnQ7xCSkdKQrJvGkcm+vmAZBar2NxkvErVk78AfK8 mIx63skBfWL0ZvuQCT4oGb6i3cRJHnPynHDbKrPo= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Tue, 19 Jul 2016 22:44:09 +0200 Message-Id: <1468961051-6447-2-git-send-email-sven@narfation.org> X-Mailer: git-send-email 2.8.1 In-Reply-To: <3501586.3dVhjbZHzy@sven-edge> References: <3501586.3dVhjbZHzy@sven-edge> Subject: [B.A.T.M.A.N.] [PATCH 2/4] batctl: Use monotonic time source for icmp timing X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" 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 --- Makefile | 2 +- functions.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index e980587..57d9a4e 100755 --- a/Makefile +++ b/Makefile @@ -46,7 +46,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 f9feca4..bc4f9f8 100644 --- a/functions.c +++ b/functions.c @@ -48,6 +48,7 @@ #include #include #include +#include #include "main.h" #include "functions.h" @@ -59,7 +60,7 @@ #define PATH_BUFF_LEN 200 -static struct timeval start_time; +static struct timespec start_time; static char *host_name; char *line_ptr = NULL; @@ -80,23 +81,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)