dns: prefer monotonic clock for timeouts
authorA. Wilcox <AWilcox@Wilcox-Tech.com>
Fri, 30 Dec 2022 03:21:32 +0000 (21:21 -0600)
committerRich Felker <dalias@aerifal.cx>
Sun, 12 Feb 2023 23:03:24 +0000 (18:03 -0500)
Before this commit, DNS timeouts always used CLOCK_REALTIME, which
could produce spurious timeouts or delays if wall time changed for
whatever reason.

Now we try CLOCK_MONOTONIC and only fall back to CLOCK_REALTIME when
it is unavailable.

src/network/res_msend.c

index 11c6aa0..fef7e3a 100644 (file)
@@ -25,7 +25,8 @@ static void cleanup(void *p)
 static unsigned long mtime()
 {
        struct timespec ts;
-       clock_gettime(CLOCK_REALTIME, &ts);
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0 && errno == ENOSYS)
+               clock_gettime(CLOCK_REALTIME, &ts);
        return (unsigned long)ts.tv_sec * 1000
                + ts.tv_nsec / 1000000;
 }