>From 550ad69881e4b012bb2a9d2848f998319ebc445d Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Wed, 26 Jun 2024 16:23:59 -0700 Subject: [PATCH] traceroute: Modernize time functions. * bootstrap.conf (gnulib_modules): Add gettime, pselect, timespec, and timespec-sub. * src/Makefile.am (traceroute_LDADD): Add $(CLOCK_TIME_LIB), $(PTHREAD_SIGMASK_LIB), and $(SELECT_LIB). * src/traceroute.c (trace_t): Use a timespec instead of timeval. (do_try): Use pselect instead of select. Use current_timespec instead of gettimeofday. Use timespec_sub. (trace_write): Use current_timespec instead of gettimeofday. --- bootstrap.conf | 4 ++++ src/Makefile.am | 3 ++- src/traceroute.c | 32 +++++++++++--------------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index 67fffd05..7a572c96 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -63,6 +63,7 @@ getopt-gnu getpass getpeername gettext-h +gettime getusershell git-version-gen gitlog-to-changelog @@ -77,6 +78,7 @@ mkstemp obstack poll progname +pselect read-file readline readme-release @@ -101,6 +103,8 @@ strnlen sys_types sysexits termios +timespec +timespec-sub unistd-safer update-copyright vasnprintf diff --git a/src/Makefile.am b/src/Makefile.am index c355196f..5dce4ad8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -80,7 +80,8 @@ EXTRA_PROGRAMS += tftp bin_PROGRAMS += $(traceroute_BUILD) traceroute_SOURCES = traceroute.c -traceroute_LDADD = $(top_builddir)/libicmp/libicmp.a $(LDADD) $(LIBIDN) +traceroute_LDADD = $(top_builddir)/libicmp/libicmp.a $(LDADD) $(LIBIDN) \ + $(CLOCK_TIME_LIB) $(PTHREAD_SIGMASK_LIB) $(SELECT_LIB) EXTRA_PROGRAMS += traceroute inetdaemon_PROGRAMS += $(inetd_BUILD) diff --git a/src/traceroute.c b/src/traceroute.c index 3ea4f3b6..0f658e90 100644 --- a/src/traceroute.c +++ b/src/traceroute.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #ifdef HAVE_LOCALE_H # include @@ -79,7 +80,7 @@ typedef struct trace int no_ident; struct sockaddr_in to, from; int ttl; - struct timeval tsent; + struct timespec tsent; } trace_t; void trace_init (trace_t * t, const struct sockaddr_in to, @@ -358,7 +359,7 @@ do_try (trace_t *trace, const int hop, { fd_set readset; int ret, tries, readonly = 0; - struct timeval now, time; + struct timespec timeout, diff; double triptime = 0.0; uint32_t prev_addr = 0; @@ -372,26 +373,18 @@ do_try (trace_t *trace, const int hop, FD_ZERO (&readset); FD_SET (fd, &readset); - memset (&time, 0, sizeof (time)); /* 64-bit issue. */ - time.tv_sec = opt_wait; - time.tv_usec = 0; + /* *INDENT-OFF* */ + timeout = (struct timespec) { .tv_sec = opt_wait, .tv_nsec = 0 }; + /* *INDENT-ON* */ if (!readonly) trace_write (trace); errno = 0; - ret = select (fd + 1, &readset, NULL, NULL, &time); + ret = pselect (fd + 1, &readset, NULL, NULL, &timeout, NULL); save_errno = errno; - gettimeofday (&now, NULL); - - now.tv_usec -= trace->tsent.tv_usec; - now.tv_sec -= trace->tsent.tv_sec; - if (now.tv_usec < 0) - { - --now.tv_sec; - now.tv_usec += 1000000; - } + diff = timespec_sub (current_timespec (), trace->tsent); if (ret < 0) { @@ -417,8 +410,7 @@ do_try (trace_t *trace, const int hop, { int rc, type, code; - triptime = ((double) now.tv_sec) * 1000.0 + - ((double) now.tv_usec) / 1000.0; + triptime = timespectod (diff) * 1000.0; rc = trace_read (trace, &type, &code); @@ -692,8 +684,7 @@ trace_write (trace_t *t) } } - if (gettimeofday (&t->tsent, NULL) < 0) - error (EXIT_FAILURE, errno, "gettimeofday"); + t->tsent = current_timespec (); } break; @@ -730,8 +721,7 @@ trace_write (trace_t *t) } } - if (gettimeofday (&t->tsent, NULL) < 0) - error (EXIT_FAILURE, errno, "gettimeofday"); + t->tsent = current_timespec (); } break; -- 2.45.2