netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iputils ping,ping6: use monotonic clock to schedule pings
@ 2010-09-05 12:58 Thomas Habets
  2010-09-05 13:13 ` Jan Ceuleers
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Habets @ 2010-09-05 12:58 UTC (permalink / raw)
  To: netdev


This fixes the problem where when time is set backwards 'ping' appears
to freeze until the walltime catches up with the previously set time.

Adds dependency on librt.

diff --git a/Makefile b/Makefile
index 42f8475..a6812a3 100644
--- a/Makefile
+++ b/Makefile
@@ -28,8 +28,8 @@ all: $(TARGETS)

  tftpd: tftpd.o tftpsubs.o
  arping: arping.o -lsysfs
-ping: ping.o ping_common.o
-ping6: ping6.o ping_common.o -lresolv -lcrypto
+ping: ping.o ping_common.o -lrt
+ping6: ping6.o ping_common.o -lresolv -lcrypto -lrt
  ping.o ping6.o ping_common.o: ping_common.h
  tftpd.o tftpsubs.o: tftp.h

diff --git a/ping_common.c b/ping_common.c
index e0edf39..5883e63 100644
--- a/ping_common.c
+++ b/ping_common.c
@@ -304,6 +304,20 @@ void print_timestamp(void)
         }
  }

+int
+getclock(struct timeval *tv)
+{
+#ifdef CLOCK_MONOTONIC
+       struct timespec ts;
+       if (!clock_gettime(CLOCK_MONOTONIC, &ts)) {
+               tv->tv_sec = ts.tv_sec;
+               tv->tv_usec = ts.tv_nsec / 1000;
+               return;
+       }
+#endif
+       return gettimeofday(tv, NULL);
+}
+
  /*
   * pinger --
   *     Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
@@ -324,13 +338,13 @@ int pinger(void)

         /* Check that packets < rate*time + preload */
         if (cur_time.tv_sec == 0) {
-               gettimeofday(&cur_time, NULL);
+               getclock(&cur_time);
                 tokens = interval*(preload-1);
         } else {
                 long ntokens;
                 struct timeval tv;

-               gettimeofday(&tv, NULL);
+               getclock(&tv);
                 ntokens = (tv.tv_sec - cur_time.tv_sec)*1000 +
                         (tv.tv_usec-cur_time.tv_usec)/1000;
                 if (!interval) {

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-09-21  6:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-05 12:58 [PATCH] iputils ping,ping6: use monotonic clock to schedule pings Thomas Habets
2010-09-05 13:13 ` Jan Ceuleers
2010-09-05 13:25   ` Thomas Habets
2010-09-21  5:40     ` YOSHIFUJI Hideaki
2010-09-21  6:50       ` Thomas Habets

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).