[Git][NTPsec/ntpsec][master] 2 commits: Fix ntpfrob/bumpclock to work with large values.
Hal Murray
gitlab at mg.gitlab.com
Fri Mar 23 08:26:21 UTC 2018
Hal Murray pushed to branch master at NTPsec / ntpsec
Commits:
e114b4e2 by Hal Murray at 2018-03-23T08:07:25Z
Fix ntpfrob/bumpclock to work with large values.
- - - - -
c8c888f8 by Hal Murray at 2018-03-23T08:11:17Z
Add logging of year(s) on large clock steps. Issue #674
- - - - -
4 changed files:
- libntp/systime.c
- ntpfrob/bumpclock.c
- ntpfrob/main.c
- ntpfrob/ntpfrob.h
Changes:
=====================================
libntp/systime.c
=====================================
--- a/libntp/systime.c
+++ b/libntp/systime.c
@@ -317,6 +317,7 @@ step_systime(
{
time_t pivot; /* for ntp era unfolding */
struct timespec timets;
+ struct timespec old, new;
struct calendar jd;
l_fp fp_ofs, fp_sys; /* offset and target system time in FP */
@@ -370,6 +371,7 @@ step_systime(
/* get the current time as l_fp (without fuzz) and as struct timespec */
get_ostime(&timets);
+ old = timets;
fp_sys = tspec_stamp_to_lfp(timets);
/* get the target time as l_fp */
@@ -377,6 +379,7 @@ step_systime(
/* unfold the new system time */
timets = lfp_stamp_to_tspec(fp_sys, pivot);
+ new = timets;
/* now set new system time */
if (settime(&timets) != 0) {
@@ -387,6 +390,28 @@ step_systime(
/* <--- time-critical path ended with call to the settime hook <--- */
msyslog(LOG_WARNING, "CLOCK: time stepped by %Lf", step);
+ if (abs(step) > 86400) {
+ /* Get the full year (both old and new) into the log file.
+ * Issue #474 */
+ struct tm oldtm, newtm;
+ char oldbuf[100], newbuf[100];
+ if (!localtime_r(&old.tv_sec, &oldtm)) {
+ oldtm.tm_year = 9999-1900;
+ oldtm.tm_mon = 98;
+ oldtm.tm_mday = 99;
+ }
+ snprintf(oldbuf, sizeof(oldbuf), "%04d-%02d-%02d",
+ oldtm.tm_year+1900, oldtm.tm_mon+1, oldtm.tm_mday);
+ if (!localtime_r(&new.tv_sec, &newtm)) {
+ newtm.tm_year = 9999-1900;
+ newtm.tm_mon = 98;
+ newtm.tm_mday = 99;
+ }
+ snprintf(newbuf, sizeof(newbuf), "%04d-%02d-%02d",
+ newtm.tm_year+1900, newtm.tm_mon+1, newtm.tm_mday);
+ msyslog(LOG_WARNING, "CLOCK: time changed from %s to %s",
+ oldbuf, newbuf);
+ }
sys_residual = 0;
lamport_violated = (step < 0);
=====================================
ntpfrob/bumpclock.c
=====================================
--- a/ntpfrob/bumpclock.c
+++ b/ntpfrob/bumpclock.c
@@ -16,18 +16,27 @@
#define NS_PER_S 1000000000
-void bumpclock(int bump)
+void bumpclock(int64_t bump)
{
struct timespec was, set, now;
int rc1, rc2, rc3;
int er1, er2, er3;
- printf("Bumping clock by %d microseconds.\n", bump);
+#if NTP_SIZEOF_LONG > 4
+ printf("Bumping clock by %ld microseconds.\n", bump);
+#else
+ printf("Bumping clock by %lld microseconds.\n", bump);
+#endif
rc1 = clock_gettime(CLOCK_REALTIME, &was);
er1 = errno;
set = was;
+ if (bump > 1000000 || bump < -1000000) {
+ int64_t sec = bump/1000000;
+ bump -= sec*1000000;
+ set.tv_sec += sec;
+ }
bump *= 1000;
/* coverity[tainted_data] */
set.tv_nsec += bump;
@@ -47,7 +56,7 @@ void bumpclock(int bump)
/* Defer printing so it doesn't distort timing. */
if (rc1)
- printf("Couldn't get time: %s\n", strerror(er1));
+ printf("Couldn't get old time: %s\n", strerror(er1));
else
printf("Was: %ld.%09ld\n", (long)was.tv_sec, was.tv_nsec);
@@ -58,7 +67,7 @@ void bumpclock(int bump)
printf("Set: %ld.%09ld\n", (long)set.tv_sec, set.tv_nsec);
if (rc3)
- printf("Couldn't set time: %s\n", strerror(er3));
+ printf("Couldn't get new time: %s\n", strerror(er3));
else
printf("Now: %ld.%09ld\n", (long)now.tv_sec, now.tv_nsec);
}
=====================================
ntpfrob/main.c
=====================================
--- a/ntpfrob/main.c
+++ b/ntpfrob/main.c
@@ -56,7 +56,11 @@ main(int argc, char **argv)
#endif /* HAVE_ADJTIMEX */
break;
case 'b':
- bumpclock(atoi(optarg));
+#if NTP_SIZEOF_LONG > 4
+ bumpclock(atol(optarg));
+#else
+ bumpclock(atoll(optarg));
+#endif
break;
case 'c':
jitter(mode);
=====================================
ntpfrob/ntpfrob.h
=====================================
--- a/ntpfrob/ntpfrob.h
+++ b/ntpfrob/ntpfrob.h
@@ -2,11 +2,12 @@
* Our methods, one per linked module
*/
#include <stdbool.h> /* for bool */
+#include <stdint.h> /* for int64_t */
#include "ntp_fp.h" /* for l_fp */
typedef enum {plain_text, raw, json} iomode;
-extern void bumpclock(int);
+extern void bumpclock(int64_t);
extern void get_clocktime(l_fp *now);
extern void jitter(const iomode mode) __attribute__((noreturn));
extern void ppscheck(const char *device) __attribute__((noreturn));
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/f927578b0efcf8676a9bf190b0692514123b19b2...c8c888f8f2ef295c0ea5f854127069b3812e4c09
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/f927578b0efcf8676a9bf190b0692514123b19b2...c8c888f8f2ef295c0ea5f854127069b3812e4c09
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180323/0349752c/attachment.html>
More information about the vc
mailing list