]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1574] sntp:set_time doesn't set tv_usec correctly.
authorDave Hart <hart@ntp.org>
Tue, 9 Nov 2010 03:52:20 +0000 (03:52 +0000)
committerDave Hart <hart@ntp.org>
Tue, 9 Nov 2010 03:52:20 +0000 (03:52 +0000)
bk: 4cd8c574D3uD9mh1MUTcTAy891Ft9Q

ChangeLog
sntp/main.c

index 805affdec75782d96b01a3ca6aeb92f230e843ef..58c89e32a533dbeb2fd6bd638b9284a35cd75bed 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+---
+
+* [Bug 1574] sntp:set_time doesn't set tv_usec correctly.
+
 ---
 (4.2.6p3-RC8) 2010/10/29 Released by Harlan Stenn <stenn@ntp.org>
 
index 943e64319d462fa08791005ab3ee1ed10a9a1206..3c8101124a80edd9a4233bd2994af42318f86814 100644 (file)
@@ -27,6 +27,18 @@ int sntp_main (int argc, char **argv);
 int on_wire (struct addrinfo *host, struct addrinfo *bcastaddr);
 int set_time (double offset);
 
+#define NORMALIZE_TIMEVAL(tv)                          \
+do {                                                   \
+       while ((tv).tv_usec < 0) {                      \
+               (tv).tv_usec += 1000000;                \
+               (tv).tv_sec--;                          \
+       }                                               \
+       while ((tv).tv_usec > 999999) {                 \
+               (tv).tv_usec -= 1000000;                \
+               (tv).tv_sec++;                          \
+       }                                               \
+} while (0)
+
 
 int 
 main (
@@ -397,23 +409,26 @@ set_time(
 {
        struct timeval tp;
 
-       if(ENABLED_OPT(SETTOD)) {
-               GETTIMEOFDAY(&tp, (struct timezone *)NULL);
+       if (ENABLED_OPT(SETTOD)) {
+               GETTIMEOFDAY(&tp, NULL);
 
-               tp.tv_sec += (int) offset;
-               tp.tv_usec += offset - (double)((int)offset);
+               tp.tv_sec += (long)offset;
+               tp.tv_usec += 1e6 * (offset - (long)offset);
+               NORMALIZE_TIMEVAL(tp);
 
-               if(SETTIMEOFDAY(&tp, (struct timezone *)NULL) < 0) {
+               if (SETTIMEOFDAY(&tp, NULL) < 0) {
                        printf("set_time: settimeofday(): Time not set: %s\n",
                                strerror(errno));
                        return -1;
                }
                return 0;
        }
-       tp.tv_sec = (int) offset;
-       tp.tv_usec = offset - (double)((int)offset);
 
-       if(ADJTIMEOFDAY(&tp, NULL) < 0) {
+       tp.tv_sec = (long)offset;
+       tp.tv_usec = 1e6 * (offset - (long)offset);
+       NORMALIZE_TIMEVAL(tp);
+
+       if (ADJTIMEOFDAY(&tp, NULL) < 0) {
                printf("set_time: adjtime(): Time not set: %s\n",
                        strerror(errno));
                return -1;