From patchwork Sat Mar 28 15:16:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kwok Cheung Yeung X-Patchwork-Id: 5871 Received: (qmail 11801 invoked by alias); 28 Mar 2015 15:17:09 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 11791 invoked by uid 89); 28 Mar 2015 15:17:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, T_FROM_12LTRDOM autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Message-ID: <5516C5EB.9080106@codesourcery.com> Date: Sat, 28 Mar 2015 15:16:59 +0000 From: Kwok Cheung Yeung User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Subject: [PATCH] Use monotonic timer for send_dg This patch addresses a rare scenario where a change in the system clock may result in an incorrect timeout. In send_dg (in resolv/res_send.c), eventually called from functions such as getaddrinfo: evNowTime(&now); evConsTime(&timeout, seconds, 0); evAddTime(&finish, &now, &timeout); ... recompute_resend: evNowTime(&now); ... evSubTime(&timeout, &finish, &now); ... ptimeout = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000; ... n = __poll (pfd, 1, ptimeout); If in between the first and second calls to evNowTime the system clock changes, then the updated value of timeout will also be modified (e.g. if the time shifts back an hour, then the timeout would be extended by an hour). This patch addresses this by making evNowTime use a monotonic clock if available, since it cannot go backwards in time. If not available, then it falls back to using the realtime clock as before. I have run a full 'make xcheck' before and after, with no regressions found. Kwok Use monotonic timer for send_dg * resolv/res_send.c: Include time.h instead of sys/time.h. (evNowTime): Obtain current time from monotonic clock if possible. --- resolv/res_send.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) #include @@ -169,6 +169,11 @@ static void evNowTime(struct timespec *res) { struct timeval now; +#ifdef _POSIX_MONOTONIC_CLOCK + if (clock_gettime(CLOCK_MONOTONIC, res) == 0) + return; +#endif + if (gettimeofday(&now, NULL) < 0) evConsTime(res, 0, 0); else diff --git a/resolv/res_send.c b/resolv/res_send.c index c35fb66..b7176dd 100644 --- a/resolv/res_send.c +++ b/resolv/res_send.c @@ -76,7 +76,7 @@ static const char rcsid[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixi #include #include #include -#include +#include #include #include